12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159
  1. /* tslint:disable */
  2. (function (factory) {
  3. if (typeof module === "object" && typeof module.exports === "object") {
  4. var v = factory(require, exports);
  5. if (v !== undefined) module.exports = v;
  6. }
  7. else if (typeof define === "function" && define.amd) {
  8. define(["require", "exports"], factory);
  9. }
  10. })(function (require, exports) {
  11. "use strict";
  12. Object.defineProperty(exports, "__esModule", { value: true });
  13. var win = window;
  14. var doc = document;
  15. /*! Hammer.JS - v2.0.6 - 2015-12-23
  16. * http://hammerjs.github.io/
  17. *
  18. * Copyright (c) 2015 Jorik Tangelder;
  19. * Licensed under the license */
  20. var VENDOR_PREFIXES = ['', 'webkit', 'Moz', 'MS', 'ms', 'o'];
  21. var TEST_ELEMENT = doc.createElement('div');
  22. var TYPE_FUNCTION = 'function';
  23. var round = Math.round;
  24. var abs = Math.abs;
  25. var now = Date.now;
  26. /**
  27. * set a timeout with a given scope
  28. * @param {Function} fn
  29. * @param {Number} timeout
  30. * @param {Object} context
  31. * @returns {number}
  32. */
  33. function setTimeoutContext(fn, timeout, context) {
  34. return setTimeout(bindFn(fn, context), timeout);
  35. }
  36. /**
  37. * if the argument is an array, we want to execute the fn on each entry
  38. * if it aint an array we don't want to do a thing.
  39. * this is used by all the methods that accept a single and array argument.
  40. * @param {*|Array} arg
  41. * @param {String} fn
  42. * @param {Object} [context]
  43. * @returns {Boolean}
  44. */
  45. function invokeArrayArg(arg, fn, context) {
  46. if (Array.isArray(arg)) {
  47. each(arg, context[fn], context);
  48. return true;
  49. }
  50. return false;
  51. }
  52. /**
  53. * walk objects and arrays
  54. * @param {Object} obj
  55. * @param {Function} iterator
  56. * @param {Object} context
  57. */
  58. function each(obj, iterator, context) {
  59. var i;
  60. if (!obj) {
  61. return;
  62. }
  63. if (obj.forEach) {
  64. obj.forEach(iterator, context);
  65. }
  66. else if (obj.length !== undefined) {
  67. i = 0;
  68. while (i < obj.length) {
  69. iterator.call(context, obj[i], i, obj);
  70. i++;
  71. }
  72. }
  73. else {
  74. for (i in obj) {
  75. obj.hasOwnProperty(i) && iterator.call(context, obj[i], i, obj);
  76. }
  77. }
  78. }
  79. /**
  80. * simple class inheritance
  81. * @param {Function} child
  82. * @param {Function} base
  83. * @param {Object} [properties]
  84. */
  85. function inherit(child, base, properties) {
  86. var baseP = base.prototype, childP;
  87. childP = child.prototype = Object.create(baseP);
  88. childP.constructor = child;
  89. childP._super = baseP;
  90. if (properties) {
  91. Object.assign(childP, properties);
  92. }
  93. }
  94. /**
  95. * simple function bind
  96. * @param {Function} fn
  97. * @param {Object} context
  98. * @returns {Function}
  99. */
  100. function bindFn(fn, context) {
  101. return function boundFn() {
  102. return fn.apply(context, arguments);
  103. };
  104. }
  105. /**
  106. * let a boolean value also be a function that must return a boolean
  107. * this first item in args will be used as the context
  108. * @param {Boolean|Function} val
  109. * @param {Array} [args]
  110. * @returns {Boolean}
  111. */
  112. function boolOrFn(val, args) {
  113. if (typeof val == TYPE_FUNCTION) {
  114. return val.apply(args ? args[0] || undefined : undefined, args);
  115. }
  116. return val;
  117. }
  118. /**
  119. * use the val2 when val1 is undefined
  120. * @param {*} val1
  121. * @param {*} val2
  122. * @returns {*}
  123. */
  124. function ifUndefined(val1, val2) {
  125. return (val1 === undefined) ? val2 : val1;
  126. }
  127. /**
  128. * addEventListener with multiple events at once
  129. * @param {EventTarget} target
  130. * @param {String} types
  131. * @param {Function} handler
  132. */
  133. function addEventListeners(target, types, handler) {
  134. each(splitStr(types), function (type) {
  135. target.addEventListener(type, handler, false);
  136. });
  137. }
  138. /**
  139. * removeEventListener with multiple events at once
  140. * @param {EventTarget} target
  141. * @param {String} types
  142. * @param {Function} handler
  143. */
  144. function removeEventListeners(target, types, handler) {
  145. each(splitStr(types), function (type) {
  146. target.removeEventListener(type, handler, false);
  147. });
  148. }
  149. /**
  150. * find if a node is in the given parent
  151. * @method hasParent
  152. * @param {HTMLElement} node
  153. * @param {HTMLElement} parent
  154. * @return {Boolean} found
  155. */
  156. function hasParent(node, parent) {
  157. while (node) {
  158. if (node == parent) {
  159. return true;
  160. }
  161. node = node.parentNode;
  162. }
  163. return false;
  164. }
  165. /**
  166. * small indexOf wrapper
  167. * @param {String} str
  168. * @param {String} find
  169. * @returns {Boolean} found
  170. */
  171. function inStr(str, find) {
  172. return str.indexOf(find) > -1;
  173. }
  174. /**
  175. * split string on whitespace
  176. * @param {String} str
  177. * @returns {Array} words
  178. */
  179. function splitStr(str) {
  180. return str.trim().split(/\s+/g);
  181. }
  182. /**
  183. * find if a array contains the object using indexOf or a simple polyFill
  184. * @param {Array} src
  185. * @param {String} find
  186. * @param {String} [findByKey]
  187. * @return {Boolean|Number} false when not found, or the index
  188. */
  189. function inArray(src, find, findByKey) {
  190. if (src.indexOf && !findByKey) {
  191. return src.indexOf(find);
  192. }
  193. else {
  194. var i = 0;
  195. while (i < src.length) {
  196. if ((findByKey && src[i][findByKey] == find) || (!findByKey && src[i] === find)) {
  197. return i;
  198. }
  199. i++;
  200. }
  201. return -1;
  202. }
  203. }
  204. /**
  205. * convert array-like objects to real arrays
  206. * @param {Object} obj
  207. * @returns {Array}
  208. */
  209. function toArray(obj) {
  210. return Array.prototype.slice.call(obj, 0);
  211. }
  212. /**
  213. * unique array with objects based on a key (like 'id') or just by the array's value
  214. * @param {Array} src [{id:1},{id:2},{id:1}]
  215. * @param {String} [key]
  216. * @param {Boolean} [sort=False]
  217. * @returns {Array} [{id:1},{id:2}]
  218. */
  219. function uniqueArray(src, key, sort) {
  220. var results = [];
  221. var values = [];
  222. var i = 0;
  223. while (i < src.length) {
  224. var val = key ? src[i][key] : src[i];
  225. if (inArray(values, val) < 0) {
  226. results.push(src[i]);
  227. }
  228. values[i] = val;
  229. i++;
  230. }
  231. if (sort) {
  232. if (!key) {
  233. results = results.sort();
  234. }
  235. else {
  236. results = results.sort(function sortUniqueArray(a, b) {
  237. return a[key] > b[key] ? 1 : 0;
  238. });
  239. }
  240. }
  241. return results;
  242. }
  243. /**
  244. * get the prefixed property
  245. * @param {Object} obj
  246. * @param {String} property
  247. * @returns {String|Undefined} prefixed
  248. */
  249. function prefixed(obj, property) {
  250. var prefix, prop;
  251. var camelProp = property[0].toUpperCase() + property.slice(1);
  252. var i = 0;
  253. while (i < VENDOR_PREFIXES.length) {
  254. prefix = VENDOR_PREFIXES[i];
  255. prop = (prefix) ? prefix + camelProp : property;
  256. if (prop in obj) {
  257. return prop;
  258. }
  259. i++;
  260. }
  261. return undefined;
  262. }
  263. /**
  264. * get a unique id
  265. * @returns {number} uniqueId
  266. */
  267. var _uniqueId = 1;
  268. function uniqueId() {
  269. return _uniqueId++;
  270. }
  271. /**
  272. * get the window object of an element
  273. * @param {HTMLElement} element
  274. * @returns {DocumentView|Window}
  275. */
  276. function getWindowForElement(element) {
  277. var doc = element.ownerDocument || element;
  278. return (doc.defaultView || doc.parentWindow || window);
  279. }
  280. var MOBILE_REGEX = /mobile|tablet|ip(ad|hone|od)|android/i;
  281. var SUPPORT_TOUCH = ('ontouchstart' in window);
  282. var SUPPORT_POINTER_EVENTS = prefixed(window, 'PointerEvent') !== undefined;
  283. var SUPPORT_ONLY_TOUCH = SUPPORT_TOUCH && MOBILE_REGEX.test(navigator.userAgent);
  284. var INPUT_TYPE_TOUCH = 'touch';
  285. var INPUT_TYPE_PEN = 'pen';
  286. var INPUT_TYPE_MOUSE = 'mouse';
  287. var INPUT_TYPE_KINECT = 'kinect';
  288. var COMPUTE_INTERVAL = 25;
  289. var INPUT_START = 1;
  290. var INPUT_MOVE = 2;
  291. var INPUT_END = 4;
  292. var INPUT_CANCEL = 8;
  293. var DIRECTION_NONE = 1;
  294. exports.DIRECTION_LEFT = 2;
  295. exports.DIRECTION_RIGHT = 4;
  296. var DIRECTION_UP = 8;
  297. var DIRECTION_DOWN = 16;
  298. exports.DIRECTION_HORIZONTAL = exports.DIRECTION_LEFT | exports.DIRECTION_RIGHT;
  299. exports.DIRECTION_VERTICAL = DIRECTION_UP | DIRECTION_DOWN;
  300. var DIRECTION_ALL = exports.DIRECTION_HORIZONTAL | exports.DIRECTION_VERTICAL;
  301. var PROPS_XY = ['x', 'y'];
  302. var PROPS_CLIENT_XY = ['clientX', 'clientY'];
  303. /**
  304. * create new input type manager
  305. * @param {Manager} manager
  306. * @param {Function} callback
  307. * @returns {Input}
  308. * @constructor
  309. */
  310. function Input(manager, callback) {
  311. var self = this;
  312. this.manager = manager;
  313. this.callback = callback;
  314. this.element = manager.element;
  315. this.target = manager.options.inputTarget;
  316. // smaller wrapper around the handler, for the scope and the enabled state of the manager,
  317. // so when disabled the input events are completely bypassed.
  318. this.domHandler = function (ev) {
  319. if (boolOrFn(manager.options.enable, [manager])) {
  320. self.handler(ev);
  321. }
  322. };
  323. this.init();
  324. }
  325. Input.prototype = {
  326. /**
  327. * should handle the inputEvent data and trigger the callback
  328. * @virtual
  329. */
  330. handler: function () { },
  331. /**
  332. * bind the events
  333. */
  334. init: function () {
  335. this.evEl && addEventListeners(this.element, this.evEl, this.domHandler);
  336. this.evTarget && addEventListeners(this.target, this.evTarget, this.domHandler);
  337. this.evWin && addEventListeners(getWindowForElement(this.element), this.evWin, this.domHandler);
  338. },
  339. /**
  340. * unbind the events
  341. */
  342. destroy: function () {
  343. this.evEl && removeEventListeners(this.element, this.evEl, this.domHandler);
  344. this.evTarget && removeEventListeners(this.target, this.evTarget, this.domHandler);
  345. this.evWin && removeEventListeners(getWindowForElement(this.element), this.evWin, this.domHandler);
  346. }
  347. };
  348. /**
  349. * create new input type manager
  350. * called by the Manager constructor
  351. * @param {Hammer} manager
  352. * @returns {Input}
  353. */
  354. function createInputInstance(manager) {
  355. var Type;
  356. var inputClass = manager.options.inputClass;
  357. if (inputClass) {
  358. Type = inputClass;
  359. }
  360. else if (SUPPORT_POINTER_EVENTS) {
  361. Type = PointerEventInput;
  362. }
  363. else if (SUPPORT_ONLY_TOUCH) {
  364. Type = TouchInput;
  365. }
  366. else if (!SUPPORT_TOUCH) {
  367. Type = MouseInput;
  368. }
  369. else {
  370. Type = TouchMouseInput;
  371. }
  372. return new (Type)(manager, inputHandler);
  373. }
  374. /**
  375. * handle input events
  376. * @param {Manager} manager
  377. * @param {String} eventType
  378. * @param {Object} input
  379. */
  380. function inputHandler(manager, eventType, input) {
  381. var pointersLen = input.pointers.length;
  382. var changedPointersLen = input.changedPointers.length;
  383. var isFirst = (eventType & INPUT_START && (pointersLen - changedPointersLen === 0));
  384. var isFinal = (eventType & (INPUT_END | INPUT_CANCEL) && (pointersLen - changedPointersLen === 0));
  385. input.isFirst = !!isFirst;
  386. input.isFinal = !!isFinal;
  387. if (isFirst) {
  388. manager.session = {};
  389. }
  390. // source event is the normalized value of the domEvents
  391. // like 'touchstart, mouseup, pointerdown'
  392. input.eventType = eventType;
  393. // compute scale, rotation etc
  394. computeInputData(manager, input);
  395. // emit secret event
  396. manager.emit('hammer.input', input);
  397. manager.recognize(input);
  398. manager.session.prevInput = input;
  399. }
  400. /**
  401. * extend the data with some usable properties like scale, rotate, velocity etc
  402. * @param {Object} manager
  403. * @param {Object} input
  404. */
  405. function computeInputData(manager, input) {
  406. var session = manager.session;
  407. var pointers = input.pointers;
  408. var pointersLength = pointers.length;
  409. // store the first input to calculate the distance and direction
  410. if (!session.firstInput) {
  411. session.firstInput = simpleCloneInputData(input);
  412. }
  413. // to compute scale and rotation we need to store the multiple touches
  414. if (pointersLength > 1 && !session.firstMultiple) {
  415. session.firstMultiple = simpleCloneInputData(input);
  416. }
  417. else if (pointersLength === 1) {
  418. session.firstMultiple = false;
  419. }
  420. var firstInput = session.firstInput;
  421. var firstMultiple = session.firstMultiple;
  422. var offsetCenter = firstMultiple ? firstMultiple.center : firstInput.center;
  423. var center = input.center = getCenter(pointers);
  424. input.timeStamp = now();
  425. input.deltaTime = input.timeStamp - firstInput.timeStamp;
  426. input.angle = getAngle(offsetCenter, center);
  427. input.distance = getDistance(offsetCenter, center);
  428. computeDeltaXY(session, input);
  429. input.offsetDirection = getDirection(input.deltaX, input.deltaY);
  430. var overallVelocity = getVelocity(input.deltaTime, input.deltaX, input.deltaY);
  431. input.overallVelocityX = overallVelocity.x;
  432. input.overallVelocityY = overallVelocity.y;
  433. input.overallVelocity = (abs(overallVelocity.x) > abs(overallVelocity.y)) ? overallVelocity.x : overallVelocity.y;
  434. input.scale = firstMultiple ? getScale(firstMultiple.pointers, pointers) : 1;
  435. input.rotation = firstMultiple ? getRotation(firstMultiple.pointers, pointers) : 0;
  436. input.maxPointers = !session.prevInput ? input.pointers.length : ((input.pointers.length >
  437. session.prevInput.maxPointers) ? input.pointers.length : session.prevInput.maxPointers);
  438. computeIntervalInputData(session, input);
  439. // find the correct target
  440. var target = manager.element;
  441. if (hasParent(input.srcEvent.target, target)) {
  442. target = input.srcEvent.target;
  443. }
  444. input.target = target;
  445. }
  446. function computeDeltaXY(session, input) {
  447. var center = input.center;
  448. var offset = session.offsetDelta || {};
  449. var prevDelta = session.prevDelta || {};
  450. var prevInput = session.prevInput || {};
  451. if (input.eventType === INPUT_START || prevInput.eventType === INPUT_END) {
  452. prevDelta = session.prevDelta = {
  453. x: prevInput.deltaX || 0,
  454. y: prevInput.deltaY || 0
  455. };
  456. offset = session.offsetDelta = {
  457. x: center.x,
  458. y: center.y
  459. };
  460. }
  461. input.deltaX = prevDelta.x + (center.x - offset.x);
  462. input.deltaY = prevDelta.y + (center.y - offset.y);
  463. }
  464. /**
  465. * velocity is calculated every x ms
  466. * @param {Object} session
  467. * @param {Object} input
  468. */
  469. function computeIntervalInputData(session, input) {
  470. var last = session.lastInterval || input, deltaTime = input.timeStamp - last.timeStamp, velocity, velocityX, velocityY, direction;
  471. if (input.eventType != INPUT_CANCEL && (deltaTime > COMPUTE_INTERVAL || last.velocity === undefined)) {
  472. var deltaX = input.deltaX - last.deltaX;
  473. var deltaY = input.deltaY - last.deltaY;
  474. var v = getVelocity(deltaTime, deltaX, deltaY);
  475. velocityX = v.x;
  476. velocityY = v.y;
  477. velocity = (abs(v.x) > abs(v.y)) ? v.x : v.y;
  478. direction = getDirection(deltaX, deltaY);
  479. session.lastInterval = input;
  480. }
  481. else {
  482. // use latest velocity info if it doesn't overtake a minimum period
  483. velocity = last.velocity;
  484. velocityX = last.velocityX;
  485. velocityY = last.velocityY;
  486. direction = last.direction;
  487. }
  488. input.velocity = velocity;
  489. input.velocityX = velocityX;
  490. input.velocityY = velocityY;
  491. input.direction = direction;
  492. }
  493. /**
  494. * create a simple clone from the input used for storage of firstInput and firstMultiple
  495. * @param {Object} input
  496. * @returns {Object} clonedInputData
  497. */
  498. function simpleCloneInputData(input) {
  499. // make a simple copy of the pointers because we will get a reference if we don't
  500. // we only need clientXY for the calculations
  501. var pointers = [];
  502. var i = 0;
  503. while (i < input.pointers.length) {
  504. pointers[i] = {
  505. clientX: round(input.pointers[i].clientX),
  506. clientY: round(input.pointers[i].clientY)
  507. };
  508. i++;
  509. }
  510. return {
  511. timeStamp: now(),
  512. pointers: pointers,
  513. center: getCenter(pointers),
  514. deltaX: input.deltaX,
  515. deltaY: input.deltaY
  516. };
  517. }
  518. /**
  519. * get the center of all the pointers
  520. * @param {Array} pointers
  521. * @return {Object} center contains `x` and `y` properties
  522. */
  523. function getCenter(pointers) {
  524. var pointersLength = pointers.length;
  525. // no need to loop when only one touch
  526. if (pointersLength === 1) {
  527. return {
  528. x: round(pointers[0].clientX),
  529. y: round(pointers[0].clientY)
  530. };
  531. }
  532. var x = 0, y = 0, i = 0;
  533. while (i < pointersLength) {
  534. x += pointers[i].clientX;
  535. y += pointers[i].clientY;
  536. i++;
  537. }
  538. return {
  539. x: round(x / pointersLength),
  540. y: round(y / pointersLength)
  541. };
  542. }
  543. /**
  544. * calculate the velocity between two points. unit is in px per ms.
  545. * @param {Number} deltaTime
  546. * @param {Number} x
  547. * @param {Number} y
  548. * @return {Object} velocity `x` and `y`
  549. */
  550. function getVelocity(deltaTime, x, y) {
  551. return {
  552. x: x / deltaTime || 0,
  553. y: y / deltaTime || 0
  554. };
  555. }
  556. /**
  557. * get the direction between two points
  558. * @param {Number} x
  559. * @param {Number} y
  560. * @return {Number} direction
  561. */
  562. function getDirection(x, y) {
  563. if (x === y) {
  564. return DIRECTION_NONE;
  565. }
  566. if (abs(x) >= abs(y)) {
  567. return x < 0 ? exports.DIRECTION_LEFT : exports.DIRECTION_RIGHT;
  568. }
  569. return y < 0 ? DIRECTION_UP : DIRECTION_DOWN;
  570. }
  571. /**
  572. * calculate the absolute distance between two points
  573. * @param {Object} p1 {x, y}
  574. * @param {Object} p2 {x, y}
  575. * @param {Array} [props] containing x and y keys
  576. * @return {Number} distance
  577. */
  578. function getDistance(p1, p2, props) {
  579. if (!props) {
  580. props = PROPS_XY;
  581. }
  582. var x = p2[props[0]] - p1[props[0]], y = p2[props[1]] - p1[props[1]];
  583. return Math.sqrt((x * x) + (y * y));
  584. }
  585. /**
  586. * calculate the angle between two coordinates
  587. * @param {Object} p1
  588. * @param {Object} p2
  589. * @param {Array} [props] containing x and y keys
  590. * @return {Number} angle
  591. */
  592. function getAngle(p1, p2, props) {
  593. if (!props) {
  594. props = PROPS_XY;
  595. }
  596. var x = p2[props[0]] - p1[props[0]], y = p2[props[1]] - p1[props[1]];
  597. return Math.atan2(y, x) * 180 / Math.PI;
  598. }
  599. /**
  600. * calculate the rotation degrees between two pointersets
  601. * @param {Array} start array of pointers
  602. * @param {Array} end array of pointers
  603. * @return {Number} rotation
  604. */
  605. function getRotation(start, end) {
  606. return getAngle(end[1], end[0], PROPS_CLIENT_XY) + getAngle(start[1], start[0], PROPS_CLIENT_XY);
  607. }
  608. /**
  609. * calculate the scale factor between two pointersets
  610. * no scale is 1, and goes down to 0 when pinched together, and bigger when pinched out
  611. * @param {Array} start array of pointers
  612. * @param {Array} end array of pointers
  613. * @return {Number} scale
  614. */
  615. function getScale(start, end) {
  616. return getDistance(end[0], end[1], PROPS_CLIENT_XY) / getDistance(start[0], start[1], PROPS_CLIENT_XY);
  617. }
  618. var MOUSE_INPUT_MAP = {
  619. mousedown: INPUT_START,
  620. mousemove: INPUT_MOVE,
  621. mouseup: INPUT_END
  622. };
  623. var MOUSE_ELEMENT_EVENTS = 'mousedown';
  624. var MOUSE_WINDOW_EVENTS = 'mousemove mouseup';
  625. /**
  626. * Mouse events input
  627. * @constructor
  628. * @extends Input
  629. */
  630. function MouseInput(_manager, _handler) {
  631. this.evEl = MOUSE_ELEMENT_EVENTS;
  632. this.evWin = MOUSE_WINDOW_EVENTS;
  633. this.allow = true; // used by Input.TouchMouse to disable mouse events
  634. this.pressed = false; // mousedown state
  635. Input.apply(this, arguments);
  636. }
  637. inherit(MouseInput, Input, {
  638. /**
  639. * handle mouse events
  640. * @param {Object} ev
  641. */
  642. handler: function MEhandler(ev) {
  643. var eventType = MOUSE_INPUT_MAP[ev.type];
  644. // on start we want to have the left mouse button down
  645. if (eventType & INPUT_START && ev.button === 0) {
  646. this.pressed = true;
  647. }
  648. if (eventType & INPUT_MOVE && ev.which !== 1) {
  649. eventType = INPUT_END;
  650. }
  651. // mouse must be down, and mouse events are allowed (see the TouchMouse input)
  652. if (!this.pressed || !this.allow) {
  653. return;
  654. }
  655. if (eventType & INPUT_END) {
  656. this.pressed = false;
  657. }
  658. this.callback(this.manager, eventType, {
  659. pointers: [ev],
  660. changedPointers: [ev],
  661. pointerType: INPUT_TYPE_MOUSE,
  662. srcEvent: ev
  663. });
  664. }
  665. });
  666. var POINTER_INPUT_MAP = {
  667. pointerdown: INPUT_START,
  668. pointermove: INPUT_MOVE,
  669. pointerup: INPUT_END,
  670. pointercancel: INPUT_CANCEL,
  671. pointerout: INPUT_CANCEL
  672. };
  673. // in IE10 the pointer types is defined as an enum
  674. var IE10_POINTER_TYPE_ENUM = {
  675. 2: INPUT_TYPE_TOUCH,
  676. 3: INPUT_TYPE_PEN,
  677. 4: INPUT_TYPE_MOUSE,
  678. 5: INPUT_TYPE_KINECT // see https://twitter.com/jacobrossi/status/480596438489890816
  679. };
  680. var POINTER_ELEMENT_EVENTS = 'pointerdown';
  681. var POINTER_WINDOW_EVENTS = 'pointermove pointerup pointercancel';
  682. // IE10 has prefixed support, and case-sensitive
  683. if (win.MSPointerEvent && !win.PointerEvent) {
  684. POINTER_ELEMENT_EVENTS = 'MSPointerDown';
  685. POINTER_WINDOW_EVENTS = 'MSPointerMove MSPointerUp MSPointerCancel';
  686. }
  687. /**
  688. * Pointer events input
  689. * @constructor
  690. * @extends Input
  691. */
  692. function PointerEventInput() {
  693. this.evEl = POINTER_ELEMENT_EVENTS;
  694. this.evWin = POINTER_WINDOW_EVENTS;
  695. Input.apply(this, arguments);
  696. this.store = (this.manager.session.pointerEvents = []);
  697. }
  698. inherit(PointerEventInput, Input, {
  699. /**
  700. * handle mouse events
  701. * @param {Object} ev
  702. */
  703. handler: function PEhandler(ev) {
  704. var store = this.store;
  705. var removePointer = false;
  706. var eventTypeNormalized = ev.type.toLowerCase().replace('ms', '');
  707. var eventType = POINTER_INPUT_MAP[eventTypeNormalized];
  708. var pointerType = IE10_POINTER_TYPE_ENUM[ev.pointerType] || ev.pointerType;
  709. var isTouch = (pointerType == INPUT_TYPE_TOUCH);
  710. // get index of the event in the store
  711. var storeIndex = inArray(store, ev.pointerId, 'pointerId');
  712. // start and mouse must be down
  713. if (eventType & INPUT_START && (ev.button === 0 || isTouch)) {
  714. if (storeIndex < 0) {
  715. store.push(ev);
  716. storeIndex = store.length - 1;
  717. }
  718. }
  719. else if (eventType & (INPUT_END | INPUT_CANCEL)) {
  720. removePointer = true;
  721. }
  722. // it not found, so the pointer hasn't been down (so it's probably a hover)
  723. if (storeIndex < 0) {
  724. return;
  725. }
  726. // update the event in the store
  727. store[storeIndex] = ev;
  728. this.callback(this.manager, eventType, {
  729. pointers: store,
  730. changedPointers: [ev],
  731. pointerType: pointerType,
  732. srcEvent: ev
  733. });
  734. if (removePointer) {
  735. // remove from the store
  736. store.splice(storeIndex, 1);
  737. }
  738. }
  739. });
  740. var SINGLE_TOUCH_INPUT_MAP = {
  741. touchstart: INPUT_START,
  742. touchmove: INPUT_MOVE,
  743. touchend: INPUT_END,
  744. touchcancel: INPUT_CANCEL
  745. };
  746. var SINGLE_TOUCH_TARGET_EVENTS = 'touchstart';
  747. var SINGLE_TOUCH_WINDOW_EVENTS = 'touchstart touchmove touchend touchcancel';
  748. /**
  749. * Touch events input
  750. * @constructor
  751. * @extends Input
  752. */
  753. function SingleTouchInput() {
  754. this.evTarget = SINGLE_TOUCH_TARGET_EVENTS;
  755. this.evWin = SINGLE_TOUCH_WINDOW_EVENTS;
  756. this.started = false;
  757. Input.apply(this, arguments);
  758. }
  759. inherit(SingleTouchInput, Input, {
  760. handler: function TEhandler(ev) {
  761. var type = SINGLE_TOUCH_INPUT_MAP[ev.type];
  762. // should we handle the touch events?
  763. if (type === INPUT_START) {
  764. this.started = true;
  765. }
  766. if (!this.started) {
  767. return;
  768. }
  769. var touches = normalizeSingleTouches.call(this, ev, type);
  770. // when done, reset the started state
  771. if (type & (INPUT_END | INPUT_CANCEL) && touches[0].length - touches[1].length === 0) {
  772. this.started = false;
  773. }
  774. this.callback(this.manager, type, {
  775. pointers: touches[0],
  776. changedPointers: touches[1],
  777. pointerType: INPUT_TYPE_TOUCH,
  778. srcEvent: ev
  779. });
  780. }
  781. });
  782. /**
  783. * @this {TouchInput}
  784. * @param {Object} ev
  785. * @param {Number} type flag
  786. * @returns {undefined|Array} [all, changed]
  787. */
  788. function normalizeSingleTouches(ev, type) {
  789. var all = toArray(ev.touches);
  790. var changed = toArray(ev.changedTouches);
  791. if (type & (INPUT_END | INPUT_CANCEL)) {
  792. all = uniqueArray(all.concat(changed), 'identifier', true);
  793. }
  794. return [all, changed];
  795. }
  796. var TOUCH_INPUT_MAP = {
  797. touchstart: INPUT_START,
  798. touchmove: INPUT_MOVE,
  799. touchend: INPUT_END,
  800. touchcancel: INPUT_CANCEL
  801. };
  802. var TOUCH_TARGET_EVENTS = 'touchstart touchmove touchend touchcancel';
  803. /**
  804. * Multi-user touch events input
  805. * @constructor
  806. * @extends Input
  807. */
  808. function TouchInput(_manager, _handler) {
  809. this.evTarget = TOUCH_TARGET_EVENTS;
  810. this.targetIds = {};
  811. Input.apply(this, arguments);
  812. }
  813. inherit(TouchInput, Input, {
  814. handler: function MTEhandler(ev) {
  815. var type = TOUCH_INPUT_MAP[ev.type];
  816. var touches = getTouches.call(this, ev, type);
  817. if (!touches) {
  818. return;
  819. }
  820. this.callback(this.manager, type, {
  821. pointers: touches[0],
  822. changedPointers: touches[1],
  823. pointerType: INPUT_TYPE_TOUCH,
  824. srcEvent: ev
  825. });
  826. }
  827. });
  828. /**
  829. * @this {TouchInput}
  830. * @param {Object} ev
  831. * @param {Number} type flag
  832. * @returns {undefined|Array} [all, changed]
  833. */
  834. function getTouches(ev, type) {
  835. var allTouches = toArray(ev.touches);
  836. var targetIds = this.targetIds;
  837. // when there is only one touch, the process can be simplified
  838. if (type & (INPUT_START | INPUT_MOVE) && allTouches.length === 1) {
  839. targetIds[allTouches[0].identifier] = true;
  840. return [allTouches, allTouches];
  841. }
  842. var i, targetTouches, changedTouches = toArray(ev.changedTouches), changedTargetTouches = [], target = this.target;
  843. // get target touches from touches
  844. targetTouches = allTouches.filter(function (touch) {
  845. return hasParent(touch.target, target);
  846. });
  847. // collect touches
  848. if (type === INPUT_START) {
  849. i = 0;
  850. while (i < targetTouches.length) {
  851. targetIds[targetTouches[i].identifier] = true;
  852. i++;
  853. }
  854. }
  855. // filter changed touches to only contain touches that exist in the collected target ids
  856. i = 0;
  857. while (i < changedTouches.length) {
  858. if (targetIds[changedTouches[i].identifier]) {
  859. changedTargetTouches.push(changedTouches[i]);
  860. }
  861. // cleanup removed touches
  862. if (type & (INPUT_END | INPUT_CANCEL)) {
  863. delete targetIds[changedTouches[i].identifier];
  864. }
  865. i++;
  866. }
  867. if (!changedTargetTouches.length) {
  868. return;
  869. }
  870. return [
  871. // merge targetTouches with changedTargetTouches so it contains ALL touches, including 'end' and 'cancel'
  872. uniqueArray(targetTouches.concat(changedTargetTouches), 'identifier', true),
  873. changedTargetTouches
  874. ];
  875. }
  876. /**
  877. * Combined touch and mouse input
  878. *
  879. * Touch has a higher priority then mouse, and while touching no mouse events are allowed.
  880. * This because touch devices also emit mouse events while doing a touch.
  881. *
  882. * @constructor
  883. * @extends Input
  884. */
  885. function TouchMouseInput() {
  886. Input.apply(this, arguments);
  887. var handler = bindFn(this.handler, this);
  888. this.touch = new TouchInput(this.manager, handler);
  889. this.mouse = new MouseInput(this.manager, handler);
  890. }
  891. inherit(TouchMouseInput, Input, {
  892. /**
  893. * handle mouse and touch events
  894. * @param {Hammer} manager
  895. * @param {String} inputEvent
  896. * @param {Object} inputData
  897. */
  898. handler: function TMEhandler(manager, inputEvent, inputData) {
  899. var isTouch = (inputData.pointerType == INPUT_TYPE_TOUCH), isMouse = (inputData.pointerType == INPUT_TYPE_MOUSE);
  900. // when we're in a touch event, so block all upcoming mouse events
  901. // most mobile browser also emit mouseevents, right after touchstart
  902. if (isTouch) {
  903. this.mouse.allow = false;
  904. }
  905. else if (isMouse && !this.mouse.allow) {
  906. return;
  907. }
  908. // reset the allowMouse when we're done
  909. if (inputEvent & (INPUT_END | INPUT_CANCEL)) {
  910. this.mouse.allow = true;
  911. }
  912. this.callback(manager, inputEvent, inputData);
  913. },
  914. /**
  915. * remove the event listeners
  916. */
  917. destroy: function destroy() {
  918. this.touch.destroy();
  919. this.mouse.destroy();
  920. }
  921. });
  922. var PREFIXED_TOUCH_ACTION = prefixed(TEST_ELEMENT.style, 'touchAction');
  923. var NATIVE_TOUCH_ACTION = PREFIXED_TOUCH_ACTION !== undefined;
  924. // magical touchAction value
  925. var TOUCH_ACTION_COMPUTE = 'compute';
  926. var TOUCH_ACTION_AUTO = 'auto';
  927. var TOUCH_ACTION_MANIPULATION = 'manipulation'; // not implemented
  928. var TOUCH_ACTION_NONE = 'none';
  929. var TOUCH_ACTION_PAN_X = 'pan-x';
  930. var TOUCH_ACTION_PAN_Y = 'pan-y';
  931. /**
  932. * Touch Action
  933. * sets the touchAction property or uses the js alternative
  934. * @param {Manager} manager
  935. * @param {String} value
  936. * @constructor
  937. */
  938. function TouchAction(manager, value) {
  939. this.manager = manager;
  940. this.set(value);
  941. }
  942. TouchAction.prototype = {
  943. /**
  944. * set the touchAction value on the element or enable the polyfill
  945. * @param {String} value
  946. */
  947. set: function (value) {
  948. // find out the touch-action by the event handlers
  949. if (value == TOUCH_ACTION_COMPUTE) {
  950. value = this.compute();
  951. }
  952. if (NATIVE_TOUCH_ACTION && this.manager.element.style) {
  953. this.manager.element.style[PREFIXED_TOUCH_ACTION] = value;
  954. }
  955. this.actions = value.toLowerCase().trim();
  956. },
  957. /**
  958. * just re-set the touchAction value
  959. */
  960. update: function () {
  961. this.set(this.manager.options.touchAction);
  962. },
  963. /**
  964. * compute the value for the touchAction property based on the recognizer's settings
  965. * @returns {String} value
  966. */
  967. compute: function () {
  968. var actions = [];
  969. each(this.manager.recognizers, function (recognizer) {
  970. if (boolOrFn(recognizer.options.enable, [recognizer])) {
  971. actions = actions.concat(recognizer.getTouchAction());
  972. }
  973. });
  974. return cleanTouchActions(actions.join(' '));
  975. },
  976. /**
  977. * this method is called on each input cycle and provides the preventing of the browser behavior
  978. * @param {Object} input
  979. */
  980. preventDefaults: function (input) {
  981. // not needed with native support for the touchAction property
  982. if (NATIVE_TOUCH_ACTION) {
  983. return;
  984. }
  985. var srcEvent = input.srcEvent;
  986. var direction = input.offsetDirection;
  987. // if the touch action did prevented once this session
  988. if (this.manager.session.prevented) {
  989. srcEvent.preventDefault();
  990. return;
  991. }
  992. var actions = this.actions;
  993. var hasNone = inStr(actions, TOUCH_ACTION_NONE);
  994. var hasPanY = inStr(actions, TOUCH_ACTION_PAN_Y);
  995. var hasPanX = inStr(actions, TOUCH_ACTION_PAN_X);
  996. if (hasNone) {
  997. //do not prevent defaults if this is a tap gesture
  998. var isTapPointer = input.pointers.length === 1;
  999. var isTapMovement = input.distance < 2;
  1000. var isTapTouchTime = input.deltaTime < 250;
  1001. if (isTapPointer && isTapMovement && isTapTouchTime) {
  1002. return;
  1003. }
  1004. }
  1005. if (hasPanX && hasPanY) {
  1006. // `pan-x pan-y` means browser handles all scrolling/panning, do not prevent
  1007. return;
  1008. }
  1009. if (hasNone ||
  1010. (hasPanY && direction & exports.DIRECTION_HORIZONTAL) ||
  1011. (hasPanX && direction & exports.DIRECTION_VERTICAL)) {
  1012. return this.preventSrc(srcEvent);
  1013. }
  1014. },
  1015. /**
  1016. * call preventDefault to prevent the browser's default behavior (scrolling in most cases)
  1017. * @param {Object} srcEvent
  1018. */
  1019. preventSrc: function (srcEvent) {
  1020. this.manager.session.prevented = true;
  1021. srcEvent.preventDefault();
  1022. }
  1023. };
  1024. /**
  1025. * when the touchActions are collected they are not a valid value, so we need to clean things up. *
  1026. * @param {String} actions
  1027. * @returns {*}
  1028. */
  1029. function cleanTouchActions(actions) {
  1030. // none
  1031. if (inStr(actions, TOUCH_ACTION_NONE)) {
  1032. return TOUCH_ACTION_NONE;
  1033. }
  1034. var hasPanX = inStr(actions, TOUCH_ACTION_PAN_X);
  1035. var hasPanY = inStr(actions, TOUCH_ACTION_PAN_Y);
  1036. // if both pan-x and pan-y are set (different recognizers
  1037. // for different directions, e.g. horizontal pan but vertical swipe?)
  1038. // we need none (as otherwise with pan-x pan-y combined none of these
  1039. // recognizers will work, since the browser would handle all panning
  1040. if (hasPanX && hasPanY) {
  1041. return TOUCH_ACTION_NONE;
  1042. }
  1043. // pan-x OR pan-y
  1044. if (hasPanX || hasPanY) {
  1045. return hasPanX ? TOUCH_ACTION_PAN_X : TOUCH_ACTION_PAN_Y;
  1046. }
  1047. // manipulation
  1048. if (inStr(actions, TOUCH_ACTION_MANIPULATION)) {
  1049. return TOUCH_ACTION_MANIPULATION;
  1050. }
  1051. return TOUCH_ACTION_AUTO;
  1052. }
  1053. /**
  1054. * Recognizer flow explained; *
  1055. * All recognizers have the initial state of POSSIBLE when a input session starts.
  1056. * The definition of a input session is from the first input until the last input, with all it's movement in it. *
  1057. * Example session for mouse-input: mousedown -> mousemove -> mouseup
  1058. *
  1059. * On each recognizing cycle (see Manager.recognize) the .recognize() method is executed
  1060. * which determines with state it should be.
  1061. *
  1062. * If the recognizer has the state FAILED, CANCELLED or RECOGNIZED (equals ENDED), it is reset to
  1063. * POSSIBLE to give it another change on the next cycle.
  1064. *
  1065. * Possible
  1066. * |
  1067. * +-----+---------------+
  1068. * | |
  1069. * +-----+-----+ |
  1070. * | | |
  1071. * Failed Cancelled |
  1072. * +-------+------+
  1073. * | |
  1074. * Recognized Began
  1075. * |
  1076. * Changed
  1077. * |
  1078. * Ended/Recognized
  1079. */
  1080. var STATE_POSSIBLE = 1;
  1081. var STATE_BEGAN = 2;
  1082. var STATE_CHANGED = 4;
  1083. var STATE_ENDED = 8;
  1084. var STATE_RECOGNIZED = STATE_ENDED;
  1085. var STATE_CANCELLED = 16;
  1086. var STATE_FAILED = 32;
  1087. /**
  1088. * Recognizer
  1089. * Every recognizer needs to extend from this class.
  1090. * @constructor
  1091. * @param {Object} options
  1092. */
  1093. function Recognizer(options) {
  1094. this.options = Object.assign({}, this.defaults, options || {});
  1095. this.id = uniqueId();
  1096. this.manager = null;
  1097. // default is enable true
  1098. this.options.enable = ifUndefined(this.options.enable, true);
  1099. this.state = STATE_POSSIBLE;
  1100. this.simultaneous = {};
  1101. this.requireFail = [];
  1102. }
  1103. Recognizer.prototype = {
  1104. /**
  1105. * @virtual
  1106. * @type {Object}
  1107. */
  1108. defaults: {},
  1109. /**
  1110. * set options
  1111. * @param {Object} options
  1112. * @return {Recognizer}
  1113. */
  1114. set: function (options) {
  1115. Object.assign(this.options, options);
  1116. // also update the touchAction, in case something changed about the directions/enabled state
  1117. this.manager && this.manager.touchAction.update();
  1118. return this;
  1119. },
  1120. /**
  1121. * recognize simultaneous with an other recognizer.
  1122. * @param {Recognizer} otherRecognizer
  1123. * @returns {Recognizer} this
  1124. */
  1125. recognizeWith: function (otherRecognizer) {
  1126. if (invokeArrayArg(otherRecognizer, 'recognizeWith', this)) {
  1127. return this;
  1128. }
  1129. var simultaneous = this.simultaneous;
  1130. otherRecognizer = getRecognizerByNameIfManager(otherRecognizer, this);
  1131. if (!simultaneous[otherRecognizer.id]) {
  1132. simultaneous[otherRecognizer.id] = otherRecognizer;
  1133. otherRecognizer.recognizeWith(this);
  1134. }
  1135. return this;
  1136. },
  1137. /**
  1138. * drop the simultaneous link. it doesnt remove the link on the other recognizer.
  1139. * @param {Recognizer} otherRecognizer
  1140. * @returns {Recognizer} this
  1141. */
  1142. dropRecognizeWith: function (otherRecognizer) {
  1143. if (invokeArrayArg(otherRecognizer, 'dropRecognizeWith', this)) {
  1144. return this;
  1145. }
  1146. otherRecognizer = getRecognizerByNameIfManager(otherRecognizer, this);
  1147. delete this.simultaneous[otherRecognizer.id];
  1148. return this;
  1149. },
  1150. /**
  1151. * recognizer can only run when an other is failing
  1152. * @param {Recognizer} otherRecognizer
  1153. * @returns {Recognizer} this
  1154. */
  1155. requireFailure: function (otherRecognizer) {
  1156. if (invokeArrayArg(otherRecognizer, 'requireFailure', this)) {
  1157. return this;
  1158. }
  1159. var requireFail = this.requireFail;
  1160. otherRecognizer = getRecognizerByNameIfManager(otherRecognizer, this);
  1161. if (inArray(requireFail, otherRecognizer) === -1) {
  1162. requireFail.push(otherRecognizer);
  1163. otherRecognizer.requireFailure(this);
  1164. }
  1165. return this;
  1166. },
  1167. /**
  1168. * drop the requireFailure link. it does not remove the link on the other recognizer.
  1169. * @param {Recognizer} otherRecognizer
  1170. * @returns {Recognizer} this
  1171. */
  1172. dropRequireFailure: function (otherRecognizer) {
  1173. if (invokeArrayArg(otherRecognizer, 'dropRequireFailure', this)) {
  1174. return this;
  1175. }
  1176. otherRecognizer = getRecognizerByNameIfManager(otherRecognizer, this);
  1177. var index = inArray(this.requireFail, otherRecognizer);
  1178. if (index > -1) {
  1179. this.requireFail.splice(index, 1);
  1180. }
  1181. return this;
  1182. },
  1183. /**
  1184. * has require failures boolean
  1185. * @returns {boolean}
  1186. */
  1187. hasRequireFailures: function () {
  1188. return this.requireFail.length > 0;
  1189. },
  1190. /**
  1191. * if the recognizer can recognize simultaneous with an other recognizer
  1192. * @param {Recognizer} otherRecognizer
  1193. * @returns {Boolean}
  1194. */
  1195. canRecognizeWith: function (otherRecognizer) {
  1196. return !!this.simultaneous[otherRecognizer.id];
  1197. },
  1198. /**
  1199. * You should use `tryEmit` instead of `emit` directly to check
  1200. * that all the needed recognizers has failed before emitting.
  1201. * @param {Object} input
  1202. */
  1203. emit: function (input) {
  1204. var self = this;
  1205. var state = this.state;
  1206. function emit(event) {
  1207. self.manager.emit(event, input);
  1208. }
  1209. // 'panstart' and 'panmove'
  1210. if (state < STATE_ENDED) {
  1211. emit(self.options.event + stateStr(state));
  1212. }
  1213. emit(self.options.event); // simple 'eventName' events
  1214. if (input.additionalEvent) {
  1215. emit(input.additionalEvent);
  1216. }
  1217. // panend and pancancel
  1218. if (state >= STATE_ENDED) {
  1219. emit(self.options.event + stateStr(state));
  1220. }
  1221. },
  1222. /**
  1223. * Check that all the require failure recognizers has failed,
  1224. * if true, it emits a gesture event,
  1225. * otherwise, setup the state to FAILED.
  1226. * @param {Object} input
  1227. */
  1228. tryEmit: function (input) {
  1229. if (this.canEmit()) {
  1230. return this.emit(input);
  1231. }
  1232. // it's failing anyway
  1233. this.state = STATE_FAILED;
  1234. },
  1235. /**
  1236. * can we emit?
  1237. * @returns {boolean}
  1238. */
  1239. canEmit: function () {
  1240. var i = 0;
  1241. while (i < this.requireFail.length) {
  1242. if (!(this.requireFail[i].state & (STATE_FAILED | STATE_POSSIBLE))) {
  1243. return false;
  1244. }
  1245. i++;
  1246. }
  1247. return true;
  1248. },
  1249. /**
  1250. * update the recognizer
  1251. * @param {Object} inputData
  1252. */
  1253. recognize: function (inputData) {
  1254. // make a new copy of the inputData
  1255. // so we can change the inputData without messing up the other recognizers
  1256. var inputDataClone = Object.assign({}, inputData);
  1257. // is is enabled and allow recognizing?
  1258. if (!boolOrFn(this.options.enable, [this, inputDataClone])) {
  1259. this.reset();
  1260. this.state = STATE_FAILED;
  1261. return;
  1262. }
  1263. // reset when we've reached the end
  1264. if (this.state & (STATE_RECOGNIZED | STATE_CANCELLED | STATE_FAILED)) {
  1265. this.state = STATE_POSSIBLE;
  1266. }
  1267. this.state = this.process(inputDataClone);
  1268. // the recognizer has recognized a gesture
  1269. // so trigger an event
  1270. if (this.state & (STATE_BEGAN | STATE_CHANGED | STATE_ENDED | STATE_CANCELLED)) {
  1271. this.tryEmit(inputDataClone);
  1272. }
  1273. },
  1274. /**
  1275. * return the state of the recognizer
  1276. * the actual recognizing happens in this method
  1277. * @virtual
  1278. * @param {Object} inputData
  1279. * @returns {Const} STATE
  1280. */
  1281. process: function (_inputData) { },
  1282. /**
  1283. * return the preferred touch-action
  1284. * @virtual
  1285. * @returns {Array}
  1286. */
  1287. getTouchAction: function () { },
  1288. /**
  1289. * called when the gesture isn't allowed to recognize
  1290. * like when another is being recognized or it is disabled
  1291. * @virtual
  1292. */
  1293. reset: function () { }
  1294. };
  1295. /**
  1296. * get a usable string, used as event postfix
  1297. * @param {Const} state
  1298. * @returns {String} state
  1299. */
  1300. function stateStr(state) {
  1301. if (state & STATE_CANCELLED) {
  1302. return 'cancel';
  1303. }
  1304. else if (state & STATE_ENDED) {
  1305. return 'end';
  1306. }
  1307. else if (state & STATE_CHANGED) {
  1308. return 'move';
  1309. }
  1310. else if (state & STATE_BEGAN) {
  1311. return 'start';
  1312. }
  1313. return '';
  1314. }
  1315. /**
  1316. * direction cons to string
  1317. * @param {Const} direction
  1318. * @returns {String}
  1319. */
  1320. function directionStr(direction) {
  1321. if (direction == DIRECTION_DOWN) {
  1322. return 'down';
  1323. }
  1324. else if (direction == DIRECTION_UP) {
  1325. return 'up';
  1326. }
  1327. else if (direction == exports.DIRECTION_LEFT) {
  1328. return 'left';
  1329. }
  1330. else if (direction == exports.DIRECTION_RIGHT) {
  1331. return 'right';
  1332. }
  1333. return '';
  1334. }
  1335. /**
  1336. * get a recognizer by name if it is bound to a manager
  1337. * @param {Recognizer|String} otherRecognizer
  1338. * @param {Recognizer} recognizer
  1339. * @returns {Recognizer}
  1340. */
  1341. function getRecognizerByNameIfManager(otherRecognizer, recognizer) {
  1342. var manager = recognizer.manager;
  1343. if (manager) {
  1344. return manager.get(otherRecognizer);
  1345. }
  1346. return otherRecognizer;
  1347. }
  1348. /**
  1349. * This recognizer is just used as a base for the simple attribute recognizers.
  1350. * @constructor
  1351. * @extends Recognizer
  1352. */
  1353. function AttrRecognizer() {
  1354. Recognizer.apply(this, arguments);
  1355. }
  1356. inherit(AttrRecognizer, Recognizer, {
  1357. /**
  1358. * @namespace
  1359. * @memberof AttrRecognizer
  1360. */
  1361. defaults: {
  1362. /**
  1363. * @type {Number}
  1364. * @default 1
  1365. */
  1366. pointers: 1
  1367. },
  1368. /**
  1369. * Used to check if it the recognizer receives valid input, like input.distance > 10.
  1370. * @memberof AttrRecognizer
  1371. * @param {Object} input
  1372. * @returns {Boolean} recognized
  1373. */
  1374. attrTest: function (input) {
  1375. var optionPointers = this.options.pointers;
  1376. return optionPointers === 0 || input.pointers.length === optionPointers;
  1377. },
  1378. /**
  1379. * Process the input and return the state for the recognizer
  1380. * @memberof AttrRecognizer
  1381. * @param {Object} input
  1382. * @returns {*} State
  1383. */
  1384. process: function (input) {
  1385. var state = this.state;
  1386. var eventType = input.eventType;
  1387. var isRecognized = state & (STATE_BEGAN | STATE_CHANGED);
  1388. var isValid = this.attrTest(input);
  1389. // on cancel input and we've recognized before, return STATE_CANCELLED
  1390. if (isRecognized && (eventType & INPUT_CANCEL || !isValid)) {
  1391. return state | STATE_CANCELLED;
  1392. }
  1393. else if (isRecognized || isValid) {
  1394. if (eventType & INPUT_END) {
  1395. return state | STATE_ENDED;
  1396. }
  1397. else if (!(state & STATE_BEGAN)) {
  1398. return STATE_BEGAN;
  1399. }
  1400. return state | STATE_CHANGED;
  1401. }
  1402. return STATE_FAILED;
  1403. }
  1404. });
  1405. /**
  1406. * Pan
  1407. * Recognized when the pointer is down and moved in the allowed direction.
  1408. * @constructor
  1409. * @extends AttrRecognizer
  1410. */
  1411. function PanRecognizer() {
  1412. AttrRecognizer.apply(this, arguments);
  1413. this.pX = null;
  1414. this.pY = null;
  1415. }
  1416. inherit(PanRecognizer, AttrRecognizer, {
  1417. /**
  1418. * @namespace
  1419. * @memberof PanRecognizer
  1420. */
  1421. defaults: {
  1422. event: 'pan',
  1423. threshold: 10,
  1424. pointers: 1,
  1425. direction: DIRECTION_ALL
  1426. },
  1427. getTouchAction: function () {
  1428. var direction = this.options.direction;
  1429. var actions = [];
  1430. if (direction & exports.DIRECTION_HORIZONTAL) {
  1431. actions.push(TOUCH_ACTION_PAN_Y);
  1432. }
  1433. if (direction & exports.DIRECTION_VERTICAL) {
  1434. actions.push(TOUCH_ACTION_PAN_X);
  1435. }
  1436. return actions;
  1437. },
  1438. directionTest: function (input) {
  1439. var options = this.options;
  1440. var hasMoved = true;
  1441. var distance = input.distance;
  1442. var direction = input.direction;
  1443. var x = input.deltaX;
  1444. var y = input.deltaY;
  1445. // lock to axis?
  1446. if (!(direction & options.direction)) {
  1447. if (options.direction & exports.DIRECTION_HORIZONTAL) {
  1448. direction = (x === 0) ? DIRECTION_NONE : (x < 0) ? exports.DIRECTION_LEFT : exports.DIRECTION_RIGHT;
  1449. hasMoved = x != this.pX;
  1450. distance = Math.abs(input.deltaX);
  1451. }
  1452. else {
  1453. direction = (y === 0) ? DIRECTION_NONE : (y < 0) ? DIRECTION_UP : DIRECTION_DOWN;
  1454. hasMoved = y != this.pY;
  1455. distance = Math.abs(input.deltaY);
  1456. }
  1457. }
  1458. input.direction = direction;
  1459. return hasMoved && distance > options.threshold && direction & options.direction;
  1460. },
  1461. attrTest: function (input) {
  1462. return AttrRecognizer.prototype.attrTest.call(this, input) &&
  1463. (this.state & STATE_BEGAN || (!(this.state & STATE_BEGAN) && this.directionTest(input)));
  1464. },
  1465. emit: function (input) {
  1466. this.pX = input.deltaX;
  1467. this.pY = input.deltaY;
  1468. var direction = directionStr(input.direction);
  1469. if (direction) {
  1470. input.additionalEvent = this.options.event + direction;
  1471. }
  1472. this._super.emit.call(this, input);
  1473. }
  1474. });
  1475. /**
  1476. * Pinch
  1477. * Recognized when two or more pointers are moving toward (zoom-in) or away from each other (zoom-out).
  1478. * @constructor
  1479. * @extends AttrRecognizer
  1480. */
  1481. function PinchRecognizer() {
  1482. AttrRecognizer.apply(this, arguments);
  1483. }
  1484. inherit(PinchRecognizer, AttrRecognizer, {
  1485. /**
  1486. * @namespace
  1487. * @memberof PinchRecognizer
  1488. */
  1489. defaults: {
  1490. event: 'pinch',
  1491. threshold: 0,
  1492. pointers: 2
  1493. },
  1494. getTouchAction: function () {
  1495. return [TOUCH_ACTION_NONE];
  1496. },
  1497. attrTest: function (input) {
  1498. return this._super.attrTest.call(this, input) &&
  1499. (Math.abs(input.scale - 1) > this.options.threshold || this.state & STATE_BEGAN);
  1500. },
  1501. emit: function (input) {
  1502. if (input.scale !== 1) {
  1503. var inOut = input.scale < 1 ? 'in' : 'out';
  1504. input.additionalEvent = this.options.event + inOut;
  1505. }
  1506. this._super.emit.call(this, input);
  1507. }
  1508. });
  1509. /**
  1510. * Press
  1511. * Recognized when the pointer is down for x ms without any movement.
  1512. * @constructor
  1513. * @extends Recognizer
  1514. */
  1515. function PressRecognizer() {
  1516. Recognizer.apply(this, arguments);
  1517. this._timer = null;
  1518. this._input = null;
  1519. }
  1520. inherit(PressRecognizer, Recognizer, {
  1521. /**
  1522. * @namespace
  1523. * @memberof PressRecognizer
  1524. */
  1525. defaults: {
  1526. event: 'press',
  1527. pointers: 1,
  1528. time: 251,
  1529. threshold: 9 // a minimal movement is ok, but keep it low
  1530. },
  1531. getTouchAction: function () {
  1532. return [TOUCH_ACTION_AUTO];
  1533. },
  1534. process: function (input) {
  1535. var options = this.options;
  1536. var validPointers = input.pointers.length === options.pointers;
  1537. var validMovement = input.distance < options.threshold;
  1538. var validTime = input.deltaTime > options.time;
  1539. this._input = input;
  1540. // we only allow little movement
  1541. // and we've reached an end event, so a tap is possible
  1542. if (!validMovement || !validPointers || (input.eventType & (INPUT_END | INPUT_CANCEL) && !validTime)) {
  1543. this.reset();
  1544. }
  1545. else if (input.eventType & INPUT_START) {
  1546. this.reset();
  1547. this._timer = setTimeoutContext(function () {
  1548. this.state = STATE_RECOGNIZED;
  1549. this.tryEmit();
  1550. }, options.time, this);
  1551. }
  1552. else if (input.eventType & INPUT_END) {
  1553. return STATE_RECOGNIZED;
  1554. }
  1555. return STATE_FAILED;
  1556. },
  1557. reset: function () {
  1558. clearTimeout(this._timer);
  1559. },
  1560. emit: function (input) {
  1561. if (this.state !== STATE_RECOGNIZED) {
  1562. return;
  1563. }
  1564. if (input && (input.eventType & INPUT_END)) {
  1565. this.manager.emit(this.options.event + 'up', input);
  1566. }
  1567. else {
  1568. this._input.timeStamp = now();
  1569. this.manager.emit(this.options.event, this._input);
  1570. }
  1571. }
  1572. });
  1573. /**
  1574. * Rotate
  1575. * Recognized when two or more pointer are moving in a circular motion.
  1576. * @constructor
  1577. * @extends AttrRecognizer
  1578. */
  1579. function RotateRecognizer() {
  1580. AttrRecognizer.apply(this, arguments);
  1581. }
  1582. inherit(RotateRecognizer, AttrRecognizer, {
  1583. /**
  1584. * @namespace
  1585. * @memberof RotateRecognizer
  1586. */
  1587. defaults: {
  1588. event: 'rotate',
  1589. threshold: 0,
  1590. pointers: 2
  1591. },
  1592. getTouchAction: function () {
  1593. return [TOUCH_ACTION_NONE];
  1594. },
  1595. attrTest: function (input) {
  1596. return this._super.attrTest.call(this, input) &&
  1597. (Math.abs(input.rotation) > this.options.threshold || this.state & STATE_BEGAN);
  1598. }
  1599. });
  1600. /**
  1601. * Swipe
  1602. * Recognized when the pointer is moving fast (velocity), with enough distance in the allowed direction.
  1603. * @constructor
  1604. * @extends AttrRecognizer
  1605. */
  1606. function SwipeRecognizer() {
  1607. AttrRecognizer.apply(this, arguments);
  1608. }
  1609. inherit(SwipeRecognizer, AttrRecognizer, {
  1610. /**
  1611. * @namespace
  1612. * @memberof SwipeRecognizer
  1613. */
  1614. defaults: {
  1615. event: 'swipe',
  1616. threshold: 10,
  1617. velocity: 0.3,
  1618. direction: exports.DIRECTION_HORIZONTAL | exports.DIRECTION_VERTICAL,
  1619. pointers: 1
  1620. },
  1621. getTouchAction: function () {
  1622. return PanRecognizer.prototype.getTouchAction.call(this);
  1623. },
  1624. attrTest: function (input) {
  1625. var direction = this.options.direction;
  1626. var velocity;
  1627. if (direction & (exports.DIRECTION_HORIZONTAL | exports.DIRECTION_VERTICAL)) {
  1628. velocity = input.overallVelocity;
  1629. }
  1630. else if (direction & exports.DIRECTION_HORIZONTAL) {
  1631. velocity = input.overallVelocityX;
  1632. }
  1633. else if (direction & exports.DIRECTION_VERTICAL) {
  1634. velocity = input.overallVelocityY;
  1635. }
  1636. return this._super.attrTest.call(this, input) &&
  1637. direction & input.offsetDirection &&
  1638. input.distance > this.options.threshold &&
  1639. input.maxPointers == this.options.pointers &&
  1640. abs(velocity) > this.options.velocity && input.eventType & INPUT_END;
  1641. },
  1642. emit: function (input) {
  1643. var direction = directionStr(input.offsetDirection);
  1644. if (direction) {
  1645. this.manager.emit(this.options.event + direction, input);
  1646. }
  1647. this.manager.emit(this.options.event, input);
  1648. }
  1649. });
  1650. /**
  1651. * A tap is ecognized when the pointer is doing a small tap/click. Multiple taps are recognized if they occur
  1652. * between the given interval and position. The delay option can be used to recognize multi-taps without firing
  1653. * a single tap.
  1654. *
  1655. * The eventData from the emitted event contains the property `tapCount`, which contains the amount of
  1656. * multi-taps being recognized.
  1657. * @constructor
  1658. * @extends Recognizer
  1659. */
  1660. function TapRecognizer() {
  1661. Recognizer.apply(this, arguments);
  1662. // previous time and center,
  1663. // used for tap counting
  1664. this.pTime = false;
  1665. this.pCenter = false;
  1666. this._timer = null;
  1667. this._input = null;
  1668. this.count = 0;
  1669. }
  1670. inherit(TapRecognizer, Recognizer, {
  1671. /**
  1672. * @namespace
  1673. * @memberof PinchRecognizer
  1674. */
  1675. defaults: {
  1676. event: 'tap',
  1677. pointers: 1,
  1678. taps: 1,
  1679. interval: 300,
  1680. time: 250,
  1681. threshold: 9,
  1682. posThreshold: 10 // a multi-tap can be a bit off the initial position
  1683. },
  1684. getTouchAction: function () {
  1685. return [TOUCH_ACTION_MANIPULATION];
  1686. },
  1687. process: function (input) {
  1688. var options = this.options;
  1689. var validPointers = input.pointers.length === options.pointers;
  1690. var validMovement = input.distance < options.threshold;
  1691. var validTouchTime = input.deltaTime < options.time;
  1692. this.reset();
  1693. if ((input.eventType & INPUT_START) && (this.count === 0)) {
  1694. return this.failTimeout();
  1695. }
  1696. // we only allow little movement
  1697. // and we've reached an end event, so a tap is possible
  1698. if (validMovement && validTouchTime && validPointers) {
  1699. if (input.eventType != INPUT_END) {
  1700. return this.failTimeout();
  1701. }
  1702. var validInterval = this.pTime ? (input.timeStamp - this.pTime < options.interval) : true;
  1703. var validMultiTap = !this.pCenter || getDistance(this.pCenter, input.center) < options.posThreshold;
  1704. this.pTime = input.timeStamp;
  1705. this.pCenter = input.center;
  1706. if (!validMultiTap || !validInterval) {
  1707. this.count = 1;
  1708. }
  1709. else {
  1710. this.count += 1;
  1711. }
  1712. this._input = input;
  1713. // if tap count matches we have recognized it,
  1714. // else it has began recognizing...
  1715. var tapCount = this.count % options.taps;
  1716. if (tapCount === 0) {
  1717. // no failing requirements, immediately trigger the tap event
  1718. // or wait as long as the multitap interval to trigger
  1719. if (!this.hasRequireFailures()) {
  1720. return STATE_RECOGNIZED;
  1721. }
  1722. else {
  1723. this._timer = setTimeoutContext(function () {
  1724. this.state = STATE_RECOGNIZED;
  1725. this.tryEmit();
  1726. }, options.interval, this);
  1727. return STATE_BEGAN;
  1728. }
  1729. }
  1730. }
  1731. return STATE_FAILED;
  1732. },
  1733. failTimeout: function () {
  1734. this._timer = setTimeoutContext(function () {
  1735. this.state = STATE_FAILED;
  1736. }, this.options.interval, this);
  1737. return STATE_FAILED;
  1738. },
  1739. reset: function () {
  1740. clearTimeout(this._timer);
  1741. },
  1742. emit: function () {
  1743. if (this.state == STATE_RECOGNIZED) {
  1744. this._input.tapCount = this.count;
  1745. this.manager.emit(this.options.event, this._input);
  1746. }
  1747. }
  1748. });
  1749. /**
  1750. * Simple way to create a manager with a default set of recognizers.
  1751. * @param {HTMLElement} element
  1752. * @param {Object} [options]
  1753. * @constructor
  1754. */
  1755. function Hammer(element, options) {
  1756. options = options || {};
  1757. options.recognizers = ifUndefined(options.recognizers, _defaults.preset);
  1758. return new Manager(element, options);
  1759. }
  1760. exports.Hammer = Hammer;
  1761. /**
  1762. * default settings
  1763. * @namespace
  1764. */
  1765. var _defaults = {
  1766. /**
  1767. * set if DOM events are being triggered.
  1768. * But this is slower and unused by simple implementations, so disabled by default.
  1769. * @type {Boolean}
  1770. * @default false
  1771. */
  1772. domEvents: false,
  1773. /**
  1774. * The value for the touchAction property/fallback.
  1775. * When set to `compute` it will magically set the correct value based on the added recognizers.
  1776. * @type {String}
  1777. * @default compute
  1778. */
  1779. touchAction: TOUCH_ACTION_COMPUTE,
  1780. /**
  1781. * @type {Boolean}
  1782. * @default true
  1783. */
  1784. enable: true,
  1785. /**
  1786. * EXPERIMENTAL FEATURE -- can be removed/changed
  1787. * Change the parent input target element.
  1788. * If Null, then it is being set the to main element.
  1789. * @type {Null|EventTarget}
  1790. * @default null
  1791. */
  1792. inputTarget: null,
  1793. /**
  1794. * force an input class
  1795. * @type {Null|Function}
  1796. * @default null
  1797. */
  1798. inputClass: null,
  1799. /**
  1800. * Default recognizer setup when calling `Hammer()`
  1801. * When creating a new Manager these will be skipped.
  1802. * @type {Array}
  1803. */
  1804. preset: [
  1805. // RecognizerClass, options, [recognizeWith, ...], [requireFailure, ...]
  1806. [RotateRecognizer, { enable: false }],
  1807. [PinchRecognizer, { enable: false }, ['rotate']],
  1808. [SwipeRecognizer, { direction: exports.DIRECTION_HORIZONTAL }],
  1809. [PanRecognizer, { direction: exports.DIRECTION_HORIZONTAL }, ['swipe']],
  1810. [TapRecognizer],
  1811. [TapRecognizer, { event: 'doubletap', taps: 2 }, ['tap']],
  1812. [PressRecognizer]
  1813. ],
  1814. /**
  1815. * Some CSS properties can be used to improve the working of Hammer.
  1816. * Add them to this method and they will be set when creating a new Manager.
  1817. * @namespace
  1818. */
  1819. cssProps: {
  1820. /**
  1821. * Disables text selection to improve the dragging gesture. Mainly for desktop browsers.
  1822. * @type {String}
  1823. * @default 'none'
  1824. */
  1825. userSelect: 'none',
  1826. /**
  1827. * Disable the Windows Phone grippers when pressing an element.
  1828. * @type {String}
  1829. * @default 'none'
  1830. */
  1831. touchSelect: 'none',
  1832. /**
  1833. * Disables the default callout shown when you touch and hold a touch target.
  1834. * On iOS, when you touch and hold a touch target such as a link, Safari displays
  1835. * a callout containing information about the link. This property allows you to disable that callout.
  1836. * @type {String}
  1837. * @default 'none'
  1838. */
  1839. touchCallout: 'none',
  1840. /**
  1841. * Specifies whether zooming is enabled. Used by IE10>
  1842. * @type {String}
  1843. * @default 'none'
  1844. */
  1845. contentZooming: 'none',
  1846. /**
  1847. * Specifies that an entire element should be draggable instead of its contents. Mainly for desktop browsers.
  1848. * @type {String}
  1849. * @default 'none'
  1850. */
  1851. userDrag: 'none',
  1852. /**
  1853. * Overrides the highlight color shown when the user taps a link or a JavaScript
  1854. * clickable element in iOS. This property obeys the alpha value, if specified.
  1855. * @type {String}
  1856. * @default 'rgba(0,0,0,0)'
  1857. */
  1858. tapHighlightColor: 'rgba(0,0,0,0)'
  1859. }
  1860. };
  1861. var STOP = 1;
  1862. var FORCED_STOP = 2;
  1863. /**
  1864. * Manager
  1865. * @param {HTMLElement} element
  1866. * @param {Object} [options]
  1867. * @constructor
  1868. */
  1869. function Manager(element, options) {
  1870. this.options = Object.assign({}, _defaults, options || {});
  1871. this.options.inputTarget = this.options.inputTarget || element;
  1872. this.handlers = {};
  1873. this.session = {};
  1874. this.recognizers = [];
  1875. this.element = element;
  1876. this.input = createInputInstance(this);
  1877. this.touchAction = new TouchAction(this, this.options.touchAction);
  1878. toggleCssProps(this, true);
  1879. each(this.options.recognizers, function (item) {
  1880. var recognizer = this.add(new (item[0])(item[1]));
  1881. item[2] && recognizer.recognizeWith(item[2]);
  1882. item[3] && recognizer.requireFailure(item[3]);
  1883. }, this);
  1884. }
  1885. Manager.prototype = {
  1886. /**
  1887. * set options
  1888. * @param {Object} options
  1889. * @returns {Manager}
  1890. */
  1891. set: function (options) {
  1892. Object.assign(this.options, options);
  1893. // Options that need a little more setup
  1894. if (options.touchAction) {
  1895. this.touchAction.update();
  1896. }
  1897. if (options.inputTarget) {
  1898. // Clean up existing event listeners and reinitialize
  1899. this.input.destroy();
  1900. this.input.target = options.inputTarget;
  1901. this.input.init();
  1902. }
  1903. return this;
  1904. },
  1905. /**
  1906. * stop recognizing for this session.
  1907. * This session will be discarded, when a new [input]start event is fired.
  1908. * When forced, the recognizer cycle is stopped immediately.
  1909. * @param {Boolean} [force]
  1910. */
  1911. stop: function (force) {
  1912. this.session.stopped = force ? FORCED_STOP : STOP;
  1913. },
  1914. /**
  1915. * run the recognizers!
  1916. * called by the inputHandler function on every movement of the pointers (touches)
  1917. * it walks through all the recognizers and tries to detect the gesture that is being made
  1918. * @param {Object} inputData
  1919. */
  1920. recognize: function (inputData) {
  1921. var session = this.session;
  1922. if (session.stopped) {
  1923. return;
  1924. }
  1925. // run the touch-action polyfill
  1926. this.touchAction.preventDefaults(inputData);
  1927. var recognizer;
  1928. var recognizers = this.recognizers;
  1929. // this holds the recognizer that is being recognized.
  1930. // so the recognizer's state needs to be BEGAN, CHANGED, ENDED or RECOGNIZED
  1931. // if no recognizer is detecting a thing, it is set to `null`
  1932. var curRecognizer = session.curRecognizer;
  1933. // reset when the last recognizer is recognized
  1934. // or when we're in a new session
  1935. if (!curRecognizer || (curRecognizer && curRecognizer.state & STATE_RECOGNIZED)) {
  1936. curRecognizer = session.curRecognizer = null;
  1937. }
  1938. var i = 0;
  1939. while (i < recognizers.length) {
  1940. recognizer = recognizers[i];
  1941. // find out if we are allowed try to recognize the input for this one.
  1942. // 1. allow if the session is NOT forced stopped (see the .stop() method)
  1943. // 2. allow if we still haven't recognized a gesture in this session, or the this recognizer is the one
  1944. // that is being recognized.
  1945. // 3. allow if the recognizer is allowed to run simultaneous with the current recognized recognizer.
  1946. // this can be setup with the `recognizeWith()` method on the recognizer.
  1947. if (session.stopped !== FORCED_STOP && (!curRecognizer || recognizer == curRecognizer ||
  1948. recognizer.canRecognizeWith(curRecognizer))) {
  1949. recognizer.recognize(inputData);
  1950. }
  1951. else {
  1952. recognizer.reset();
  1953. }
  1954. // if the recognizer has been recognizing the input as a valid gesture, we want to store this one as the
  1955. // current active recognizer. but only if we don't already have an active recognizer
  1956. if (!curRecognizer && recognizer.state & (STATE_BEGAN | STATE_CHANGED | STATE_ENDED)) {
  1957. curRecognizer = session.curRecognizer = recognizer;
  1958. }
  1959. i++;
  1960. }
  1961. },
  1962. /**
  1963. * get a recognizer by its event name.
  1964. * @param {Recognizer|String} recognizer
  1965. * @returns {Recognizer|Null}
  1966. */
  1967. get: function (recognizer) {
  1968. if (recognizer instanceof Recognizer) {
  1969. return recognizer;
  1970. }
  1971. var recognizers = this.recognizers;
  1972. for (var i = 0; i < recognizers.length; i++) {
  1973. if (recognizers[i].options.event == recognizer) {
  1974. return recognizers[i];
  1975. }
  1976. }
  1977. return null;
  1978. },
  1979. /**
  1980. * add a recognizer to the manager
  1981. * existing recognizers with the same event name will be removed
  1982. * @param {Recognizer} recognizer
  1983. * @returns {Recognizer|Manager}
  1984. */
  1985. add: function (recognizer) {
  1986. if (invokeArrayArg(recognizer, 'add', this)) {
  1987. return this;
  1988. }
  1989. // remove existing
  1990. var existing = this.get(recognizer.options.event);
  1991. if (existing) {
  1992. this.remove(existing);
  1993. }
  1994. this.recognizers.push(recognizer);
  1995. recognizer.manager = this;
  1996. this.touchAction.update();
  1997. return recognizer;
  1998. },
  1999. /**
  2000. * remove a recognizer by name or instance
  2001. * @param {Recognizer|String} recognizer
  2002. * @returns {Manager}
  2003. */
  2004. remove: function (recognizer) {
  2005. if (invokeArrayArg(recognizer, 'remove', this)) {
  2006. return this;
  2007. }
  2008. recognizer = this.get(recognizer);
  2009. // let's make sure this recognizer exists
  2010. if (recognizer) {
  2011. var recognizers = this.recognizers;
  2012. var index = inArray(recognizers, recognizer);
  2013. if (index !== -1) {
  2014. recognizers.splice(index, 1);
  2015. this.touchAction.update();
  2016. }
  2017. }
  2018. return this;
  2019. },
  2020. /**
  2021. * bind event
  2022. * @param {String} events
  2023. * @param {Function} handler
  2024. * @returns {EventEmitter} this
  2025. */
  2026. on: function (events, handler) {
  2027. var handlers = this.handlers;
  2028. each(splitStr(events), function (event) {
  2029. handlers[event] = handlers[event] || [];
  2030. handlers[event].push(handler);
  2031. });
  2032. return this;
  2033. },
  2034. /**
  2035. * unbind event, leave emit blank to remove all handlers
  2036. * @param {String} events
  2037. * @param {Function} [handler]
  2038. * @returns {EventEmitter} this
  2039. */
  2040. off: function (events, handler) {
  2041. var handlers = this.handlers;
  2042. each(splitStr(events), function (event) {
  2043. if (!handler) {
  2044. delete handlers[event];
  2045. }
  2046. else {
  2047. handlers[event] && handlers[event].splice(inArray(handlers[event], handler), 1);
  2048. }
  2049. });
  2050. return this;
  2051. },
  2052. /**
  2053. * emit event to the listeners
  2054. * @param {String} event
  2055. * @param {Object} data
  2056. */
  2057. emit: function (event, data) {
  2058. // we also want to trigger dom events
  2059. if (this.options.domEvents) {
  2060. triggerDomEvent(event, data);
  2061. }
  2062. // no handlers, so skip it all
  2063. var handlers = this.handlers[event] && this.handlers[event].slice();
  2064. if (!handlers || !handlers.length) {
  2065. return;
  2066. }
  2067. data.type = event;
  2068. data.preventDefault = function () {
  2069. data.srcEvent.preventDefault();
  2070. };
  2071. var i = 0;
  2072. while (i < handlers.length) {
  2073. handlers[i](data);
  2074. i++;
  2075. }
  2076. },
  2077. /**
  2078. * destroy the manager and unbinds all events
  2079. * it doesn't unbind dom events, that is the user own responsibility
  2080. */
  2081. destroy: function () {
  2082. this.element && toggleCssProps(this, false);
  2083. this.handlers = {};
  2084. this.session = {};
  2085. this.input.destroy();
  2086. this.element = null;
  2087. }
  2088. };
  2089. /**
  2090. * add/remove the css properties as defined in manager.options.cssProps
  2091. * @param {Manager} manager
  2092. * @param {Boolean} add
  2093. */
  2094. function toggleCssProps(manager, add) {
  2095. var element = manager.element;
  2096. if (!element.style) {
  2097. return;
  2098. }
  2099. each(manager.options.cssProps, function (value, name) {
  2100. element.style[prefixed(element.style, name)] = add ? value : '';
  2101. });
  2102. }
  2103. /**
  2104. * trigger dom event
  2105. * @param {String} event
  2106. * @param {Object} data
  2107. */
  2108. function triggerDomEvent(event, data) {
  2109. var gestureEvent = doc.createEvent('Event');
  2110. gestureEvent.initEvent(event, true, true);
  2111. gestureEvent.gesture = data;
  2112. data.target.dispatchEvent(gestureEvent);
  2113. }
  2114. Object.assign(Hammer, {
  2115. INPUT_START: INPUT_START,
  2116. INPUT_MOVE: INPUT_MOVE,
  2117. INPUT_END: INPUT_END,
  2118. INPUT_CANCEL: INPUT_CANCEL,
  2119. STATE_POSSIBLE: STATE_POSSIBLE,
  2120. STATE_BEGAN: STATE_BEGAN,
  2121. STATE_CHANGED: STATE_CHANGED,
  2122. STATE_ENDED: STATE_ENDED,
  2123. STATE_RECOGNIZED: STATE_RECOGNIZED,
  2124. STATE_CANCELLED: STATE_CANCELLED,
  2125. STATE_FAILED: STATE_FAILED,
  2126. DIRECTION_NONE: DIRECTION_NONE,
  2127. DIRECTION_LEFT: exports.DIRECTION_LEFT,
  2128. DIRECTION_RIGHT: exports.DIRECTION_RIGHT,
  2129. DIRECTION_UP: DIRECTION_UP,
  2130. DIRECTION_DOWN: DIRECTION_DOWN,
  2131. DIRECTION_HORIZONTAL: exports.DIRECTION_HORIZONTAL,
  2132. DIRECTION_VERTICAL: exports.DIRECTION_VERTICAL,
  2133. DIRECTION_ALL: DIRECTION_ALL,
  2134. Manager: Manager,
  2135. Input: Input,
  2136. TouchAction: TouchAction,
  2137. TouchInput: TouchInput,
  2138. MouseInput: MouseInput,
  2139. PointerEventInput: PointerEventInput,
  2140. TouchMouseInput: TouchMouseInput,
  2141. SingleTouchInput: SingleTouchInput,
  2142. Recognizer: Recognizer,
  2143. AttrRecognizer: AttrRecognizer,
  2144. Tap: TapRecognizer,
  2145. Pan: PanRecognizer,
  2146. Swipe: SwipeRecognizer,
  2147. Pinch: PinchRecognizer,
  2148. Rotate: RotateRecognizer,
  2149. Press: PressRecognizer,
  2150. on: addEventListeners,
  2151. off: removeEventListeners,
  2152. each: each,
  2153. inherit: inherit,
  2154. bindFn: bindFn,
  2155. prefixed: prefixed
  2156. });
  2157. win.Hammer = Hammer;
  2158. });
  2159. //# sourceMappingURL=hammer.js.map