-
Notifications
You must be signed in to change notification settings - Fork 3
/
TouchToMouseEmulator.js
104 lines (85 loc) · 3.66 KB
/
TouchToMouseEmulator.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
/**
* @author Swagatam Mitra
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, document, console, brackets, $, Mustache */
define(function (require, exports, module) {
"use strict";
var _cachedClientX,
_cachedClientY;
var LATENCY_CORRECTION_TIMER,
LATENCY_CORRECTION_TIME = 500;
function _detectClick(touchPoint) {
return (_cachedClientX === touchPoint.clientX && _cachedClientY === touchPoint.clientY);
}
function _triggerContextMenu(originalEvent, touchPoint) {
var simulatedEvent = document.createEvent("MouseEvent");
// initMouseEvent(type, canBubble, cancelable, view, clickCount,
// screenX, screenY, clientX, clientY, ctrlKey,
// altKey, shiftKey, metaKey, button, relatedTarget);
simulatedEvent.initMouseEvent("contextmenu", true, true, window, 1,
touchPoint.screenX, touchPoint.screenY,
touchPoint.clientX, touchPoint.clientY, originalEvent.ctrlKey,
originalEvent.altKey, originalEvent.shiftKey, originalEvent.metaKey, 0, null);
touchPoint.target.dispatchEvent(simulatedEvent);
}
function _triggerEvent(type, originalEvent, touchPoint) {
var simulatedEvent = document.createEvent("MouseEvent");
// initMouseEvent(type, canBubble, cancelable, view, clickCount,
// screenX, screenY, clientX, clientY, ctrlKey,
// altKey, shiftKey, metaKey, button, relatedTarget);
simulatedEvent.initMouseEvent(type, true, true, window, 1,
touchPoint.screenX, touchPoint.screenY,
touchPoint.clientX, touchPoint.clientY, originalEvent.ctrlKey,
originalEvent.altKey, originalEvent.shiftKey, originalEvent.metaKey, 0, null);
touchPoint.target.dispatchEvent(simulatedEvent);
}
function _isMultiTouch(event) {
return event.changedTouches.length > 1;
}
function _simulate(event) {
var touches = event.changedTouches,
touchPoint = touches[0],
type = "";
switch (event.type) {
case "touchstart":
type = "mousedown";
break;
case "touchmove":
type = "mousemove";
break;
case "touchend":
type = "mouseup";
break;
default:
return;
}
_triggerEvent(type, event, touchPoint);
if (_detectClick(touchPoint) && !LATENCY_CORRECTION_TIMER) {
_triggerEvent("click", event, touchPoint);
_cachedClientX = -1;
_cachedClientY = -1;
LATENCY_CORRECTION_TIMER = setTimeout(function () {
clearTimeout(LATENCY_CORRECTION_TIMER);
LATENCY_CORRECTION_TIMER = null;
}, LATENCY_CORRECTION_TIME);
} else {
_cachedClientX = touchPoint.clientX;
_cachedClientY = touchPoint.clientY;
}
}
function touchHandler(event) {
if (!_isMultiTouch(event) && !event.altKey) {
_simulate(event);
event.preventDefault();
} else {
return;
}
}
function _init() {
document.addEventListener("touchstart", touchHandler, true);
document.addEventListener("touchmove", touchHandler, true);
document.addEventListener("touchend", touchHandler, true);
}
exports.initTouchShim = _init;
});