generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.js
236 lines (211 loc) · 6.8 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { hasCallback, getCallback } from './callbackRegistry.js';
const EVENT_PREFIX = 'data-aegis-event-on-';
const EVENT_PREFIX_LENGTH = EVENT_PREFIX.length;
const DATA_PREFIX = 'aegisEventOn';
const DATA_PREFIX_LENGTH = DATA_PREFIX.length;
const once = 'data-aegis-event-once',
passive = 'data-aegis-event-passive',
capture = 'data-aegis-event-capture';
const eventAttrs = [
EVENT_PREFIX + 'abort',
EVENT_PREFIX + 'blur',
EVENT_PREFIX + 'focus',
EVENT_PREFIX + 'cancel',
EVENT_PREFIX + 'auxclick',
EVENT_PREFIX + 'beforeinput',
EVENT_PREFIX + 'beforetoggle',
EVENT_PREFIX + 'canplay',
EVENT_PREFIX + 'canplaythrough',
EVENT_PREFIX + 'change',
EVENT_PREFIX + 'click',
EVENT_PREFIX + 'close',
EVENT_PREFIX + 'contextmenu',
EVENT_PREFIX + 'copy',
EVENT_PREFIX + 'cuechange',
EVENT_PREFIX + 'cut',
EVENT_PREFIX + 'dblclick',
EVENT_PREFIX + 'drag',
EVENT_PREFIX + 'dragend',
EVENT_PREFIX + 'dragenter',
EVENT_PREFIX + 'dragexit',
EVENT_PREFIX + 'dragleave',
EVENT_PREFIX + 'dragover',
EVENT_PREFIX + 'dragstart',
EVENT_PREFIX + 'drop',
EVENT_PREFIX + 'durationchange',
EVENT_PREFIX + 'emptied',
EVENT_PREFIX + 'ended',
EVENT_PREFIX + 'formdata',
EVENT_PREFIX + 'input',
EVENT_PREFIX + 'invalid',
EVENT_PREFIX + 'keydown',
EVENT_PREFIX + 'keypress',
EVENT_PREFIX + 'keyup',
EVENT_PREFIX + 'load',
EVENT_PREFIX + 'loadeddata',
EVENT_PREFIX + 'loadedmetadata',
EVENT_PREFIX + 'loadstart',
EVENT_PREFIX + 'mousedown',
EVENT_PREFIX + 'mouseenter',
EVENT_PREFIX + 'mouseleave',
EVENT_PREFIX + 'mousemove',
EVENT_PREFIX + 'mouseout',
EVENT_PREFIX + 'mouseover',
EVENT_PREFIX + 'mouseup',
EVENT_PREFIX + 'wheel',
EVENT_PREFIX + 'paste',
EVENT_PREFIX + 'pause',
EVENT_PREFIX + 'play',
EVENT_PREFIX + 'playing',
EVENT_PREFIX + 'progress',
EVENT_PREFIX + 'ratechange',
EVENT_PREFIX + 'reset',
EVENT_PREFIX + 'resize',
EVENT_PREFIX + 'scroll',
EVENT_PREFIX + 'scrollend',
EVENT_PREFIX + 'securitypolicyviolation',
EVENT_PREFIX + 'seeked',
EVENT_PREFIX + 'seeking',
EVENT_PREFIX + 'select',
EVENT_PREFIX + 'slotchange',
EVENT_PREFIX + 'stalled',
EVENT_PREFIX + 'submit',
EVENT_PREFIX + 'suspend',
EVENT_PREFIX + 'timeupdate',
EVENT_PREFIX + 'volumechange',
EVENT_PREFIX + 'waiting',
EVENT_PREFIX + 'selectstart',
EVENT_PREFIX + 'selectionchange',
EVENT_PREFIX + 'toggle',
EVENT_PREFIX + 'pointercancel',
EVENT_PREFIX + 'pointerdown',
EVENT_PREFIX + 'pointerup',
EVENT_PREFIX + 'pointermove',
EVENT_PREFIX + 'pointerout',
EVENT_PREFIX + 'pointerover',
EVENT_PREFIX + 'pointerenter',
EVENT_PREFIX + 'pointerleave',
EVENT_PREFIX + 'gotpointercapture',
EVENT_PREFIX + 'lostpointercapture',
EVENT_PREFIX + 'mozfullscreenchange',
EVENT_PREFIX + 'mozfullscreenerror',
EVENT_PREFIX + 'animationcancel',
EVENT_PREFIX + 'animationend',
EVENT_PREFIX + 'animationiteration',
EVENT_PREFIX + 'animationstart',
EVENT_PREFIX + 'transitioncancel',
EVENT_PREFIX + 'transitionend',
EVENT_PREFIX + 'transitionrun',
EVENT_PREFIX + 'transitionstart',
EVENT_PREFIX + 'webkitanimationend',
EVENT_PREFIX + 'webkitanimationiteration',
EVENT_PREFIX + 'webkitanimationstart',
EVENT_PREFIX + 'webkittransitionend',
EVENT_PREFIX + 'error',
];
let selector = eventAttrs.map(attr => `[${CSS.escape(attr)}]`).join(', ');
const attrToProp = attr => `on${attr[EVENT_PREFIX_LENGTH].toUpperCase()}${attr.substring(EVENT_PREFIX_LENGTH + 1)}`;
const attrEntriesMap = attr => [attrToProp(attr), attr];
const isEventDataAttr = ([name]) => name.startsWith(DATA_PREFIX);
const DATA_EVENTS = Object.fromEntries([...eventAttrs].map(attrEntriesMap));
export const EVENTS = { ...DATA_EVENTS, once, passive, capture };
export function registerEventAttribute(attr, {
addListeners = false,
base = document.body,
signal,
} = {}) {
const fullAttr = EVENT_PREFIX + attr.toLowerCase();
if (! eventAttrs.includes(fullAttr)) {
const sel = `[${CSS.escape(fullAttr)}]`;
const prop = attrToProp(fullAttr);
eventAttrs.push(fullAttr);
EVENTS[prop] = fullAttr;
selector += `, ${sel}`;
if (addListeners) {
requestAnimationFrame(() => {
const config = { attrFilter: { [prop]: sel }, signal };
[base, ...base.querySelectorAll(sel)].forEach(el => _addListeners(el, config));
});
}
}
return fullAttr;
}
const observer = new MutationObserver(records => {
records.forEach(record => {
switch(record.type) {
case 'childList':
[...record.addedNodes]
.filter(node => node.nodeType === Node.ELEMENT_NODE)
.forEach(node => attachListeners(node));
break;
case 'attributes':
if (typeof record.oldValue === 'string' && hasCallback(record.oldValue)) {
record.target.removeEventListener(
record.attributeName.substring(EVENT_PREFIX_LENGTH),
getCallback(record.oldValue), {
once: record.target.hasAttribute(once),
capture: record.target.hasAttribute(capture),
passive: record.target.hasAttribute(passive),
}
);
}
if (
record.target.hasAttribute(record.attributeName)
&& hasCallback(record.target.getAttribute(record.attributeName))
) {
record.target.addEventListener(
record.attributeName.substring(EVENT_PREFIX_LENGTH),
getCallback(record.target.getAttribute(record.attributeName)), {
once: record.target.hasAttribute(once),
capture: record.target.hasAttribute(capture),
passive: record.target.hasAttribute(passive),
}
);
}
break;
}
});
});
function _addListeners(el, { signal, attrFilter = EVENTS } = {}) {
const dataset = el.dataset;
for (const [attr, val] of Object.entries(dataset).filter(isEventDataAttr)) {
try {
const event = 'on' + attr.substring(DATA_PREFIX_LENGTH);
if (attrFilter.hasOwnProperty(event) && hasCallback(val)) {
el.addEventListener(event.substring(2).toLowerCase(), getCallback(val), {
passive: dataset.hasOwnProperty('aegisEventPassive'),
capture: dataset.hasOwnProperty('aegisEventCapture'),
once: dataset.hasOwnProperty('aegisEventOnce'),
signal,
});
}
} catch(err) {
console.error(err);
}
}
}
export function attachListeners(target, { signal } = {}) {
const nodes = target instanceof Element && target.matches(selector)
? [target, ...target.querySelectorAll(selector)]
: target.querySelectorAll(selector);
nodes.forEach(el => _addListeners(el, { signal }));
return target;
}
export const observeEvents = (root = document) => {
attachListeners(root);
observer.observe(root, {
subtree: true,
childList:true,
attributes: true,
attributeOldValue: true,
attributeFilter: eventAttrs,
});
};
export const disconnectEventsObserver = () => observer.disconnect();
export function setGlobalErrorHandler(callback, { capture, once, passive, signal } = {}) {
if (callback instanceof Function) {
globalThis.addEventListener('error', callback, { capture, once, passive, signal });
} else {
throw new TypeError('Callback is not a function.');
}
}