Skip to content

Commit 5586f77

Browse files
committed
Pass original event object to triggers, too
1 parent 0b9f5d0 commit 5586f77

2 files changed

Lines changed: 29 additions & 24 deletions

File tree

internal/LoadAndSave.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ async function saveOption(event) {
4848

4949
let [option, optionValue] = HtmlMod.getIdAndOptionsFromElement(elOption);
5050

51-
console.info("save option", elOption, option, optionValue);
51+
const saveTriggerValue = await Trigger.runSaveTrigger(option, optionValue, event);
5252

53-
const saveTriggerValue = await Trigger.runSaveTrigger(option, optionValue);
53+
console.info("save option", elOption, option, optionValue, event, saveTriggerValue);
5454

5555
try {
56-
const result = await Trigger.runOverrideSave(option, optionValue, saveTriggerValue);
56+
const result = await Trigger.runOverrideSave(option, optionValue, saveTriggerValue, event);
5757

5858
// destructre data, if it has been returned
5959
if (result.command === Trigger.CONTINUE_RESULT) {

internal/Trigger.js

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const triggers = {
5252
* @callback saveTrigger
5353
* @param {Object} optionValue the value of the changed option
5454
* @param {string} option the name of the option that has been changed
55+
* @param {Event} event the event (input or change) that triggered saving
5556
* @return {Promise} optionally, to use await
5657
*/
5758

@@ -69,6 +70,7 @@ const triggers = {
6970
* @param {string} param.option the name of the option that has been changed
7071
* @param {Array} param.saveTriggerValues all values returned by potentially
7172
* previously run save triggers
73+
* @param {Event} param.event the event (input or change) that triggered saving
7274
* @returns {Promise} recommend
7375
* @throws {Error} if saving e.g. fails, this will automatically trigger a generic
7476
* error to be shown in the UI
@@ -111,10 +113,11 @@ const triggers = {
111113
* @function
112114
* @param {string} [option]
113115
* @param {Object} [optionValue] will be automatically retrieved, if not given
116+
* @param {Event} event the event (input or change) that triggered saving
114117
* @returns {Promise}
115118
* @see {@link saveTrigger}
116119
*/
117-
export async function runSaveTrigger(option, optionValue) {
120+
export async function runSaveTrigger(option, optionValue, event) {
118121
if (option === undefined) {
119122
console.info("run all save triggers");
120123

@@ -133,32 +136,33 @@ export async function runSaveTrigger(option, optionValue) {
133136
optionValue = await OptionsModel.getOption(option);
134137
}
135138

136-
console.info("runSaveTrigger:", option, optionValue);
139+
console.info("runSaveTrigger:", option, optionValue, event);
137140

138141
// run all registered triggers for that option
139142
const promises = [];
140143
for (const trigger of triggers.onSave.filter((trigger) => trigger.option === option)) {
141-
promises.push(trigger.triggerFunc(optionValue, option));
144+
promises.push(trigger.triggerFunc(optionValue, option, event));
142145
}
143146
return Promise.all(promises);
144147
}
145148

146149
/**
147-
* Executes special handling for applying certain settings.
148-
*
149-
* E.g. when a setting is saved, it executes to apply some options live, so the
150-
* user immediately sees the change or the change is immediately applied.
151-
* If no parameters are passed, this gets and applies all options.
152-
*
153-
* @protected
154-
* @function
155-
* @param {string} option
156-
* @param {Object} optionValue
157-
* @param {Array} saveTriggerValues value returned by potentially run safe triggers
158-
* @returns {Promise}
159-
* @see {@link overrideSave}
160-
*/
161-
export async function runOverrideSave(option, optionValue, saveTriggerValues) {
150+
* Executes special handling for applying certain settings.
151+
*
152+
* E.g. when a setting is saved, it executes to apply some options live, so the
153+
* user immediately sees the change or the change is immediately applied.
154+
* If no parameters are passed, this gets and applies all options.
155+
*
156+
* @protected
157+
* @function
158+
* @param {string} option
159+
* @param {Object} optionValue
160+
* @param {Array} saveTriggerValues value returned by potentially run safe triggers
161+
* @param {Event} event the event (input or change) that triggered saving
162+
* @returns {Promise}
163+
* @see {@link overrideSave}
164+
*/
165+
export async function runOverrideSave(option, optionValue, saveTriggerValues, event) {
162166
// run all registered triggers for that option
163167
const allRegisteredOverrides = triggers.overrideSave.filter((trigger) => trigger.option === option);
164168
if (allRegisteredOverrides.length === 0) {
@@ -170,7 +174,8 @@ export async function runOverrideSave(option, optionValue, saveTriggerValues) {
170174
let lastPromise = Promise.resolve({
171175
option,
172176
optionValue,
173-
saveTriggerValues
177+
saveTriggerValues,
178+
event
174179
});
175180

176181
for (const trigger of allRegisteredOverrides) {
@@ -279,7 +284,7 @@ export async function runOverrideLoad(option, optionValue, elOption, optionValue
279284
* @function
280285
* @param {Event} event
281286
* @returns {void}
282-
* @throws {Error}
287+
* @throws {TypeError}
283288
*/
284289
export function runHtmlEventTrigger(event) {
285290
const elOption = event.target;
@@ -296,7 +301,7 @@ export function runHtmlEventTrigger(event) {
296301
triggerType = "onChange";
297302
break;
298303
default:
299-
throw new Error("invalid event type attached");
304+
throw new TypeError("invalid event type attached");
300305
}
301306

302307
// run all registered triggers for that option

0 commit comments

Comments
 (0)