-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
dom-event.ts
393 lines (341 loc) · 12.9 KB
/
dom-event.ts
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import type { EventData, ListenerEntry, Observable } from '../observable/index';
import type { ViewBase } from '../../ui/core/view-base';
const timeOrigin = Date.now();
/**
* Purely a performance utility. We fall back to an empty array on various
* optional accesses, so reusing the same one and treating it as immutable
* avoids unnecessary allocations on a relatively hot path of the library.
*/
const emptyArray = [] as const;
export class DOMEvent implements Event {
/**
* @private
* Internal API to facilitate testing - to be removed once we've completed
* the breaking changes to migrate fully to DOMEvents.
*
* Gets the last event to be dispatched, allowing you to access the DOM
* Event that corresponds to the currently-running callback.
*/
static unstable_currentEvent: DOMEvent | null = null;
readonly NONE = 0;
readonly CAPTURING_PHASE = 1;
readonly AT_TARGET = 2;
readonly BUBBLING_PHASE = 3;
/**
* Returns true or false depending on how event was initialized. Its return
* value does not always carry meaning, but true can indicate that part of
* the operation during which event was dispatched, can be canceled by
* invoking the preventDefault() method.
*/
readonly cancelable: boolean = false;
/**
* Returns true or false depending on how event was initialized. True if
* event goes through its target's ancestors in reverse tree order, and
* false otherwise.
*/
readonly bubbles: boolean = false;
private _canceled = false;
/** @deprecated Setting this value does nothing. */
cancelBubble = false;
/**
* Returns true or false depending on how event was initialized. True if
* event invokes listeners past a ShadowRoot node that is the root of its
* target, and false otherwise.
*/
readonly composed: boolean;
/**
* Returns true if event was dispatched by the user agent, and false
* otherwise.
* For now, all NativeScript events will have isTrusted: false.
*/
readonly isTrusted: boolean = false;
/** @deprecated Use defaultPrevented instead. */
get returnValue() {
return !this.defaultPrevented;
}
/**
* Returns the event's timestamp as the number of milliseconds measured
* relative to the time origin.
*/
readonly timeStamp: DOMHighResTimeStamp = timeOrigin - Date.now();
/** @deprecated */
get srcElement(): Observable | null {
return this.target;
}
/**
* Returns true if preventDefault() was invoked successfully to indicate
* cancelation, and false otherwise.
*/
get defaultPrevented() {
return this._canceled;
}
private _eventPhase: 0 | 1 | 2 | 3 = this.NONE;
/**
* Returns the event's phase, which is one of NONE, CAPTURING_PHASE,
* AT_TARGET, and BUBBLING_PHASE.
*/
get eventPhase() {
return this._eventPhase;
}
private set eventPhase(value: 0 | 1 | 2 | 3) {
this._eventPhase = value;
}
private _currentTarget: Observable | null = null;
/**
* Returns the object whose event listener's callback is currently being
* invoked.
*/
get currentTarget() {
return this._currentTarget;
}
private set currentTarget(value: Observable | null) {
this._currentTarget = value;
}
private _target: Observable | null = null;
/** Returns the object to which event is dispatched (its target). */
get target() {
return this._target;
}
private set target(value: Observable | null) {
this._target = value;
}
// From CustomEvent rather than Event. Can consider factoring out this
// aspect into DOMCustomEvent.
readonly detail: unknown | null;
private propagationState: EventPropagationState = EventPropagationState.resume;
constructor(
/**
* Returns the type of event, e.g. "click", "hashchange", or "submit".
*/
public type: string,
options: CustomEventInit = {}
) {
const { bubbles = false, cancelable = false, composed = false, detail = null } = options;
this.bubbles = bubbles;
this.cancelable = cancelable;
this.composed = composed;
this.detail = detail;
}
/**
* Returns the invocation target objects of event's path (objects on which
* listeners will be invoked), except for any nodes in shadow trees of which
* the shadow root's mode is "closed" that are not reachable from event's
* currentTarget.
*/
composedPath(): Observable[] {
if (!this.target) {
return [];
}
// Walk up the target's parents if it has parents (is a ViewBase or
// subclass of ViewBase) or not (is an Observable).
return this.target.isViewBase() ? this.getEventPath(this.target, 'bubble') : [this.target];
}
/**
* Returns the event path by walking up the target's parents.
*
* - 'capture' paths are ordered from root to target.
* - 'bubble' paths are ordered from target to root.
* @example
* [Page, StackLayout, Button] // 'capture'
* @example
* [Button, StackLayout, Page] // 'bubble'
*/
private getEventPath(responder: ViewBase, path: 'capture' | 'bubble'): ViewBase[] {
const chain = [responder];
let nextResponder = responder.parent;
while (nextResponder) {
path === 'capture' ? chain.unshift(nextResponder) : chain.push(nextResponder);
// TODO: decide whether to walk up from Page to Frame, and whether
// to then walk from Frame to Application or something.
nextResponder = nextResponder?.parent;
}
return chain;
}
/** @deprecated */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
initEvent(type: string, bubbles?: boolean, cancelable?: boolean): void {
// This would be trivial to implement, but it's quite nice for `bubbles`
// and `cancelable` to not have backing variables.
throw new Error('Deprecated; use Event() instead.');
}
/**
* If invoked when the cancelable attribute value is true, and while
* executing a listener for the event with passive set to false, signals to
* the operation that caused event to be dispatched that it needs to be
* canceled.
*/
preventDefault(): void {
if (!this.cancelable) {
return;
}
this._canceled = true;
}
/**
* Invoking this method prevents event from reaching any registered event
* listeners after the current one finishes running and, when dispatched in
* a tree, also prevents event from reaching any other objects.
*/
stopImmediatePropagation(): void {
this.propagationState = EventPropagationState.stopImmediate;
}
/**
* When dispatched in a tree, invoking this method prevents event from
* reaching any objects other than the current object.
*/
stopPropagation(): void {
this.propagationState = EventPropagationState.stop;
}
/**
* Dispatches a synthetic event event to target and returns true if either
* event's cancelable attribute value is false or its preventDefault()
* method was not invoked, and false otherwise.
*/
dispatchTo({ target, data, getGlobalEventHandlersPreHandling, getGlobalEventHandlersPostHandling }: { target: Observable; data: EventData; getGlobalEventHandlersPreHandling?: () => readonly ListenerEntry[]; getGlobalEventHandlersPostHandling?: () => readonly ListenerEntry[] }): boolean {
if (this.eventPhase !== this.NONE) {
throw new Error('Tried to dispatch a dispatching event');
}
this.eventPhase = this.CAPTURING_PHASE;
this.target = target;
this._canceled = false;
// Internal API to facilitate testing - to be removed once we've
// completed the breaking changes to migrate fully to DOMEvents.
DOMEvent.unstable_currentEvent = this;
/**
* Resets any internal state to allow the event to be redispatched. Call
* this before returning.
*/
const reset = () => {
this.currentTarget = null;
this.target = null;
this.eventPhase = this.NONE;
this.propagationState = EventPropagationState.resume;
};
// `Observable.removeEventListener` would likely suffice, but grabbing
// the static method named `removeEventListener` on the target's class
// allows us to be robust to the possiblity of the case of the target
// overriding it (however unlikely).
const removeGlobalEventListener = (target.constructor as unknown as typeof target).removeEventListener.bind(target.constructor) as Observable['removeEventListener'];
// Global event handlers are a NativeScript-only concept, so we'll not
// try to add new formal event phases for them (as that could break DOM
// libraries expecting strictly four phases).
//
// Instead, events handled by global event handlers will exhibit the
// following values:
// - For 'pre-handling phase' global event handlers:
// - eventPhase: CAPTURING_PHASE
// - currentTarget: null
// - For 'post-handling phase' global event handlers:
// - eventPhase: BUBBLING_PHASE
// - currentTarget: The value of currentTarget following the capturing
// and bubbling phases.
// So effectively, we don't make any changes when handling a global
// event. This keeps behaviour as consistent with DOM Events as
// possible.
this.handleEvent({
data,
isGlobal: true,
getListenersForType: () => getGlobalEventHandlersPreHandling?.() ?? emptyArray,
removeEventListener: removeGlobalEventListener,
phase: this.CAPTURING_PHASE,
});
const eventPath = target.isViewBase() ? this.getEventPath(target, 'capture') : [target];
// Capturing phase, e.g. [Page, StackLayout, Button]
for (const currentTarget of eventPath) {
this.currentTarget = currentTarget;
this.eventPhase = this.target === this.currentTarget ? this.AT_TARGET : this.CAPTURING_PHASE;
this.handleEvent({
data,
isGlobal: false,
getListenersForType: () => currentTarget.getEventList(this.type) ?? emptyArray,
removeEventListener: currentTarget.removeEventListener.bind(currentTarget) as Observable['removeEventListener'],
phase: this.CAPTURING_PHASE,
});
if (this.propagationState !== EventPropagationState.resume) {
reset();
return this.returnValue;
}
}
// Bubbling phase, e.g. [Button, StackLayout, Page]
// It's correct to dispatch the event to the target during both phases.
for (const currentTarget of eventPath.reverse()) {
this.currentTarget = currentTarget;
this.eventPhase = this.target === this.currentTarget ? this.AT_TARGET : this.BUBBLING_PHASE;
this.handleEvent({
data,
isGlobal: false,
getListenersForType: () => currentTarget.getEventList(this.type) ?? emptyArray,
removeEventListener: currentTarget.removeEventListener.bind(currentTarget) as Observable['removeEventListener'],
phase: this.BUBBLING_PHASE,
});
if (this.propagationState !== EventPropagationState.resume) {
reset();
return this.returnValue;
}
// If the event doesn't bubble, then, having dispatched it at the
// target (the first iteration of this loop) we don't let it
// propagate any further.
if (!this.bubbles) {
reset();
break;
}
// Restore event phase in case it changed to AT_TARGET during
// this.handleEvent().
this.eventPhase = this.BUBBLING_PHASE;
}
this.handleEvent({
data,
isGlobal: true,
getListenersForType: () => getGlobalEventHandlersPostHandling?.() ?? emptyArray,
removeEventListener: removeGlobalEventListener,
phase: this.BUBBLING_PHASE,
});
reset();
return this.returnValue;
}
private handleEvent({ data, isGlobal, getListenersForType, phase, removeEventListener }: { data: EventData; isGlobal: boolean; getListenersForType: () => readonly ListenerEntry[]; phase: 0 | 1 | 2 | 3; removeEventListener: (eventName: string, callback?: any, thisArg?: any, capture?: boolean) => void }) {
// Work on a copy of the array, as any callback could modify the
// original array during the loop.
const listenersForTypeCopy = getListenersForType().slice();
for (let i = listenersForTypeCopy.length - 1; i >= 0; i--) {
const listener = listenersForTypeCopy[i];
const { callback, capture, thisArg, once, passive } = listener;
// The event listener may have been removed since we took a copy of
// the array, so bail out if so.
//
// We simply use a strict equality check here because we trust that
// the listeners provider will never allow two deeply-equal
// listeners into the array.
if (!getListenersForType().includes(listener)) {
continue;
}
// Handle only the events appropriate to the phase. Global events
// (a NativeScript-only concept) are allowed to be handled
// regardless of phase, for backwards-compatibility.
if (!isGlobal && ((phase === this.CAPTURING_PHASE && !capture) || (phase === this.BUBBLING_PHASE && capture))) {
continue;
}
if (once) {
removeEventListener(this.type, callback, thisArg, capture);
}
// Consistent with the original implementation, we only apply
// context to the function if thisArg is truthy.
const returnValue = callback.apply(thisArg || undefined, [data]);
// This ensures that errors thrown inside asynchronous functions do
// not get swallowed.
if (returnValue instanceof Promise) {
returnValue.catch(console.error);
}
if (passive && event.defaultPrevented) {
console.warn('Unexpected call to event.preventDefault() in passive event listener.');
}
if (this.propagationState === EventPropagationState.stopImmediate) {
return;
}
}
}
}
enum EventPropagationState {
resume,
stop,
stopImmediate,
}