jquery.hammer.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. /*! jQuery plugin for Hammer.JS - v1.0.0 - 2014-01-02
  2. * http://eightmedia.github.com/hammer.js
  3. *
  4. * Copyright (c) 2014 Jorik Tangelder <j.tangelder@gmail.com>;
  5. * Licensed under the MIT license *//*! Hammer.JS - v1.0.6 - 2014-01-02
  6. * http://eightmedia.github.com/hammer.js
  7. *
  8. * Copyright (c) 2014 Jorik Tangelder <j.tangelder@gmail.com>;
  9. * Licensed under the MIT license */
  10. (function(window, undefined) {
  11. 'use strict';
  12. /**
  13. * Hammer
  14. * use this to create instances
  15. * @param {HTMLElement} element
  16. * @param {Object} options
  17. * @returns {Hammer.Instance}
  18. * @constructor
  19. */
  20. var Hammer = function(element, options) {
  21. return new Hammer.Instance(element, options || {});
  22. };
  23. // default settings
  24. Hammer.defaults = {
  25. // add styles and attributes to the element to prevent the browser from doing
  26. // its native behavior. this doesnt prevent the scrolling, but cancels
  27. // the contextmenu, tap highlighting etc
  28. // set to false to disable this
  29. stop_browser_behavior: {
  30. // this also triggers onselectstart=false for IE
  31. userSelect : 'none',
  32. // this makes the element blocking in IE10 >, you could experiment with the value
  33. // see for more options this issue; https://github.com/EightMedia/hammer.js/issues/241
  34. touchAction : 'none',
  35. touchCallout : 'none',
  36. contentZooming : 'none',
  37. userDrag : 'none',
  38. tapHighlightColor: 'rgba(0,0,0,0)'
  39. }
  40. //
  41. // more settings are defined per gesture at gestures.js
  42. //
  43. };
  44. // detect touchevents
  45. Hammer.HAS_POINTEREVENTS = window.navigator.pointerEnabled || window.navigator.msPointerEnabled;
  46. Hammer.HAS_TOUCHEVENTS = ('ontouchstart' in window);
  47. // dont use mouseevents on mobile devices
  48. Hammer.MOBILE_REGEX = /mobile|tablet|ip(ad|hone|od)|android|silk/i;
  49. Hammer.NO_MOUSEEVENTS = Hammer.HAS_TOUCHEVENTS && window.navigator.userAgent.match(Hammer.MOBILE_REGEX);
  50. // eventtypes per touchevent (start, move, end)
  51. // are filled by Hammer.event.determineEventTypes on setup
  52. Hammer.EVENT_TYPES = {};
  53. // direction defines
  54. Hammer.DIRECTION_DOWN = 'down';
  55. Hammer.DIRECTION_LEFT = 'left';
  56. Hammer.DIRECTION_UP = 'up';
  57. Hammer.DIRECTION_RIGHT = 'right';
  58. // pointer type
  59. Hammer.POINTER_MOUSE = 'mouse';
  60. Hammer.POINTER_TOUCH = 'touch';
  61. Hammer.POINTER_PEN = 'pen';
  62. // touch event defines
  63. Hammer.EVENT_START = 'start';
  64. Hammer.EVENT_MOVE = 'move';
  65. Hammer.EVENT_END = 'end';
  66. // hammer document where the base events are added at
  67. Hammer.DOCUMENT = window.document;
  68. // plugins and gestures namespaces
  69. Hammer.plugins = Hammer.plugins || {};
  70. Hammer.gestures = Hammer.gestures || {};
  71. // if the window events are set...
  72. Hammer.READY = false;
  73. /**
  74. * setup events to detect gestures on the document
  75. */
  76. function setup() {
  77. if(Hammer.READY) {
  78. return;
  79. }
  80. // find what eventtypes we add listeners to
  81. Hammer.event.determineEventTypes();
  82. // Register all gestures inside Hammer.gestures
  83. Hammer.utils.each(Hammer.gestures, function(gesture){
  84. Hammer.detection.register(gesture);
  85. });
  86. // Add touch events on the document
  87. Hammer.event.onTouch(Hammer.DOCUMENT, Hammer.EVENT_MOVE, Hammer.detection.detect);
  88. Hammer.event.onTouch(Hammer.DOCUMENT, Hammer.EVENT_END, Hammer.detection.detect);
  89. // Hammer is ready...!
  90. Hammer.READY = true;
  91. }
  92. Hammer.utils = {
  93. /**
  94. * extend method,
  95. * also used for cloning when dest is an empty object
  96. * @param {Object} dest
  97. * @param {Object} src
  98. * @parm {Boolean} merge do a merge
  99. * @returns {Object} dest
  100. */
  101. extend: function extend(dest, src, merge) {
  102. for(var key in src) {
  103. if(dest[key] !== undefined && merge) {
  104. continue;
  105. }
  106. dest[key] = src[key];
  107. }
  108. return dest;
  109. },
  110. /**
  111. * for each
  112. * @param obj
  113. * @param iterator
  114. */
  115. each: function(obj, iterator, context) {
  116. var i, length;
  117. // native forEach on arrays
  118. if ('forEach' in obj) {
  119. obj.forEach(iterator, context);
  120. }
  121. // arrays
  122. else if(obj.length !== undefined) {
  123. for (i = 0, length = obj.length; i < length; i++) {
  124. if (iterator.call(context, obj[i], i, obj) === false) {
  125. return;
  126. }
  127. }
  128. }
  129. // objects
  130. else {
  131. for (i in obj) {
  132. if (obj.hasOwnProperty(i) && iterator.call(context, obj[i], i, obj) === false) {
  133. return;
  134. }
  135. }
  136. }
  137. },
  138. /**
  139. * find if a node is in the given parent
  140. * used for event delegation tricks
  141. * @param {HTMLElement} node
  142. * @param {HTMLElement} parent
  143. * @returns {boolean} has_parent
  144. */
  145. hasParent: function(node, parent) {
  146. while(node) {
  147. if(node == parent) {
  148. return true;
  149. }
  150. node = node.parentNode;
  151. }
  152. return false;
  153. },
  154. /**
  155. * get the center of all the touches
  156. * @param {Array} touches
  157. * @returns {Object} center
  158. */
  159. getCenter: function getCenter(touches) {
  160. var valuesX = [], valuesY = [];
  161. Hammer.utils.each(touches, function(touch) {
  162. // I prefer clientX because it ignore the scrolling position
  163. valuesX.push(typeof touch.clientX !== 'undefined' ? touch.clientX : touch.pageX );
  164. valuesY.push(typeof touch.clientY !== 'undefined' ? touch.clientY : touch.pageY );
  165. });
  166. return {
  167. pageX: ((Math.min.apply(Math, valuesX) + Math.max.apply(Math, valuesX)) / 2),
  168. pageY: ((Math.min.apply(Math, valuesY) + Math.max.apply(Math, valuesY)) / 2)
  169. };
  170. },
  171. /**
  172. * calculate the velocity between two points
  173. * @param {Number} delta_time
  174. * @param {Number} delta_x
  175. * @param {Number} delta_y
  176. * @returns {Object} velocity
  177. */
  178. getVelocity: function getVelocity(delta_time, delta_x, delta_y) {
  179. return {
  180. x: Math.abs(delta_x / delta_time) || 0,
  181. y: Math.abs(delta_y / delta_time) || 0
  182. };
  183. },
  184. /**
  185. * calculate the angle between two coordinates
  186. * @param {Touch} touch1
  187. * @param {Touch} touch2
  188. * @returns {Number} angle
  189. */
  190. getAngle: function getAngle(touch1, touch2) {
  191. var y = touch2.pageY - touch1.pageY,
  192. x = touch2.pageX - touch1.pageX;
  193. return Math.atan2(y, x) * 180 / Math.PI;
  194. },
  195. /**
  196. * angle to direction define
  197. * @param {Touch} touch1
  198. * @param {Touch} touch2
  199. * @returns {String} direction constant, like Hammer.DIRECTION_LEFT
  200. */
  201. getDirection: function getDirection(touch1, touch2) {
  202. var x = Math.abs(touch1.pageX - touch2.pageX),
  203. y = Math.abs(touch1.pageY - touch2.pageY);
  204. if(x >= y) {
  205. return touch1.pageX - touch2.pageX > 0 ? Hammer.DIRECTION_LEFT : Hammer.DIRECTION_RIGHT;
  206. }
  207. else {
  208. return touch1.pageY - touch2.pageY > 0 ? Hammer.DIRECTION_UP : Hammer.DIRECTION_DOWN;
  209. }
  210. },
  211. /**
  212. * calculate the distance between two touches
  213. * @param {Touch} touch1
  214. * @param {Touch} touch2
  215. * @returns {Number} distance
  216. */
  217. getDistance: function getDistance(touch1, touch2) {
  218. var x = touch2.pageX - touch1.pageX,
  219. y = touch2.pageY - touch1.pageY;
  220. return Math.sqrt((x * x) + (y * y));
  221. },
  222. /**
  223. * calculate the scale factor between two touchLists (fingers)
  224. * no scale is 1, and goes down to 0 when pinched together, and bigger when pinched out
  225. * @param {Array} start
  226. * @param {Array} end
  227. * @returns {Number} scale
  228. */
  229. getScale: function getScale(start, end) {
  230. // need two fingers...
  231. if(start.length >= 2 && end.length >= 2) {
  232. return this.getDistance(end[0], end[1]) /
  233. this.getDistance(start[0], start[1]);
  234. }
  235. return 1;
  236. },
  237. /**
  238. * calculate the rotation degrees between two touchLists (fingers)
  239. * @param {Array} start
  240. * @param {Array} end
  241. * @returns {Number} rotation
  242. */
  243. getRotation: function getRotation(start, end) {
  244. // need two fingers
  245. if(start.length >= 2 && end.length >= 2) {
  246. return this.getAngle(end[1], end[0]) -
  247. this.getAngle(start[1], start[0]);
  248. }
  249. return 0;
  250. },
  251. /**
  252. * boolean if the direction is vertical
  253. * @param {String} direction
  254. * @returns {Boolean} is_vertical
  255. */
  256. isVertical: function isVertical(direction) {
  257. return (direction == Hammer.DIRECTION_UP || direction == Hammer.DIRECTION_DOWN);
  258. },
  259. /**
  260. * stop browser default behavior with css props
  261. * @param {HtmlElement} element
  262. * @param {Object} css_props
  263. */
  264. stopDefaultBrowserBehavior: function stopDefaultBrowserBehavior(element, css_props) {
  265. if(!css_props || !element || !element.style) {
  266. return;
  267. }
  268. // with css properties for modern browsers
  269. Hammer.utils.each(['webkit', 'khtml', 'moz', 'Moz', 'ms', 'o', ''], function(vendor) {
  270. Hammer.utils.each(css_props, function(prop) {
  271. // vender prefix at the property
  272. if(vendor) {
  273. prop = vendor + prop.substring(0, 1).toUpperCase() + prop.substring(1);
  274. }
  275. // set the style
  276. if(prop in element.style) {
  277. element.style[prop] = prop;
  278. }
  279. });
  280. });
  281. // also the disable onselectstart
  282. if(css_props.userSelect == 'none') {
  283. element.onselectstart = function() {
  284. return false;
  285. };
  286. }
  287. // and disable ondragstart
  288. if(css_props.userDrag == 'none') {
  289. element.ondragstart = function() {
  290. return false;
  291. };
  292. }
  293. }
  294. };
  295. /**
  296. * create new hammer instance
  297. * all methods should return the instance itself, so it is chainable.
  298. * @param {HTMLElement} element
  299. * @param {Object} [options={}]
  300. * @returns {Hammer.Instance}
  301. * @constructor
  302. */
  303. Hammer.Instance = function(element, options) {
  304. var self = this;
  305. // setup HammerJS window events and register all gestures
  306. // this also sets up the default options
  307. setup();
  308. this.element = element;
  309. // start/stop detection option
  310. this.enabled = true;
  311. // merge options
  312. this.options = Hammer.utils.extend(
  313. Hammer.utils.extend({}, Hammer.defaults),
  314. options || {});
  315. // add some css to the element to prevent the browser from doing its native behavoir
  316. if(this.options.stop_browser_behavior) {
  317. Hammer.utils.stopDefaultBrowserBehavior(this.element, this.options.stop_browser_behavior);
  318. }
  319. // start detection on touchstart
  320. Hammer.event.onTouch(element, Hammer.EVENT_START, function(ev) {
  321. if(self.enabled) {
  322. Hammer.detection.startDetect(self, ev);
  323. }
  324. });
  325. // return instance
  326. return this;
  327. };
  328. Hammer.Instance.prototype = {
  329. /**
  330. * bind events to the instance
  331. * @param {String} gesture
  332. * @param {Function} handler
  333. * @returns {Hammer.Instance}
  334. */
  335. on: function onEvent(gesture, handler) {
  336. var gestures = gesture.split(' ');
  337. Hammer.utils.each(gestures, function(gesture) {
  338. this.element.addEventListener(gesture, handler, false);
  339. }, this);
  340. return this;
  341. },
  342. /**
  343. * unbind events to the instance
  344. * @param {String} gesture
  345. * @param {Function} handler
  346. * @returns {Hammer.Instance}
  347. */
  348. off: function offEvent(gesture, handler) {
  349. var gestures = gesture.split(' ');
  350. Hammer.utils.each(gestures, function(gesture) {
  351. this.element.removeEventListener(gesture, handler, false);
  352. }, this);
  353. return this;
  354. },
  355. /**
  356. * trigger gesture event
  357. * @param {String} gesture
  358. * @param {Object} [eventData]
  359. * @returns {Hammer.Instance}
  360. */
  361. trigger: function triggerEvent(gesture, eventData) {
  362. // optional
  363. if(!eventData) {
  364. eventData = {};
  365. }
  366. // create DOM event
  367. var event = Hammer.DOCUMENT.createEvent('Event');
  368. event.initEvent(gesture, true, true);
  369. event.gesture = eventData;
  370. // trigger on the target if it is in the instance element,
  371. // this is for event delegation tricks
  372. var element = this.element;
  373. if(Hammer.utils.hasParent(eventData.target, element)) {
  374. element = eventData.target;
  375. }
  376. element.dispatchEvent(event);
  377. return this;
  378. },
  379. /**
  380. * enable of disable hammer.js detection
  381. * @param {Boolean} state
  382. * @returns {Hammer.Instance}
  383. */
  384. enable: function enable(state) {
  385. this.enabled = state;
  386. return this;
  387. }
  388. };
  389. /**
  390. * this holds the last move event,
  391. * used to fix empty touchend issue
  392. * see the onTouch event for an explanation
  393. * @type {Object}
  394. */
  395. var last_move_event = null;
  396. /**
  397. * when the mouse is hold down, this is true
  398. * @type {Boolean}
  399. */
  400. var enable_detect = false;
  401. /**
  402. * when touch events have been fired, this is true
  403. * @type {Boolean}
  404. */
  405. var touch_triggered = false;
  406. Hammer.event = {
  407. /**
  408. * simple addEventListener
  409. * @param {HTMLElement} element
  410. * @param {String} type
  411. * @param {Function} handler
  412. */
  413. bindDom: function(element, type, handler) {
  414. var types = type.split(' ');
  415. Hammer.utils.each(types, function(type){
  416. element.addEventListener(type, handler, false);
  417. });
  418. },
  419. /**
  420. * touch events with mouse fallback
  421. * @param {HTMLElement} element
  422. * @param {String} eventType like Hammer.EVENT_MOVE
  423. * @param {Function} handler
  424. */
  425. onTouch: function onTouch(element, eventType, handler) {
  426. var self = this;
  427. this.bindDom(element, Hammer.EVENT_TYPES[eventType], function bindDomOnTouch(ev) {
  428. var sourceEventType = ev.type.toLowerCase();
  429. // onmouseup, but when touchend has been fired we do nothing.
  430. // this is for touchdevices which also fire a mouseup on touchend
  431. if(sourceEventType.match(/mouse/) && touch_triggered) {
  432. return;
  433. }
  434. // mousebutton must be down or a touch event
  435. else if(sourceEventType.match(/touch/) || // touch events are always on screen
  436. sourceEventType.match(/pointerdown/) || // pointerevents touch
  437. (sourceEventType.match(/mouse/) && ev.which === 1) // mouse is pressed
  438. ) {
  439. enable_detect = true;
  440. }
  441. // mouse isn't pressed
  442. else if(sourceEventType.match(/mouse/) && !ev.which) {
  443. enable_detect = false;
  444. }
  445. // we are in a touch event, set the touch triggered bool to true,
  446. // this for the conflicts that may occur on ios and android
  447. if(sourceEventType.match(/touch|pointer/)) {
  448. touch_triggered = true;
  449. }
  450. // count the total touches on the screen
  451. var count_touches = 0;
  452. // when touch has been triggered in this detection session
  453. // and we are now handling a mouse event, we stop that to prevent conflicts
  454. if(enable_detect) {
  455. // update pointerevent
  456. if(Hammer.HAS_POINTEREVENTS && eventType != Hammer.EVENT_END) {
  457. count_touches = Hammer.PointerEvent.updatePointer(eventType, ev);
  458. }
  459. // touch
  460. else if(sourceEventType.match(/touch/)) {
  461. count_touches = ev.touches.length;
  462. }
  463. // mouse
  464. else if(!touch_triggered) {
  465. count_touches = sourceEventType.match(/up/) ? 0 : 1;
  466. }
  467. // if we are in a end event, but when we remove one touch and
  468. // we still have enough, set eventType to move
  469. if(count_touches > 0 && eventType == Hammer.EVENT_END) {
  470. eventType = Hammer.EVENT_MOVE;
  471. }
  472. // no touches, force the end event
  473. else if(!count_touches) {
  474. eventType = Hammer.EVENT_END;
  475. }
  476. // store the last move event
  477. if(count_touches || last_move_event === null) {
  478. last_move_event = ev;
  479. }
  480. // trigger the handler
  481. handler.call(Hammer.detection, self.collectEventData(element, eventType, self.getTouchList(last_move_event, eventType), ev));
  482. // remove pointerevent from list
  483. if(Hammer.HAS_POINTEREVENTS && eventType == Hammer.EVENT_END) {
  484. count_touches = Hammer.PointerEvent.updatePointer(eventType, ev);
  485. }
  486. }
  487. // on the end we reset everything
  488. if(!count_touches) {
  489. last_move_event = null;
  490. enable_detect = false;
  491. touch_triggered = false;
  492. Hammer.PointerEvent.reset();
  493. }
  494. });
  495. },
  496. /**
  497. * we have different events for each device/browser
  498. * determine what we need and set them in the Hammer.EVENT_TYPES constant
  499. */
  500. determineEventTypes: function determineEventTypes() {
  501. // determine the eventtype we want to set
  502. var types;
  503. // pointerEvents magic
  504. if(Hammer.HAS_POINTEREVENTS) {
  505. types = Hammer.PointerEvent.getEvents();
  506. }
  507. // on Android, iOS, blackberry, windows mobile we dont want any mouseevents
  508. else if(Hammer.NO_MOUSEEVENTS) {
  509. types = [
  510. 'touchstart',
  511. 'touchmove',
  512. 'touchend touchcancel'];
  513. }
  514. // for non pointer events browsers and mixed browsers,
  515. // like chrome on windows8 touch laptop
  516. else {
  517. types = [
  518. 'touchstart mousedown',
  519. 'touchmove mousemove',
  520. 'touchend touchcancel mouseup'];
  521. }
  522. Hammer.EVENT_TYPES[Hammer.EVENT_START] = types[0];
  523. Hammer.EVENT_TYPES[Hammer.EVENT_MOVE] = types[1];
  524. Hammer.EVENT_TYPES[Hammer.EVENT_END] = types[2];
  525. },
  526. /**
  527. * create touchlist depending on the event
  528. * @param {Object} ev
  529. * @param {String} eventType used by the fakemultitouch plugin
  530. */
  531. getTouchList: function getTouchList(ev/*, eventType*/) {
  532. // get the fake pointerEvent touchlist
  533. if(Hammer.HAS_POINTEREVENTS) {
  534. return Hammer.PointerEvent.getTouchList();
  535. }
  536. // get the touchlist
  537. else if(ev.touches) {
  538. return ev.touches;
  539. }
  540. // make fake touchlist from mouse position
  541. else {
  542. ev.identifier = 1;
  543. return [ev];
  544. }
  545. },
  546. /**
  547. * collect event data for Hammer js
  548. * @param {HTMLElement} element
  549. * @param {String} eventType like Hammer.EVENT_MOVE
  550. * @param {Object} eventData
  551. */
  552. collectEventData: function collectEventData(element, eventType, touches, ev) {
  553. // find out pointerType
  554. var pointerType = Hammer.POINTER_TOUCH;
  555. if(ev.type.match(/mouse/) || Hammer.PointerEvent.matchType(Hammer.POINTER_MOUSE, ev)) {
  556. pointerType = Hammer.POINTER_MOUSE;
  557. }
  558. return {
  559. center : Hammer.utils.getCenter(touches),
  560. timeStamp : new Date().getTime(),
  561. target : ev.target,
  562. touches : touches,
  563. eventType : eventType,
  564. pointerType: pointerType,
  565. srcEvent : ev,
  566. /**
  567. * prevent the browser default actions
  568. * mostly used to disable scrolling of the browser
  569. */
  570. preventDefault: function() {
  571. if(this.srcEvent.preventManipulation) {
  572. this.srcEvent.preventManipulation();
  573. }
  574. if(this.srcEvent.preventDefault) {
  575. this.srcEvent.preventDefault();
  576. }
  577. },
  578. /**
  579. * stop bubbling the event up to its parents
  580. */
  581. stopPropagation: function() {
  582. this.srcEvent.stopPropagation();
  583. },
  584. /**
  585. * immediately stop gesture detection
  586. * might be useful after a swipe was detected
  587. * @return {*}
  588. */
  589. stopDetect: function() {
  590. return Hammer.detection.stopDetect();
  591. }
  592. };
  593. }
  594. };
  595. Hammer.PointerEvent = {
  596. /**
  597. * holds all pointers
  598. * @type {Object}
  599. */
  600. pointers: {},
  601. /**
  602. * get a list of pointers
  603. * @returns {Array} touchlist
  604. */
  605. getTouchList: function() {
  606. var self = this;
  607. var touchlist = [];
  608. // we can use forEach since pointerEvents only is in IE10
  609. Hammer.utils.each(self.pointers, function(pointer){
  610. touchlist.push(pointer);
  611. });
  612. return touchlist;
  613. },
  614. /**
  615. * update the position of a pointer
  616. * @param {String} type Hammer.EVENT_END
  617. * @param {Object} pointerEvent
  618. */
  619. updatePointer: function(type, pointerEvent) {
  620. if(type == Hammer.EVENT_END) {
  621. this.pointers = {};
  622. }
  623. else {
  624. pointerEvent.identifier = pointerEvent.pointerId;
  625. this.pointers[pointerEvent.pointerId] = pointerEvent;
  626. }
  627. return Object.keys(this.pointers).length;
  628. },
  629. /**
  630. * check if ev matches pointertype
  631. * @param {String} pointerType Hammer.POINTER_MOUSE
  632. * @param {PointerEvent} ev
  633. */
  634. matchType: function(pointerType, ev) {
  635. if(!ev.pointerType) {
  636. return false;
  637. }
  638. var pt = ev.pointerType,
  639. types = {};
  640. types[Hammer.POINTER_MOUSE] = (pt === ev.MSPOINTER_TYPE_MOUSE || pt === Hammer.POINTER_MOUSE);
  641. types[Hammer.POINTER_TOUCH] = (pt === ev.MSPOINTER_TYPE_TOUCH || pt === Hammer.POINTER_TOUCH);
  642. types[Hammer.POINTER_PEN] = (pt === ev.MSPOINTER_TYPE_PEN || pt === Hammer.POINTER_PEN);
  643. return types[pointerType];
  644. },
  645. /**
  646. * get events
  647. */
  648. getEvents: function() {
  649. return [
  650. 'pointerdown MSPointerDown',
  651. 'pointermove MSPointerMove',
  652. 'pointerup pointercancel MSPointerUp MSPointerCancel'
  653. ];
  654. },
  655. /**
  656. * reset the list
  657. */
  658. reset: function() {
  659. this.pointers = {};
  660. }
  661. };
  662. Hammer.detection = {
  663. // contains all registred Hammer.gestures in the correct order
  664. gestures: [],
  665. // data of the current Hammer.gesture detection session
  666. current : null,
  667. // the previous Hammer.gesture session data
  668. // is a full clone of the previous gesture.current object
  669. previous: null,
  670. // when this becomes true, no gestures are fired
  671. stopped : false,
  672. /**
  673. * start Hammer.gesture detection
  674. * @param {Hammer.Instance} inst
  675. * @param {Object} eventData
  676. */
  677. startDetect: function startDetect(inst, eventData) {
  678. // already busy with a Hammer.gesture detection on an element
  679. if(this.current) {
  680. return;
  681. }
  682. this.stopped = false;
  683. this.current = {
  684. inst : inst, // reference to HammerInstance we're working for
  685. startEvent: Hammer.utils.extend({}, eventData), // start eventData for distances, timing etc
  686. lastEvent : false, // last eventData
  687. name : '' // current gesture we're in/detected, can be 'tap', 'hold' etc
  688. };
  689. this.detect(eventData);
  690. },
  691. /**
  692. * Hammer.gesture detection
  693. * @param {Object} eventData
  694. */
  695. detect: function detect(eventData) {
  696. if(!this.current || this.stopped) {
  697. return;
  698. }
  699. // extend event data with calculations about scale, distance etc
  700. eventData = this.extendEventData(eventData);
  701. // instance options
  702. var inst_options = this.current.inst.options;
  703. // call Hammer.gesture handlers
  704. Hammer.utils.each(this.gestures, function(gesture) {
  705. // only when the instance options have enabled this gesture
  706. if(!this.stopped && inst_options[gesture.name] !== false) {
  707. // if a handler returns false, we stop with the detection
  708. if(gesture.handler.call(gesture, eventData, this.current.inst) === false) {
  709. this.stopDetect();
  710. return false;
  711. }
  712. }
  713. }, this);
  714. // store as previous event event
  715. if(this.current) {
  716. this.current.lastEvent = eventData;
  717. }
  718. // endevent, but not the last touch, so dont stop
  719. if(eventData.eventType == Hammer.EVENT_END && !eventData.touches.length - 1) {
  720. this.stopDetect();
  721. }
  722. return eventData;
  723. },
  724. /**
  725. * clear the Hammer.gesture vars
  726. * this is called on endDetect, but can also be used when a final Hammer.gesture has been detected
  727. * to stop other Hammer.gestures from being fired
  728. */
  729. stopDetect: function stopDetect() {
  730. // clone current data to the store as the previous gesture
  731. // used for the double tap gesture, since this is an other gesture detect session
  732. this.previous = Hammer.utils.extend({}, this.current);
  733. // reset the current
  734. this.current = null;
  735. // stopped!
  736. this.stopped = true;
  737. },
  738. /**
  739. * extend eventData for Hammer.gestures
  740. * @param {Object} ev
  741. * @returns {Object} ev
  742. */
  743. extendEventData: function extendEventData(ev) {
  744. var startEv = this.current.startEvent;
  745. // if the touches change, set the new touches over the startEvent touches
  746. // this because touchevents don't have all the touches on touchstart, or the
  747. // user must place his fingers at the EXACT same time on the screen, which is not realistic
  748. // but, sometimes it happens that both fingers are touching at the EXACT same time
  749. if(startEv && (ev.touches.length != startEv.touches.length || ev.touches === startEv.touches)) {
  750. // extend 1 level deep to get the touchlist with the touch objects
  751. startEv.touches = [];
  752. Hammer.utils.each(ev.touches, function(touch) {
  753. startEv.touches.push(Hammer.utils.extend({}, touch));
  754. });
  755. }
  756. var delta_time = ev.timeStamp - startEv.timeStamp
  757. , delta_x = ev.center.pageX - startEv.center.pageX
  758. , delta_y = ev.center.pageY - startEv.center.pageY
  759. , velocity = Hammer.utils.getVelocity(delta_time, delta_x, delta_y)
  760. , interimAngle
  761. , interimDirection;
  762. // end events (e.g. dragend) don't have useful values for interimDirection & interimAngle
  763. // because the previous event has exactly the same coordinates
  764. // so for end events, take the previous values of interimDirection & interimAngle
  765. // instead of recalculating them and getting a spurious '0'
  766. if(ev.eventType === 'end') {
  767. interimAngle = this.current.lastEvent && this.current.lastEvent.interimAngle;
  768. interimDirection = this.current.lastEvent && this.current.lastEvent.interimDirection;
  769. }
  770. else {
  771. interimAngle = this.current.lastEvent && Hammer.utils.getAngle(this.current.lastEvent.center, ev.center);
  772. interimDirection = this.current.lastEvent && Hammer.utils.getDirection(this.current.lastEvent.center, ev.center);
  773. }
  774. Hammer.utils.extend(ev, {
  775. deltaTime: delta_time,
  776. deltaX: delta_x,
  777. deltaY: delta_y,
  778. velocityX: velocity.x,
  779. velocityY: velocity.y,
  780. distance: Hammer.utils.getDistance(startEv.center, ev.center),
  781. angle: Hammer.utils.getAngle(startEv.center, ev.center),
  782. interimAngle: interimAngle,
  783. direction: Hammer.utils.getDirection(startEv.center, ev.center),
  784. interimDirection: interimDirection,
  785. scale: Hammer.utils.getScale(startEv.touches, ev.touches),
  786. rotation: Hammer.utils.getRotation(startEv.touches, ev.touches),
  787. startEvent: startEv
  788. });
  789. return ev;
  790. },
  791. /**
  792. * register new gesture
  793. * @param {Object} gesture object, see gestures.js for documentation
  794. * @returns {Array} gestures
  795. */
  796. register: function register(gesture) {
  797. // add an enable gesture options if there is no given
  798. var options = gesture.defaults || {};
  799. if(options[gesture.name] === undefined) {
  800. options[gesture.name] = true;
  801. }
  802. // extend Hammer default options with the Hammer.gesture options
  803. Hammer.utils.extend(Hammer.defaults, options, true);
  804. // set its index
  805. gesture.index = gesture.index || 1000;
  806. // add Hammer.gesture to the list
  807. this.gestures.push(gesture);
  808. // sort the list by index
  809. this.gestures.sort(function(a, b) {
  810. if(a.index < b.index) { return -1; }
  811. if(a.index > b.index) { return 1; }
  812. return 0;
  813. });
  814. return this.gestures;
  815. }
  816. };
  817. /**
  818. * Drag
  819. * Move with x fingers (default 1) around on the page. Blocking the scrolling when
  820. * moving left and right is a good practice. When all the drag events are blocking
  821. * you disable scrolling on that area.
  822. * @events drag, drapleft, dragright, dragup, dragdown
  823. */
  824. Hammer.gestures.Drag = {
  825. name : 'drag',
  826. index : 50,
  827. defaults : {
  828. drag_min_distance : 10,
  829. // Set correct_for_drag_min_distance to true to make the starting point of the drag
  830. // be calculated from where the drag was triggered, not from where the touch started.
  831. // Useful to avoid a jerk-starting drag, which can make fine-adjustments
  832. // through dragging difficult, and be visually unappealing.
  833. correct_for_drag_min_distance: true,
  834. // set 0 for unlimited, but this can conflict with transform
  835. drag_max_touches : 1,
  836. // prevent default browser behavior when dragging occurs
  837. // be careful with it, it makes the element a blocking element
  838. // when you are using the drag gesture, it is a good practice to set this true
  839. drag_block_horizontal : false,
  840. drag_block_vertical : false,
  841. // drag_lock_to_axis keeps the drag gesture on the axis that it started on,
  842. // It disallows vertical directions if the initial direction was horizontal, and vice versa.
  843. drag_lock_to_axis : false,
  844. // drag lock only kicks in when distance > drag_lock_min_distance
  845. // This way, locking occurs only when the distance has become large enough to reliably determine the direction
  846. drag_lock_min_distance : 25
  847. },
  848. triggered: false,
  849. handler : function dragGesture(ev, inst) {
  850. // current gesture isnt drag, but dragged is true
  851. // this means an other gesture is busy. now call dragend
  852. if(Hammer.detection.current.name != this.name && this.triggered) {
  853. inst.trigger(this.name + 'end', ev);
  854. this.triggered = false;
  855. return;
  856. }
  857. // max touches
  858. if(inst.options.drag_max_touches > 0 &&
  859. ev.touches.length > inst.options.drag_max_touches) {
  860. return;
  861. }
  862. switch(ev.eventType) {
  863. case Hammer.EVENT_START:
  864. this.triggered = false;
  865. break;
  866. case Hammer.EVENT_MOVE:
  867. // when the distance we moved is too small we skip this gesture
  868. // or we can be already in dragging
  869. if(ev.distance < inst.options.drag_min_distance &&
  870. Hammer.detection.current.name != this.name) {
  871. return;
  872. }
  873. // we are dragging!
  874. if(Hammer.detection.current.name != this.name) {
  875. Hammer.detection.current.name = this.name;
  876. if(inst.options.correct_for_drag_min_distance && ev.distance > 0) {
  877. // When a drag is triggered, set the event center to drag_min_distance pixels from the original event center.
  878. // Without this correction, the dragged distance would jumpstart at drag_min_distance pixels instead of at 0.
  879. // It might be useful to save the original start point somewhere
  880. var factor = Math.abs(inst.options.drag_min_distance / ev.distance);
  881. Hammer.detection.current.startEvent.center.pageX += ev.deltaX * factor;
  882. Hammer.detection.current.startEvent.center.pageY += ev.deltaY * factor;
  883. // recalculate event data using new start point
  884. ev = Hammer.detection.extendEventData(ev);
  885. }
  886. }
  887. // lock drag to axis?
  888. if(Hammer.detection.current.lastEvent.drag_locked_to_axis || (inst.options.drag_lock_to_axis && inst.options.drag_lock_min_distance <= ev.distance)) {
  889. ev.drag_locked_to_axis = true;
  890. }
  891. var last_direction = Hammer.detection.current.lastEvent.direction;
  892. if(ev.drag_locked_to_axis && last_direction !== ev.direction) {
  893. // keep direction on the axis that the drag gesture started on
  894. if(Hammer.utils.isVertical(last_direction)) {
  895. ev.direction = (ev.deltaY < 0) ? Hammer.DIRECTION_UP : Hammer.DIRECTION_DOWN;
  896. }
  897. else {
  898. ev.direction = (ev.deltaX < 0) ? Hammer.DIRECTION_LEFT : Hammer.DIRECTION_RIGHT;
  899. }
  900. }
  901. // first time, trigger dragstart event
  902. if(!this.triggered) {
  903. inst.trigger(this.name + 'start', ev);
  904. this.triggered = true;
  905. }
  906. // trigger normal event
  907. inst.trigger(this.name, ev);
  908. // direction event, like dragdown
  909. inst.trigger(this.name + ev.direction, ev);
  910. // block the browser events
  911. if((inst.options.drag_block_vertical && Hammer.utils.isVertical(ev.direction)) ||
  912. (inst.options.drag_block_horizontal && !Hammer.utils.isVertical(ev.direction))) {
  913. ev.preventDefault();
  914. }
  915. break;
  916. case Hammer.EVENT_END:
  917. // trigger dragend
  918. if(this.triggered) {
  919. inst.trigger(this.name + 'end', ev);
  920. }
  921. this.triggered = false;
  922. break;
  923. }
  924. }
  925. };
  926. /**
  927. * Hold
  928. * Touch stays at the same place for x time
  929. * @events hold
  930. */
  931. Hammer.gestures.Hold = {
  932. name : 'hold',
  933. index : 10,
  934. defaults: {
  935. hold_timeout : 500,
  936. hold_threshold: 1
  937. },
  938. timer : null,
  939. handler : function holdGesture(ev, inst) {
  940. switch(ev.eventType) {
  941. case Hammer.EVENT_START:
  942. // clear any running timers
  943. clearTimeout(this.timer);
  944. // set the gesture so we can check in the timeout if it still is
  945. Hammer.detection.current.name = this.name;
  946. // set timer and if after the timeout it still is hold,
  947. // we trigger the hold event
  948. this.timer = setTimeout(function() {
  949. if(Hammer.detection.current.name == 'hold') {
  950. inst.trigger('hold', ev);
  951. }
  952. }, inst.options.hold_timeout);
  953. break;
  954. // when you move or end we clear the timer
  955. case Hammer.EVENT_MOVE:
  956. if(ev.distance > inst.options.hold_threshold) {
  957. clearTimeout(this.timer);
  958. }
  959. break;
  960. case Hammer.EVENT_END:
  961. clearTimeout(this.timer);
  962. break;
  963. }
  964. }
  965. };
  966. /**
  967. * Release
  968. * Called as last, tells the user has released the screen
  969. * @events release
  970. */
  971. Hammer.gestures.Release = {
  972. name : 'release',
  973. index : Infinity,
  974. handler: function releaseGesture(ev, inst) {
  975. if(ev.eventType == Hammer.EVENT_END) {
  976. inst.trigger(this.name, ev);
  977. }
  978. }
  979. };
  980. /**
  981. * Swipe
  982. * triggers swipe events when the end velocity is above the threshold
  983. * @events swipe, swipeleft, swiperight, swipeup, swipedown
  984. */
  985. Hammer.gestures.Swipe = {
  986. name : 'swipe',
  987. index : 40,
  988. defaults: {
  989. // set 0 for unlimited, but this can conflict with transform
  990. swipe_min_touches: 1,
  991. swipe_max_touches: 1,
  992. swipe_velocity : 0.7
  993. },
  994. handler : function swipeGesture(ev, inst) {
  995. if(ev.eventType == Hammer.EVENT_END) {
  996. // max touches
  997. if(inst.options.swipe_max_touches > 0 &&
  998. ev.touches.length < inst.options.swipe_min_touches &&
  999. ev.touches.length > inst.options.swipe_max_touches) {
  1000. return;
  1001. }
  1002. // when the distance we moved is too small we skip this gesture
  1003. // or we can be already in dragging
  1004. if(ev.velocityX > inst.options.swipe_velocity ||
  1005. ev.velocityY > inst.options.swipe_velocity) {
  1006. // trigger swipe events
  1007. inst.trigger(this.name, ev);
  1008. inst.trigger(this.name + ev.direction, ev);
  1009. }
  1010. }
  1011. }
  1012. };
  1013. /**
  1014. * Tap/DoubleTap
  1015. * Quick touch at a place or double at the same place
  1016. * @events tap, doubletap
  1017. */
  1018. Hammer.gestures.Tap = {
  1019. name : 'tap',
  1020. index : 100,
  1021. defaults: {
  1022. tap_max_touchtime : 250,
  1023. tap_max_distance : 10,
  1024. tap_always : true,
  1025. doubletap_distance: 20,
  1026. doubletap_interval: 300
  1027. },
  1028. handler : function tapGesture(ev, inst) {
  1029. if(ev.eventType == Hammer.EVENT_END && ev.srcEvent.type != 'touchcancel') {
  1030. // previous gesture, for the double tap since these are two different gesture detections
  1031. var prev = Hammer.detection.previous,
  1032. did_doubletap = false;
  1033. // when the touchtime is higher then the max touch time
  1034. // or when the moving distance is too much
  1035. if(ev.deltaTime > inst.options.tap_max_touchtime ||
  1036. ev.distance > inst.options.tap_max_distance) {
  1037. return;
  1038. }
  1039. // check if double tap
  1040. if(prev && prev.name == 'tap' &&
  1041. (ev.timeStamp - prev.lastEvent.timeStamp) < inst.options.doubletap_interval &&
  1042. ev.distance < inst.options.doubletap_distance) {
  1043. inst.trigger('doubletap', ev);
  1044. did_doubletap = true;
  1045. }
  1046. // do a single tap
  1047. if(!did_doubletap || inst.options.tap_always) {
  1048. Hammer.detection.current.name = 'tap';
  1049. inst.trigger(Hammer.detection.current.name, ev);
  1050. }
  1051. }
  1052. }
  1053. };
  1054. /**
  1055. * Touch
  1056. * Called as first, tells the user has touched the screen
  1057. * @events touch
  1058. */
  1059. Hammer.gestures.Touch = {
  1060. name : 'touch',
  1061. index : -Infinity,
  1062. defaults: {
  1063. // call preventDefault at touchstart, and makes the element blocking by
  1064. // disabling the scrolling of the page, but it improves gestures like
  1065. // transforming and dragging.
  1066. // be careful with using this, it can be very annoying for users to be stuck
  1067. // on the page
  1068. prevent_default : false,
  1069. // disable mouse events, so only touch (or pen!) input triggers events
  1070. prevent_mouseevents: false
  1071. },
  1072. handler : function touchGesture(ev, inst) {
  1073. if(inst.options.prevent_mouseevents && ev.pointerType == Hammer.POINTER_MOUSE) {
  1074. ev.stopDetect();
  1075. return;
  1076. }
  1077. if(inst.options.prevent_default) {
  1078. ev.preventDefault();
  1079. }
  1080. if(ev.eventType == Hammer.EVENT_START) {
  1081. inst.trigger(this.name, ev);
  1082. }
  1083. }
  1084. };
  1085. /**
  1086. * Transform
  1087. * User want to scale or rotate with 2 fingers
  1088. * @events transform, pinch, pinchin, pinchout, rotate
  1089. */
  1090. Hammer.gestures.Transform = {
  1091. name : 'transform',
  1092. index : 45,
  1093. defaults : {
  1094. // factor, no scale is 1, zoomin is to 0 and zoomout until higher then 1
  1095. transform_min_scale : 0.01,
  1096. // rotation in degrees
  1097. transform_min_rotation: 1,
  1098. // prevent default browser behavior when two touches are on the screen
  1099. // but it makes the element a blocking element
  1100. // when you are using the transform gesture, it is a good practice to set this true
  1101. transform_always_block: false
  1102. },
  1103. triggered: false,
  1104. handler : function transformGesture(ev, inst) {
  1105. // current gesture isnt drag, but dragged is true
  1106. // this means an other gesture is busy. now call dragend
  1107. if(Hammer.detection.current.name != this.name && this.triggered) {
  1108. inst.trigger(this.name + 'end', ev);
  1109. this.triggered = false;
  1110. return;
  1111. }
  1112. // atleast multitouch
  1113. if(ev.touches.length < 2) {
  1114. return;
  1115. }
  1116. // prevent default when two fingers are on the screen
  1117. if(inst.options.transform_always_block) {
  1118. ev.preventDefault();
  1119. }
  1120. switch(ev.eventType) {
  1121. case Hammer.EVENT_START:
  1122. this.triggered = false;
  1123. break;
  1124. case Hammer.EVENT_MOVE:
  1125. var scale_threshold = Math.abs(1 - ev.scale);
  1126. var rotation_threshold = Math.abs(ev.rotation);
  1127. // when the distance we moved is too small we skip this gesture
  1128. // or we can be already in dragging
  1129. if(scale_threshold < inst.options.transform_min_scale &&
  1130. rotation_threshold < inst.options.transform_min_rotation) {
  1131. return;
  1132. }
  1133. // we are transforming!
  1134. Hammer.detection.current.name = this.name;
  1135. // first time, trigger dragstart event
  1136. if(!this.triggered) {
  1137. inst.trigger(this.name + 'start', ev);
  1138. this.triggered = true;
  1139. }
  1140. inst.trigger(this.name, ev); // basic transform event
  1141. // trigger rotate event
  1142. if(rotation_threshold > inst.options.transform_min_rotation) {
  1143. inst.trigger('rotate', ev);
  1144. }
  1145. // trigger pinch event
  1146. if(scale_threshold > inst.options.transform_min_scale) {
  1147. inst.trigger('pinch', ev);
  1148. inst.trigger('pinch' + ((ev.scale < 1) ? 'in' : 'out'), ev);
  1149. }
  1150. break;
  1151. case Hammer.EVENT_END:
  1152. // trigger dragend
  1153. if(this.triggered) {
  1154. inst.trigger(this.name + 'end', ev);
  1155. }
  1156. this.triggered = false;
  1157. break;
  1158. }
  1159. }
  1160. };
  1161. // Based off Lo-Dash's excellent UMD wrapper (slightly modified) - https://github.com/bestiejs/lodash/blob/master/lodash.js#L5515-L5543
  1162. // some AMD build optimizers, like r.js, check for specific condition patterns like the following:
  1163. if(typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
  1164. // define as an anonymous module
  1165. define(function() {
  1166. return Hammer;
  1167. });
  1168. // check for `exports` after `define` in case a build optimizer adds an `exports` object
  1169. }
  1170. else if(typeof module === 'object' && typeof module.exports === 'object') {
  1171. module.exports = Hammer;
  1172. }
  1173. else {
  1174. window.Hammer = Hammer;
  1175. }
  1176. })(this);
  1177. /*! jQuery plugin for Hammer.JS - v1.0.0 - 2014-01-02
  1178. * http://eightmedia.github.com/hammer.js
  1179. *
  1180. * Copyright (c) 2014 Jorik Tangelder <j.tangelder@gmail.com>;
  1181. * Licensed under the MIT license */(function(window, undefined) {
  1182. 'use strict';
  1183. function setup(Hammer, $) {
  1184. /**
  1185. * bind dom events
  1186. * this overwrites addEventListener
  1187. * @param {HTMLElement} element
  1188. * @param {String} eventTypes
  1189. * @param {Function} handler
  1190. */
  1191. Hammer.event.bindDom = function(element, eventTypes, handler) {
  1192. $(element).on(eventTypes, function(ev) {
  1193. var data = ev.originalEvent || ev;
  1194. if(data.pageX === undefined) {
  1195. data.pageX = ev.pageX;
  1196. data.pageY = ev.pageY;
  1197. }
  1198. if(!data.target) {
  1199. data.target = ev.target;
  1200. }
  1201. if(data.which === undefined) {
  1202. data.which = data.button;
  1203. }
  1204. if(!data.preventDefault) {
  1205. data.preventDefault = ev.preventDefault;
  1206. }
  1207. if(!data.stopPropagation) {
  1208. data.stopPropagation = ev.stopPropagation;
  1209. }
  1210. handler.call(this, data);
  1211. });
  1212. };
  1213. /**
  1214. * the methods are called by the instance, but with the jquery plugin
  1215. * we use the jquery event methods instead.
  1216. * @this {Hammer.Instance}
  1217. * @return {jQuery}
  1218. */
  1219. Hammer.Instance.prototype.on = function(types, handler) {
  1220. return $(this.element).on(types, handler);
  1221. };
  1222. Hammer.Instance.prototype.off = function(types, handler) {
  1223. return $(this.element).off(types, handler);
  1224. };
  1225. /**
  1226. * trigger events
  1227. * this is called by the gestures to trigger an event like 'tap'
  1228. * @this {Hammer.Instance}
  1229. * @param {String} gesture
  1230. * @param {Object} eventData
  1231. * @return {jQuery}
  1232. */
  1233. Hammer.Instance.prototype.trigger = function(gesture, eventData) {
  1234. var el = $(this.element);
  1235. if(el.has(eventData.target).length) {
  1236. el = $(eventData.target);
  1237. }
  1238. return el.trigger({
  1239. type : gesture,
  1240. gesture: eventData
  1241. });
  1242. };
  1243. /**
  1244. * jQuery plugin
  1245. * create instance of Hammer and watch for gestures,
  1246. * and when called again you can change the options
  1247. * @param {Object} [options={}]
  1248. * @return {jQuery}
  1249. */
  1250. $.fn.hammer = function(options) {
  1251. return this.each(function() {
  1252. var el = $(this);
  1253. var inst = el.data('hammer');
  1254. // start new hammer instance
  1255. if(!inst) {
  1256. el.data('hammer', new Hammer(this, options || {}));
  1257. }
  1258. // change the options
  1259. else if(inst && options) {
  1260. Hammer.utils.extend(inst.options, options);
  1261. }
  1262. });
  1263. };
  1264. }
  1265. // Based off Lo-Dash's excellent UMD wrapper (slightly modified) - https://github.com/bestiejs/lodash/blob/master/lodash.js#L5515-L5543
  1266. // some AMD build optimizers, like r.js, check for specific condition patterns like the following:
  1267. if(typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
  1268. // define as an anonymous module
  1269. define(['hammer', 'jquery'], setup);
  1270. }
  1271. else {
  1272. setup(window.Hammer, window.jQuery || window.Zepto);
  1273. }
  1274. })(this);