This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathtouch.js
422 lines (379 loc) · 11.8 KB
/
touch.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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import targeting from './targeting';
import dispatcher from './dispatcher';
import Installer from './installer';
import mouseEvents from './mouse';
var captureInfo = dispatcher.captureInfo;
var findTarget = targeting.findTarget.bind(targeting);
var allShadows = targeting.allShadows.bind(targeting);
var pointermap = dispatcher.pointermap;
// this should be long enough to ignore compat mouse events made by touch
var DEDUP_TIMEOUT = 2500;
var ATTRIB = 'touch-action';
var INSTALLER;
// bitmask for _scrollType
var UP = 1;
var DOWN = 2;
var LEFT = 4;
var RIGHT = 8;
var AUTO = UP | DOWN | LEFT | RIGHT;
// handler block for native touch events
var touchEvents = {
events: [
'touchstart',
'touchmove',
'touchforcechange',
'touchend',
'touchcancel'
],
register: function(target) {
INSTALLER.enableOnSubtree(target);
},
unregister: function() {
// TODO(dfreedman): is it worth it to disconnect the MO?
},
elementAdded: function(el) {
var a = el.getAttribute(ATTRIB);
var st = this.touchActionToScrollType(a);
if (typeof st === "number") {
el._scrollType = st;
dispatcher.listen(el, this.events);
// set touch-action on shadows as well
allShadows(el).forEach(function(s) {
s._scrollType = st;
dispatcher.listen(s, this.events);
}, this);
}
},
elementRemoved: function(el) {
// In some cases, an element is removed before a touchend.
// When this is the case, we should wait for the touchend before unlistening,
// because we still want pointer events to bubble up after removing from DOM.
if (pointermap.size > 0) {
var evts = this.events;
el.addEventListener('touchend', function() {
el._scrollType = undefined;
dispatcher.unlisten(el, evts);
});
} else {
el._scrollType = undefined;
dispatcher.unlisten(el, this.events);
}
// remove touch-action from shadow
allShadows(el).forEach(function(s) {
s._scrollType = undefined;
dispatcher.unlisten(s, this.events);
}, this);
},
elementChanged: function(el, oldValue) {
var a = el.getAttribute(ATTRIB);
var st = this.touchActionToScrollType(a);
var oldSt = this.touchActionToScrollType(oldValue);
// simply update scrollType if listeners are already established
if (typeof st === "number" && typeof oldSt === "number") {
el._scrollType = st;
allShadows(el).forEach(function(s) {
s._scrollType = st;
}, this);
} else if (typeof oldSt === "number") {
this.elementRemoved(el);
} else if (typeof st === "number") {
this.elementAdded(el);
}
},
scrollTypes: {
UP: function(s) {
return s.includes('pan-y') || s.includes('pan-up') ? UP : 0;
},
DOWN: function(s) {
return s.includes('pan-y') || s.includes('pan-down') ? DOWN : 0;
},
LEFT: function(s) {
return s.includes('pan-x') || s.includes('pan-left') ? LEFT : 0;
},
RIGHT: function(s) {
return s.includes('pan-x') || s.includes('pan-right') ? RIGHT : 0;
}
},
touchActionToScrollType: function(touchAction) {
if (!touchAction) {
return;
}
if (touchAction === "auto") {
return AUTO;
}
if (touchAction === "none") {
return 0;
}
var s = touchAction.split(' ');
var st = this.scrollTypes;
// construct a bitmask of allowed scroll directions
return st.UP(s) | st.DOWN(s) | st.LEFT(s) | st.RIGHT(s);
},
POINTER_TYPE: 'touch',
firstTouch: null,
isPrimaryTouch: function(inTouch) {
return this.firstTouch === inTouch.identifier;
},
setPrimaryTouch: function(inTouch) {
// set primary touch if there no pointers, or the only pointer is the mouse
if (pointermap.size === 0 || (pointermap.size === 1 && pointermap.has(1))) {
this.firstTouch = inTouch.identifier;
this.firstXY = { X: inTouch.clientX, Y: inTouch.clientY };
this.scrolling = false;
}
},
removePrimaryPointer: function(inPointer) {
if (inPointer.isPrimary) {
this.firstTouch = null;
this.firstXY = null;
}
},
typeToButtons: function(type) {
var ret = 0;
if (type === 'touchstart' || type === 'touchmove' || type === 'touchforcechange') {
ret = 1;
}
return ret;
},
touchToPointer: function(inTouch) {
var cte = this.currentTouchEvent;
var e = dispatcher.cloneEvent(inTouch);
// We reserve pointerId 1 for Mouse.
// Touch identifiers can start at 0.
// Add 2 to the touch identifier for compatibility.
var id = e.pointerId = inTouch.identifier + 2;
e.target = captureInfo[id] || findTarget(e);
e.bubbles = true;
e.cancelable = true;
e.button = 0;
e.buttons = this.typeToButtons(cte.type);
e.width = (inTouch.radiusX || inTouch.webkitRadiusX || 0) * 2;
e.height = (inTouch.radiusY || inTouch.webkitRadiusY || 0) * 2;
e.pressure = inTouch.force !== undefined ?
inTouch.force :
inTouch.webkitForce !== undefined ?
inTouch.webkitForce : undefined;
e.isPrimary = this.isPrimaryTouch(inTouch);
if (inTouch.altitudeAngle) {
var tan = Math.tan(inTouch.altitudeAngle);
var radToDeg = 180 / Math.PI;
e.tiltX = Math.atan(Math.cos(inTouch.azimuthAngle) / tan) * radToDeg;
e.tiltY = Math.atan(Math.sin(inTouch.azimuthAngle) / tan) * radToDeg;
} else {
e.tiltX = 0;
e.tiltY = 0;
}
if (inTouch.touchType === 'stylus') {
e.pointerType = 'pen';
} else {
e.pointerType = this.POINTER_TYPE;
}
// forward modifier keys
e.altKey = cte.altKey;
e.ctrlKey = cte.ctrlKey;
e.metaKey = cte.metaKey;
e.shiftKey = cte.shiftKey;
// forward touch preventDefaults
var self = this;
e.preventDefault = function() {
self.scrolling = false;
self.firstXY = null;
cte.preventDefault();
};
return e;
},
processTouches: function(inEvent, inFunction) {
var tl = inEvent.changedTouches;
this.currentTouchEvent = inEvent;
for (var i = 0, t; i < tl.length; i++) {
t = tl[i];
inFunction.call(this, this.touchToPointer(t));
}
},
// For single axis scrollers, determines whether the element should emit
// pointer events or behave as a scroller
shouldScroll: function(inEvent) {
if (this.firstXY) {
var ret;
var st = inEvent.currentTarget._scrollType;
if (st === 0) {
// this element is a `touch-action: none`, should never scroll
ret = false;
} else if (st === AUTO) {
// this element is a `touch-action: auto`, should always scroll
ret = true;
} else {
var t = inEvent.changedTouches[0];
var dy = t.clientY - this.firstXY.Y;
var dya = Math.abs(dy);
var dx = t.clientX - this.firstXY.X;
var dxa = Math.abs(dx);
var up = st & UP;
var down = st & DOWN;
var left = st & LEFT;
var right = st & RIGHT;
if (left && right) {
// should scroll on the x axis
ret = dxa > dya;
} else if (left) {
// should scroll left
ret = dxa > dya && dx > 0;
} else if (right) {
// should scroll right
ret = dxa > dya && dx < 0;
}
if (!ret) {
if (up && down) {
// should scroll on the y axis
ret = dxa < dya;
} else if (up) {
// should scroll up
ret = dxa < dya && dy > 0;
} else if (down) {
// should scroll down
ret = dxa < dya && dy < 0;
}
}
}
this.firstXY = null;
return ret;
}
},
findTouch: function(inTL, inId) {
for (var i = 0, l = inTL.length, t; i < l && (t = inTL[i]); i++) {
if (t.identifier === inId) {
return true;
}
}
},
// In some instances, a touchstart can happen without a touchend. This
// leaves the pointermap in a broken state.
// Therefore, on every touchstart, we remove the touches that did not fire a
// touchend event.
// To keep state globally consistent, we fire a
// pointercancel for this "abandoned" touch
vacuumTouches: function(inEvent) {
var tl = inEvent.touches;
// pointermap.size should be < tl.length here, as the touchstart has not
// been processed yet.
if (pointermap.size >= tl.length) {
var d = [];
pointermap.forEach(function(value, key) {
// Never remove pointerId == 1, which is mouse.
// Touch identifiers are 2 smaller than their pointerId, which is the
// index in pointermap.
if (key !== 1 && !this.findTouch(tl, key - 2)) {
var p = value.out;
d.push(p);
}
}, this);
d.forEach(this.cancelOut, this);
}
},
touchstart: function(inEvent) {
this.vacuumTouches(inEvent);
this.setPrimaryTouch(inEvent.changedTouches[0]);
this.dedupSynthMouse(inEvent);
if (!this.scrolling) {
this.processTouches(inEvent, this.overDown);
}
},
overDown: function(inPointer) {
pointermap.set(inPointer.pointerId, {
target: inPointer.target,
out: inPointer,
outTarget: inPointer.target
});
dispatcher.enterOver(inPointer);
dispatcher.down(inPointer);
},
// Called when pressure or tilt changes without the x/y changing
touchforcechange: function(inEvent) {
this.touchmove(inEvent);
},
touchmove: function(inEvent) {
if (!this.scrolling) {
if (this.shouldScroll(inEvent)) {
this.scrolling = true;
this.touchcancel(inEvent);
} else {
if (inEvent.type !== 'touchforcechange') {
inEvent.preventDefault();
}
this.processTouches(inEvent, this.moveOverOut);
}
}
},
moveOverOut: function(inPointer) {
var event = inPointer;
var pointer = pointermap.get(event.pointerId);
// a finger drifted off the screen, ignore it
if (!pointer) {
return;
}
var outEvent = pointer.out;
var outTarget = pointer.outTarget;
dispatcher.move(event);
if (outEvent && outTarget !== event.target) {
outEvent.relatedTarget = event.target;
event.relatedTarget = outTarget;
// recover from retargeting by shadow
outEvent.target = outTarget;
if (event.target) {
dispatcher.leaveOut(outEvent);
dispatcher.enterOver(event);
} else {
// clean up case when finger leaves the screen
event.target = outTarget;
event.relatedTarget = null;
this.cancelOut(event);
}
}
pointer.out = event;
pointer.outTarget = event.target;
},
touchend: function(inEvent) {
this.dedupSynthMouse(inEvent);
this.processTouches(inEvent, this.upOut);
},
upOut: function(inPointer) {
if (!this.scrolling) {
dispatcher.up(inPointer);
dispatcher.leaveOut(inPointer);
}
this.cleanUpPointer(inPointer);
},
touchcancel: function(inEvent) {
this.processTouches(inEvent, this.cancelOut);
},
cancelOut: function(inPointer) {
dispatcher.cancel(inPointer);
dispatcher.leaveOut(inPointer);
this.cleanUpPointer(inPointer);
},
cleanUpPointer: function(inPointer) {
pointermap.delete(inPointer.pointerId);
this.removePrimaryPointer(inPointer);
},
// prevent synth mouse events from creating pointer events
dedupSynthMouse: function(inEvent) {
var lts = mouseEvents.lastTouches;
var t = inEvent.changedTouches[0];
// only the primary finger will synth mouse events
if (this.isPrimaryTouch(t)) {
// remember x/y of last touch
var lt = { x: t.clientX, y: t.clientY };
lts.push(lt);
var fn = (function(lts, lt) {
var i = lts.indexOf(lt);
if (i > -1) {
lts.splice(i, 1);
}
}).bind(null, lts, lt);
setTimeout(fn, DEDUP_TIMEOUT);
}
}
};
INSTALLER = new Installer(touchEvents.elementAdded, touchEvents.elementRemoved,
touchEvents.elementChanged, touchEvents);
export default touchEvents;