Skip to content

Commit

Permalink
Initial work on within/notWithin triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
bennothommo committed Jul 19, 2024
1 parent 1319d58 commit 0107abb
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion modules/system/assets/js/snowboard/extras/Trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ export default class Trigger extends PluginBase {
'oneof',
'allof',
'focus',
'within',
'notWithin',
].includes(condition.name.toLowerCase()));
}

Expand Down Expand Up @@ -386,6 +388,12 @@ export default class Trigger extends PluginBase {
this.createFocusedCondition(trigger),
);
break;
case 'within':
case 'notWithin':

This comment has been minimized.

Copy link
@mjauvin

mjauvin Jul 19, 2024

Member

cases are lowercase...

trigger.get('conditionCallbacks').push(
this.createWithinCondition(trigger, (condition.name === 'within'), ...condition.parameters),
);
break;
default:
}
});
Expand Down Expand Up @@ -648,7 +656,47 @@ export default class Trigger extends PluginBase {
this.addEvent(element, trigger, 'blur');
});

return () => Array.from(supportedElements).some((element) => document.activeElement === element);
return Array.from(supportedElements).some((element) => document.activeElement === element);
}

/**
* Creates a trigger that fires when all supported elements are within a specific element(s).
*
* @param {TriggerEntity} trigger
* @param {boolean} isWithin If the elements must be within the selector.
* @param {string} selector The selector to check if the elements are within.
*/
createWithinCondition(trigger, isWithin, selector) {
const supportedElements = new Set();

trigger.get('elements').forEach((element) => {
// All elements are supported (technically)
supportedElements.add(element);
});

supportedElements.forEach((element) => {
this.addEvent(element, trigger, 'click');
this.addEvent(element, trigger, 'change');
this.addEvent(element, trigger, 'focus');
this.addEvent(element, trigger, 'blur');
});

return Array.from(supportedElements).every(
(element) => {
let within = false;

document.querySelectorAll(selector).forEach((parent) => {
if (within === true) {
return;
}
if (parent.contains(element)) {
within = true;
}
});

return within === isWithin;
},
);
}

/**
Expand Down

0 comments on commit 0107abb

Please sign in to comment.