forked from olton/metroui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtouch-handler.js
More file actions
56 lines (47 loc) · 1.68 KB
/
touch-handler.js
File metadata and controls
56 lines (47 loc) · 1.68 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
var hasTouch = 'ontouchend' in window, eventTimer;
var moveDirection = 'undefined', startX, startY, deltaX, deltaY, mouseDown = false;
function addTouchEvents(element){
if (hasTouch) {
element.addEventListener("touchstart", touch2Mouse, true);
element.addEventListener("touchmove", touch2Mouse, true);
element.addEventListener("touchend", touch2Mouse, true);
}
}
function touch2Mouse(e)
{
var theTouch = e.changedTouches[0];
var mouseEv;
switch(e.type)
{
case "touchstart": mouseEv="mousedown"; break;
case "touchend": mouseEv="mouseup"; break;
case "touchmove": mouseEv="mousemove"; break;
default: return;
}
if (mouseEv == "mousedown") {
eventTimer = (new Date()).getTime();
startX = theTouch.clientX;
startY = theTouch.clientY;
mouseDown = true;
}
if (mouseEv == "mouseup") {
if ((new Date()).getTime() - eventTimer <= 500) {
mouseEv = "click";
} else if ((new Date()).getTime() - eventTimer > 1000) {
mouseEv = "longclick";
}
eventTimer = 0;
mouseDown = false;
}
if (mouseEv == "mousemove") {
if (mouseDown) {
deltaX = theTouch.clientX - startX;
deltaY = theTouch.clientY - startY;
moveDirection = deltaX > deltaY ? 'horizontal' : 'vertical';
}
}
var mouseEvent = document.createEvent("MouseEvent");
mouseEvent.initMouseEvent(mouseEv, true, true, window, 1, theTouch.screenX, theTouch.screenY, theTouch.clientX, theTouch.clientY, false, false, false, false, 0, null);
theTouch.target.dispatchEvent(mouseEvent);
e.preventDefault();
}