forked from KODerFunk/delegated-events
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delegated-events.js
167 lines (140 loc) · 4.28 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
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
import SelectorSet from 'selector-set';
const eventsPoolMap = new WeakMap();
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 !== Node.ELEMENT_NODE) 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 dispatch(event) {
const eventsPool = eventsPoolMap.get(event.currentTarget);
const capture = event.eventPhase === Event.CAPTURING_PHASE;
const events = capture ? eventsPool.captureEvents : eventsPool.bubbleEvents;
const selectors = events[event.type];
if (!selectors) return;
const queue = matches(selectors, event.target, capture);
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.has(event)) break;
const matched = queue[i];
currentTargets.set(event, matched.node);
for (let j = 0, len2 = matched.observers.length; j < len2; j++) {
if (immediatePropagationStopped.has(event)) break;
matched.observers[j].data.call(matched.node, event);
}
}
currentTargets.delete(event);
defineCurrentTarget(event);
}
/**
* @param {Element | Document} element — host element
* @param {string} name — event type
* @param {string} selector
* @param {EventListener} fn
* @param {boolean | AddEventListenerOptions} [options]
*/
export function on(element, name, selector, fn, options = {}) {
let eventsPool = eventsPoolMap.get(element);
if (!eventsPool) {
eventsPool = {
bubbleEvents: {},
captureEvents: {}
};
eventsPoolMap.set(element, eventsPool);
}
const capture = typeof options === 'boolean' ? options : options.capture;
const events = capture ? eventsPool.captureEvents : eventsPool.bubbleEvents;
let selectors = events[name];
if (!selectors) {
selectors = new SelectorSet();
events[name] = selectors;
element.addEventListener(name, dispatch, capture);
}
selectors.add(selector, fn);
}
/**
* @param {Element | Document} element — host element
* @param {string} name — event type
* @param {string} [selector]
* @param {EventListener} [fn]
* @param {boolean | AddEventListenerOptions} [options]
*/
export function off(element, name, selector, fn, options = {}) {
const eventsPool = eventsPoolMap.get(element);
if (!eventsPool) return;
const capture = typeof options === 'boolean' ? options : options.capture;
const events = capture ? eventsPool.captureEvents : eventsPool.bubbleEvents;
const selectors = events[name];
if (!selectors) return;
if (selector) {
if (fn) {
selectors.remove(selector, fn);
} else {
selectors.remove(selector);
}
if (selectors.size) return;
}
delete events[name];
element.removeEventListener(name, dispatch, capture);
}
/**
* @param {EventTarget} target
* @param {string} name — event type
* @param {any} [detail]
*/
export function fire(target, name, detail) {
return target.dispatchEvent(
new CustomEvent(name, {
bubbles: true,
cancelable: true,
detail: detail
})
);
}