-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
88 lines (73 loc) · 2.43 KB
/
index.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
// @flow
import { collectAncestors } from '../DOMComponentTree';
import SyntheticEvent from './SyntheticEvent';
export const Phases = {
CAPTURING: 1,
BUBBLING: 3,
};
const ElementHandlers: WeakMap<
Element | Text,
Map<string, Function>,
> = new WeakMap();
function traverseTwoPhase(
inst: Element,
callback: Function,
event: SyntheticEvent,
) {
const path = collectAncestors(inst);
let i;
for (i = path.length; i-- > 0; )
if (callback(path[i], Phases.CAPTURING, event)) return;
for (i = 0; i < path.length; i++)
if (callback(path[i], Phases.BUBBLING, event)) return;
}
export function listenTo(
domElement: Element,
eventName: string,
value: ?Function,
lastValue: ?Function,
) {
let handlers = ElementHandlers.get(domElement);
if (!handlers) ElementHandlers.set(domElement, (handlers = new Map()));
eventName = eventName.toLowerCase();
let phase = Phases.BUBBLING;
if (eventName.endsWith('capture')) {
eventName = eventName.slice(0, -7);
phase = Phases.CAPTURING;
}
const originalName = eventName;
const key = `${originalName}-${phase}`;
eventName = originalName === 'change' ? 'input' : originalName;
if (!value) {
domElement.removeEventListener(eventName, handlerProxy, true);
handlers.delete(key);
} else {
if (!lastValue) domElement.addEventListener(eventName, handlerProxy, true);
handlers.set(key, value);
}
}
const HandledEvents = new WeakSet();
export function handlerProxy(nativeEvent: Event) {
if (HandledEvents.has(nativeEvent)) return;
HandledEvents.add(nativeEvent);
const target = (nativeEvent: any).target;
dispatchSyntheticEvent(target, new SyntheticEvent(nativeEvent));
// if the event is an input event we need to also check for onChange events
if (nativeEvent.type === 'input') {
dispatchSyntheticEvent(target, new SyntheticEvent(nativeEvent, 'change'));
}
}
export function dispatchSyntheticEvent(target: Element, event: SyntheticEvent) {
// TODO: probably more traversal options for different events,
// e.g. mouseenter/leave for portal compat
traverseTwoPhase(target, executeHandlers, event);
}
function executeHandlers(element: Element, phase, event): ?boolean {
const handlers = ElementHandlers.get(element);
const handler = handlers && handlers.get(`${event.type}-${phase}`);
if (!handler) return;
event.currentTarget = element;
event.eventPhase = phase;
handler.call(element, event);
return event.isPropagationStopped();
}