Skip to content

Commit

Permalink
Allow trigger actions to be one-way
Browse files Browse the repository at this point in the history
  • Loading branch information
bennothommo committed Jul 19, 2024
1 parent 7635ca3 commit 1319d58
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions modules/system/assets/js/snowboard/extras/Trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default class Trigger extends PluginBase {
return;
}

const triggerParts = /([a-z0-9\-.:_]+?)(?:(?:-)(closest-parent|condition|when|action|parent|priority|do))?$/i.exec(
const triggerParts = /([a-z0-9\-.:_]+?)(?:(?:-)(closest-parent|condition|when|action|parent|priority|do))?(?:(?<=(?:action|do)(\.oneway)?)(\.oneway))?$/i.exec(
dashStyle.replace('trigger-', '').toLowerCase(),
);

Expand Down Expand Up @@ -194,7 +194,7 @@ export default class Trigger extends PluginBase {
*
* @param {string} command
* @param {string} allowMultiple
* @returns {{name: string, parameters: string[]}[]}
* @returns {{name: string, parameters: string[], oneWay: boolean}[]}
*/
parseCommand(command, allowMultiple = true) {
// Support old-format value command (value[foo,bar])
Expand All @@ -219,6 +219,7 @@ export default class Trigger extends PluginBase {
return [{
name: 'value',
parameters: values,
oneWay: false,
}];
}

Expand All @@ -237,18 +238,41 @@ export default class Trigger extends PluginBase {
}

if (!command.includes(':')) {
if (
command.includes('.oneway')
&& (command.startsWith('do') || command.startsWith('action'))
) {
return [{
name: command.replace('.oneway', ''),
parameters: [],
oneWay: true,
}];
}

return [{
name: command,
parameters: [],
oneWay: false,
}];
}

const [name, parameters] = command.split(':', 2);
let [name] = command.split(':', 2);
const [, parameters] = command.split(':', 2);
let oneWay = false;

if (
name.includes('.oneway')
&& (name.startsWith('do') || name.startsWith('action'))
) {
name = command.replace('.oneway', '');
oneWay = true;
}

if (!parameters.includes(',')) {
return [{
name,
parameters: [parameters],
oneWay,
}];
}

Expand All @@ -259,6 +283,7 @@ export default class Trigger extends PluginBase {
return [{
name,
parameters: splitValues,
oneWay,
}];
}

Expand Down Expand Up @@ -294,8 +319,6 @@ export default class Trigger extends PluginBase {
'oneof',
'allof',
'focus',
'attr',
'class',
].includes(condition.name.toLowerCase()));
}

Expand Down Expand Up @@ -683,6 +706,11 @@ export default class Trigger extends PluginBase {
return;
}

// A one-way action won't occur if the condition is not met
if (action.oneWay && !conditionMet) {
return;
}

switch (action.name) {
case 'show':
case 'hide':
Expand Down Expand Up @@ -743,6 +771,7 @@ export default class Trigger extends PluginBase {
? action.parameters.slice(1)
: action.parameters,
);
break;
default:
}
});
Expand All @@ -765,7 +794,7 @@ export default class Trigger extends PluginBase {
if (element.dataset.originalDisplay !== undefined) {
element.style.display = element.dataset.originalDisplay;
delete element.dataset.originalDisplay;
} else if (element.style.display ) {
} else if (element.style.display) {
element.style.display = null;
}

Expand Down

0 comments on commit 1319d58

Please sign in to comment.