-
Notifications
You must be signed in to change notification settings - Fork 53
/
delegated-events.js
137 lines (113 loc) · 3.33 KB
/
delegated-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
import SelectorSet from 'selector-set';
const bubbleEvents = {};
const captureEvents = {};
const propagationStopped = new WeakMap();
const immediatePropagationStopped = new WeakMap();
const currentTargets = new WeakMap();
const currentTargetDesc = Object.getOwnPropertyDescriptor(
Event.prototype,
'currentTarget'
);
function before(subject, verb, fn) {
const source = subject[verb];
subject[verb] = function () {
fn.apply(subject, arguments);
return source.apply(subject, arguments);
};
return subject;
}
function matches(selectors, target, reverse) {
const queue = [];
let node = target;
do {
if (node.nodeType !== 1) break;
const matches = selectors.matches(node);
if (matches.length) {
const matched = {node: node, observers: matches};
if (reverse) {
queue.unshift(matched);
} else {
queue.push(matched);
}
}
} while ((node = node.parentElement));
return queue;
}
function trackPropagation() {
propagationStopped.set(this, true);
}
function trackImmediate() {
propagationStopped.set(this, true);
immediatePropagationStopped.set(this, true);
}
function getCurrentTarget() {
return currentTargets.get(this) || null;
}
function defineCurrentTarget(event, getter) {
if (!currentTargetDesc) return;
Object.defineProperty(event, 'currentTarget', {
configurable: true,
enumerable: true,
get: getter || currentTargetDesc.get
});
}
function canDispatch(event) {
try {
event.eventPhase;
return true;
} catch (_) {
return false;
}
}
function dispatch(event) {
if (!canDispatch(event)) return;
const events = event.eventPhase === 1 ? captureEvents : bubbleEvents;
const selectors = events[event.type];
if (!selectors) return;
const queue = matches(selectors, event.target, event.eventPhase === 1);
if (!queue.length) return;
before(event, 'stopPropagation', trackPropagation);
before(event, 'stopImmediatePropagation', trackImmediate);
defineCurrentTarget(event, getCurrentTarget);
for (let i = 0, len1 = queue.length; i < len1; i++) {
if (propagationStopped.get(event)) break;
const matched = queue[i];
currentTargets.set(event, matched.node);
for (let j = 0, len2 = matched.observers.length; j < len2; j++) {
if (immediatePropagationStopped.get(event)) break;
matched.observers[j].data.call(matched.node, event);
}
}
currentTargets.delete(event);
defineCurrentTarget(event);
}
export function on(name, selector, fn, options = {}) {
const capture = options.capture ? true : false;
const events = capture ? captureEvents : bubbleEvents;
let selectors = events[name];
if (!selectors) {
selectors = new SelectorSet();
events[name] = selectors;
document.addEventListener(name, dispatch, capture);
}
selectors.add(selector, fn);
}
export function off(name, selector, fn, options = {}) {
const capture = options.capture ? true : false;
const events = capture ? captureEvents : bubbleEvents;
const selectors = events[name];
if (!selectors) return;
selectors.remove(selector, fn);
if (selectors.size) return;
delete events[name];
document.removeEventListener(name, dispatch, capture);
}
export function fire(target, name, detail) {
return target.dispatchEvent(
new CustomEvent(name, {
bubbles: true,
cancelable: true,
detail: detail
})
);
}