forked from intel/appframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
touch.js
92 lines (83 loc) · 2.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
redirectMouseToTouch = function(type, originalEvent)
{
//stop propagation, and remove default behavior for everything but INPUT, TEXTAREA & SELECT fields
// originalEvent.stopPropagation();
if (originalEvent.target.tagName.toUpperCase().indexOf("SELECT") == -1 &&
originalEvent.target.tagName.toUpperCase().indexOf("TEXTAREA") == -1 &&
originalEvent.target.tagName.toUpperCase().indexOf("INPUT") == -1) //SELECT, TEXTAREA & INPUT
{
//if(type != 'touchstart')
//originalEvent.stopPropagation();//originalEvent.preventDefault();
//else
//originalEvent.preventDefault();
originalEvent.stopPropagation();
}
var touchevt = document.createEvent("Event");
touchevt.initEvent(type, true, true);
touchevt.touches = new Array();
touchevt.touches[0] = new Object();
touchevt.touches[0].pageX = originalEvent.pageX;
touchevt.touches[0].pageY = originalEvent.pageY;
touchevt.touches[0].target = originalEvent.target;
touchevt.changedTouches = touchevt.touches; //for jqtouch
touchevt.targetTouches = touchevt.touches; //for jqtouch
touchevt.target = originalEvent.target;
originalEvent.target.dispatchEvent(touchevt);
return touchevt;
}
emulateTouchEvents = function()
{
var ee = document;
document.mouseMoving = false;
document.addEventListener("mousedown", function(e)
{
try
{
this.mouseMoving = true;
var touchevt = redirectMouseToTouch("touchstart", e);
if (document.ontouchstart)
document.ontouchstart(touchevt);
if(e.target.ontouchstart)
e.target.ontouchstart(e);
} catch (e) {
}
});
//ee[x].onmouseup=function(e)
document.addEventListener("mouseup", function(e)
{
try
{
this.mouseMoving = false;
var touchevt = redirectMouseToTouch("touchend", e);
if (document.ontouchend)
document.ontouchend(touchevt);
if(e.target.ontouchend)
e.target.ontouchend(e);
}
catch (e) {
}
});
//ee[x].onmousemove=function(e)
document.addEventListener("mousemove", function(e)
{
try
{
if (!this.mouseMoving)
return
var touchevt = redirectMouseToTouch("touchmove", e);
if (document.ontouchmove)
document.ontouchmove(touchevt);
if(e.target.ontouchmove)
e.target.ontouchmove(e);
}
catch (e) {
}
});
// }
}
emulateTouchEvents();
window.addEventListener("resize",function(){
var touchevt = document.createEvent("Event");
touchevt.initEvent("orientationchange", true, true);
document.dispatchEvent(touchevt);
},false);