forked from furf/jquery-ui-touch-punch
-
Notifications
You must be signed in to change notification settings - Fork 5
/
jquery.touch-punch.js
158 lines (125 loc) · 5.43 KB
/
jquery.touch-punch.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
/*!
* jQuery Touch Punch 0.1
*
* extended from jQuery UI Touch Punch 0.2.2 Copyright 2011, Dave Furfero
* Copyright 2013, Michael Angstadt
- modified eliminate the requirement of jQuery UI usage
- extended into jQuery plugin to be used on specific elements
- usage is for elements that you've defined mouse event handlers for
and want to extend touch interface events to simulate these events properly
on touch interface devices
- ex. $('.draggableObject').touchDraggable();
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* Depends:
* jquery.js
*/
(function ($) {
$.fn.touchDraggable = function () {
//Detect touch support
$.support.touch = 'ontouchend' in document;
//if we don't support touch, ignore it
if (!$.support.touch) {
return;
}
//hook up the touch events to the mouse events
this._touchHandled = false;
this._touchMoved = false;
/**
* Simulate a mouse event based on a corresponding touch event
* @param {Object} event A touch event
* @param {String} simulatedType The corresponding mouse event
*/
function simulateMouseEvent(event, simulatedType) {
// Ignore multi-touch events
if (event.touches.length > 1) {
return;
}
event.preventDefault();
var touch = event.changedTouches[0],
simulatedEvent = document.createEvent('MouseEvents');
// Initialize the simulated mouse event using the touch event's coordinates
simulatedEvent.initMouseEvent(
simulatedType, // type
true, // bubbles
true, // cancelable
window, // view
1, // detail
touch.screenX, // screenX
touch.screenY, // screenY
touch.clientX, // clientX
touch.clientY, // clientY
false, // ctrlKey
false, // altKey
false, // shiftKey
false, // metaKey
0, // button
null // relatedTarget
);
// Dispatch the simulated event to the target element
event.target.dispatchEvent(simulatedEvent);
}
//incase we've got multiple items
var theseElements = this;
for (var i = 0; i < theseElements.length; i++) {
/**
* Add an event handler to the jQuery object's element for 'touchstart'
* using any pre-defined handlers for typical browser events
*/
theseElements[i].addEventListener("touchstart", function (event) {
var self = this;
// Ignore the event if another element is already being handled
// removed test to see if the mouse is handling it - b/c we already
// control if we aren't on a touch device.
if (self._touchHandled) {
return;
}
// Set the flag to prevent other elements from inheriting the touch event
self._touchHandled = true;
// Track movement to determine if interaction was a click
self._touchMoved = false;
// Simulate the mouseover event
simulateMouseEvent(event, 'mouseover');
// Simulate the mousemove event
simulateMouseEvent(event, 'mousemove');
// Simulate the mousedown event
simulateMouseEvent(event, 'mousedown');
});
/**
* Add an event handler to the jQuery object's element for 'touchmove'
* using any pre-defined handlers for typical browser events
*/
theseElements[i].addEventListener("touchmove", function (event) {
// Ignore event if not handled
if (!this._touchHandled) {
return;
}
// Interaction was not a click
this._touchMoved = true;
// Simulate the mousemove event
simulateMouseEvent(event, 'mousemove');
});
/**
* Add an event handler to the jQuery object's element for 'touchend'
* using any pre-defined handlers for typical browser events
*/
theseElements[i].addEventListener("touchend", function (event) {
// Ignore event if not handled
if (!this._touchHandled) {
return;
}
// Simulate the mouseup event
simulateMouseEvent(event, 'mouseup');
// Simulate the mouseout event
simulateMouseEvent(event, 'mouseout');
// If the touch interaction did not move, it should trigger a click
if (!this._touchMoved) {
// Simulate the click event
simulateMouseEvent(event, 'click');
}
// Unset the flag to allow other widgets to inherit the touch event
this._touchHandled = false;
});
}
};
})(jQuery);