-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjquery.touch.js
290 lines (246 loc) · 12.4 KB
/
jquery.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
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
/*!
* jQueryTouch v0.0.6
* https://github.com/a-fung/jQueryTouch
*
* Copyright 2012 Man Kwan Liu
* Released under the Apache License Version 2.0
* http://www.apache.org/licenses/
*
* Date: Wed Oct 2012 23:14:09 GMT-0700 (Pacific Daylight Time)
*/
(function ($) {
// initiate touch handler on jQuery objects
$.fn.touchInit = function (options) {
if (!options || typeof (options) != "object") {
options = {};
}
// default options
options = $.extend(
{
// prevent default click/touch handlers
preventDefault: true,
// handle mouse event
mouse: true,
// handle pen event (IE 10 only)
pen: true,
// max number of touch (mouse) point to handle
// more touch events at the same time will be ignored
// -1 means unlimited
maxtouch: -1,
// add prefix to the touch events triggered
// useful when you want to add handlers with different options
prefix: ""
},
options);
// nothing to do
if (options.maxtouch == 0) return this;
if (window.navigator.msPointerEnabled) { // IE10 with touch
if (options.preventDefault) {
var cssNeeded = this.data("_touchtrack-ms-touch-action");
if (cssNeeded == undefined || cssNeeded == null) {
cssNeeded = [];
}
cssNeeded[options.prefix + "_track"] = true;
this.data("_touchtrack-ms-touch-action", cssNeeded);
// make default pan and zoom disabled
this.css("-ms-touch-action", "none");
}
}
this.each(function () {
var _this = this,
_touch_handler = null,
touches = null;
// an array to store the touches we are handling
$(this).data(options.prefix + "_touches", []);
_touch_handler = function (event) {
var touchArray = [];
if (event.pointerType) {
// check if mouse/pen event should be ignored
if ((event.pointerType == event.MSPOINTER_TYPE_MOUSE && !options.mouse) || (event.pointerType == event.MSPOINTER_TYPE_PEN && !options.pen)) {
return;
}
touchArray[0] = {
id: event.pointerId,
clientX: event.clientX,
clientY: event.clientY,
pageX: event.pageX,
pageY: event.pageY,
screenX: event.screenX,
screenY: event.screenY
};
} else if (event.changedTouches) {
// there might be more than 1 touch points in one native event in webkit browsers
// loop thru the changedTouches list
for (var i = 0; i < event.changedTouches.length; i++) {
touchArray[i] = {
id: event.changedTouches[i].identifier,
clientX: event.changedTouches[i].clientX,
clientY: event.changedTouches[i].clientY,
pageX: event.changedTouches[i].pageX,
pageY: event.changedTouches[i].pageY,
screenX: event.changedTouches[i].screenX,
screenY: event.changedTouches[i].screenY
};
}
} else {
// This is a mouse event
// Give it an ID of -1
touchArray[0] = {
id: -1,
clientX: event.clientX,
clientY: event.clientY,
pageX: event.pageX,
pageY: event.pageY,
screenX: event.screenX,
screenY: event.screenY
};
}
// loop thru the touches in this event
for (var i = 0; i < touchArray.length; i++) {
var touch = touchArray[i],
currentTouchIndex = null,
newTouch = true,
eventType;
touches = $(_this).data(options.prefix + "_touches");
// search the touch array to see if this is a touch we're handling
for (var j = 0; j < touches.length; j++) {
if (touches[j].id == touch.id) {
newTouch = false;
touches[j] = touch; // update the touch object
$(_this).data(options.prefix + "_touches", touches);
currentTouchIndex = j;
break;
}
}
// this is not a touch we're handling
if (newTouch && this != _this) {
continue;
}
if (event.type == "touchstart" || event.type == "MSPointerDown" || event.type == "mousedown") { // "start" of a touch/click
if (newTouch) {
if (options.maxtouch < 0 || options.maxtouch > touches.length) { // check to see if we have quota to handle this touch
// add the touch to touch array
touches[touches.length] = touch;
$(this).data(options.prefix + "_touches", touches);
} else {
// ignore
continue;
}
// add "move", "end" and "cancel" event listeners only after a "start" event
if (event.pointerType) {
document.addEventListener("MSPointerMove", _touch_handler, false);
document.addEventListener("MSPointerUp", _touch_handler, false);
document.addEventListener("MSPointerCancel", _touch_handler, false);
} else {
document.addEventListener("touchmove", _touch_handler, false);
document.addEventListener("touchend", _touch_handler, false);
document.addEventListener("touchcancel", _touch_handler, false);
if (options.mouse) { // ignore mouse?
$(document).on("mousemove", _touch_handler);
$(document).on("mouseup", _touch_handler);
}
}
}
eventType = "start";
} else if (event.type == "touchmove" || event.type == "MSPointerMove" || event.type == "mousemove") { // "move" event
eventType = "move";
} else if (event.type == "touchend" || event.type == "touchcancel" || event.type == "MSPointerUp" || event.type == "MSPointerCancel" || event.type == "mouseup") { // "end"/"cancel" event
// remove the current touch from touch array
if (touches.length - 1 != currentTouchIndex) {
touches[currentTouchIndex] = touches[touches.length - 1];
}
touches.pop();
$(_this).data(options.prefix + "_touches", touches);
// if the array is empty, removing the "move", "end" and "cancel" event listeners
if (touches.length == 0) {
if (event.pointerType) {
document.removeEventListener("MSPointerMove", _touch_handler, false);
document.removeEventListener("MSPointerUp", _touch_handler, false);
document.removeEventListener("MSPointerCancel", _touch_handler, false);
} else {
document.removeEventListener("touchmove", _touch_handler, false);
document.removeEventListener("touchend", _touch_handler, false);
document.removeEventListener("touchcancel", _touch_handler, false);
if (options.mouse) { // ignore mouse?
$(document).off("mousemove", _touch_handler);
$(document).off("mouseup", _touch_handler);
}
}
}
eventType = "end";
} else { // Unknown event
continue;
}
// the event we are triggering
var tEvent = $.Event(options.prefix + "touch_" + eventType);
// get the touch coordinates from the touch object
tEvent = $.extend(
tEvent,
{
originalType: event.type,
clientX: touch.clientX,
clientY: touch.clientY,
pageX: touch.pageX,
pageY: touch.pageY,
screenX: touch.screenX,
screenY: touch.screenY,
touches: touches
});
// trigger the event in a try/catch block
// because we do not want any exception to stop the current function
try { $(_this).trigger(tEvent); } catch (error) { console.log(error); }
}
// prevent default handlers
if (options.preventDefault) {
event.preventDefault && event.preventDefault();
return false;
}
};
// only add "start" handlers
if (window.navigator.msPointerEnabled) {
this.addEventListener("MSPointerDown", _touch_handler, false);
} else {
this.addEventListener("touchstart", _touch_handler, false);
options.mouse && $(this).on("mousedown", _touch_handler);
}
// store the event handler for dispose method
$(this).data(options.prefix + "_touch_handler", _touch_handler);
});
return this;
};
// dispose all the touch handlers
$.fn.touchDispose = function (prefix) {
if (!prefix || typeof (prefix) != "string") {
prefix = "";
}
if (window.navigator.msPointerEnabled) { // IE10 with touch
var cssNeeded = this.data("_touchtrack-ms-touch-action");
if (cssNeeded == undefined || cssNeeded == null) {
cssNeeded = [];
}
delete cssNeeded[options.prefix + "_track"];
this.data("_touchtrack-ms-touch-action", cssNeeded);
var i = 0;
for (j in cssNeeded) i++;
if (i == 0) this.css("-ms-touch-action", "");
}
this.each(function () {
// remove all handlers
var _touch_handler = $(this).data(prefix + "_touch_handler");
this.removeEventListener("MSPointerDown", _touch_handler, false);
this.removeEventListener("touchstart", _touch_handler, false);
$(this).off("mousedown", _touch_handler);
document.removeEventListener("MSPointerMove", _touch_handler, false);
document.removeEventListener("MSPointerUp", _touch_handler, false);
document.removeEventListener("MSPointerCancel", _touch_handler, false);
document.removeEventListener("touchmove", _touch_handler, false);
document.removeEventListener("touchend", _touch_handler, false);
document.removeEventListener("touchcancel", _touch_handler, false);
$(this).off("mousemove", _touch_handler);
$(this).off("mouseup", _touch_handler);
$(this).removeData(prefix + "_touch_handler");
$(this).removeData(prefix + "_touches");
});
return this;
};
})(jQuery);