forked from stenciljs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.ts
More file actions
253 lines (207 loc) · 6.23 KB
/
event.ts
File metadata and controls
253 lines (207 loc) · 6.23 KB
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
import { NODE_NAMES } from './constants';
import { MockDocument } from './document';
import { MockElement } from './node';
import { MockWindow } from './window';
export class MockEvent {
bubbles = false;
cancelBubble = false;
cancelable = false;
composed = false;
currentTarget: MockElement = null;
defaultPrevented = false;
srcElement: MockElement = null;
target: MockElement = null;
timeStamp: number;
type: string;
constructor(type: string, eventInitDict?: EventInit) {
if (typeof type !== 'string') {
throw new Error(`Event type required`);
}
this.type = type;
this.timeStamp = Date.now();
if (eventInitDict != null) {
Object.assign(this, eventInitDict);
}
}
preventDefault() {
this.defaultPrevented = true;
}
stopPropagation() {
this.cancelBubble = true;
}
stopImmediatePropagation() {
this.cancelBubble = true;
}
/**
* @ref https://developer.mozilla.org/en-US/docs/Web/API/Event/composedPath
* @returns a composed path of the event
*/
composedPath(): MockElement[] {
const composedPath: MockElement[] = [];
let currentElement = this.target;
while (currentElement) {
composedPath.push(currentElement);
if (!currentElement.parentElement && currentElement.nodeName === NODE_NAMES.DOCUMENT_NODE) {
// the current element doesn't have a parent, but we've detected it's our root document node. push the window
// object associated with the document onto the path
composedPath.push((currentElement as MockDocument).defaultView);
break;
}
/**
* bubble up the parent chain until we arrive to the HTML element. Here we continue
* with the document object instead of the parent element since the parent element
* is `null` for HTML elements.
*/
if (currentElement.parentElement == null && currentElement.tagName === 'HTML') {
currentElement = currentElement.ownerDocument;
} else {
currentElement = currentElement.parentElement;
}
}
return composedPath;
}
}
export class MockCustomEvent extends MockEvent {
detail: any = null;
constructor(type: string, customEventInitDic?: CustomEventInit) {
super(type);
if (customEventInitDic != null) {
Object.assign(this, customEventInitDic);
}
}
}
export class MockKeyboardEvent extends MockEvent {
code = '';
key = '';
altKey = false;
ctrlKey = false;
metaKey = false;
shiftKey = false;
location = 0;
repeat = false;
constructor(type: string, keyboardEventInitDic?: KeyboardEventInit) {
super(type);
if (keyboardEventInitDic != null) {
Object.assign(this, keyboardEventInitDic);
}
}
}
export class MockMouseEvent extends MockEvent {
screenX = 0;
screenY = 0;
clientX = 0;
clientY = 0;
ctrlKey = false;
shiftKey = false;
altKey = false;
metaKey = false;
button = 0;
buttons = 0;
relatedTarget: EventTarget = null;
constructor(type: string, mouseEventInitDic?: MouseEventInit) {
super(type);
if (mouseEventInitDic != null) {
Object.assign(this, mouseEventInitDic);
}
}
}
export class MockUIEvent extends MockEvent {
detail: number | null = null;
view: MockWindow | null = null;
constructor(type: string, uiEventInitDic?: UIEventInit) {
super(type);
if (uiEventInitDic != null) {
Object.assign(this, uiEventInitDic);
}
}
}
export class MockFocusEvent extends MockUIEvent {
relatedTarget: EventTarget | null = null;
constructor(type: 'blur' | 'focus', focusEventInitDic?: FocusEventInit) {
super(type);
if (focusEventInitDic != null) {
Object.assign(this, focusEventInitDic);
}
}
}
export class MockEventListener {
type: string;
handler: (ev?: any) => void;
constructor(type: string, handler: any) {
this.type = type;
this.handler = handler;
}
}
export function addEventListener(elm: any, type: string, handler: any) {
const target: EventTarget = elm;
if (target.__listeners == null) {
target.__listeners = [];
}
target.__listeners.push(new MockEventListener(type, handler));
}
export function removeEventListener(elm: any, type: string, handler: any) {
const target: EventTarget = elm;
if (target != null && Array.isArray(target.__listeners) === true) {
const elmListener = target.__listeners.find((e) => e.type === type && e.handler === handler);
if (elmListener != null) {
const index = target.__listeners.indexOf(elmListener);
target.__listeners.splice(index, 1);
}
}
}
export function resetEventListeners(target: any) {
if (target != null && (target as EventTarget).__listeners != null) {
(target as EventTarget).__listeners = null;
}
}
function triggerEventListener(elm: any, ev: MockEvent) {
if (elm == null || ev.cancelBubble === true) {
return;
}
const target: EventTarget = elm;
ev.currentTarget = elm;
if (Array.isArray(target.__listeners) === true) {
const listeners = target.__listeners.filter((e) => e.type === ev.type);
listeners.forEach((listener) => {
try {
listener.handler.call(target, ev);
} catch (err) {
console.error(err);
}
});
}
if (ev.bubbles === false) {
return;
}
if (elm.nodeName === NODE_NAMES.DOCUMENT_NODE) {
triggerEventListener((elm as MockDocument).defaultView, ev);
} else if (elm.parentElement == null && elm.tagName === 'HTML') {
triggerEventListener(elm.ownerDocument, ev);
} else {
const nextTarget = getNextEventTarget(elm, ev);
triggerEventListener(nextTarget, ev);
}
}
function getNextEventTarget(elm: any, ev: MockEvent) {
// If current element has a parent, bubble to parent
if (elm.parentElement) {
return elm.parentElement;
}
// If current element is a Shadow Root (has host property), bubble to the host
if (elm.host && ev.composed) {
return elm.host;
}
// If we're at a Shadow Root boundary and event is composed, bubble to Shadow Host
if (ev.composed && elm.parentNode && elm.parentNode.host) {
return elm.parentNode.host;
}
return null;
}
export function dispatchEvent(currentTarget: any, ev: MockEvent) {
ev.target = currentTarget;
triggerEventListener(currentTarget, ev);
return true;
}
export interface EventTarget {
__listeners: MockEventListener[];
}