-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdelegate.js
More file actions
21 lines (17 loc) · 858 Bytes
/
delegate.js
File metadata and controls
21 lines (17 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const trigger = e => e.composedPath()[0];
const matchesTrigger = (e, selectorString) => trigger(e).matches(selectorString);
// create on listener
export const delegate = (target) => (eventName, selectorString, event, remove = false) => { // focus doesn't work with this, focus doesn't bubble, need focusin
if (target.getAttribute(eventName) === "true" && remove) {
console.log("removing old listeners")
target.listeners.forEach( e => target.removeEventListener(eventName, e) );
} else {
target.setAttribute(eventName, "true");
}
const func = (e) => {
e.trigger = trigger(e); // Do I need this? e.target seems to work in many (all?) cases
if (selectorString === "" || matchesTrigger(e, selectorString)) event(e);
}
target.addEventListener(eventName, func)
target.listeners = target.listeners ? [ ...target.listeners, func ] : [ func ];
}