jquery.touchSwipe.js.html 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jquery.touchSwipe.js - Documentation</title>
  6. <script src="scripts/prettify/prettify.js"></script>
  7. <script src="scripts/prettify/lang-css.js"></script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
  12. <link type="text/css" rel="stylesheet" href="styles/prettify.css">
  13. <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
  14. </head>
  15. <body>
  16. <input type="checkbox" id="nav-trigger" class="nav-trigger" />
  17. <label for="nav-trigger" class="navicon-button x">
  18. <div class="navicon"></div>
  19. </label>
  20. <label for="nav-trigger" class="overlay"></label>
  21. <nav>
  22. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="$.html">$</a></li><li><a href="$.fn.html">fn</a></li><li><a href="$.fn.swipe.html">swipe</a><ul class='methods'><li data-type='method'><a href="$.fn.swipe.html#destroy">destroy</a></li><li data-type='method'><a href="$.fn.swipe.html#disable">disable</a></li><li data-type='method'><a href="$.fn.swipe.html#enable">enable</a></li><li data-type='method'><a href="$.fn.swipe.html#option">option</a></li></ul></li></ul><h3>Events</h3><ul><li><a href="$.fn.swipe.html#event:click">click</a></li><li><a href="$.fn.swipe.html#event:doubleTap">doubleTap</a></li><li><a href="$.fn.swipe.html#event:hold">hold</a></li><li><a href="$.fn.swipe.html#event:longTap">longTap</a></li><li><a href="$.fn.swipe.html#event:pinchIn">pinchIn</a></li><li><a href="$.fn.swipe.html#event:pinchOut">pinchOut</a></li><li><a href="$.fn.swipe.html#event:pinchStatus">pinchStatus</a></li><li><a href="$.fn.swipe.html#event:swipe">swipe</a></li><li><a href="$.fn.swipe.html#event:swipeDown">swipeDown</a></li><li><a href="$.fn.swipe.html#event:swipeLeft">swipeLeft</a></li><li><a href="$.fn.swipe.html#event:swipeRight">swipeRight</a></li><li><a href="$.fn.swipe.html#event:swipeStatus">swipeStatus</a></li><li><a href="$.fn.swipe.html#event:swipeUp">swipeUp</a></li><li><a href="$.fn.swipe.html#event:tap">tap</a></li></ul><h3>Namespaces</h3><ul><li><a href="$.fn.swipe.defaults.html">defaults</a></li><li><a href="$.fn.swipe.directions.html">directions</a></li><li><a href="$.fn.swipe.fingers.html">fingers</a></li><li><a href="$.fn.swipe.pageScroll.html">pageScroll</a></li><li><a href="$.fn.swipe.phases.html">phases</a></li></ul><h3>Tutorials</h3><ul><li><a href="tutorial-Any_finger_swipe.html">Any finger swipe</a></li><li><a href="tutorial-Basic_swipe.html">Basic swipe</a></li><li><a href="tutorial-Enable_and_destroy.html">Enable and destroy</a></li><li><a href="tutorial-Excluded_children.html">Excluded children</a></li><li><a href="tutorial-Finger_swipe.html">Finger swipe</a></li><li><a href="tutorial-Handlers_and_events.html">Handlers and events</a></li><li><a href="tutorial-Hold.html">Hold</a></li><li><a href="tutorial-Image_gallery_example.html">Image gallery example</a></li><li><a href="tutorial-Options.html">Options</a></li><li><a href="tutorial-Page_scrolling.html">Page scrolling</a></li><li><a href="tutorial-Page_zoom.html">Page zoom</a></li><li><a href="tutorial-Pinch.html">Pinch</a></li><li><a href="tutorial-Pinch_and_Swipe.html">Pinch and Swipe</a></li><li><a href="tutorial-Pinch_status.html">Pinch status</a></li><li><a href="tutorial-Single_swipe.html">Single swipe</a></li><li><a href="tutorial-Stop_propegation.html">Stop_propegation</a></li><li><a href="tutorial-Swipe_status.html">Swipe status</a></li><li><a href="tutorial-Tap_vs_swipe.html">Tap vs swipe</a></li><li><a href="tutorial-Thresholds.html">Thresholds</a></li><li><a href="tutorial-Trigger_handlers.html">Trigger handlers</a></li><li><a href="tutorial-index_.html">index</a></li></ul>
  23. </nav>
  24. <div id="main">
  25. <h1 class="page-title">jquery.touchSwipe.js</h1>
  26. <section>
  27. <article>
  28. <pre class="prettyprint source linenums"><code>/*!
  29. * @fileOverview TouchSwipe - jQuery Plugin
  30. * @version 1.6.18
  31. *
  32. * @author Matt Bryson http://www.github.com/mattbryson
  33. * @see https://github.com/mattbryson/TouchSwipe-Jquery-Plugin
  34. * @see http://labs.rampinteractive.co.uk/touchSwipe/
  35. * @see http://plugins.jquery.com/project/touchSwipe
  36. * @license
  37. * Copyright (c) 2010-2015 Matt Bryson
  38. * Dual licensed under the MIT or GPL Version 2 licenses.
  39. *
  40. */
  41. /*
  42. *
  43. * Changelog
  44. * $Date: 2010-12-12 (Wed, 12 Dec 2010) $
  45. * $version: 1.0.0
  46. * $version: 1.0.1 - removed multibyte comments
  47. *
  48. * $Date: 2011-21-02 (Mon, 21 Feb 2011) $
  49. * $version: 1.1.0 - added allowPageScroll property to allow swiping and scrolling of page
  50. * - changed handler signatures so one handler can be used for multiple events
  51. * $Date: 2011-23-02 (Wed, 23 Feb 2011) $
  52. * $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.
  53. * - 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.
  54. * $version: 1.2.1 - removed console log!
  55. *
  56. * $version: 1.2.2 - Fixed bug where scope was not preserved in callback methods.
  57. *
  58. * $Date: 2011-28-04 (Thurs, 28 April 2011) $
  59. * $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.
  60. *
  61. * $Date: 2011-27-09 (Tues, 27 September 2011) $
  62. * $version: 1.2.5 - Added support for testing swipes with mouse on desktop browser (thanks to https://github.com/joelhy)
  63. *
  64. * $Date: 2012-14-05 (Mon, 14 May 2012) $
  65. * $version: 1.2.6 - Added timeThreshold between start and end touch, so user can ignore slow swipes (thanks to Mark Chase). Default is null, all swipes are detected
  66. *
  67. * $Date: 2012-05-06 (Tues, 05 June 2012) $
  68. * $version: 1.2.7 - Changed time threshold to have null default for backwards compatibility. Added duration param passed back in events, and refactored how time is handled.
  69. *
  70. * $Date: 2012-05-06 (Tues, 05 June 2012) $
  71. * $version: 1.2.8 - Added the possibility to return a value like null or false in the trigger callback. In that way we can control when the touch start/move should take effect or not (simply by returning in some cases return null; or return false;) This effects the ontouchstart/ontouchmove event.
  72. *
  73. * $Date: 2012-06-06 (Wed, 06 June 2012) $
  74. * $version: 1.3.0 - Refactored whole plugin to allow for methods to be executed, as well as exposed defaults for user override. Added 'enable', 'disable', and 'destroy' methods
  75. *
  76. * $Date: 2012-05-06 (Fri, 05 June 2012) $
  77. * $version: 1.3.1 - Bug fixes - bind() with false as last argument is no longer supported in jQuery 1.6, also, if you just click, the duration is now returned correctly.
  78. *
  79. * $Date: 2012-29-07 (Sun, 29 July 2012) $
  80. * $version: 1.3.2 - Added fallbackToMouseEvents option to NOT capture mouse events on non touch devices.
  81. * - Added "all" fingers value to the fingers property, so any combination of fingers triggers the swipe, allowing event handlers to check the finger count
  82. *
  83. * $Date: 2012-09-08 (Thurs, 9 Aug 2012) $
  84. * $version: 1.3.3 - Code tidy prep for min version
  85. *
  86. * $Date: 2012-04-10 (wed, 4 Oct 2012) $
  87. * $version: 1.4.0 - Added pinch support, pinchIn and pinchOut
  88. *
  89. * $Date: 2012-11-10 (Thurs, 11 Oct 2012) $
  90. * $version: 1.5.0 - Added excludedElements, a jquery selector that specifies child elements that do NOT trigger swipes. By default, this is .noSwipe
  91. *
  92. * $Date: 2012-22-10 (Mon, 22 Oct 2012) $
  93. * $version: 1.5.1 - Fixed bug with jQuery 1.8 and trailing comma in excludedElements
  94. * - Fixed bug with IE and eventPreventDefault()
  95. * $Date: 2013-01-12 (Fri, 12 Jan 2013) $
  96. * $version: 1.6.0 - Fixed bugs with pinching, mainly when both pinch and swipe enabled, as well as adding time threshold for multifinger gestures, so releasing one finger beofre the other doesnt trigger as single finger gesture.
  97. * - made the demo site all static local HTML pages so they can be run locally by a developer
  98. * - added jsDoc comments and added documentation for the plugin
  99. * - code tidy
  100. * - added triggerOnTouchLeave property that will end the event when the user swipes off the element.
  101. * $Date: 2013-03-23 (Sat, 23 Mar 2013) $
  102. * $version: 1.6.1 - Added support for ie8 touch events
  103. * $version: 1.6.2 - Added support for events binding with on / off / bind in jQ for all callback names.
  104. * - Deprecated the 'click' handler in favour of tap.
  105. * - added cancelThreshold property
  106. * - added option method to update init options at runtime
  107. * $version 1.6.3 - added doubletap, longtap events and longTapThreshold, doubleTapThreshold property
  108. *
  109. * $Date: 2013-04-04 (Thurs, 04 April 2013) $
  110. * $version 1.6.4 - Fixed bug with cancelThreshold introduced in 1.6.3, where swipe status no longer fired start event, and stopped once swiping back.
  111. *
  112. * $Date: 2013-08-24 (Sat, 24 Aug 2013) $
  113. * $version 1.6.5 - Merged a few pull requests fixing various bugs, added AMD support.
  114. *
  115. * $Date: 2014-06-04 (Wed, 04 June 2014) $
  116. * $version 1.6.6 - Merge of pull requests.
  117. * - IE10 touch support
  118. * - Only prevent default event handling on valid swipe
  119. * - Separate license/changelog comment
  120. * - Detect if the swipe is valid at the end of the touch event.
  121. * - Pass fingerdata to event handlers.
  122. * - Add 'hold' gesture
  123. * - Be more tolerant about the tap distance
  124. * - Typos and minor fixes
  125. *
  126. * $Date: 2015-22-01 (Thurs, 22 Jan 2015) $
  127. * $version 1.6.7 - Added patch from https://github.com/mattbryson/TouchSwipe-Jquery-Plugin/issues/206 to fix memory leak
  128. *
  129. * $Date: 2015-2-2 (Mon, 2 Feb 2015) $
  130. * $version 1.6.8 - Added preventDefaultEvents option to proxy events regardless.
  131. * - Fixed issue with swipe and pinch not triggering at the same time
  132. *
  133. * $Date: 2015-9-6 (Tues, 9 June 2015) $
  134. * $version 1.6.9 - Added PR from jdalton/hybrid to fix pointer events
  135. * - Added scrolling demo
  136. * - Added version property to plugin
  137. *
  138. * $Date: 2015-1-10 (Wed, 1 October 2015) $
  139. * $version 1.6.10 - Added PR from beatspace to fix tap events
  140. * $version 1.6.11 - Added PRs from indri-indri ( Doc tidyup), kkirsche ( Bower tidy up ), UziTech (preventDefaultEvents fixes )
  141. * - Allowed setting multiple options via .swipe("options", options_hash) and more simply .swipe(options_hash) or exisitng instances
  142. * $version 1.6.12 - Fixed bug with multi finger releases above 2 not triggering events
  143. *
  144. * $Date: 2015-12-18 (Fri, 18 December 2015) $
  145. * $version 1.6.13 - Added PRs
  146. * - Fixed #267 allowPageScroll not working correctly
  147. * $version 1.6.14 - Fixed #220 / #248 doubletap not firing with swipes, #223 commonJS compatible
  148. * $version 1.6.15 - More bug fixes
  149. *
  150. * $Date: 2016-04-29 (Fri, 29 April 2016) $
  151. * $version 1.6.16 - Swipes with 0 distance now allow default events to trigger. So tapping any form elements or A tags will allow default interaction, but swiping will trigger a swipe.
  152. * Removed the a, input, select etc from the excluded Children list as the 0 distance tap solves that issue.
  153. * $Date: 2016-05-19 (Fri, 29 April 2016) $
  154. * $version 1.6.17 - Fixed context issue when calling instance methods via $("selector").swipe("method");
  155. * $version 1.6.18 - now honors fallbackToMouseEvents=false for MS Pointer events when a Mouse is used.
  156. *
  157. * $Date: 2018-09-17 (Mon, 17 September 2018) $
  158. * $version 1.6.19 - replaced jQuery bind with on, replaced deprecated `navigator.pointerEvents` with `window.PointerEvents`
  159. */
  160. /**
  161. * See (http://jquery.com/).
  162. * @name $
  163. * @class
  164. * See the jQuery Library (http://jquery.com/) for full details. This just
  165. * documents the function and classes that are added to jQuery by this plug-in.
  166. */
  167. /**
  168. * See (http://jquery.com/)
  169. * @name fn
  170. * @class
  171. * See the jQuery Library (http://jquery.com/) for full details. This just
  172. * documents the function and classes that are added to jQuery by this plug-in.
  173. * @memberOf $
  174. */
  175. (function(factory) {
  176. if (typeof define === 'function' &amp;&amp; define.amd &amp;&amp; define.amd.jQuery) {
  177. // AMD. Register as anonymous module.
  178. define(['jquery'], factory);
  179. } else if (typeof module !== 'undefined' &amp;&amp; module.exports) {
  180. // CommonJS Module
  181. factory(require("jquery"));
  182. } else {
  183. // Browser globals.
  184. factory(jQuery);
  185. }
  186. }(function($) {
  187. "use strict";
  188. //Constants
  189. var VERSION = "1.6.18",
  190. LEFT = "left",
  191. RIGHT = "right",
  192. UP = "up",
  193. DOWN = "down",
  194. IN = "in",
  195. OUT = "out",
  196. NONE = "none",
  197. AUTO = "auto",
  198. SWIPE = "swipe",
  199. PINCH = "pinch",
  200. TAP = "tap",
  201. DOUBLE_TAP = "doubletap",
  202. LONG_TAP = "longtap",
  203. HOLD = "hold",
  204. HORIZONTAL = "horizontal",
  205. VERTICAL = "vertical",
  206. ALL_FINGERS = "all",
  207. DOUBLE_TAP_THRESHOLD = 10,
  208. PHASE_START = "start",
  209. PHASE_MOVE = "move",
  210. PHASE_END = "end",
  211. PHASE_CANCEL = "cancel",
  212. SUPPORTS_TOUCH = 'ontouchstart' in window,
  213. SUPPORTS_POINTER_IE10 = window.navigator.msPointerEnabled &amp;&amp; !window.PointerEvent &amp;&amp; !SUPPORTS_TOUCH,
  214. SUPPORTS_POINTER = (window.PointerEvent || window.navigator.msPointerEnabled) &amp;&amp; !SUPPORTS_TOUCH,
  215. PLUGIN_NS = 'TouchSwipe';
  216. /**
  217. * The default configuration, and available options to configure touch swipe with.
  218. * You can set the default values by updating any of the properties prior to instantiation.
  219. * @name $.fn.swipe.defaults
  220. * @namespace
  221. * @property {int} [fingers=1] The number of fingers to detect in a swipe. Any swipes that do not meet this requirement will NOT trigger swipe handlers.
  222. * @property {int} [threshold=75] The number of pixels that the user must move their finger by before it is considered a swipe.
  223. * @property {int} [cancelThreshold=null] The number of pixels that the user must move their finger back from the original swipe direction to cancel the gesture.
  224. * @property {int} [pinchThreshold=20] The number of pixels that the user must pinch their finger by before it is considered a pinch.
  225. * @property {int} [maxTimeThreshold=null] Time, in milliseconds, between touchStart and touchEnd must NOT exceed in order to be considered a swipe.
  226. * @property {int} [fingerReleaseThreshold=250] Time in milliseconds between releasing multiple fingers. If 2 fingers are down, and are released one after the other, if they are within this threshold, it counts as a simultaneous release.
  227. * @property {int} [longTapThreshold=500] Time in milliseconds between tap and release for a long tap
  228. * @property {int} [doubleTapThreshold=200] Time in milliseconds between 2 taps to count as a double tap
  229. * @property {function} [swipe=null] A handler to catch all swipes. See {@link $.fn.swipe#event:swipe}
  230. * @property {function} [swipeLeft=null] A handler that is triggered for "left" swipes. See {@link $.fn.swipe#event:swipeLeft}
  231. * @property {function} [swipeRight=null] A handler that is triggered for "right" swipes. See {@link $.fn.swipe#event:swipeRight}
  232. * @property {function} [swipeUp=null] A handler that is triggered for "up" swipes. See {@link $.fn.swipe#event:swipeUp}
  233. * @property {function} [swipeDown=null] A handler that is triggered for "down" swipes. See {@link $.fn.swipe#event:swipeDown}
  234. * @property {function} [swipeStatus=null] A handler triggered for every phase of the swipe. See {@link $.fn.swipe#event:swipeStatus}
  235. * @property {function} [pinchIn=null] A handler triggered for pinch in events. See {@link $.fn.swipe#event:pinchIn}
  236. * @property {function} [pinchOut=null] A handler triggered for pinch out events. See {@link $.fn.swipe#event:pinchOut}
  237. * @property {function} [pinchStatus=null] A handler triggered for every phase of a pinch. See {@link $.fn.swipe#event:pinchStatus}
  238. * @property {function} [tap=null] A handler triggered when a user just taps on the item, rather than swipes it. If they do not move, tap is triggered, if they do move, it is not.
  239. * @property {function} [doubleTap=null] A handler triggered when a user double taps on the item. The delay between taps can be set with the doubleTapThreshold property. See {@link $.fn.swipe.defaults#doubleTapThreshold}
  240. * @property {function} [longTap=null] A handler triggered when a user long taps on the item. The delay between start and end can be set with the longTapThreshold property. See {@link $.fn.swipe.defaults#longTapThreshold}
  241. * @property (function) [hold=null] A handler triggered when a user reaches longTapThreshold on the item. See {@link $.fn.swipe.defaults#longTapThreshold}
  242. * @property {boolean} [triggerOnTouchEnd=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.
  243. * @property {boolean} [triggerOnTouchLeave=false] If true, then when the user leaves the swipe object, the swipe will end and trigger appropriate handlers.
  244. * @property {string|undefined} [allowPageScroll='auto'] How the browser handles page scrolls when the user is swiping on a touchSwipe object. See {@link $.fn.swipe.pageScroll}. &lt;br/>&lt;br/>
  245. &lt;code>"auto"&lt;/code> : all undefined swipes will cause the page to scroll in that direction. &lt;br/>
  246. &lt;code>"none"&lt;/code> : the page will not scroll when user swipes. &lt;br/>
  247. &lt;code>"horizontal"&lt;/code> : will force page to scroll on horizontal swipes. &lt;br/>
  248. &lt;code>"vertical"&lt;/code> : will force page to scroll on vertical swipes. &lt;br/>
  249. * @property {boolean} [fallbackToMouseEvents=true] If true mouse events are used when run on a non touch device, false will stop swipes being triggered by mouse events on non touch devices.
  250. * @property {string} [excludedElements=".noSwipe"] A jquery selector that specifies child elements that do NOT trigger swipes. By default this excludes elements with the class .noSwipe .
  251. * @property {boolean} [preventDefaultEvents=true] by default default events are cancelled, so the page doesn't move. You can disable this so both native events fire as well as your handlers.
  252. */
  253. var defaults = {
  254. fingers: 1,
  255. threshold: 75,
  256. cancelThreshold: null,
  257. pinchThreshold: 20,
  258. maxTimeThreshold: null,
  259. fingerReleaseThreshold: 250,
  260. longTapThreshold: 500,
  261. doubleTapThreshold: 200,
  262. swipe: null,
  263. swipeLeft: null,
  264. swipeRight: null,
  265. swipeUp: null,
  266. swipeDown: null,
  267. swipeStatus: null,
  268. pinchIn: null,
  269. pinchOut: null,
  270. pinchStatus: null,
  271. click: null, //Deprecated since 1.6.2
  272. tap: null,
  273. doubleTap: null,
  274. longTap: null,
  275. hold: null,
  276. triggerOnTouchEnd: true,
  277. triggerOnTouchLeave: false,
  278. allowPageScroll: "auto",
  279. fallbackToMouseEvents: true,
  280. excludedElements: ".noSwipe",
  281. preventDefaultEvents: true
  282. };
  283. /**
  284. * Applies TouchSwipe behaviour to one or more jQuery objects.
  285. * The TouchSwipe plugin can be instantiated via this method, or methods within
  286. * TouchSwipe can be executed via this method as per jQuery plugin architecture.
  287. * An existing plugin can have its options changed simply by re calling .swipe(options)
  288. * @see TouchSwipe
  289. * @class
  290. * @param {Mixed} method If the current DOMNode is a TouchSwipe object, and &lt;code>method&lt;/code> is a TouchSwipe method, then
  291. * the &lt;code>method&lt;/code> is executed, and any following arguments are passed to the TouchSwipe method.
  292. * If &lt;code>method&lt;/code> is an object, then the TouchSwipe class is instantiated on the current DOMNode, passing the
  293. * configuration properties defined in the object. See TouchSwipe
  294. *
  295. */
  296. $.fn.swipe = function(method) {
  297. var $this = $(this),
  298. plugin = $this.data(PLUGIN_NS);
  299. //Check if we are already instantiated and trying to execute a method
  300. if (plugin &amp;&amp; typeof method === 'string') {
  301. if (plugin[method]) {
  302. return plugin[method].apply(plugin, Array.prototype.slice.call(arguments, 1));
  303. } else {
  304. $.error('Method ' + method + ' does not exist on jQuery.swipe');
  305. }
  306. }
  307. //Else update existing plugin with new options hash
  308. else if (plugin &amp;&amp; typeof method === 'object') {
  309. plugin['option'].apply(plugin, arguments);
  310. }
  311. //Else not instantiated and trying to pass init object (or nothing)
  312. else if (!plugin &amp;&amp; (typeof method === 'object' || !method)) {
  313. return init.apply(this, arguments);
  314. }
  315. return $this;
  316. };
  317. /**
  318. * The version of the plugin
  319. * @readonly
  320. */
  321. $.fn.swipe.version = VERSION;
  322. //Expose our defaults so a user could override the plugin defaults
  323. $.fn.swipe.defaults = defaults;
  324. /**
  325. * The phases that a touch event goes through. The &lt;code>phase&lt;/code> is passed to the event handlers.
  326. * These properties are read only, attempting to change them will not alter the values passed to the event handlers.
  327. * @namespace
  328. * @readonly
  329. * @property {string} PHASE_START Constant indicating the start phase of the touch event. Value is &lt;code>"start"&lt;/code>.
  330. * @property {string} PHASE_MOVE Constant indicating the move phase of the touch event. Value is &lt;code>"move"&lt;/code>.
  331. * @property {string} PHASE_END Constant indicating the end phase of the touch event. Value is &lt;code>"end"&lt;/code>.
  332. * @property {string} PHASE_CANCEL Constant indicating the cancel phase of the touch event. Value is &lt;code>"cancel"&lt;/code>.
  333. */
  334. $.fn.swipe.phases = {
  335. PHASE_START: PHASE_START,
  336. PHASE_MOVE: PHASE_MOVE,
  337. PHASE_END: PHASE_END,
  338. PHASE_CANCEL: PHASE_CANCEL
  339. };
  340. /**
  341. * The direction constants that are passed to the event handlers.
  342. * These properties are read only, attempting to change them will not alter the values passed to the event handlers.
  343. * @namespace
  344. * @readonly
  345. * @property {string} LEFT Constant indicating the left direction. Value is &lt;code>"left"&lt;/code>.
  346. * @property {string} RIGHT Constant indicating the right direction. Value is &lt;code>"right"&lt;/code>.
  347. * @property {string} UP Constant indicating the up direction. Value is &lt;code>"up"&lt;/code>.
  348. * @property {string} DOWN Constant indicating the down direction. Value is &lt;code>"cancel"&lt;/code>.
  349. * @property {string} IN Constant indicating the in direction. Value is &lt;code>"in"&lt;/code>.
  350. * @property {string} OUT Constant indicating the out direction. Value is &lt;code>"out"&lt;/code>.
  351. */
  352. $.fn.swipe.directions = {
  353. LEFT: LEFT,
  354. RIGHT: RIGHT,
  355. UP: UP,
  356. DOWN: DOWN,
  357. IN: IN,
  358. OUT: OUT
  359. };
  360. /**
  361. * The page scroll constants that can be used to set the value of &lt;code>allowPageScroll&lt;/code> option
  362. * These properties are read only
  363. * @namespace
  364. * @readonly
  365. * @see $.fn.swipe.defaults#allowPageScroll
  366. * @property {string} NONE Constant indicating no page scrolling is allowed. Value is &lt;code>"none"&lt;/code>.
  367. * @property {string} HORIZONTAL Constant indicating horizontal page scrolling is allowed. Value is &lt;code>"horizontal"&lt;/code>.
  368. * @property {string} VERTICAL Constant indicating vertical page scrolling is allowed. Value is &lt;code>"vertical"&lt;/code>.
  369. * @property {string} AUTO Constant indicating either horizontal or vertical will be allowed, depending on the swipe handlers registered. Value is &lt;code>"auto"&lt;/code>.
  370. */
  371. $.fn.swipe.pageScroll = {
  372. NONE: NONE,
  373. HORIZONTAL: HORIZONTAL,
  374. VERTICAL: VERTICAL,
  375. AUTO: AUTO
  376. };
  377. /**
  378. * Constants representing the number of fingers used in a swipe. These are used to set both the value of &lt;code>fingers&lt;/code> in the
  379. * options object, as well as the value of the &lt;code>fingers&lt;/code> event property.
  380. * These properties are read only, attempting to change them will not alter the values passed to the event handlers.
  381. * @namespace
  382. * @readonly
  383. * @see $.fn.swipe.defaults#fingers
  384. * @property {string} ONE Constant indicating 1 finger is to be detected / was detected. Value is &lt;code>1&lt;/code>.
  385. * @property {string} TWO Constant indicating 2 fingers are to be detected / were detected. Value is &lt;code>2&lt;/code>.
  386. * @property {string} THREE Constant indicating 3 finger are to be detected / were detected. Value is &lt;code>3&lt;/code>.
  387. * @property {string} FOUR Constant indicating 4 finger are to be detected / were detected. Not all devices support this. Value is &lt;code>4&lt;/code>.
  388. * @property {string} FIVE Constant indicating 5 finger are to be detected / were detected. Not all devices support this. Value is &lt;code>5&lt;/code>.
  389. * @property {string} ALL Constant indicating any combination of finger are to be detected. Value is &lt;code>"all"&lt;/code>.
  390. */
  391. $.fn.swipe.fingers = {
  392. ONE: 1,
  393. TWO: 2,
  394. THREE: 3,
  395. FOUR: 4,
  396. FIVE: 5,
  397. ALL: ALL_FINGERS
  398. };
  399. /**
  400. * Initialise the plugin for each DOM element matched
  401. * This creates a new instance of the main TouchSwipe class for each DOM element, and then
  402. * saves a reference to that instance in the elements data property.
  403. * @internal
  404. */
  405. function init(options) {
  406. //Prep and extend the options
  407. if (options &amp;&amp; (options.allowPageScroll === undefined &amp;&amp; (options.swipe !== undefined || options.swipeStatus !== undefined))) {
  408. options.allowPageScroll = NONE;
  409. }
  410. //Check for deprecated options
  411. //Ensure that any old click handlers are assigned to the new tap, unless we have a tap
  412. if (options.click !== undefined &amp;&amp; options.tap === undefined) {
  413. options.tap = options.click;
  414. }
  415. if (!options) {
  416. options = {};
  417. }
  418. //pass empty object so we dont modify the defaults
  419. options = $.extend({}, $.fn.swipe.defaults, options);
  420. //For each element instantiate the plugin
  421. return this.each(function() {
  422. var $this = $(this);
  423. //Check we havent already initialised the plugin
  424. var plugin = $this.data(PLUGIN_NS);
  425. if (!plugin) {
  426. plugin = new TouchSwipe(this, options);
  427. $this.data(PLUGIN_NS, plugin);
  428. }
  429. });
  430. }
  431. /**
  432. * Main TouchSwipe Plugin Class.
  433. * Do not use this to construct your TouchSwipe object, use the jQuery plugin method $.fn.swipe(); {@link $.fn.swipe}
  434. * @private
  435. * @name TouchSwipe
  436. * @param {DOMNode} element The HTML DOM object to apply to plugin to
  437. * @param {Object} options The options to configure the plugin with. @link {$.fn.swipe.defaults}
  438. * @see $.fh.swipe.defaults
  439. * @see $.fh.swipe
  440. * @class
  441. */
  442. function TouchSwipe(element, options) {
  443. //take a local/instacne level copy of the options - should make it this.options really...
  444. var options = $.extend({}, options);
  445. var useTouchEvents = (SUPPORTS_TOUCH || SUPPORTS_POINTER || !options.fallbackToMouseEvents),
  446. START_EV = useTouchEvents ? (SUPPORTS_POINTER ? (SUPPORTS_POINTER_IE10 ? 'MSPointerDown' : 'pointerdown') : 'touchstart') : 'mousedown',
  447. MOVE_EV = useTouchEvents ? (SUPPORTS_POINTER ? (SUPPORTS_POINTER_IE10 ? 'MSPointerMove' : 'pointermove') : 'touchmove') : 'mousemove',
  448. END_EV = useTouchEvents ? (SUPPORTS_POINTER ? (SUPPORTS_POINTER_IE10 ? 'MSPointerUp' : 'pointerup') : 'touchend') : 'mouseup',
  449. LEAVE_EV = useTouchEvents ? (SUPPORTS_POINTER ? 'mouseleave' : null) : 'mouseleave', //we manually detect leave on touch devices, so null event here
  450. CANCEL_EV = (SUPPORTS_POINTER ? (SUPPORTS_POINTER_IE10 ? 'MSPointerCancel' : 'pointercancel') : 'touchcancel');
  451. //touch properties
  452. var distance = 0,
  453. direction = null,
  454. currentDirection = null,
  455. duration = 0,
  456. startTouchesDistance = 0,
  457. endTouchesDistance = 0,
  458. pinchZoom = 1,
  459. pinchDistance = 0,
  460. pinchDirection = 0,
  461. maximumsMap = null;
  462. //jQuery wrapped element for this instance
  463. var $element = $(element);
  464. //Current phase of th touch cycle
  465. var phase = "start";
  466. // the current number of fingers being used.
  467. var fingerCount = 0;
  468. //track mouse points / delta
  469. var fingerData = {};
  470. //track times
  471. var startTime = 0,
  472. endTime = 0,
  473. previousTouchEndTime = 0,
  474. fingerCountAtRelease = 0,
  475. doubleTapStartTime = 0;
  476. //Timeouts
  477. var singleTapTimeout = null,
  478. holdTimeout = null;
  479. // Add gestures to all swipable areas if supported
  480. try {
  481. $element.on(START_EV, touchStart);
  482. $element.on(CANCEL_EV, touchCancel);
  483. } catch (e) {
  484. $.error('events not supported ' + START_EV + ',' + CANCEL_EV + ' on jQuery.swipe');
  485. }
  486. //
  487. //Public methods
  488. //
  489. /**
  490. * re-enables the swipe plugin with the previous configuration
  491. * @function
  492. * @name $.fn.swipe#enable
  493. * @return {DOMNode} The Dom element that was registered with TouchSwipe
  494. * @example $("#element").swipe("enable");
  495. */
  496. this.enable = function() {
  497. //Incase we are already enabled, clean up...
  498. this.disable();
  499. $element.on(START_EV, touchStart);
  500. $element.on(CANCEL_EV, touchCancel);
  501. return $element;
  502. };
  503. /**
  504. * disables the swipe plugin
  505. * @function
  506. * @name $.fn.swipe#disable
  507. * @return {DOMNode} The Dom element that is now registered with TouchSwipe
  508. * @example $("#element").swipe("disable");
  509. */
  510. this.disable = function() {
  511. removeListeners();
  512. return $element;
  513. };
  514. /**
  515. * Destroy the swipe plugin completely. To use any swipe methods, you must re initialise the plugin.
  516. * @function
  517. * @name $.fn.swipe#destroy
  518. * @example $("#element").swipe("destroy");
  519. */
  520. this.destroy = function() {
  521. removeListeners();
  522. $element.data(PLUGIN_NS, null);
  523. $element = null;
  524. };
  525. /**
  526. * Allows run time updating of the swipe configuration options.
  527. * @function
  528. * @name $.fn.swipe#option
  529. * @param {String} property The option property to get or set, or a has of multiple options to set
  530. * @param {Object} [value] The value to set the property to
  531. * @return {Object} If only a property name is passed, then that property value is returned. If nothing is passed the current options hash is returned.
  532. * @example $("#element").swipe("option", "threshold"); // return the threshold
  533. * @example $("#element").swipe("option", "threshold", 100); // set the threshold after init
  534. * @example $("#element").swipe("option", {threshold:100, fingers:3} ); // set multiple properties after init
  535. * @example $("#element").swipe({threshold:100, fingers:3} ); // set multiple properties after init - the "option" method is optional!
  536. * @example $("#element").swipe("option"); // Return the current options hash
  537. * @see $.fn.swipe.defaults
  538. *
  539. */
  540. this.option = function(property, value) {
  541. if (typeof property === 'object') {
  542. options = $.extend(options, property);
  543. } else if (options[property] !== undefined) {
  544. if (value === undefined) {
  545. return options[property];
  546. } else {
  547. options[property] = value;
  548. }
  549. } else if (!property) {
  550. return options;
  551. } else {
  552. $.error('Option ' + property + ' does not exist on jQuery.swipe.options');
  553. }
  554. return null;
  555. }
  556. //
  557. // Private methods
  558. //
  559. //
  560. // EVENTS
  561. //
  562. /**
  563. * Event handler for a touch start event.
  564. * Stops the default click event from triggering and stores where we touched
  565. * @inner
  566. * @param {object} jqEvent The normalised jQuery event object.
  567. */
  568. function touchStart(jqEvent) {
  569. //If we already in a touch event (a finger already in use) then ignore subsequent ones..
  570. if (getTouchInProgress()) {
  571. return;
  572. }
  573. //Check if this element matches any in the excluded elements selectors, or its parent is excluded, if so, DON'T swipe
  574. if ($(jqEvent.target).closest(options.excludedElements, $element).length > 0) {
  575. return;
  576. }
  577. //As we use Jquery bind for events, we need to target the original event object
  578. //If these events are being programmatically triggered, we don't have an original event object, so use the Jq one.
  579. var event = jqEvent.originalEvent ? jqEvent.originalEvent : jqEvent;
  580. //If we have a pointer event, whoes type is 'mouse' and we have said NO mouse events, then dont do anything.
  581. if(event.pointerType &amp;&amp; event.pointerType=="mouse" &amp;&amp; options.fallbackToMouseEvents==false) {
  582. return;
  583. };
  584. var ret,
  585. touches = event.touches,
  586. evt = touches ? touches[0] : event;
  587. phase = PHASE_START;
  588. //If we support touches, get the finger count
  589. if (touches) {
  590. // get the total number of fingers touching the screen
  591. fingerCount = touches.length;
  592. }
  593. //Else this is the desktop, so stop the browser from dragging content
  594. else if (options.preventDefaultEvents !== false) {
  595. jqEvent.preventDefault(); //call this on jq event so we are cross browser
  596. }
  597. //clear vars..
  598. distance = 0;
  599. direction = null;
  600. currentDirection=null;
  601. pinchDirection = null;
  602. duration = 0;
  603. startTouchesDistance = 0;
  604. endTouchesDistance = 0;
  605. pinchZoom = 1;
  606. pinchDistance = 0;
  607. maximumsMap = createMaximumsData();
  608. cancelMultiFingerRelease();
  609. //Create the default finger data
  610. createFingerData(0, evt);
  611. // check the number of fingers is what we are looking for, or we are capturing pinches
  612. if (!touches || (fingerCount === options.fingers || options.fingers === ALL_FINGERS) || hasPinches()) {
  613. // get the coordinates of the touch
  614. startTime = getTimeStamp();
  615. if (fingerCount == 2) {
  616. //Keep track of the initial pinch distance, so we can calculate the diff later
  617. //Store second finger data as start
  618. createFingerData(1, touches[1]);
  619. startTouchesDistance = endTouchesDistance = calculateTouchesDistance(fingerData[0].start, fingerData[1].start);
  620. }
  621. if (options.swipeStatus || options.pinchStatus) {
  622. ret = triggerHandler(event, phase);
  623. }
  624. } else {
  625. //A touch with more or less than the fingers we are looking for, so cancel
  626. ret = false;
  627. }
  628. //If we have a return value from the users handler, then return and cancel
  629. if (ret === false) {
  630. phase = PHASE_CANCEL;
  631. triggerHandler(event, phase);
  632. return ret;
  633. } else {
  634. if (options.hold) {
  635. holdTimeout = setTimeout($.proxy(function() {
  636. //Trigger the event
  637. $element.trigger('hold', [event.target]);
  638. //Fire the callback
  639. if (options.hold) {
  640. ret = options.hold.call($element, event, event.target);
  641. }
  642. }, this), options.longTapThreshold);
  643. }
  644. setTouchInProgress(true);
  645. }
  646. return null;
  647. };
  648. /**
  649. * Event handler for a touch move event.
  650. * If we change fingers during move, then cancel the event
  651. * @inner
  652. * @param {object} jqEvent The normalised jQuery event object.
  653. */
  654. function touchMove(jqEvent) {
  655. //As we use Jquery bind for events, we need to target the original event object
  656. //If these events are being programmatically triggered, we don't have an original event object, so use the Jq one.
  657. var event = jqEvent.originalEvent ? jqEvent.originalEvent : jqEvent;
  658. //If we are ending, cancelling, or within the threshold of 2 fingers being released, don't track anything..
  659. if (phase === PHASE_END || phase === PHASE_CANCEL || inMultiFingerRelease())
  660. return;
  661. var ret,
  662. touches = event.touches,
  663. evt = touches ? touches[0] : event;
  664. //Update the finger data
  665. var currentFinger = updateFingerData(evt);
  666. endTime = getTimeStamp();
  667. if (touches) {
  668. fingerCount = touches.length;
  669. }
  670. if (options.hold) {
  671. clearTimeout(holdTimeout);
  672. }
  673. phase = PHASE_MOVE;
  674. //If we have 2 fingers get Touches distance as well
  675. if (fingerCount == 2) {
  676. //Keep track of the initial pinch distance, so we can calculate the diff later
  677. //We do this here as well as the start event, in case they start with 1 finger, and the press 2 fingers
  678. if (startTouchesDistance == 0) {
  679. //Create second finger if this is the first time...
  680. createFingerData(1, touches[1]);
  681. startTouchesDistance = endTouchesDistance = calculateTouchesDistance(fingerData[0].start, fingerData[1].start);
  682. } else {
  683. //Else just update the second finger
  684. updateFingerData(touches[1]);
  685. endTouchesDistance = calculateTouchesDistance(fingerData[0].end, fingerData[1].end);
  686. pinchDirection = calculatePinchDirection(fingerData[0].end, fingerData[1].end);
  687. }
  688. pinchZoom = calculatePinchZoom(startTouchesDistance, endTouchesDistance);
  689. pinchDistance = Math.abs(startTouchesDistance - endTouchesDistance);
  690. }
  691. if ((fingerCount === options.fingers || options.fingers === ALL_FINGERS) || !touches || hasPinches()) {
  692. //The overall direction of the swipe. From start to now.
  693. direction = calculateDirection(currentFinger.start, currentFinger.end);
  694. //The immediate direction of the swipe, direction between the last movement and this one.
  695. currentDirection = calculateDirection(currentFinger.last, currentFinger.end);
  696. //Check if we need to prevent default event (page scroll / pinch zoom) or not
  697. validateDefaultEvent(jqEvent, currentDirection);
  698. //Distance and duration are all off the main finger
  699. distance = calculateDistance(currentFinger.start, currentFinger.end);
  700. duration = calculateDuration();
  701. //Cache the maximum distance we made in this direction
  702. setMaxDistance(direction, distance);
  703. //Trigger status handler
  704. ret = triggerHandler(event, phase);
  705. //If we trigger end events when threshold are met, or trigger events when touch leaves element
  706. if (!options.triggerOnTouchEnd || options.triggerOnTouchLeave) {
  707. var inBounds = true;
  708. //If checking if we leave the element, run the bounds check (we can use touchleave as its not supported on webkit)
  709. if (options.triggerOnTouchLeave) {
  710. var bounds = getbounds(this);
  711. inBounds = isInBounds(currentFinger.end, bounds);
  712. }
  713. //Trigger end handles as we swipe if thresholds met or if we have left the element if the user has asked to check these..
  714. if (!options.triggerOnTouchEnd &amp;&amp; inBounds) {
  715. phase = getNextPhase(PHASE_MOVE);
  716. }
  717. //We end if out of bounds here, so set current phase to END, and check if its modified
  718. else if (options.triggerOnTouchLeave &amp;&amp; !inBounds) {
  719. phase = getNextPhase(PHASE_END);
  720. }
  721. if (phase == PHASE_CANCEL || phase == PHASE_END) {
  722. triggerHandler(event, phase);
  723. }
  724. }
  725. } else {
  726. phase = PHASE_CANCEL;
  727. triggerHandler(event, phase);
  728. }
  729. if (ret === false) {
  730. phase = PHASE_CANCEL;
  731. triggerHandler(event, phase);
  732. }
  733. }
  734. /**
  735. * Event handler for a touch end event.
  736. * Calculate the direction and trigger events
  737. * @inner
  738. * @param {object} jqEvent The normalised jQuery event object.
  739. */
  740. function touchEnd(jqEvent) {
  741. //As we use Jquery bind for events, we need to target the original event object
  742. //If these events are being programmatically triggered, we don't have an original event object, so use the Jq one.
  743. var event = jqEvent.originalEvent ? jqEvent.originalEvent : jqEvent,
  744. touches = event.touches;
  745. //If we are still in a touch with the device wait a fraction and see if the other finger comes up
  746. //if it does within the threshold, then we treat it as a multi release, not a single release and end the touch / swipe
  747. if (touches) {
  748. if (touches.length &amp;&amp; !inMultiFingerRelease()) {
  749. startMultiFingerRelease(event);
  750. return true;
  751. } else if (touches.length &amp;&amp; inMultiFingerRelease()) {
  752. return true;
  753. }
  754. }
  755. //If a previous finger has been released, check how long ago, if within the threshold, then assume it was a multifinger release.
  756. //This is used to allow 2 fingers to release fractionally after each other, whilst maintaining the event as containing 2 fingers, not 1
  757. if (inMultiFingerRelease()) {
  758. fingerCount = fingerCountAtRelease;
  759. }
  760. //Set end of swipe
  761. endTime = getTimeStamp();
  762. //Get duration incase move was never fired
  763. duration = calculateDuration();
  764. //If we trigger handlers at end of swipe OR, we trigger during, but they didnt trigger and we are still in the move phase
  765. if (didSwipeBackToCancel() || !validateSwipeDistance()) {
  766. phase = PHASE_CANCEL;
  767. triggerHandler(event, phase);
  768. } else if (options.triggerOnTouchEnd || (options.triggerOnTouchEnd === false &amp;&amp; phase === PHASE_MOVE)) {
  769. //call this on jq event so we are cross browser
  770. if (options.preventDefaultEvents !== false &amp;&amp; jqEvent.cancelable !== false) {
  771. jqEvent.preventDefault();
  772. }
  773. phase = PHASE_END;
  774. triggerHandler(event, phase);
  775. }
  776. //Special cases - A tap should always fire on touch end regardless,
  777. //So here we manually trigger the tap end handler by itself
  778. //We dont run trigger handler as it will re-trigger events that may have fired already
  779. else if (!options.triggerOnTouchEnd &amp;&amp; hasTap()) {
  780. //Trigger the pinch events...
  781. phase = PHASE_END;
  782. triggerHandlerForGesture(event, phase, TAP);
  783. } else if (phase === PHASE_MOVE) {
  784. phase = PHASE_CANCEL;
  785. triggerHandler(event, phase);
  786. }
  787. setTouchInProgress(false);
  788. return null;
  789. }
  790. /**
  791. * Event handler for a touch cancel event.
  792. * Clears current vars
  793. * @inner
  794. */
  795. function touchCancel() {
  796. // reset the variables back to default values
  797. fingerCount = 0;
  798. endTime = 0;
  799. startTime = 0;
  800. startTouchesDistance = 0;
  801. endTouchesDistance = 0;
  802. pinchZoom = 1;
  803. //If we were in progress of tracking a possible multi touch end, then re set it.
  804. cancelMultiFingerRelease();
  805. setTouchInProgress(false);
  806. }
  807. /**
  808. * Event handler for a touch leave event.
  809. * This is only triggered on desktops, in touch we work this out manually
  810. * as the touchleave event is not supported in webkit
  811. * @inner
  812. */
  813. function touchLeave(jqEvent) {
  814. //If these events are being programmatically triggered, we don't have an original event object, so use the Jq one.
  815. var event = jqEvent.originalEvent ? jqEvent.originalEvent : jqEvent;
  816. //If we have the trigger on leave property set....
  817. if (options.triggerOnTouchLeave) {
  818. phase = getNextPhase(PHASE_END);
  819. triggerHandler(event, phase);
  820. }
  821. }
  822. /**
  823. * Removes all listeners that were associated with the plugin
  824. * @inner
  825. */
  826. function removeListeners() {
  827. $element.off(START_EV, touchStart);
  828. $element.off(CANCEL_EV, touchCancel);
  829. $element.off(MOVE_EV, touchMove);
  830. $element.off(END_EV, touchEnd);
  831. //we only have leave events on desktop, we manually calculate leave on touch as its not supported in webkit
  832. if (LEAVE_EV) {
  833. $element.off(LEAVE_EV, touchLeave);
  834. }
  835. setTouchInProgress(false);
  836. }
  837. /**
  838. * Checks if the time and distance thresholds have been met, and if so then the appropriate handlers are fired.
  839. */
  840. function getNextPhase(currentPhase) {
  841. var nextPhase = currentPhase;
  842. // Ensure we have valid swipe (under time and over distance and check if we are out of bound...)
  843. var validTime = validateSwipeTime();
  844. var validDistance = validateSwipeDistance();
  845. var didCancel = didSwipeBackToCancel();
  846. //If we have exceeded our time, then cancel
  847. if (!validTime || didCancel) {
  848. nextPhase = PHASE_CANCEL;
  849. }
  850. //Else if we are moving, and have reached distance then end
  851. else if (validDistance &amp;&amp; currentPhase == PHASE_MOVE &amp;&amp; (!options.triggerOnTouchEnd || options.triggerOnTouchLeave)) {
  852. nextPhase = PHASE_END;
  853. }
  854. //Else if we have ended by leaving and didn't reach distance, then cancel
  855. else if (!validDistance &amp;&amp; currentPhase == PHASE_END &amp;&amp; options.triggerOnTouchLeave) {
  856. nextPhase = PHASE_CANCEL;
  857. }
  858. return nextPhase;
  859. }
  860. /**
  861. * Trigger the relevant event handler
  862. * 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"
  863. * @param {object} event the original event object
  864. * @param {string} phase the phase of the swipe (start, end cancel etc) {@link $.fn.swipe.phases}
  865. * @inner
  866. */
  867. function triggerHandler(event, phase) {
  868. var ret,
  869. touches = event.touches;
  870. // SWIPE GESTURES
  871. if (didSwipe() || hasSwipes()) {
  872. ret = triggerHandlerForGesture(event, phase, SWIPE);
  873. }
  874. // PINCH GESTURES (if the above didn't cancel)
  875. if ((didPinch() || hasPinches()) &amp;&amp; ret !== false) {
  876. ret = triggerHandlerForGesture(event, phase, PINCH);
  877. }
  878. // CLICK / TAP (if the above didn't cancel)
  879. if (didDoubleTap() &amp;&amp; ret !== false) {
  880. //Trigger the tap events...
  881. ret = triggerHandlerForGesture(event, phase, DOUBLE_TAP);
  882. }
  883. // CLICK / TAP (if the above didn't cancel)
  884. else if (didLongTap() &amp;&amp; ret !== false) {
  885. //Trigger the tap events...
  886. ret = triggerHandlerForGesture(event, phase, LONG_TAP);
  887. }
  888. // CLICK / TAP (if the above didn't cancel)
  889. else if (didTap() &amp;&amp; ret !== false) {
  890. //Trigger the tap event..
  891. ret = triggerHandlerForGesture(event, phase, TAP);
  892. }
  893. // If we are cancelling the gesture, then manually trigger the reset handler
  894. if (phase === PHASE_CANCEL) {
  895. touchCancel(event);
  896. }
  897. // If we are ending the gesture, then manually trigger the reset handler IF all fingers are off
  898. if (phase === PHASE_END) {
  899. //If we support touch, then check that all fingers are off before we cancel
  900. if (touches) {
  901. if (!touches.length) {
  902. touchCancel(event);
  903. }
  904. } else {
  905. touchCancel(event);
  906. }
  907. }
  908. return ret;
  909. }
  910. /**
  911. * Trigger the relevant event handler
  912. * 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"
  913. * @param {object} event the original event object
  914. * @param {string} phase the phase of the swipe (start, end cancel etc) {@link $.fn.swipe.phases}
  915. * @param {string} gesture the gesture to trigger a handler for : PINCH or SWIPE {@link $.fn.swipe.gestures}
  916. * @return Boolean False, to indicate that the event should stop propagation, or void.
  917. * @inner
  918. */
  919. function triggerHandlerForGesture(event, phase, gesture) {
  920. var ret;
  921. //SWIPES....
  922. if (gesture == SWIPE) {
  923. //Trigger status every time..
  924. $element.trigger('swipeStatus', [phase, direction || null, distance || 0, duration || 0, fingerCount, fingerData, currentDirection]);
  925. if (options.swipeStatus) {
  926. ret = options.swipeStatus.call($element, event, phase, direction || null, distance || 0, duration || 0, fingerCount, fingerData, currentDirection);
  927. //If the status cancels, then dont run the subsequent event handlers..
  928. if (ret === false) return false;
  929. }
  930. if (phase == PHASE_END &amp;&amp; validateSwipe()) {
  931. //Cancel any taps that were in progress...
  932. clearTimeout(singleTapTimeout);
  933. clearTimeout(holdTimeout);
  934. $element.trigger('swipe', [direction, distance, duration, fingerCount, fingerData, currentDirection]);
  935. if (options.swipe) {
  936. ret = options.swipe.call($element, event, direction, distance, duration, fingerCount, fingerData, currentDirection);
  937. //If the status cancels, then dont run the subsequent event handlers..
  938. if (ret === false) return false;
  939. }
  940. //trigger direction specific event handlers
  941. switch (direction) {
  942. case LEFT:
  943. $element.trigger('swipeLeft', [direction, distance, duration, fingerCount, fingerData, currentDirection]);
  944. if (options.swipeLeft) {
  945. ret = options.swipeLeft.call($element, event, direction, distance, duration, fingerCount, fingerData, currentDirection);
  946. }
  947. break;
  948. case RIGHT:
  949. $element.trigger('swipeRight', [direction, distance, duration, fingerCount, fingerData, currentDirection]);
  950. if (options.swipeRight) {
  951. ret = options.swipeRight.call($element, event, direction, distance, duration, fingerCount, fingerData, currentDirection);
  952. }
  953. break;
  954. case UP:
  955. $element.trigger('swipeUp', [direction, distance, duration, fingerCount, fingerData, currentDirection]);
  956. if (options.swipeUp) {
  957. ret = options.swipeUp.call($element, event, direction, distance, duration, fingerCount, fingerData, currentDirection);
  958. }
  959. break;
  960. case DOWN:
  961. $element.trigger('swipeDown', [direction, distance, duration, fingerCount, fingerData, currentDirection]);
  962. if (options.swipeDown) {
  963. ret = options.swipeDown.call($element, event, direction, distance, duration, fingerCount, fingerData, currentDirection);
  964. }
  965. break;
  966. }
  967. }
  968. }
  969. //PINCHES....
  970. if (gesture == PINCH) {
  971. $element.trigger('pinchStatus', [phase, pinchDirection || null, pinchDistance || 0, duration || 0, fingerCount, pinchZoom, fingerData]);
  972. if (options.pinchStatus) {
  973. ret = options.pinchStatus.call($element, event, phase, pinchDirection || null, pinchDistance || 0, duration || 0, fingerCount, pinchZoom, fingerData);
  974. //If the status cancels, then dont run the subsequent event handlers..
  975. if (ret === false) return false;
  976. }
  977. if (phase == PHASE_END &amp;&amp; validatePinch()) {
  978. switch (pinchDirection) {
  979. case IN:
  980. $element.trigger('pinchIn', [pinchDirection || null, pinchDistance || 0, duration || 0, fingerCount, pinchZoom, fingerData]);
  981. if (options.pinchIn) {
  982. ret = options.pinchIn.call($element, event, pinchDirection || null, pinchDistance || 0, duration || 0, fingerCount, pinchZoom, fingerData);
  983. }
  984. break;
  985. case OUT:
  986. $element.trigger('pinchOut', [pinchDirection || null, pinchDistance || 0, duration || 0, fingerCount, pinchZoom, fingerData]);
  987. if (options.pinchOut) {
  988. ret = options.pinchOut.call($element, event, pinchDirection || null, pinchDistance || 0, duration || 0, fingerCount, pinchZoom, fingerData);
  989. }
  990. break;
  991. }
  992. }
  993. }
  994. if (gesture == TAP) {
  995. if (phase === PHASE_CANCEL || phase === PHASE_END) {
  996. clearTimeout(singleTapTimeout);
  997. clearTimeout(holdTimeout);
  998. //If we are also looking for doubelTaps, wait incase this is one...
  999. if (hasDoubleTap() &amp;&amp; !inDoubleTap()) {
  1000. doubleTapStartTime = getTimeStamp();
  1001. //Now wait for the double tap timeout, and trigger this single tap
  1002. //if its not cancelled by a double tap
  1003. singleTapTimeout = setTimeout($.proxy(function() {
  1004. doubleTapStartTime = null;
  1005. $element.trigger('tap', [event.target]);
  1006. if (options.tap) {
  1007. ret = options.tap.call($element, event, event.target);
  1008. }
  1009. }, this), options.doubleTapThreshold);
  1010. } else {
  1011. doubleTapStartTime = null;
  1012. $element.trigger('tap', [event.target]);
  1013. if (options.tap) {
  1014. ret = options.tap.call($element, event, event.target);
  1015. }
  1016. }
  1017. }
  1018. } else if (gesture == DOUBLE_TAP) {
  1019. if (phase === PHASE_CANCEL || phase === PHASE_END) {
  1020. clearTimeout(singleTapTimeout);
  1021. clearTimeout(holdTimeout);
  1022. doubleTapStartTime = null;
  1023. $element.trigger('doubletap', [event.target]);
  1024. if (options.doubleTap) {
  1025. ret = options.doubleTap.call($element, event, event.target);
  1026. }
  1027. }
  1028. } else if (gesture == LONG_TAP) {
  1029. if (phase === PHASE_CANCEL || phase === PHASE_END) {
  1030. clearTimeout(singleTapTimeout);
  1031. doubleTapStartTime = null;
  1032. $element.trigger('longtap', [event.target]);
  1033. if (options.longTap) {
  1034. ret = options.longTap.call($element, event, event.target);
  1035. }
  1036. }
  1037. }
  1038. return ret;
  1039. }
  1040. //
  1041. // GESTURE VALIDATION
  1042. //
  1043. /**
  1044. * Checks the user has swipe far enough
  1045. * @return Boolean if &lt;code>threshold&lt;/code> has been set, return true if the threshold was met, else false.
  1046. * If no threshold was set, then we return true.
  1047. * @inner
  1048. */
  1049. function validateSwipeDistance() {
  1050. var valid = true;
  1051. //If we made it past the min swipe distance..
  1052. if (options.threshold !== null) {
  1053. valid = distance >= options.threshold;
  1054. }
  1055. return valid;
  1056. }
  1057. /**
  1058. * Checks the user has swiped back to cancel.
  1059. * @return Boolean if &lt;code>cancelThreshold&lt;/code> has been set, return true if the cancelThreshold was met, else false.
  1060. * If no cancelThreshold was set, then we return true.
  1061. * @inner
  1062. */
  1063. function didSwipeBackToCancel() {
  1064. var cancelled = false;
  1065. if (options.cancelThreshold !== null &amp;&amp; direction !== null) {
  1066. cancelled = (getMaxDistance(direction) - distance) >= options.cancelThreshold;
  1067. }
  1068. return cancelled;
  1069. }
  1070. /**
  1071. * Checks the user has pinched far enough
  1072. * @return Boolean if &lt;code>pinchThreshold&lt;/code> has been set, return true if the threshold was met, else false.
  1073. * If no threshold was set, then we return true.
  1074. * @inner
  1075. */
  1076. function validatePinchDistance() {
  1077. if (options.pinchThreshold !== null) {
  1078. return pinchDistance >= options.pinchThreshold;
  1079. }
  1080. return true;
  1081. }
  1082. /**
  1083. * Checks that the time taken to swipe meets the minimum / maximum requirements
  1084. * @return Boolean
  1085. * @inner
  1086. */
  1087. function validateSwipeTime() {
  1088. var result;
  1089. //If no time set, then return true
  1090. if (options.maxTimeThreshold) {
  1091. if (duration >= options.maxTimeThreshold) {
  1092. result = false;
  1093. } else {
  1094. result = true;
  1095. }
  1096. } else {
  1097. result = true;
  1098. }
  1099. return result;
  1100. }
  1101. /**
  1102. * Checks direction of the swipe and the value allowPageScroll to see if we should allow or prevent the default behaviour from occurring.
  1103. * This will essentially allow page scrolling or not when the user is swiping on a touchSwipe object.
  1104. * @param {object} jqEvent The normalised jQuery representation of the event object.
  1105. * @param {string} direction The direction of the event. See {@link $.fn.swipe.directions}
  1106. * @see $.fn.swipe.directions
  1107. * @inner
  1108. */
  1109. function validateDefaultEvent(jqEvent, direction) {
  1110. //If the option is set, allways allow the event to bubble up (let user handle weirdness)
  1111. if (options.preventDefaultEvents === false) {
  1112. return;
  1113. }
  1114. if (options.allowPageScroll === NONE) {
  1115. jqEvent.preventDefault();
  1116. } else {
  1117. var auto = options.allowPageScroll === AUTO;
  1118. switch (direction) {
  1119. case LEFT:
  1120. if ((options.swipeLeft &amp;&amp; auto) || (!auto &amp;&amp; options.allowPageScroll != HORIZONTAL)) {
  1121. jqEvent.preventDefault();
  1122. }
  1123. break;
  1124. case RIGHT:
  1125. if ((options.swipeRight &amp;&amp; auto) || (!auto &amp;&amp; options.allowPageScroll != HORIZONTAL)) {
  1126. jqEvent.preventDefault();
  1127. }
  1128. break;
  1129. case UP:
  1130. if ((options.swipeUp &amp;&amp; auto) || (!auto &amp;&amp; options.allowPageScroll != VERTICAL)) {
  1131. jqEvent.preventDefault();
  1132. }
  1133. break;
  1134. case DOWN:
  1135. if ((options.swipeDown &amp;&amp; auto) || (!auto &amp;&amp; options.allowPageScroll != VERTICAL)) {
  1136. jqEvent.preventDefault();
  1137. }
  1138. break;
  1139. case NONE:
  1140. break;
  1141. }
  1142. }
  1143. }
  1144. // PINCHES
  1145. /**
  1146. * Returns true of the current pinch meets the thresholds
  1147. * @return Boolean
  1148. * @inner
  1149. */
  1150. function validatePinch() {
  1151. var hasCorrectFingerCount = validateFingers();
  1152. var hasEndPoint = validateEndPoint();
  1153. var hasCorrectDistance = validatePinchDistance();
  1154. return hasCorrectFingerCount &amp;&amp; hasEndPoint &amp;&amp; hasCorrectDistance;
  1155. }
  1156. /**
  1157. * Returns true if any Pinch events have been registered
  1158. * @return Boolean
  1159. * @inner
  1160. */
  1161. function hasPinches() {
  1162. //Enure we dont return 0 or null for false values
  1163. return !!(options.pinchStatus || options.pinchIn || options.pinchOut);
  1164. }
  1165. /**
  1166. * Returns true if we are detecting pinches, and have one
  1167. * @return Boolean
  1168. * @inner
  1169. */
  1170. function didPinch() {
  1171. //Enure we dont return 0 or null for false values
  1172. return !!(validatePinch() &amp;&amp; hasPinches());
  1173. }
  1174. // SWIPES
  1175. /**
  1176. * Returns true if the current swipe meets the thresholds
  1177. * @return Boolean
  1178. * @inner
  1179. */
  1180. function validateSwipe() {
  1181. //Check validity of swipe
  1182. var hasValidTime = validateSwipeTime();
  1183. var hasValidDistance = validateSwipeDistance();
  1184. var hasCorrectFingerCount = validateFingers();
  1185. var hasEndPoint = validateEndPoint();
  1186. var didCancel = didSwipeBackToCancel();
  1187. // if the user swiped more than the minimum length, perform the appropriate action
  1188. // hasValidDistance is null when no distance is set
  1189. var valid = !didCancel &amp;&amp; hasEndPoint &amp;&amp; hasCorrectFingerCount &amp;&amp; hasValidDistance &amp;&amp; hasValidTime;
  1190. return valid;
  1191. }
  1192. /**
  1193. * Returns true if any Swipe events have been registered
  1194. * @return Boolean
  1195. * @inner
  1196. */
  1197. function hasSwipes() {
  1198. //Enure we dont return 0 or null for false values
  1199. return !!(options.swipe || options.swipeStatus || options.swipeLeft || options.swipeRight || options.swipeUp || options.swipeDown);
  1200. }
  1201. /**
  1202. * Returns true if we are detecting swipes and have one
  1203. * @return Boolean
  1204. * @inner
  1205. */
  1206. function didSwipe() {
  1207. //Enure we dont return 0 or null for false values
  1208. return !!(validateSwipe() &amp;&amp; hasSwipes());
  1209. }
  1210. /**
  1211. * Returns true if we have matched the number of fingers we are looking for
  1212. * @return Boolean
  1213. * @inner
  1214. */
  1215. function validateFingers() {
  1216. //The number of fingers we want were matched, or on desktop we ignore
  1217. return ((fingerCount === options.fingers || options.fingers === ALL_FINGERS) || !SUPPORTS_TOUCH);
  1218. }
  1219. /**
  1220. * Returns true if we have an end point for the swipe
  1221. * @return Boolean
  1222. * @inner
  1223. */
  1224. function validateEndPoint() {
  1225. //We have an end value for the finger
  1226. return fingerData[0].end.x !== 0;
  1227. }
  1228. // TAP / CLICK
  1229. /**
  1230. * Returns true if a click / tap events have been registered
  1231. * @return Boolean
  1232. * @inner
  1233. */
  1234. function hasTap() {
  1235. //Enure we dont return 0 or null for false values
  1236. return !!(options.tap);
  1237. }
  1238. /**
  1239. * Returns true if a double tap events have been registered
  1240. * @return Boolean
  1241. * @inner
  1242. */
  1243. function hasDoubleTap() {
  1244. //Enure we dont return 0 or null for false values
  1245. return !!(options.doubleTap);
  1246. }
  1247. /**
  1248. * Returns true if any long tap events have been registered
  1249. * @return Boolean
  1250. * @inner
  1251. */
  1252. function hasLongTap() {
  1253. //Enure we dont return 0 or null for false values
  1254. return !!(options.longTap);
  1255. }
  1256. /**
  1257. * Returns true if we could be in the process of a double tap (one tap has occurred, we are listening for double taps, and the threshold hasn't past.
  1258. * @return Boolean
  1259. * @inner
  1260. */
  1261. function validateDoubleTap() {
  1262. if (doubleTapStartTime == null) {
  1263. return false;
  1264. }
  1265. var now = getTimeStamp();
  1266. return (hasDoubleTap() &amp;&amp; ((now - doubleTapStartTime) &lt;= options.doubleTapThreshold));
  1267. }
  1268. /**
  1269. * Returns true if we could be in the process of a double tap (one tap has occurred, we are listening for double taps, and the threshold hasn't past.
  1270. * @return Boolean
  1271. * @inner
  1272. */
  1273. function inDoubleTap() {
  1274. return validateDoubleTap();
  1275. }
  1276. /**
  1277. * Returns true if we have a valid tap
  1278. * @return Boolean
  1279. * @inner
  1280. */
  1281. function validateTap() {
  1282. return ((fingerCount === 1 || !SUPPORTS_TOUCH) &amp;&amp; (isNaN(distance) || distance &lt; options.threshold));
  1283. }
  1284. /**
  1285. * Returns true if we have a valid long tap
  1286. * @return Boolean
  1287. * @inner
  1288. */
  1289. function validateLongTap() {
  1290. //slight threshold on moving finger
  1291. return ((duration > options.longTapThreshold) &amp;&amp; (distance &lt; DOUBLE_TAP_THRESHOLD));
  1292. }
  1293. /**
  1294. * Returns true if we are detecting taps and have one
  1295. * @return Boolean
  1296. * @inner
  1297. */
  1298. function didTap() {
  1299. //Enure we dont return 0 or null for false values
  1300. return !!(validateTap() &amp;&amp; hasTap());
  1301. }
  1302. /**
  1303. * Returns true if we are detecting double taps and have one
  1304. * @return Boolean
  1305. * @inner
  1306. */
  1307. function didDoubleTap() {
  1308. //Enure we dont return 0 or null for false values
  1309. return !!(validateDoubleTap() &amp;&amp; hasDoubleTap());
  1310. }
  1311. /**
  1312. * Returns true if we are detecting long taps and have one
  1313. * @return Boolean
  1314. * @inner
  1315. */
  1316. function didLongTap() {
  1317. //Enure we dont return 0 or null for false values
  1318. return !!(validateLongTap() &amp;&amp; hasLongTap());
  1319. }
  1320. // MULTI FINGER TOUCH
  1321. /**
  1322. * Starts tracking the time between 2 finger releases, and keeps track of how many fingers we initially had up
  1323. * @inner
  1324. */
  1325. function startMultiFingerRelease(event) {
  1326. previousTouchEndTime = getTimeStamp();
  1327. fingerCountAtRelease = event.touches.length + 1;
  1328. }
  1329. /**
  1330. * Cancels the tracking of time between 2 finger releases, and resets counters
  1331. * @inner
  1332. */
  1333. function cancelMultiFingerRelease() {
  1334. previousTouchEndTime = 0;
  1335. fingerCountAtRelease = 0;
  1336. }
  1337. /**
  1338. * Checks if we are in the threshold between 2 fingers being released
  1339. * @return Boolean
  1340. * @inner
  1341. */
  1342. function inMultiFingerRelease() {
  1343. var withinThreshold = false;
  1344. if (previousTouchEndTime) {
  1345. var diff = getTimeStamp() - previousTouchEndTime
  1346. if (diff &lt;= options.fingerReleaseThreshold) {
  1347. withinThreshold = true;
  1348. }
  1349. }
  1350. return withinThreshold;
  1351. }
  1352. /**
  1353. * gets a data flag to indicate that a touch is in progress
  1354. * @return Boolean
  1355. * @inner
  1356. */
  1357. function getTouchInProgress() {
  1358. //strict equality to ensure only true and false are returned
  1359. return !!($element.data(PLUGIN_NS + '_intouch') === true);
  1360. }
  1361. /**
  1362. * Sets a data flag to indicate that a touch is in progress
  1363. * @param {boolean} val The value to set the property to
  1364. * @inner
  1365. */
  1366. function setTouchInProgress(val) {
  1367. //If destroy is called in an event handler, we have no el, and we have already cleaned up, so return.
  1368. if(!$element) { return; }
  1369. //Add or remove event listeners depending on touch status
  1370. if (val === true) {
  1371. $element.on(MOVE_EV, touchMove);
  1372. $element.on(END_EV, touchEnd);
  1373. //we only have leave events on desktop, we manually calcuate leave on touch as its not supported in webkit
  1374. if (LEAVE_EV) {
  1375. $element.on(LEAVE_EV, touchLeave);
  1376. }
  1377. } else {
  1378. $element.off(MOVE_EV, touchMove, false);
  1379. $element.off(END_EV, touchEnd, false);
  1380. //we only have leave events on desktop, we manually calcuate leave on touch as its not supported in webkit
  1381. if (LEAVE_EV) {
  1382. $element.off(LEAVE_EV, touchLeave, false);
  1383. }
  1384. }
  1385. //strict equality to ensure only true and false can update the value
  1386. $element.data(PLUGIN_NS + '_intouch', val === true);
  1387. }
  1388. /**
  1389. * Creates the finger data for the touch/finger in the event object.
  1390. * @param {int} id The id to store the finger data under (usually the order the fingers were pressed)
  1391. * @param {object} evt The event object containing finger data
  1392. * @return finger data object
  1393. * @inner
  1394. */
  1395. function createFingerData(id, evt) {
  1396. var f = {
  1397. start: {
  1398. x: 0,
  1399. y: 0
  1400. },
  1401. last: {
  1402. x: 0,
  1403. y: 0
  1404. },
  1405. end: {
  1406. x: 0,
  1407. y: 0
  1408. }
  1409. };
  1410. f.start.x = f.last.x = f.end.x = evt.pageX || evt.clientX;
  1411. f.start.y = f.last.y = f.end.y = evt.pageY || evt.clientY;
  1412. fingerData[id] = f;
  1413. return f;
  1414. }
  1415. /**
  1416. * Updates the finger data for a particular event object
  1417. * @param {object} evt The event object containing the touch/finger data to upadte
  1418. * @return a finger data object.
  1419. * @inner
  1420. */
  1421. function updateFingerData(evt) {
  1422. var id = evt.identifier !== undefined ? evt.identifier : 0;
  1423. var f = getFingerData(id);
  1424. if (f === null) {
  1425. f = createFingerData(id, evt);
  1426. }
  1427. f.last.x = f.end.x;
  1428. f.last.y = f.end.y;
  1429. f.end.x = evt.pageX || evt.clientX;
  1430. f.end.y = evt.pageY || evt.clientY;
  1431. return f;
  1432. }
  1433. /**
  1434. * Returns a finger data object by its event ID.
  1435. * Each touch event has an identifier property, which is used
  1436. * to track repeat touches
  1437. * @param {int} id The unique id of the finger in the sequence of touch events.
  1438. * @return a finger data object.
  1439. * @inner
  1440. */
  1441. function getFingerData(id) {
  1442. return fingerData[id] || null;
  1443. }
  1444. /**
  1445. * Sets the maximum distance swiped in the given direction.
  1446. * If the new value is lower than the current value, the max value is not changed.
  1447. * @param {string} direction The direction of the swipe
  1448. * @param {int} distance The distance of the swipe
  1449. * @inner
  1450. */
  1451. function setMaxDistance(direction, distance) {
  1452. if(direction==NONE) return;
  1453. distance = Math.max(distance, getMaxDistance(direction));
  1454. maximumsMap[direction].distance = distance;
  1455. }
  1456. /**
  1457. * gets the maximum distance swiped in the given direction.
  1458. * @param {string} direction The direction of the swipe
  1459. * @return int The distance of the swipe
  1460. * @inner
  1461. */
  1462. function getMaxDistance(direction) {
  1463. if (maximumsMap[direction]) return maximumsMap[direction].distance;
  1464. return undefined;
  1465. }
  1466. /**
  1467. * Creats a map of directions to maximum swiped values.
  1468. * @return Object A dictionary of maximum values, indexed by direction.
  1469. * @inner
  1470. */
  1471. function createMaximumsData() {
  1472. var maxData = {};
  1473. maxData[LEFT] = createMaximumVO(LEFT);
  1474. maxData[RIGHT] = createMaximumVO(RIGHT);
  1475. maxData[UP] = createMaximumVO(UP);
  1476. maxData[DOWN] = createMaximumVO(DOWN);
  1477. return maxData;
  1478. }
  1479. /**
  1480. * Creates a map maximum swiped values for a given swipe direction
  1481. * @param {string} The direction that these values will be associated with
  1482. * @return Object Maximum values
  1483. * @inner
  1484. */
  1485. function createMaximumVO(dir) {
  1486. return {
  1487. direction: dir,
  1488. distance: 0
  1489. }
  1490. }
  1491. //
  1492. // MATHS / UTILS
  1493. //
  1494. /**
  1495. * Calculate the duration of the swipe
  1496. * @return int
  1497. * @inner
  1498. */
  1499. function calculateDuration() {
  1500. return endTime - startTime;
  1501. }
  1502. /**
  1503. * Calculate the distance between 2 touches (pinch)
  1504. * @param {point} startPoint A point object containing x and y co-ordinates
  1505. * @param {point} endPoint A point object containing x and y co-ordinates
  1506. * @return int;
  1507. * @inner
  1508. */
  1509. function calculateTouchesDistance(startPoint, endPoint) {
  1510. var diffX = Math.abs(startPoint.x - endPoint.x);
  1511. var diffY = Math.abs(startPoint.y - endPoint.y);
  1512. return Math.round(Math.sqrt(diffX * diffX + diffY * diffY));
  1513. }
  1514. /**
  1515. * Calculate the zoom factor between the start and end distances
  1516. * @param {int} startDistance Distance (between 2 fingers) the user started pinching at
  1517. * @param {int} endDistance Distance (between 2 fingers) the user ended pinching at
  1518. * @return float The zoom value from 0 to 1.
  1519. * @inner
  1520. */
  1521. function calculatePinchZoom(startDistance, endDistance) {
  1522. var percent = (endDistance / startDistance) * 1;
  1523. return percent.toFixed(2);
  1524. }
  1525. /**
  1526. * Returns the pinch direction, either IN or OUT for the given points
  1527. * @return string Either {@link $.fn.swipe.directions.IN} or {@link $.fn.swipe.directions.OUT}
  1528. * @see $.fn.swipe.directions
  1529. * @inner
  1530. */
  1531. function calculatePinchDirection() {
  1532. if (pinchZoom &lt; 1) {
  1533. return OUT;
  1534. } else {
  1535. return IN;
  1536. }
  1537. }
  1538. /**
  1539. * Calculate the length / distance of the swipe
  1540. * @param {point} startPoint A point object containing x and y co-ordinates
  1541. * @param {point} endPoint A point object containing x and y co-ordinates
  1542. * @return int
  1543. * @inner
  1544. */
  1545. function calculateDistance(startPoint, endPoint) {
  1546. return Math.round(Math.sqrt(Math.pow(endPoint.x - startPoint.x, 2) + Math.pow(endPoint.y - startPoint.y, 2)));
  1547. }
  1548. /**
  1549. * Calculate the angle of the swipe
  1550. * @param {point} startPoint A point object containing x and y co-ordinates
  1551. * @param {point} endPoint A point object containing x and y co-ordinates
  1552. * @return int
  1553. * @inner
  1554. */
  1555. function calculateAngle(startPoint, endPoint) {
  1556. var x = startPoint.x - endPoint.x;
  1557. var y = endPoint.y - startPoint.y;
  1558. var r = Math.atan2(y, x); //radians
  1559. var angle = Math.round(r * 180 / Math.PI); //degrees
  1560. //ensure value is positive
  1561. if (angle &lt; 0) {
  1562. angle = 360 - Math.abs(angle);
  1563. }
  1564. return angle;
  1565. }
  1566. /**
  1567. * Calculate the direction of the swipe
  1568. * This will also call calculateAngle to get the latest angle of swipe
  1569. * @param {point} startPoint A point object containing x and y co-ordinates
  1570. * @param {point} endPoint A point object containing x and y co-ordinates
  1571. * @return string Either {@link $.fn.swipe.directions.LEFT} / {@link $.fn.swipe.directions.RIGHT} / {@link $.fn.swipe.directions.DOWN} / {@link $.fn.swipe.directions.UP}
  1572. * @see $.fn.swipe.directions
  1573. * @inner
  1574. */
  1575. function calculateDirection(startPoint, endPoint) {
  1576. if( comparePoints(startPoint, endPoint) ) {
  1577. return NONE;
  1578. }
  1579. var angle = calculateAngle(startPoint, endPoint);
  1580. if ((angle &lt;= 45) &amp;&amp; (angle >= 0)) {
  1581. return LEFT;
  1582. } else if ((angle &lt;= 360) &amp;&amp; (angle >= 315)) {
  1583. return LEFT;
  1584. } else if ((angle >= 135) &amp;&amp; (angle &lt;= 225)) {
  1585. return RIGHT;
  1586. } else if ((angle > 45) &amp;&amp; (angle &lt; 135)) {
  1587. return DOWN;
  1588. } else {
  1589. return UP;
  1590. }
  1591. }
  1592. /**
  1593. * Returns a MS time stamp of the current time
  1594. * @return int
  1595. * @inner
  1596. */
  1597. function getTimeStamp() {
  1598. var now = new Date();
  1599. return now.getTime();
  1600. }
  1601. /**
  1602. * Returns a bounds object with left, right, top and bottom properties for the element specified.
  1603. * @param {DomNode} The DOM node to get the bounds for.
  1604. */
  1605. function getbounds(el) {
  1606. el = $(el);
  1607. var offset = el.offset();
  1608. var bounds = {
  1609. left: offset.left,
  1610. right: offset.left + el.outerWidth(),
  1611. top: offset.top,
  1612. bottom: offset.top + el.outerHeight()
  1613. }
  1614. return bounds;
  1615. }
  1616. /**
  1617. * Checks if the point object is in the bounds object.
  1618. * @param {object} point A point object.
  1619. * @param {int} point.x The x value of the point.
  1620. * @param {int} point.y The x value of the point.
  1621. * @param {object} bounds The bounds object to test
  1622. * @param {int} bounds.left The leftmost value
  1623. * @param {int} bounds.right The righttmost value
  1624. * @param {int} bounds.top The topmost value
  1625. * @param {int} bounds.bottom The bottommost value
  1626. */
  1627. function isInBounds(point, bounds) {
  1628. return (point.x > bounds.left &amp;&amp; point.x &lt; bounds.right &amp;&amp; point.y > bounds.top &amp;&amp; point.y &lt; bounds.bottom);
  1629. };
  1630. /**
  1631. * Checks if the two points are equal
  1632. * @param {object} point A point object.
  1633. * @param {object} point B point object.
  1634. * @return true of the points match
  1635. */
  1636. function comparePoints(pointA, pointB) {
  1637. return (pointA.x == pointB.x &amp;&amp; pointA.y == pointB.y);
  1638. }
  1639. }
  1640. /**
  1641. * A catch all handler that is triggered for all swipe directions.
  1642. * @name $.fn.swipe#swipe
  1643. * @event
  1644. * @default null
  1645. * @param {EventObject} event The original event object
  1646. * @param {int} direction The direction the user swiped in. See {@link $.fn.swipe.directions}
  1647. * @param {int} distance The distance the user swiped
  1648. * @param {int} duration The duration of the swipe in milliseconds
  1649. * @param {int} fingerCount The number of fingers used. See {@link $.fn.swipe.fingers}
  1650. * @param {object} fingerData The coordinates of fingers in event
  1651. * @param {string} currentDirection The current direction the user is swiping.
  1652. */
  1653. /**
  1654. * A handler that is triggered for "left" swipes.
  1655. * @name $.fn.swipe#swipeLeft
  1656. * @event
  1657. * @default null
  1658. * @param {EventObject} event The original event object
  1659. * @param {int} direction The direction the user swiped in. See {@link $.fn.swipe.directions}
  1660. * @param {int} distance The distance the user swiped
  1661. * @param {int} duration The duration of the swipe in milliseconds
  1662. * @param {int} fingerCount The number of fingers used. See {@link $.fn.swipe.fingers}
  1663. * @param {object} fingerData The coordinates of fingers in event
  1664. * @param {string} currentDirection The current direction the user is swiping.
  1665. */
  1666. /**
  1667. * A handler that is triggered for "right" swipes.
  1668. * @name $.fn.swipe#swipeRight
  1669. * @event
  1670. * @default null
  1671. * @param {EventObject} event The original event object
  1672. * @param {int} direction The direction the user swiped in. See {@link $.fn.swipe.directions}
  1673. * @param {int} distance The distance the user swiped
  1674. * @param {int} duration The duration of the swipe in milliseconds
  1675. * @param {int} fingerCount The number of fingers used. See {@link $.fn.swipe.fingers}
  1676. * @param {object} fingerData The coordinates of fingers in event
  1677. * @param {string} currentDirection The current direction the user is swiping.
  1678. */
  1679. /**
  1680. * A handler that is triggered for "up" swipes.
  1681. * @name $.fn.swipe#swipeUp
  1682. * @event
  1683. * @default null
  1684. * @param {EventObject} event The original event object
  1685. * @param {int} direction The direction the user swiped in. See {@link $.fn.swipe.directions}
  1686. * @param {int} distance The distance the user swiped
  1687. * @param {int} duration The duration of the swipe in milliseconds
  1688. * @param {int} fingerCount The number of fingers used. See {@link $.fn.swipe.fingers}
  1689. * @param {object} fingerData The coordinates of fingers in event
  1690. * @param {string} currentDirection The current direction the user is swiping.
  1691. */
  1692. /**
  1693. * A handler that is triggered for "down" swipes.
  1694. * @name $.fn.swipe#swipeDown
  1695. * @event
  1696. * @default null
  1697. * @param {EventObject} event The original event object
  1698. * @param {int} direction The direction the user swiped in. See {@link $.fn.swipe.directions}
  1699. * @param {int} distance The distance the user swiped
  1700. * @param {int} duration The duration of the swipe in milliseconds
  1701. * @param {int} fingerCount The number of fingers used. See {@link $.fn.swipe.fingers}
  1702. * @param {object} fingerData The coordinates of fingers in event
  1703. * @param {string} currentDirection The current direction the user is swiping.
  1704. */
  1705. /**
  1706. * A handler triggered for every phase of the swipe. This handler is constantly fired for the duration of the pinch.
  1707. * This is triggered regardless of swipe thresholds.
  1708. * @name $.fn.swipe#swipeStatus
  1709. * @event
  1710. * @default null
  1711. * @param {EventObject} event The original event object
  1712. * @param {string} phase The phase of the swipe event. See {@link $.fn.swipe.phases}
  1713. * @param {string} direction The direction the user swiped in. This is null if the user has yet to move. See {@link $.fn.swipe.directions}
  1714. * @param {int} distance The distance the user swiped. This is 0 if the user has yet to move.
  1715. * @param {int} duration The duration of the swipe in milliseconds
  1716. * @param {int} fingerCount The number of fingers used. See {@link $.fn.swipe.fingers}
  1717. * @param {object} fingerData The coordinates of fingers in event
  1718. * @param {string} currentDirection The current direction the user is swiping.
  1719. */
  1720. /**
  1721. * A handler triggered for pinch in events.
  1722. * @name $.fn.swipe#pinchIn
  1723. * @event
  1724. * @default null
  1725. * @param {EventObject} event The original event object
  1726. * @param {int} direction The direction the user pinched in. See {@link $.fn.swipe.directions}
  1727. * @param {int} distance The distance the user pinched
  1728. * @param {int} duration The duration of the swipe in milliseconds
  1729. * @param {int} fingerCount The number of fingers used. See {@link $.fn.swipe.fingers}
  1730. * @param {int} zoom The zoom/scale level the user pinched too, 0-1.
  1731. * @param {object} fingerData The coordinates of fingers in event
  1732. */
  1733. /**
  1734. * A handler triggered for pinch out events.
  1735. * @name $.fn.swipe#pinchOut
  1736. * @event
  1737. * @default null
  1738. * @param {EventObject} event The original event object
  1739. * @param {int} direction The direction the user pinched in. See {@link $.fn.swipe.directions}
  1740. * @param {int} distance The distance the user pinched
  1741. * @param {int} duration The duration of the swipe in milliseconds
  1742. * @param {int} fingerCount The number of fingers used. See {@link $.fn.swipe.fingers}
  1743. * @param {int} zoom The zoom/scale level the user pinched too, 0-1.
  1744. * @param {object} fingerData The coordinates of fingers in event
  1745. */
  1746. /**
  1747. * A handler triggered for all pinch events. This handler is constantly fired for the duration of the pinch. This is triggered regardless of thresholds.
  1748. * @name $.fn.swipe#pinchStatus
  1749. * @event
  1750. * @default null
  1751. * @param {EventObject} event The original event object
  1752. * @param {int} direction The direction the user pinched in. See {@link $.fn.swipe.directions}
  1753. * @param {int} distance The distance the user pinched
  1754. * @param {int} duration The duration of the swipe in milliseconds
  1755. * @param {int} fingerCount The number of fingers used. See {@link $.fn.swipe.fingers}
  1756. * @param {int} zoom The zoom/scale level the user pinched too, 0-1.
  1757. * @param {object} fingerData The coordinates of fingers in event
  1758. */
  1759. /**
  1760. * A click handler triggered when a user simply clicks, rather than swipes on an element.
  1761. * This is deprecated since version 1.6.2, any assignment to click will be assigned to the tap handler.
  1762. * You cannot use &lt;code>on&lt;/code> to bind to this event as the default jQ &lt;code>click&lt;/code> event will be triggered.
  1763. * Use the &lt;code>tap&lt;/code> event instead.
  1764. * @name $.fn.swipe#click
  1765. * @event
  1766. * @deprecated since version 1.6.2, please use {@link $.fn.swipe#tap} instead
  1767. * @default null
  1768. * @param {EventObject} event The original event object
  1769. * @param {DomObject} target The element clicked on.
  1770. */
  1771. /**
  1772. * A click / tap handler triggered when a user simply clicks or taps, rather than swipes on an element.
  1773. * @name $.fn.swipe#tap
  1774. * @event
  1775. * @default null
  1776. * @param {EventObject} event The original event object
  1777. * @param {DomObject} target The element clicked on.
  1778. */
  1779. /**
  1780. * A double tap handler triggered when a user double clicks or taps on an element.
  1781. * You can set the time delay for a double tap with the {@link $.fn.swipe.defaults#doubleTapThreshold} property.
  1782. * Note: If you set both &lt;code>doubleTap&lt;/code> and &lt;code>tap&lt;/code> handlers, the &lt;code>tap&lt;/code> event will be delayed by the &lt;code>doubleTapThreshold&lt;/code>
  1783. * as the script needs to check if its a double tap.
  1784. * @name $.fn.swipe#doubleTap
  1785. * @see $.fn.swipe.defaults#doubleTapThreshold
  1786. * @event
  1787. * @default null
  1788. * @param {EventObject} event The original event object
  1789. * @param {DomObject} target The element clicked on.
  1790. */
  1791. /**
  1792. * A long tap handler triggered once a tap has been release if the tap was longer than the longTapThreshold.
  1793. * You can set the time delay for a long tap with the {@link $.fn.swipe.defaults#longTapThreshold} property.
  1794. * @name $.fn.swipe#longTap
  1795. * @see $.fn.swipe.defaults#longTapThreshold
  1796. * @event
  1797. * @default null
  1798. * @param {EventObject} event The original event object
  1799. * @param {DomObject} target The element clicked on.
  1800. */
  1801. /**
  1802. * A hold tap handler triggered as soon as the longTapThreshold is reached
  1803. * You can set the time delay for a long tap with the {@link $.fn.swipe.defaults#longTapThreshold} property.
  1804. * @name $.fn.swipe#hold
  1805. * @see $.fn.swipe.defaults#longTapThreshold
  1806. * @event
  1807. * @default null
  1808. * @param {EventObject} event The original event object
  1809. * @param {DomObject} target The element clicked on.
  1810. */
  1811. }));
  1812. </code></pre>
  1813. </article>
  1814. </section>
  1815. </div>
  1816. <br class="clear">
  1817. <footer>
  1818. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Sep 17 2018 13:09:45 GMT+0100 (British Summer Time) using the docdash theme.
  1819. </footer>
  1820. <script>prettyPrint();</script>
  1821. <script src="scripts/linenumber.js"></script>
  1822. </body>
  1823. </html>