-
Notifications
You must be signed in to change notification settings - Fork 0
/
hand.js
633 lines (538 loc) · 24.4 KB
/
hand.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
var HANDJS = HANDJS || {};
(function () {
// Polyfilling indexOf for old browsers
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement) {
var t = Object(this);
var len = t.length >>> 0;
if (len === 0) {
return -1;
}
var n = 0;
if (arguments.length > 0) {
n = Number(arguments[1]);
if (n != n) { // shortcut for verifying if it's NaN
n = 0;
} else if (n != 0 && n != Infinity && n != -Infinity) {
n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
}
if (n >= len) {
return -1;
}
var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
for (; k < len; k++) {
if (k in t && t[k] === searchElement) {
return k;
}
}
return -1;
};
}
// Installing Hand.js
var supportedEventsNames = ["pointerdown", "pointerup", "pointermove", "pointerover", "pointerout", "pointercancel", "pointerenter", "pointerleave"];
var upperCaseEventsNames = ["PointerDown", "PointerUp", "PointerMove", "PointerOver", "PointerOut", "PointerCancel", "PointerEnter", "PointerLeave"];
var POINTER_TYPE_TOUCH = "touch";
var POINTER_TYPE_PEN = "pen";
var POINTER_TYPE_MOUSE = "mouse";
var previousTargets = {};
var _checkPreventDefault = function (object) {
while (object && !object.handjs_forcePreventDefault) {
object = object.parentNode;
}
return !!object;
};
var checkPreventDefault = function(node) {
return _checkPreventDefault(node) || _checkPreventDefault(window);
};
// Touch events
var generateTouchClonedEvent = function (sourceEvent, newName, canBubble) {
// Considering touch events are almost like super mouse events
var evObj;
if (document.createEvent) {
evObj = document.createEvent('MouseEvents');
evObj.initMouseEvent(newName, canBubble, true, window, 1, sourceEvent.screenX, sourceEvent.screenY,
sourceEvent.clientX, sourceEvent.clientY, sourceEvent.ctrlKey, sourceEvent.altKey,
sourceEvent.shiftKey, sourceEvent.metaKey, sourceEvent.button, null);
}
else {
evObj = document.createEventObject();
evObj.screenX = sourceEvent.screenX;
evObj.screenY = sourceEvent.screenY;
evObj.clientX = sourceEvent.clientX;
evObj.clientY = sourceEvent.clientY;
evObj.ctrlKey = sourceEvent.ctrlKey;
evObj.altKey = sourceEvent.altKey;
evObj.shiftKey = sourceEvent.shiftKey;
evObj.metaKey = sourceEvent.metaKey;
evObj.button = sourceEvent.button;
}
// offsets
if (evObj.offsetX === undefined) {
if (sourceEvent.offsetX !== undefined) {
// For Opera which creates readonly properties
if (Object && Object.defineProperty !== undefined) {
Object.defineProperty(evObj, "offsetX", {
writable: true
});
Object.defineProperty(evObj, "offsetY", {
writable: true
});
}
evObj.offsetX = sourceEvent.offsetX;
evObj.offsetY = sourceEvent.offsetY;
}
else if (sourceEvent.layerX !== undefined) {
evObj.offsetX = sourceEvent.layerX - sourceEvent.currentTarget.offsetLeft;
evObj.offsetY = sourceEvent.layerY - sourceEvent.currentTarget.offsetTop;
}
}
// adding missing properties
if (sourceEvent.isPrimary !== undefined)
evObj.isPrimary = sourceEvent.isPrimary;
else
evObj.isPrimary = true;
if (sourceEvent.pressure)
evObj.pressure = sourceEvent.pressure;
else {
var button = 0;
if (sourceEvent.which !== undefined)
button = sourceEvent.which;
else if (sourceEvent.button !== undefined) {
button = sourceEvent.button;
}
evObj.pressure = (button == 0) ? 0 : 0.5;
}
if (sourceEvent.rotation)
evObj.rotation = sourceEvent.rotation;
else
evObj.rotation = 0;
// Timestamp
if (sourceEvent.hwTimestamp)
evObj.hwTimestamp = sourceEvent.hwTimestamp;
else
evObj.hwTimestamp = 0;
// Tilts
if (sourceEvent.tiltX)
evObj.tiltX = sourceEvent.tiltX;
else
evObj.tiltX = 0;
if (sourceEvent.tiltY)
evObj.tiltY = sourceEvent.tiltY;
else
evObj.tiltY = 0;
// Width and Height
if (sourceEvent.height)
evObj.height = sourceEvent.height;
else
evObj.height = 0;
if (sourceEvent.width)
evObj.width = sourceEvent.width;
else
evObj.width = 0;
// preventDefault
evObj.preventDefault = function () {
if (sourceEvent.preventDefault !== undefined)
sourceEvent.preventDefault();
};
// stopPropagation
if (evObj.stopPropagation !== undefined) {
var current = evObj.stopPropagation;
evObj.stopPropagation = function () {
if (sourceEvent.stopPropagation !== undefined)
sourceEvent.stopPropagation();
current.call(this);
};
}
// Constants
evObj.POINTER_TYPE_TOUCH = POINTER_TYPE_TOUCH;
evObj.POINTER_TYPE_PEN = POINTER_TYPE_PEN;
evObj.POINTER_TYPE_MOUSE = POINTER_TYPE_MOUSE;
// Pointer values
evObj.pointerId = sourceEvent.pointerId;
evObj.pointerType = sourceEvent.pointerType;
switch (evObj.pointerType) {// Old spec version check
case 2:
evObj.pointerType = evObj.POINTER_TYPE_TOUCH;
break;
case 3:
evObj.pointerType = evObj.POINTER_TYPE_PEN;
break;
case 4:
evObj.pointerType = evObj.POINTER_TYPE_MOUSE;
break;
}
// If force preventDefault
if (sourceEvent.currentTarget && checkPreventDefault(sourceEvent.currentTarget) === true) {
evObj.preventDefault();
}
// Fire event
if (sourceEvent.target) {
sourceEvent.target.dispatchEvent(evObj);
} else {
sourceEvent.srcElement.fireEvent("on" + getMouseEquivalentEventName(newName), evObj); // We must fallback to mouse event for very old browsers
}
};
var generateMouseProxy = function (evt, eventName) {
evt.pointerId = 1;
evt.pointerType = POINTER_TYPE_MOUSE;
generateTouchClonedEvent(evt, eventName, false);
};
var generateTouchEventProxy = function (name, touchPoint, target, eventObject) {
var touchPointId = touchPoint.identifier + 2; // Just to not override mouse id
touchPoint.pointerId = touchPointId;
touchPoint.pointerType = POINTER_TYPE_TOUCH;
touchPoint.currentTarget = target;
touchPoint.target = target;
if (eventObject.preventDefault !== undefined) {
touchPoint.preventDefault = function () {
eventObject.preventDefault();
};
}
generateTouchClonedEvent(touchPoint, name, true);
};
var _checkRegisteredEvents = function (object, eventName) {
while (object && !(object.__handjsGlobalRegisteredEvents && object.__handjsGlobalRegisteredEvents[eventName])) {
object = object.parentNode;
}
return !!object;
};
var checkRegisteredEvents = function (node, eventName) {
return _checkRegisteredEvents(node, eventName) || _checkRegisteredEvents(window, eventName);
};
var generateTouchEventProxyIfRegistered = function (eventName, touchPoint, target, eventObject) { // Check if user registered this event
if (checkRegisteredEvents(target, eventName)) {
generateTouchEventProxy(eventName, touchPoint, target, eventObject);
}
};
var handleOtherEvent = function (eventObject, name, useLocalTarget, checkRegistration) {
if (eventObject.preventManipulation)
eventObject.preventManipulation();
for (var i = 0; i < eventObject.changedTouches.length; ++i) {
var touchPoint = eventObject.changedTouches[i];
if (useLocalTarget) {
previousTargets[touchPoint.identifier] = touchPoint.target;
}
if (checkRegistration) {
generateTouchEventProxyIfRegistered(name, touchPoint, previousTargets[touchPoint.identifier], eventObject);
} else {
generateTouchEventProxy(name, touchPoint, previousTargets[touchPoint.identifier], eventObject);
}
}
};
var getMouseEquivalentEventName = function (eventName) {
return eventName.toLowerCase().replace("pointer", "mouse");
};
var getPrefixEventName = function (item, prefix, eventName) {
var upperCaseIndex = supportedEventsNames.indexOf(eventName);
var newEventName = prefix + upperCaseEventsNames[upperCaseIndex];
// Fallback to PointerOver if PointerEnter is not currently supported
if (newEventName === prefix + "PointerEnter" && item["on" + prefix.toLowerCase() + "pointerenter"] === undefined) {
newEventName = prefix + "PointerOver";
}
// Fallback to PointerOut if PointerLeave is not currently supported
if (newEventName === prefix + "PointerLeave" && item["on" + prefix.toLowerCase() + "pointerleave"] === undefined) {
newEventName = prefix + "PointerOut";
}
return newEventName;
};
var registerOrUnregisterEvent = function (item, name, func, enable) {
if (item.__handjsRegisteredEvents === undefined) {
item.__handjsRegisteredEvents = [];
}
if (enable) {
if (item.__handjsRegisteredEvents[name] !== undefined) {
item.__handjsRegisteredEvents[name]++;
return;
}
item.__handjsRegisteredEvents[name] = 1;
item.addEventListener(name, func, false);
} else {
if (item.__handjsRegisteredEvents.indexOf(name) !== -1) {
item.__handjsRegisteredEvents[name]--;
if (item.__handjsRegisteredEvents[name] != 0) {
return;
}
}
item.removeEventListener(name, func);
item.__handjsRegisteredEvents[name] = 0;
}
};
var setTouchAware = function (item, eventName, enable) {
// If item is already touch aware, do nothing
if (item.onpointerdown !== undefined) {
return;
}
// IE 10
if (item.onmspointerdown !== undefined) {
var msEventName = getPrefixEventName(item, "MS", eventName);
registerOrUnregisterEvent(item, msEventName, function (evt) { generateTouchClonedEvent(evt, eventName, true); }, enable);
// We can return because MSPointerXXX integrate mouse support
return;
}
// Chrome, Firefox
if (item.ontouchstart !== undefined) {
switch (eventName) {
case "pointermove":
registerOrUnregisterEvent(item, "touchmove", function (evt) { handleOtherEvent(evt, eventName); }, enable);
break;
case "pointercancel":
registerOrUnregisterEvent(item, "touchcancel", function (evt) { handleOtherEvent(evt, eventName); }, enable);
break;
case "pointerdown":
case "pointerup":
case "pointerout":
case "pointerover":
case "pointerleave":
case "pointerenter":
// These events will be handled by the window.ontouchmove function
if (!item.__handjsGlobalRegisteredEvents) {
item.__handjsGlobalRegisteredEvents = [];
}
if (enable) {
if (item.__handjsGlobalRegisteredEvents[eventName] !== undefined) {
item.__handjsGlobalRegisteredEvents[eventName]++;
return;
}
item.__handjsGlobalRegisteredEvents[eventName] = 1;
} else {
if (item.__handjsGlobalRegisteredEvents[eventName] !== undefined) {
item.__handjsGlobalRegisteredEvents[eventName]--;
if (item.__handjsGlobalRegisteredEvents[eventName] < 0) {
item.__handjsGlobalRegisteredEvents[eventName] = 0;
}
}
}
break;
}
}
// Fallback to mouse
switch (eventName) {
case "pointerdown":
registerOrUnregisterEvent(item, "mousedown", function (evt) { generateMouseProxy(evt, eventName); }, enable);
break;
case "pointermove":
registerOrUnregisterEvent(item, "mousemove", function (evt) { generateMouseProxy(evt, eventName); }, enable);
break;
case "pointerup":
registerOrUnregisterEvent(item, "mouseup", function (evt) { generateMouseProxy(evt, eventName); }, enable);
break;
case "pointerover":
registerOrUnregisterEvent(item, "mouseover", function (evt) { generateMouseProxy(evt, eventName); }, enable);
break;
case "pointerout":
registerOrUnregisterEvent(item, "mouseout", function (evt) { generateMouseProxy(evt, eventName); }, enable);
break;
case "pointerenter":
if (item.onmouseenter === undefined) { // Fallback to mouseover
registerOrUnregisterEvent(item, "mouseover", function (evt) { generateMouseProxy(evt, eventName); }, enable);
} else {
registerOrUnregisterEvent(item, "mouseenter", function (evt) { generateMouseProxy(evt, eventName); }, enable);
}
break;
case "pointerleave":
if (item.onmouseleave === undefined) { // Fallback to mouseout
registerOrUnregisterEvent(item, "mouseout", function (evt) { generateMouseProxy(evt, eventName); }, enable);
} else {
registerOrUnregisterEvent(item, "mouseleave", function (evt) { generateMouseProxy(evt, eventName); }, enable);
}
break;
}
};
// Intercept addEventListener calls by changing the prototype
var interceptAddEventListener = function (root) {
var current = root.prototype ? root.prototype.addEventListener : root.addEventListener;
var customAddEventListener = function (name, func, capture) {
// Branch when a PointerXXX is used
if (supportedEventsNames.indexOf(name) != -1) {
setTouchAware(this, name, true);
}
if (current === undefined) {
this.attachEvent("on" + getMouseEquivalentEventName(name), func);
} else {
current.call(this, name, func, capture);
}
};
if (root.prototype) {
root.prototype.addEventListener = customAddEventListener;
} else {
root.addEventListener = customAddEventListener;
}
};
// Intercept removeEventListener calls by changing the prototype
var interceptRemoveEventListener = function (root) {
var current = root.prototype ? root.prototype.removeEventListener : root.removeEventListener;
var customRemoveEventListener = function (name, func, capture) {
// Release when a PointerXXX is used
if (supportedEventsNames.indexOf(name) != -1) {
setTouchAware(this, name, false);
}
if (current === undefined) {
this.detachEvent(getMouseEquivalentEventName(name), func);
} else {
current.call(this, name, func, capture);
}
};
if (root.prototype) {
root.prototype.removeEventListener = customRemoveEventListener;
} else {
root.removeEventListener = customRemoveEventListener;
}
};
// Hooks
interceptAddEventListener(window);
interceptAddEventListener(HTMLElement);
interceptAddEventListener(document);
interceptAddEventListener(HTMLBodyElement);
interceptAddEventListener(HTMLDivElement);
interceptAddEventListener(HTMLImageElement);
interceptAddEventListener(HTMLUListElement);
interceptAddEventListener(HTMLAnchorElement);
interceptAddEventListener(HTMLLIElement);
interceptAddEventListener(HTMLTableElement);
if (window.HTMLSpanElement) {
interceptAddEventListener(HTMLSpanElement);
}
if (window.HTMLCanvasElement) {
interceptAddEventListener(HTMLCanvasElement);
}
if (window.SVGElement) {
interceptAddEventListener(SVGElement);
}
interceptRemoveEventListener(window);
interceptRemoveEventListener(HTMLElement);
interceptRemoveEventListener(document);
interceptRemoveEventListener(HTMLBodyElement);
interceptRemoveEventListener(HTMLDivElement);
interceptRemoveEventListener(HTMLImageElement);
interceptRemoveEventListener(HTMLUListElement);
interceptRemoveEventListener(HTMLAnchorElement);
interceptRemoveEventListener(HTMLLIElement);
interceptRemoveEventListener(HTMLTableElement);
if (window.HTMLSpanElement) {
interceptRemoveEventListener(HTMLSpanElement);
}
if (window.HTMLCanvasElement) {
interceptRemoveEventListener(HTMLCanvasElement);
}
if (window.SVGElement) {
interceptRemoveEventListener(SVGElement);
}
// Handling move on window to detect pointerleave/out/over
if (window.ontouchstart !== undefined) {
window.addEventListener('touchstart', function (eventObject) {
for (var i = 0; i < eventObject.changedTouches.length; ++i) {
var touchPoint = eventObject.changedTouches[i];
previousTargets[touchPoint.identifier] = touchPoint.target;
generateTouchEventProxyIfRegistered("pointerenter", touchPoint, touchPoint.target, eventObject);
generateTouchEventProxyIfRegistered("pointerover", touchPoint, touchPoint.target, eventObject);
generateTouchEventProxyIfRegistered("pointerdown", touchPoint, touchPoint.target, eventObject);
}
});
window.addEventListener('touchend', function (eventObject) {
for (var i = 0; i < eventObject.changedTouches.length; ++i) {
var touchPoint = eventObject.changedTouches[i];
var currentTarget = previousTargets[touchPoint.identifier];
generateTouchEventProxyIfRegistered("pointerup", touchPoint, currentTarget, eventObject);
generateTouchEventProxyIfRegistered("pointerout", touchPoint, currentTarget, eventObject);
generateTouchEventProxyIfRegistered("pointerleave", touchPoint, currentTarget, eventObject);
}
});
window.addEventListener('touchmove', function (eventObject) {
for (var i = 0; i < eventObject.changedTouches.length; ++i) {
var touchPoint = eventObject.changedTouches[i];
var newTarget = document.elementFromPoint(touchPoint.clientX, touchPoint.clientY);
var currentTarget = previousTargets[touchPoint.identifier];
if (currentTarget === newTarget) {
continue; // We can skip this as the pointer is effectively over the current target
}
if (currentTarget) {
// Raise out
generateTouchEventProxyIfRegistered("pointerout", touchPoint, currentTarget, eventObject);
// Raise leave
if (!currentTarget.contains(newTarget)) { // Leave must be called if the new target is not a child of the current
generateTouchEventProxyIfRegistered("pointerleave", touchPoint, currentTarget, eventObject);
}
}
if (newTarget) {
// Raise over
generateTouchEventProxyIfRegistered("pointerover", touchPoint, newTarget, eventObject);
// Raise enter
if (!newTarget.contains(currentTarget)) { // Leave must be called if the new target is not the parent of the current
generateTouchEventProxyIfRegistered("pointerenter", touchPoint, newTarget, eventObject);
}
}
previousTargets[touchPoint.identifier] = newTarget;
}
});
}
// Extension to navigator
if (navigator.pointerEnabled === undefined) {
// Indicates if the browser will fire pointer events for pointing input
navigator.pointerEnabled = true;
// IE
if (navigator.msPointerEnabled) {
navigator.maxTouchPoints = navigator.msMaxTouchPoints;
}
}
// Handling touch-action css rule
if (document.styleSheets && document.addEventListener) {
document.addEventListener("DOMContentLoaded", function () {
if (HANDJS.doNotProcessCSS) {
return;
}
var trim = function (string) {
return string.replace(/^\s+|\s+$/, '');
};
var processStylesheet = function (unfilteredSheet) {
var globalRegex = new RegExp(".+?{.*?}", "m");
var selectorRegex = new RegExp(".+?{", "m");
while (unfilteredSheet != "") {
var filter = globalRegex.exec(unfilteredSheet);
if (!filter) {
break;
}
var block = filter[0];
unfilteredSheet = trim(unfilteredSheet.replace(block, ""));
var selectorText = trim(selectorRegex.exec(block)[0].replace("{", ""));
// Checking if the user wanted to deactivate the default behavior
if (block.replace(/\s/g, "").indexOf("touch-action:none") != -1) {
var elements = document.querySelectorAll(selectorText);
for (var elementIndex = 0; elementIndex < elements.length; elementIndex++) {
var element = elements[elementIndex];
if (element.style.msTouchAction !== undefined) {
element.style.msTouchAction = "none";
}
else {
element.handjs_forcePreventDefault = true;
}
}
}
}
}; // Looking for touch-action in referenced stylesheets
try {
for (var index = 0; index < document.styleSheets.length; index++) {
var sheet = document.styleSheets[index];
if (sheet.href == undefined) { // it is an inline style
continue;
}
// Loading the original stylesheet
var xhr = new XMLHttpRequest();
xhr.open("get", sheet.href, false);
xhr.send();
var unfilteredSheet = xhr.responseText.replace(/(\n|\r)/g, "");
processStylesheet(unfilteredSheet);
}
} catch (e) {
// Silently fail...
}
// Looking for touch-action in inline styles
var styles = document.getElementsByTagName("style");
for (var index = 0; index < styles.length; index++) {
var inlineSheet = styles[index];
var inlineUnfilteredSheet = trim(inlineSheet.innerHTML.replace(/(\n|\r)/g, ""));
processStylesheet(inlineUnfilteredSheet);
}
}, false);
}
})();