-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
35 lines (35 loc) · 812 Bytes
/
events.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function ready(fn) {
if (document.readyState !== 'loading') {
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
function addEventListener(el, eventName, eventHandler, selector) {
if (selector) {
const wrappedHandler = (e) => {
if (!e.target) return;
const el = e.target.closest(selector);
if (el) {
const newEvent = Object.create(e, {
target: {
value: el
}
});
eventHandler.call(el, newEvent);
}
};
el.addEventListener(eventName, wrappedHandler);
return wrappedHandler;
} else {
const wrappedHandler = (e) => {
eventHandler.call(el, e);
};
el.addEventListener(eventName, wrappedHandler);
return wrappedHandler;
}
}
export {
addEventListener,
ready
}