-
Notifications
You must be signed in to change notification settings - Fork 6
/
xui.touchSwipe-1.2.4.js
447 lines (374 loc) · 15.4 KB
/
xui.touchSwipe-1.2.4.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* touchSwipe - XUI Plugin
* Ported by Scott Bates
* Uncorked Studios
* http://www.uncorkedstudios.com
*
* 2011-05-17
* Initial release of XUI plugin based off of TouchSwipe version 1.2.4
* MIT License of original plugin is maintained
*
*
* originally created by:
* ----------------------------------------------------------
* touchSwipe - jQuery Plugin
* http://plugins.jquery.com/project/touchSwipe
* http://labs.skinkers.com/touchSwipe/
*
* Copyright (c) 2010 Matt Bryson (www.skinkers.com)
* Dual licensed under the MIT or GPL Version 2 licenses.
* ----------------------------------------------------------
*
* $version: 1.2.3
*
* Changelog
* $Date: 2010-12-12 (Wed, 12 Dec 2010) $
* $version: 1.0.0
* $version: 1.0.1 - removed multibyte comments
*
* $Date: 2011-21-02 (Mon, 21 Feb 2011) $
* $version: 1.1.0 - added allowPageScroll property to allow swiping and scrolling of page
* - changed handler signatures so one handler can be used for multiple events
* $Date: 2011-23-02 (Wed, 23 Feb 2011) $
* $version: 1.2.0 - added click handler. This is fired if the user simply clicks and does not swipe. The event object and click target are passed to handler.
* - If you use the http://code.google.com/p/jquery-ui-for-ipad-and-iphone/ plugin, you can also assign jQuery mouse events to children of a touchSwipe object.
* $version: 1.2.1 - removed console log!
*
* $version: 1.2.2 - Fixed bug where scope was not preserved in callback methods.
*
* $Date: 2011-28-04 (Thurs, 28 April 2011) $
* $version: 1.2.4 - Changed licence terms to be MIT or GPL inline with jQuery. Added check for support of touch events to stop non compatible browsers erroring.
*
* A jQuery plugin to capture left, right, up and down swipes on touch devices.
* You can capture 2 finger or 1 finger swipes, set the threshold and define either a catch all handler, or individual direction handlers.
* Options:
* swipe Function A catch all handler that is triggered for all swipe directions. Handler is passed 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
* swipeLeft Function A handler that is triggered for "left" swipes. Handler is passed 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
* swipeRight Function A handler that is triggered for "right" swipes. Handler is passed 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
* swipeUp Function A handler that is triggered for "up" swipes. Handler is passed 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
* swipeDown Function A handler that is triggered for "down" swipes. Handler is passed 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
* swipeStatus Function A handler triggered for every phase of the swipe. Handler is passed 4 arguments: event : The original event object, phase:The current swipe face, either "start”, "move”, "end” or "cancel”. direction : The swipe direction, either "up”, "down”, "left " or "right”.distance : The distance of the swipe.
* click Function A handler triggered when a user just clicks on the item, rather than swipes it. If they do not move, click is triggered, if they do move, it is not.
*
* fingers int Default 1. The number of fingers to trigger the swipe, 1 or 2.
* threshold int Default 75. The number of pixels that the user must move their finger by before it is considered a swipe.
* triggerOnTouchEnd Boolean Default true If true, the swipe events are triggered when the touch end event is received (user releases finger). If false, it will be triggered on reaching the threshold, and then cancel the touch event automatically.
* allowPageScroll String Default "auto". How the browser handles page scrolls when the user is swiping on a touchSwipe object.
* "auto" : all undefined swipes will cause the page to scroll in that direction.
* "none" : the page will not scroll when user swipes.
* "horizontal" : will force page to scroll on horizontal swipes.
* "vertical" : will force page to scroll on vertical swipes.
*
* This jQuery plugin will only run on devices running Mobile Webkit based browsers (iOS 2.0+, android 2.2+)
*/
xui.extend({
swipe : function(options)
{
if (!this) return false;
// Default thresholds & swipe functions
var defaults = {
fingers : 1, // int - The number of fingers to trigger the swipe, 1 or 2. Default is 1.
threshold : 75, // int - The number of pixels that the user must move their finger by before it is considered a swipe. Default is 75.
swipe : null, // Function - A catch all handler that is triggered for all swipe directions. Accepts 2 arguments, the original event object and the direction of the swipe : "left", "right", "up", "down".
swipeLeft : null, // Function - A handler that is triggered for "left" swipes. Accepts 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
swipeRight : null, // Function - A handler that is triggered for "right" swipes. Accepts 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
swipeUp : null, // Function - A handler that is triggered for "up" swipes. Accepts 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
swipeDown : null, // Function - A handler that is triggered for "down" swipes. Accepts 3 arguments, the original event object, the direction of the swipe : "left", "right", "up", "down" and the distance of the swipe.
swipeStatus : null, // Function - A handler triggered for every phase of the swipe. Handler is passed 4 arguments: event : The original event object, phase:The current swipe face, either "start”, "move”, "end” or "cancel”. direction : The swipe direction, either "up”, "down”, "left " or "right”.distance : The distance of the swipe.
click : null, // Function - A handler triggered when a user just clicks on the item, rather than swipes it. If they do not move, click is triggered, if they do move, it is not.
triggerOnTouchEnd : true, // Boolean, if true, the swipe events are triggered when the touch end event is received (user releases finger). If false, it will be triggered on reaching the threshold, and then cancel the touch event automatically.
allowPageScroll : "auto" /* How the browser handles page scrolls when the user is swiping on a touchSwipe object.
"auto" : all undefined swipes will cause the page to scroll in that direction.
"none" : the page will not scroll when user swipes.
"horizontal" : will force page to scroll on horizontal swipes.
"vertical" : will force page to scroll on vertical swipes.
*/
};
//Constants
var LEFT = "left";
var RIGHT = "right";
var UP = "up";
var DOWN = "down";
var NONE = "none";
var HORIZONTAL = "horizontal";
var VERTICAL = "vertical";
var AUTO = "auto";
var PHASE_START="start";
var PHASE_MOVE="move";
var PHASE_END="end";
var PHASE_CANCEL="cancel";
var phase="start";
if (options.allowPageScroll==undefined && (options.swipe!=undefined || options.swipeStatus!=undefined))
options.allowPageScroll=NONE;
if (options){
for ( var key in options ){
defaults[key] = options[key];
}
}
/**
* Setup each object to detect swipe gestures
*/
return this.each(function()
{
var $this = this;
var triggerElementID = null; // this variable is used to identity the triggering element
var fingerCount = 0; // the current number of fingers being used.
//track mouse points / delta
var start={x:0, y:0};
var end={x:0, y:0};
var delta={x:0, y:0};
/**
* Event handler for a touch start event.
* Stops the default click event from triggering and stores where we touched
*/
function touchStart(event)
{
phase = PHASE_START;
// get the total number of fingers touching the screen
fingerCount = event.touches.length;
//clear vars..
distance=0;
direction=null;
// check the number of fingers is what we are looking for
if ( fingerCount == defaults.fingers )
{
// get the coordinates of the touch
start.x = end.x = event.touches[0].pageX;
start.y = end.y = event.touches[0].pageY;
if (defaults.swipeStatus){
triggerHandler(event, phase);
}
}
else
{
//touch with more/less than the fingers we are looking for
touchCancel(event);
}
}
/**
* Event handler for a touch move event.
* If we change fingers during move, then cancel the event
*/
function touchMove(event)
{
if (phase == PHASE_END || phase == PHASE_CANCEL)
return;
end.x = event.touches[0].pageX;
end.y = event.touches[0].pageY;
direction = caluculateDirection();
fingerCount = event.touches.length;
phase = PHASE_MOVE
//Check if we need to prevent default evnet (page scroll) or not
validateDefaultEvent(event, direction);
if ( fingerCount == defaults.fingers )
{
distance = caluculateDistance();
if (defaults.swipeStatus)
triggerHandler(event, phase, direction, distance);
//If we trigger whilst dragging, not on touch end, then calculate now...
if (!defaults.triggerOnTouchEnd)
{
// if the user swiped more than the minimum length, perform the appropriate action
if ( distance >= defaults.threshold )
{
phase = PHASE_END;
triggerHandler(event, phase);
touchCancel(event); // reset the variables
}
}
}
else
{
phase = PHASE_CANCEL;
triggerHandler(event, phase);
touchCancel(event);
}
}
/**
* Event handler for a touch end event.
* Calculate the direction and trigger events
*/
function touchEnd(event)
{
event.preventDefault();
distance = caluculateDistance();
direction = caluculateDirection();
if (defaults.triggerOnTouchEnd)
{
phase = PHASE_END;
// check to see if more than one finger was used and that there is an ending coordinate
if ( fingerCount == defaults.fingers && end.x != 0 )
{
// if the user swiped more than the minimum length, perform the appropriate action
if ( distance >= defaults.threshold )
{
triggerHandler(event, phase);
touchCancel(event); // reset the variables
}
else
{
phase = PHASE_CANCEL;
triggerHandler(event, phase);
touchCancel(event);
}
}
else
{
phase = PHASE_CANCEL;
triggerHandler(event, phase);
touchCancel(event);
}
}
else if (phase == PHASE_MOVE)
{
phase = PHASE_CANCEL;
triggerHandler(event, phase);
touchCancel(event);
}
}
/**
* Event handler for a touch cancel event.
* Clears current vars
*/
function touchCancel(event)
{
// reset the variables back to default values
fingerCount = 0;
start.x = 0;
start.y = 0;
end.x = 0;
end.y = 0;
delta.x = 0;
delta.y = 0;
}
/**
* Trigger the relevant event handler
* The handlers are passed the original event, the element that was swiped, and in the case of the catch all handler, the direction that was swiped, "left", "right", "up", or "down"
*/
function triggerHandler(event, phase)
{
//update status
if (defaults.swipeStatus)
defaults.swipeStatus.call($this,event, phase, direction || null, distance || 0);
if (phase == PHASE_CANCEL)
{
if (defaults.click && fingerCount==1 && (isNaN(distance) || distance==0))
defaults.click.call($this,event, event.target);
}
if (phase == PHASE_END)
{
//trigger catch all event handler
if (defaults.swipe)
{
defaults.swipe.call($this,event, direction, distance);
}
//trigger direction specific event handlers
switch(direction)
{
case LEFT :
if (defaults.swipeLeft)
defaults.swipeLeft.call($this,event, direction, distance);
break;
case RIGHT :
if (defaults.swipeRight)
defaults.swipeRight.call($this,event, direction, distance);
break;
case UP :
if (defaults.swipeUp)
defaults.swipeUp.call($this,event, direction, distance);
break;
case DOWN :
if (defaults.swipeDown)
defaults.swipeDown.call($this,event, direction, distance);
break;
}
}
}
/**
* Checks direction of the swipe and the value allowPageScroll to see if we should allow or prevent the default behaviour from occurring.
* This will essentially allow page scrolling or not when the user is swiping on a touchSwipe object.
*/
function validateDefaultEvent(event, direction)
{
if( defaults.allowPageScroll==NONE )
{
event.preventDefault();
}
else
{
var auto=defaults.allowPageScroll==AUTO;
switch(direction)
{
case LEFT :
if ( (defaults.swipeLeft && auto) || (!auto && defaults.allowPageScroll!=HORIZONTAL))
event.preventDefault();
break;
case RIGHT :
if ( (defaults.swipeRight && auto) || (!auto && defaults.allowPageScroll!=HORIZONTAL))
event.preventDefault();
break;
case UP :
if ( (defaults.swipeUp && auto) || (!auto && defaults.allowPageScroll!=VERTICAL))
event.preventDefault();
break;
case DOWN :
if ( (defaults.swipeDown && auto) || (!auto && defaults.allowPageScroll!=VERTICAL))
event.preventDefault();
break;
}
}
}
/**
* Calcualte the length / distance of the swipe
*/
function caluculateDistance()
{
return Math.round(Math.sqrt(Math.pow(end.x - start.x,2) + Math.pow(end.y - start.y,2)));
}
/**
* Calcualte the angle of the swipe
*/
function caluculateAngle()
{
var X = start.x-end.x;
var Y = end.y-start.y;
var r = Math.atan2(Y,X); //radians
var angle = Math.round(r*180/Math.PI); //degrees
//ensure value is positive
if (angle < 0)
angle = 360 - Math.abs(angle);
return angle;
}
/**
* Calcualte the direction of the swipe
* This will also call caluculateAngle to get the latest angle of swipe
*/
function caluculateDirection()
{
var angle = caluculateAngle();
if ( (angle <= 45) && (angle >= 0) )
return LEFT;
else if ( (angle <= 360) && (angle >= 315) )
return LEFT;
else if ( (angle >= 135) && (angle <= 225) )
return RIGHT;
else if ( (angle > 45) && (angle < 135) )
return DOWN;
else
return UP;
}
// Add gestures to all swipable areas if supported
try
{
this.addEventListener("touchstart", touchStart, false);
this.addEventListener("touchmove", touchMove, false);
this.addEventListener("touchend", touchEnd, false);
this.addEventListener("touchcancel", touchCancel, false);
}
catch(e)
{
//touch not supported
}
});
}
});