aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'Code/Blocks/Flipboard/js/jquery.touchSwipe-1.2.5.js')
-rw-r--r--Code/Blocks/Flipboard/js/jquery.touchSwipe-1.2.5.js478
1 files changed, 478 insertions, 0 deletions
diff --git a/Code/Blocks/Flipboard/js/jquery.touchSwipe-1.2.5.js b/Code/Blocks/Flipboard/js/jquery.touchSwipe-1.2.5.js
new file mode 100644
index 0000000..f007f55
--- /dev/null
+++ b/Code/Blocks/Flipboard/js/jquery.touchSwipe-1.2.5.js
@@ -0,0 +1,478 @@
1/*
2 * touchSwipe - jQuery Plugin
3 * http://plugins.jquery.com/project/touchSwipe
4 * http://labs.skinkers.com/touchSwipe/
5 *
6 * Copyright (c) 2010 Matt Bryson (www.skinkers.com)
7 * Dual licensed under the MIT or GPL Version 2 licenses.
8 *
9 * $version: 1.2.5
10 *
11 * Changelog
12 * $Date: 2010-12-12 (Wed, 12 Dec 2010) $
13 * $version: 1.0.0
14 * $version: 1.0.1 - removed multibyte comments
15 *
16 * $Date: 2011-21-02 (Mon, 21 Feb 2011) $
17 * $version: 1.1.0 - added allowPageScroll property to allow swiping and scrolling of page
18 * - changed handler signatures so one handler can be used for multiple events
19 * $Date: 2011-23-02 (Wed, 23 Feb 2011) $
20 * $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.
21 * - 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.
22 * $version: 1.2.1 - removed console log!
23 *
24 * $version: 1.2.2 - Fixed bug where scope was not preserved in callback methods.
25 *
26 * $Date: 2011-28-04 (Thurs, 28 April 2011) $
27 * $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.
28 *
29 * $Date: 2011-27-09 (Tues, 27 September 2011) $
30 * $version: 1.2.5 - Added support for testing swipes with mouse on desktop browser (thanks to https://github.com/joelhy)
31
32 * A jQuery plugin to capture left, right, up and down swipes on touch devices.
33 * You can capture 2 finger or 1 finger swipes, set the threshold and define either a catch all handler, or individual direction handlers.
34 * Options:
35 * 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.
36 * 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.
37 * 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.
38 * 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.
39 * 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.
40 * 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.
41 * 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.
42 *
43 * fingers int Default 1. The number of fingers to trigger the swipe, 1 or 2.
44 * threshold int Default 75. The number of pixels that the user must move their finger by before it is considered a swipe.
45 * 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.
46 * allowPageScroll String Default "auto". How the browser handles page scrolls when the user is swiping on a touchSwipe object.
47 * "auto" : all undefined swipes will cause the page to scroll in that direction.
48 * "none" : the page will not scroll when user swipes.
49 * "horizontal" : will force page to scroll on horizontal swipes.
50 * "vertical" : will force page to scroll on vertical swipes.
51 *
52 * This jQuery plugin will only run on devices running Mobile Webkit based browsers (iOS 2.0+, android 2.2+)
53 */
54(function($)
55{
56
57
58
59 $.fn.swipe = function(options)
60 {
61 if (!this) return false;
62
63 // Default thresholds & swipe functions
64 var defaults = {
65
66 fingers : 1, // int - The number of fingers to trigger the swipe, 1 or 2. Default is 1.
67 threshold : 75, // int - The number of pixels that the user must move their finger by before it is considered a swipe. Default is 75.
68
69 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".
70 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.
71 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.
72 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.
73 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.
74 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.
75 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.
76
77 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.
78 allowPageScroll : "auto" /* How the browser handles page scrolls when the user is swiping on a touchSwipe object.
79 "auto" : all undefined swipes will cause the page to scroll in that direction.
80 "none" : the page will not scroll when user swipes.
81 "horizontal" : will force page to scroll on horizontal swipes.
82 "vertical" : will force page to scroll on vertical swipes.
83 */
84 };
85
86
87 //Constants
88 var LEFT = "left";
89 var RIGHT = "right";
90 var UP = "up";
91 var DOWN = "down";
92 var NONE = "none";
93 var HORIZONTAL = "horizontal";
94 var VERTICAL = "vertical";
95 var AUTO = "auto";
96
97 var PHASE_START="start";
98 var PHASE_MOVE="move";
99 var PHASE_END="end";
100 var PHASE_CANCEL="cancel";
101
102 var hasTouch = 'ontouchstart' in window,
103 START_EV = hasTouch ? 'touchstart' : 'mousedown',
104 MOVE_EV = hasTouch ? 'touchmove' : 'mousemove',
105 END_EV = hasTouch ? 'touchend' : 'mouseup',
106 CANCEL_EV = 'touchcancel';
107
108 var phase="start";
109
110 if (options.allowPageScroll==undefined && (options.swipe!=undefined || options.swipeStatus!=undefined))
111 options.allowPageScroll=NONE;
112
113 if (options)
114 $.extend(defaults, options);
115
116
117 /**
118 * Setup each object to detect swipe gestures
119 */
120 return this.each(function()
121 {
122 var that = this;
123 var $this = $(this);
124
125 var triggerElementID = null; // this variable is used to identity the triggering element
126 var fingerCount = 0; // the current number of fingers being used.
127
128 //track mouse points / delta
129 var start={x:0, y:0};
130 var end={x:0, y:0};
131 var delta={x:0, y:0};
132 // added by Codrops
133 var lastPositionX = 0;
134
135 /**
136 * Event handler for a touch start event.
137 * Stops the default click event from triggering and stores where we touched
138 */
139 function touchStart(event)
140 {
141 var evt = hasTouch ? event.touches[0] : event;
142
143 phase = PHASE_START;
144
145 if (hasTouch) {
146 // get the total number of fingers touching the screen
147 fingerCount = event.touches.length;
148 }
149
150 //clear vars..
151 distance=0;
152 direction=null;
153
154 // check the number of fingers is what we are looking for
155 if (fingerCount == defaults.fingers || !hasTouch)
156 {
157 // get the coordinates of the touch
158 start.x = end.x = evt.pageX;
159 start.y = end.y = evt.pageY;
160 // changed by Codrops
161 lastPositionX = end.x;
162
163 if (defaults.swipeStatus)
164 triggerHandler(event, phase, start, end);
165 }
166 else
167 {
168 //touch with more/less than the fingers we are looking for
169 touchCancel(event);
170 }
171
172 that.addEventListener(MOVE_EV, touchMove, false);
173 that.addEventListener(END_EV, touchEnd, false);
174
175 }
176
177 /**
178 * Event handler for a touch move event.
179 * If we change fingers during move, then cancel the event
180 */
181 function touchMove(event)
182 {
183 if (phase == PHASE_END || phase == PHASE_CANCEL)
184 return;
185
186 var evt = hasTouch ? event.touches[0] : event;
187
188 end.x = evt.pageX;
189 end.y = evt.pageY;
190
191 // changed by Codrops
192 direction = calculateDirection();
193 lastPositionX = end.x;
194
195 if (hasTouch) {
196 fingerCount = event.touches.length;
197 }
198
199 phase = PHASE_MOVE
200
201 //Check if we need to prevent default evnet (page scroll) or not
202 validateDefaultEvent(event, direction);
203
204 if ( fingerCount == defaults.fingers || !hasTouch)
205 {
206 distance = caluculateDistance();
207
208 if (defaults.swipeStatus)
209 triggerHandler(event, phase, start, end, direction, distance);
210
211 //If we trigger whilst dragging, not on touch end, then calculate now...
212 if (!defaults.triggerOnTouchEnd)
213 {
214 // if the user swiped more than the minimum length, perform the appropriate action
215 if ( distance >= defaults.threshold )
216 {
217 phase = PHASE_END;
218 triggerHandler(event, phase, start, end);
219 touchCancel(event); // reset the variables
220 }
221 }
222 }
223 else
224 {
225 phase = PHASE_CANCEL;
226 triggerHandler(event, phase, start, end);
227 touchCancel(event);
228 }
229 }
230
231 /**
232 * Event handler for a touch end event.
233 * Calculate the direction and trigger events
234 */
235 function touchEnd(event)
236 {
237 event.preventDefault();
238
239 distance = caluculateDistance();
240
241 //changed by codrops
242 //direction = caluculateDirection();
243
244 if (defaults.triggerOnTouchEnd)
245 {
246 phase = PHASE_END;
247 // check to see if more than one finger was used and that there is an ending coordinate
248 if ( (fingerCount == defaults.fingers || !hasTouch) && end.x != 0 )
249 {
250 // if the user swiped more than the minimum length, perform the appropriate action
251 if ( distance >= defaults.threshold )
252 {
253 triggerHandler(event, phase, start, end);
254 touchCancel(event); // reset the variables
255 }
256 else
257 {
258 phase = PHASE_CANCEL;
259 triggerHandler(event, phase, start, end);
260 touchCancel(event);
261 }
262 }
263 else
264 {
265 phase = PHASE_CANCEL;
266 triggerHandler(event, phase, start, end);
267 touchCancel(event);
268 }
269 }
270 else if (phase == PHASE_MOVE)
271 {
272 phase = PHASE_CANCEL;
273 triggerHandler(event, phase, start, end);
274 touchCancel(event);
275 }
276 that.removeEventListener(MOVE_EV, touchMove, false);
277 that.removeEventListener(END_EV, touchEnd, false);
278 }
279
280 /**
281 * Event handler for a touch cancel event.
282 * Clears current vars
283 */
284 function touchCancel(event)
285 {
286 // reset the variables back to default values
287 fingerCount = 0;
288
289 start.x = 0;
290 start.y = 0;
291 end.x = 0;
292 end.y = 0;
293 delta.x = 0;
294 delta.y = 0;
295 }
296
297
298 /**
299 * Trigger the relevant event handler
300 * 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"
301 */
302 // changed by Codrops added start & end
303 function triggerHandler(event, phase, start, end)
304 {
305 //update status
306 if (defaults.swipeStatus)
307 defaults.swipeStatus.call($this,event, phase, start, end, direction || null, distance || 0);
308
309
310 if (phase == PHASE_CANCEL)
311 {
312 if (defaults.click && (fingerCount==1 || !hasTouch) && (isNaN(distance) || distance==0))
313 defaults.click.call($this,event, event.target);
314 }
315
316 if (phase == PHASE_END)
317 {
318 //trigger catch all event handler
319 if (defaults.swipe)
320 {
321
322 defaults.swipe.call($this,event, direction, distance);
323
324 }
325 //trigger direction specific event handlers
326 switch(direction)
327 {
328 case LEFT :
329 if (defaults.swipeLeft)
330 defaults.swipeLeft.call($this,event, direction, distance);
331 break;
332
333 case RIGHT :
334 if (defaults.swipeRight)
335 defaults.swipeRight.call($this,event, direction, distance);
336 break;
337
338 case UP :
339 if (defaults.swipeUp)
340 defaults.swipeUp.call($this,event, direction, distance);
341 break;
342
343 case DOWN :
344 if (defaults.swipeDown)
345 defaults.swipeDown.call($this,event, direction, distance);
346 break;
347 }
348 }
349 }
350
351
352 /**
353 * Checks direction of the swipe and the value allowPageScroll to see if we should allow or prevent the default behaviour from occurring.
354 * This will essentially allow page scrolling or not when the user is swiping on a touchSwipe object.
355 */
356 function validateDefaultEvent(event, direction)
357 {
358 if( defaults.allowPageScroll==NONE )
359 {
360 event.preventDefault();
361 }
362 else
363 {
364 var auto=defaults.allowPageScroll==AUTO;
365
366 switch(direction)
367 {
368 case LEFT :
369 if ( (defaults.swipeLeft && auto) || (!auto && defaults.allowPageScroll!=HORIZONTAL))
370 event.preventDefault();
371 break;
372
373 case RIGHT :
374 if ( (defaults.swipeRight && auto) || (!auto && defaults.allowPageScroll!=HORIZONTAL))
375 event.preventDefault();
376 break;
377
378 case UP :
379 if ( (defaults.swipeUp && auto) || (!auto && defaults.allowPageScroll!=VERTICAL))
380 event.preventDefault();
381 break;
382
383 case DOWN :
384 if ( (defaults.swipeDown && auto) || (!auto && defaults.allowPageScroll!=VERTICAL))
385 event.preventDefault();
386 break;
387 }
388 }
389
390 }
391
392
393
394 /**
395 * Calcualte the length / distance of the swipe
396 */
397 function caluculateDistance()
398 {
399 //return Math.round(Math.sqrt(Math.pow(end.x - start.x,2) + Math.pow(end.y - start.y,2)));
400 return Math.round(Math.abs(end.x - start.x));
401 }
402
403 /**
404 * Calcualte the angle of the swipe
405 */
406 function caluculateAngle()
407 {
408 var X = start.x-end.x;
409 var Y = end.y-start.y;
410 var r = Math.atan2(Y,X); //radians
411 var angle = Math.round(r*180/Math.PI); //degrees
412
413 //ensure value is positive
414 if (angle < 0)
415 angle = 360 - Math.abs(angle);
416
417 return angle;
418 }
419
420 /**
421 * Calcualte the direction of the swipe
422 * This will also call caluculateAngle to get the latest angle of swipe
423 */
424 function caluculateDirection()
425 {
426 var angle = caluculateAngle();
427
428 if ( (angle <= 45) && (angle >= 0) )
429 return LEFT;
430
431 else if ( (angle <= 360) && (angle >= 315) )
432 return LEFT;
433
434 else if ( (angle >= 135) && (angle <= 225) )
435 return RIGHT;
436
437 else if ( (angle > 45) && (angle < 135) )
438 return DOWN;
439
440 else
441 return UP;
442 }
443
444 // added by codrops
445 function calculateDirection()
446 {
447 var dir;
448 if( end.x < lastPositionX ) {
449 dir = LEFT
450 }
451 else if( end.x > lastPositionX ) {
452 dir = RIGHT;
453 }
454 else {
455 dir = UP
456 }
457 return dir;
458 }
459
460 // Add gestures to all swipable areas if supported
461 try
462 {
463
464 this.addEventListener(START_EV, touchStart, false);
465 this.addEventListener(CANCEL_EV, touchCancel);
466 }
467 catch(e)
468 {
469 //touch not supported
470 }
471
472 });
473 };
474
475
476
477
478})(jQuery);
Powered by cgit v1.2.3 (git 2.41.0)