1 /*! Hammer.JS - v2.0.6 - 2015-12-23
2 * http://hammerjs.github.io/
4 * Copyright (c) 2015 Jorik Tangelder;
5 * Licensed under the license */
6 (function(window, document, exportName, undefined) {
9 var VENDOR_PREFIXES = ['', 'webkit', 'Moz', 'MS', 'ms', 'o'];
10 var TEST_ELEMENT = document.createElement('div');
12 var TYPE_FUNCTION = 'function';
14 var round = Math.round;
19 * set a timeout with a given scope
20 * @param {Function} fn
21 * @param {Number} timeout
22 * @param {Object} context
25 function setTimeoutContext(fn, timeout, context) {
26 return setTimeout(bindFn(fn, context), timeout);
30 * if the argument is an array, we want to execute the fn on each entry
31 * if it aint an array we don't want to do a thing.
32 * this is used by all the methods that accept a single and array argument.
33 * @param {*|Array} arg
35 * @param {Object} [context]
38 function invokeArrayArg(arg, fn, context) {
39 if (Array.isArray(arg)) {
40 each(arg, context[fn], context);
47 * walk objects and arrays
49 * @param {Function} iterator
50 * @param {Object} context
52 function each(obj, iterator, context) {
60 obj.forEach(iterator, context);
61 } else if (obj.length !== undefined) {
63 while (i < obj.length) {
64 iterator.call(context, obj[i], i, obj);
69 obj.hasOwnProperty(i) && iterator.call(context, obj[i], i, obj);
75 * wrap a method with a deprecation warning and stack trace
76 * @param {Function} method
77 * @param {String} name
78 * @param {String} message
79 * @returns {Function} A new function wrapping the supplied method.
81 function deprecate(method, name, message) {
82 var deprecationMessage = 'DEPRECATED METHOD: ' + name + '\n' + message + ' AT \n';
84 var e = new Error('get-stack-trace');
85 var stack = e && e.stack ? e.stack.replace(/^[^\(]+?[\n$]/gm, '')
86 .replace(/^\s+at\s+/gm, '')
87 .replace(/^Object.<anonymous>\s*\(/gm, '{anonymous}()@') : 'Unknown Stack Trace';
89 var log = window.console && (window.console.warn || window.console.log);
91 log.call(window.console, deprecationMessage, stack);
93 return method.apply(this, arguments);
99 * means that properties in dest will be overwritten by the ones in src.
100 * @param {Object} target
101 * @param {...Object} objects_to_assign
102 * @returns {Object} target
105 if (typeof Object.assign !== 'function') {
106 assign = function assign(target) {
107 if (target === undefined || target === null) {
108 throw new TypeError('Cannot convert undefined or null to object');
111 var output = Object(target);
112 for (var index = 1; index < arguments.length; index++) {
113 var source = arguments[index];
114 if (source !== undefined && source !== null) {
115 for (var nextKey in source) {
116 if (source.hasOwnProperty(nextKey)) {
117 output[nextKey] = source[nextKey];
125 assign = Object.assign;
130 * means that properties in dest will be overwritten by the ones in src.
131 * @param {Object} dest
132 * @param {Object} src
133 * @param {Boolean=false} [merge]
134 * @returns {Object} dest
136 var extend = deprecate(function extend(dest, src, merge) {
137 var keys = Object.keys(src);
139 while (i < keys.length) {
140 if (!merge || (merge && dest[keys[i]] === undefined)) {
141 dest[keys[i]] = src[keys[i]];
146 }, 'extend', 'Use `assign`.');
149 * merge the values from src in the dest.
150 * means that properties that exist in dest will not be overwritten by src
151 * @param {Object} dest
152 * @param {Object} src
153 * @returns {Object} dest
155 var merge = deprecate(function merge(dest, src) {
156 return extend(dest, src, true);
157 }, 'merge', 'Use `assign`.');
160 * simple class inheritance
161 * @param {Function} child
162 * @param {Function} base
163 * @param {Object} [properties]
165 function inherit(child, base, properties) {
166 var baseP = base.prototype,
169 childP = child.prototype = Object.create(baseP);
170 childP.constructor = child;
171 childP._super = baseP;
174 assign(childP, properties);
179 * simple function bind
180 * @param {Function} fn
181 * @param {Object} context
182 * @returns {Function}
184 function bindFn(fn, context) {
185 return function boundFn() {
186 return fn.apply(context, arguments);
191 * let a boolean value also be a function that must return a boolean
192 * this first item in args will be used as the context
193 * @param {Boolean|Function} val
194 * @param {Array} [args]
197 function boolOrFn(val, args) {
198 if (typeof val == TYPE_FUNCTION) {
199 return val.apply(args ? args[0] || undefined : undefined, args);
205 * use the val2 when val1 is undefined
210 function ifUndefined(val1, val2) {
211 return (val1 === undefined) ? val2 : val1;
215 * addEventListener with multiple events at once
216 * @param {EventTarget} target
217 * @param {String} types
218 * @param {Function} handler
220 function addEventListeners(target, types, handler) {
221 each(splitStr(types), function(type) {
222 target.addEventListener(type, handler, false);
227 * removeEventListener with multiple events at once
228 * @param {EventTarget} target
229 * @param {String} types
230 * @param {Function} handler
232 function removeEventListeners(target, types, handler) {
233 each(splitStr(types), function(type) {
234 target.removeEventListener(type, handler, false);
239 * find if a node is in the given parent
241 * @param {HTMLElement} node
242 * @param {HTMLElement} parent
243 * @return {Boolean} found
245 function hasParent(node, parent) {
247 if (node == parent) {
250 node = node.parentNode;
256 * small indexOf wrapper
257 * @param {String} str
258 * @param {String} find
259 * @returns {Boolean} found
261 function inStr(str, find) {
262 return str.indexOf(find) > -1;
266 * split string on whitespace
267 * @param {String} str
268 * @returns {Array} words
270 function splitStr(str) {
271 return str.trim().split(/\s+/g);
275 * find if a array contains the object using indexOf or a simple polyFill
277 * @param {String} find
278 * @param {String} [findByKey]
279 * @return {Boolean|Number} false when not found, or the index
281 function inArray(src, find, findByKey) {
282 if (src.indexOf && !findByKey) {
283 return src.indexOf(find);
286 while (i < src.length) {
287 if ((findByKey && src[i][findByKey] == find) || (!findByKey && src[i] === find)) {
297 * convert array-like objects to real arrays
298 * @param {Object} obj
301 function toArray(obj) {
302 return Array.prototype.slice.call(obj, 0);
306 * unique array with objects based on a key (like 'id') or just by the array's value
307 * @param {Array} src [{id:1},{id:2},{id:1}]
308 * @param {String} [key]
309 * @param {Boolean} [sort=False]
310 * @returns {Array} [{id:1},{id:2}]
312 function uniqueArray(src, key, sort) {
317 while (i < src.length) {
318 var val = key ? src[i][key] : src[i];
319 if (inArray(values, val) < 0) {
320 results.push(src[i]);
328 results = results.sort();
330 results = results.sort(function sortUniqueArray(a, b) {
331 return a[key] > b[key];
340 * get the prefixed property
341 * @param {Object} obj
342 * @param {String} property
343 * @returns {String|Undefined} prefixed
345 function prefixed(obj, property) {
347 var camelProp = property[0].toUpperCase() + property.slice(1);
350 while (i < VENDOR_PREFIXES.length) {
351 prefix = VENDOR_PREFIXES[i];
352 prop = (prefix) ? prefix + camelProp : property;
364 * @returns {number} uniqueId
367 function uniqueId() {
372 * get the window object of an element
373 * @param {HTMLElement} element
374 * @returns {DocumentView|Window}
376 function getWindowForElement(element) {
377 var doc = element.ownerDocument || element;
378 return (doc.defaultView || doc.parentWindow || window);
381 var MOBILE_REGEX = /mobile|tablet|ip(ad|hone|od)|android/i;
383 var SUPPORT_TOUCH = ('ontouchstart' in window);
384 var SUPPORT_POINTER_EVENTS = prefixed(window, 'PointerEvent') !== undefined;
385 var SUPPORT_ONLY_TOUCH = SUPPORT_TOUCH && MOBILE_REGEX.test(navigator.userAgent);
387 var INPUT_TYPE_TOUCH = 'touch';
388 var INPUT_TYPE_PEN = 'pen';
389 var INPUT_TYPE_MOUSE = 'mouse';
390 var INPUT_TYPE_KINECT = 'kinect';
392 var COMPUTE_INTERVAL = 25;
397 var INPUT_CANCEL = 8;
399 var DIRECTION_NONE = 1;
400 var DIRECTION_LEFT = 2;
401 var DIRECTION_RIGHT = 4;
402 var DIRECTION_UP = 8;
403 var DIRECTION_DOWN = 16;
405 var DIRECTION_HORIZONTAL = DIRECTION_LEFT | DIRECTION_RIGHT;
406 var DIRECTION_VERTICAL = DIRECTION_UP | DIRECTION_DOWN;
407 var DIRECTION_ALL = DIRECTION_HORIZONTAL | DIRECTION_VERTICAL;
409 var PROPS_XY = ['x', 'y'];
410 var PROPS_CLIENT_XY = ['clientX', 'clientY'];
413 * create new input type manager
414 * @param {Manager} manager
415 * @param {Function} callback
419 function Input(manager, callback) {
421 this.manager = manager;
422 this.callback = callback;
423 this.element = manager.element;
424 this.target = manager.options.inputTarget;
426 // smaller wrapper around the handler, for the scope and the enabled state of the manager,
427 // so when disabled the input events are completely bypassed.
428 this.domHandler = function(ev) {
429 if (boolOrFn(manager.options.enable, [manager])) {
440 * should handle the inputEvent data and trigger the callback
443 handler: function() { },
449 this.evEl && addEventListeners(this.element, this.evEl, this.domHandler);
450 this.evTarget && addEventListeners(this.target, this.evTarget, this.domHandler);
451 this.evWin && addEventListeners(getWindowForElement(this.element), this.evWin, this.domHandler);
457 destroy: function() {
458 this.evEl && removeEventListeners(this.element, this.evEl, this.domHandler);
459 this.evTarget && removeEventListeners(this.target, this.evTarget, this.domHandler);
460 this.evWin && removeEventListeners(getWindowForElement(this.element), this.evWin, this.domHandler);
465 * create new input type manager
466 * called by the Manager constructor
467 * @param {Hammer} manager
470 function createInputInstance(manager) {
472 var inputClass = manager.options.inputClass;
476 } else if (SUPPORT_POINTER_EVENTS) {
477 Type = PointerEventInput;
478 } else if (SUPPORT_ONLY_TOUCH) {
480 } else if (!SUPPORT_TOUCH) {
483 Type = TouchMouseInput;
485 return new (Type)(manager, inputHandler);
489 * handle input events
490 * @param {Manager} manager
491 * @param {String} eventType
492 * @param {Object} input
494 function inputHandler(manager, eventType, input) {
495 var pointersLen = input.pointers.length;
496 var changedPointersLen = input.changedPointers.length;
497 var isFirst = (eventType & INPUT_START && (pointersLen - changedPointersLen === 0));
498 var isFinal = (eventType & (INPUT_END | INPUT_CANCEL) && (pointersLen - changedPointersLen === 0));
500 input.isFirst = !!isFirst;
501 input.isFinal = !!isFinal;
504 manager.session = {};
507 // source event is the normalized value of the domEvents
508 // like 'touchstart, mouseup, pointerdown'
509 input.eventType = eventType;
511 // compute scale, rotation etc
512 computeInputData(manager, input);
515 manager.emit('hammer.input', input);
517 manager.recognize(input);
518 manager.session.prevInput = input;
522 * extend the data with some usable properties like scale, rotate, velocity etc
523 * @param {Object} manager
524 * @param {Object} input
526 function computeInputData(manager, input) {
527 var session = manager.session;
528 var pointers = input.pointers;
529 var pointersLength = pointers.length;
531 // store the first input to calculate the distance and direction
532 if (!session.firstInput) {
533 session.firstInput = simpleCloneInputData(input);
536 // to compute scale and rotation we need to store the multiple touches
537 if (pointersLength > 1 && !session.firstMultiple) {
538 session.firstMultiple = simpleCloneInputData(input);
539 } else if (pointersLength === 1) {
540 session.firstMultiple = false;
543 var firstInput = session.firstInput;
544 var firstMultiple = session.firstMultiple;
545 var offsetCenter = firstMultiple ? firstMultiple.center : firstInput.center;
547 var center = input.center = getCenter(pointers);
548 input.timeStamp = now();
549 input.deltaTime = input.timeStamp - firstInput.timeStamp;
551 input.angle = getAngle(offsetCenter, center);
552 input.distance = getDistance(offsetCenter, center);
554 computeDeltaXY(session, input);
555 input.offsetDirection = getDirection(input.deltaX, input.deltaY);
557 var overallVelocity = getVelocity(input.deltaTime, input.deltaX, input.deltaY);
558 input.overallVelocityX = overallVelocity.x;
559 input.overallVelocityY = overallVelocity.y;
560 input.overallVelocity = (abs(overallVelocity.x) > abs(overallVelocity.y)) ? overallVelocity.x : overallVelocity.y;
562 input.scale = firstMultiple ? getScale(firstMultiple.pointers, pointers) : 1;
563 input.rotation = firstMultiple ? getRotation(firstMultiple.pointers, pointers) : 0;
565 input.maxPointers = !session.prevInput ? input.pointers.length : ((input.pointers.length >
566 session.prevInput.maxPointers) ? input.pointers.length : session.prevInput.maxPointers);
568 computeIntervalInputData(session, input);
570 // find the correct target
571 var target = manager.element;
572 if (hasParent(input.srcEvent.target, target)) {
573 target = input.srcEvent.target;
575 input.target = target;
578 function computeDeltaXY(session, input) {
579 var center = input.center;
580 var offset = session.offsetDelta || {};
581 var prevDelta = session.prevDelta || {};
582 var prevInput = session.prevInput || {};
584 if (input.eventType === INPUT_START || prevInput.eventType === INPUT_END) {
585 prevDelta = session.prevDelta = {
586 x: prevInput.deltaX || 0,
587 y: prevInput.deltaY || 0
590 offset = session.offsetDelta = {
596 input.deltaX = prevDelta.x + (center.x - offset.x);
597 input.deltaY = prevDelta.y + (center.y - offset.y);
601 * velocity is calculated every x ms
602 * @param {Object} session
603 * @param {Object} input
605 function computeIntervalInputData(session, input) {
606 var last = session.lastInterval || input,
607 deltaTime = input.timeStamp - last.timeStamp,
608 velocity, velocityX, velocityY, direction;
610 if (input.eventType != INPUT_CANCEL && (deltaTime > COMPUTE_INTERVAL || last.velocity === undefined)) {
611 var deltaX = input.deltaX - last.deltaX;
612 var deltaY = input.deltaY - last.deltaY;
614 var v = getVelocity(deltaTime, deltaX, deltaY);
617 velocity = (abs(v.x) > abs(v.y)) ? v.x : v.y;
618 direction = getDirection(deltaX, deltaY);
620 session.lastInterval = input;
622 // use latest velocity info if it doesn't overtake a minimum period
623 velocity = last.velocity;
624 velocityX = last.velocityX;
625 velocityY = last.velocityY;
626 direction = last.direction;
629 input.velocity = velocity;
630 input.velocityX = velocityX;
631 input.velocityY = velocityY;
632 input.direction = direction;
636 * create a simple clone from the input used for storage of firstInput and firstMultiple
637 * @param {Object} input
638 * @returns {Object} clonedInputData
640 function simpleCloneInputData(input) {
641 // make a simple copy of the pointers because we will get a reference if we don't
642 // we only need clientXY for the calculations
645 while (i < input.pointers.length) {
647 clientX: round(input.pointers[i].clientX),
648 clientY: round(input.pointers[i].clientY)
656 center: getCenter(pointers),
657 deltaX: input.deltaX,
663 * get the center of all the pointers
664 * @param {Array} pointers
665 * @return {Object} center contains `x` and `y` properties
667 function getCenter(pointers) {
668 var pointersLength = pointers.length;
670 // no need to loop when only one touch
671 if (pointersLength === 1) {
673 x: round(pointers[0].clientX),
674 y: round(pointers[0].clientY)
678 var x = 0, y = 0, i = 0;
679 while (i < pointersLength) {
680 x += pointers[i].clientX;
681 y += pointers[i].clientY;
686 x: round(x / pointersLength),
687 y: round(y / pointersLength)
692 * calculate the velocity between two points. unit is in px per ms.
693 * @param {Number} deltaTime
696 * @return {Object} velocity `x` and `y`
698 function getVelocity(deltaTime, x, y) {
700 x: x / deltaTime || 0,
701 y: y / deltaTime || 0
706 * get the direction between two points
709 * @return {Number} direction
711 function getDirection(x, y) {
713 return DIRECTION_NONE;
716 if (abs(x) >= abs(y)) {
717 return x < 0 ? DIRECTION_LEFT : DIRECTION_RIGHT;
719 return y < 0 ? DIRECTION_UP : DIRECTION_DOWN;
723 * calculate the absolute distance between two points
724 * @param {Object} p1 {x, y}
725 * @param {Object} p2 {x, y}
726 * @param {Array} [props] containing x and y keys
727 * @return {Number} distance
729 function getDistance(p1, p2, props) {
733 var x = p2[props[0]] - p1[props[0]],
734 y = p2[props[1]] - p1[props[1]];
736 return Math.sqrt((x * x) + (y * y));
740 * calculate the angle between two coordinates
743 * @param {Array} [props] containing x and y keys
744 * @return {Number} angle
746 function getAngle(p1, p2, props) {
750 var x = p2[props[0]] - p1[props[0]],
751 y = p2[props[1]] - p1[props[1]];
752 return Math.atan2(y, x) * 180 / Math.PI;
756 * calculate the rotation degrees between two pointersets
757 * @param {Array} start array of pointers
758 * @param {Array} end array of pointers
759 * @return {Number} rotation
761 function getRotation(start, end) {
762 return getAngle(end[1], end[0], PROPS_CLIENT_XY) + getAngle(start[1], start[0], PROPS_CLIENT_XY);
766 * calculate the scale factor between two pointersets
767 * no scale is 1, and goes down to 0 when pinched together, and bigger when pinched out
768 * @param {Array} start array of pointers
769 * @param {Array} end array of pointers
770 * @return {Number} scale
772 function getScale(start, end) {
773 return getDistance(end[0], end[1], PROPS_CLIENT_XY) / getDistance(start[0], start[1], PROPS_CLIENT_XY);
776 var MOUSE_INPUT_MAP = {
777 mousedown: INPUT_START,
778 mousemove: INPUT_MOVE,
782 var MOUSE_ELEMENT_EVENTS = 'mousedown';
783 var MOUSE_WINDOW_EVENTS = 'mousemove mouseup';
790 function MouseInput() {
791 this.evEl = MOUSE_ELEMENT_EVENTS;
792 this.evWin = MOUSE_WINDOW_EVENTS;
794 this.allow = true; // used by Input.TouchMouse to disable mouse events
795 this.pressed = false; // mousedown state
797 Input.apply(this, arguments);
800 inherit(MouseInput, Input, {
802 * handle mouse events
805 handler: function MEhandler(ev) {
806 var eventType = MOUSE_INPUT_MAP[ev.type];
808 // on start we want to have the left mouse button down
809 if (eventType & INPUT_START && ev.button === 0) {
813 if (eventType & INPUT_MOVE && ev.which !== 1) {
814 eventType = INPUT_END;
817 // mouse must be down, and mouse events are allowed (see the TouchMouse input)
818 if (!this.pressed || !this.allow) {
822 if (eventType & INPUT_END) {
823 this.pressed = false;
826 this.callback(this.manager, eventType, {
828 changedPointers: [ev],
829 pointerType: INPUT_TYPE_MOUSE,
835 var POINTER_INPUT_MAP = {
836 pointerdown: INPUT_START,
837 pointermove: INPUT_MOVE,
838 pointerup: INPUT_END,
839 pointercancel: INPUT_CANCEL,
840 pointerout: INPUT_CANCEL
843 // in IE10 the pointer types is defined as an enum
844 var IE10_POINTER_TYPE_ENUM = {
848 5: INPUT_TYPE_KINECT // see https://twitter.com/jacobrossi/status/480596438489890816
851 var POINTER_ELEMENT_EVENTS = 'pointerdown';
852 var POINTER_WINDOW_EVENTS = 'pointermove pointerup pointercancel';
854 // IE10 has prefixed support, and case-sensitive
855 if (window.MSPointerEvent && !window.PointerEvent) {
856 POINTER_ELEMENT_EVENTS = 'MSPointerDown';
857 POINTER_WINDOW_EVENTS = 'MSPointerMove MSPointerUp MSPointerCancel';
861 * Pointer events input
865 function PointerEventInput() {
866 this.evEl = POINTER_ELEMENT_EVENTS;
867 this.evWin = POINTER_WINDOW_EVENTS;
869 Input.apply(this, arguments);
871 this.store = (this.manager.session.pointerEvents = []);
874 inherit(PointerEventInput, Input, {
876 * handle mouse events
879 handler: function PEhandler(ev) {
880 var store = this.store;
881 var removePointer = false;
883 var eventTypeNormalized = ev.type.toLowerCase().replace('ms', '');
884 var eventType = POINTER_INPUT_MAP[eventTypeNormalized];
885 var pointerType = IE10_POINTER_TYPE_ENUM[ev.pointerType] || ev.pointerType;
887 var isTouch = (pointerType == INPUT_TYPE_TOUCH);
889 // get index of the event in the store
890 var storeIndex = inArray(store, ev.pointerId, 'pointerId');
892 // start and mouse must be down
893 if (eventType & INPUT_START && (ev.button === 0 || isTouch)) {
894 if (storeIndex < 0) {
896 storeIndex = store.length - 1;
898 } else if (eventType & (INPUT_END | INPUT_CANCEL)) {
899 removePointer = true;
902 // it not found, so the pointer hasn't been down (so it's probably a hover)
903 if (storeIndex < 0) {
907 // update the event in the store
908 store[storeIndex] = ev;
910 this.callback(this.manager, eventType, {
912 changedPointers: [ev],
913 pointerType: pointerType,
918 // remove from the store
919 store.splice(storeIndex, 1);
924 var SINGLE_TOUCH_INPUT_MAP = {
925 touchstart: INPUT_START,
926 touchmove: INPUT_MOVE,
928 touchcancel: INPUT_CANCEL
931 var SINGLE_TOUCH_TARGET_EVENTS = 'touchstart';
932 var SINGLE_TOUCH_WINDOW_EVENTS = 'touchstart touchmove touchend touchcancel';
939 function SingleTouchInput() {
940 this.evTarget = SINGLE_TOUCH_TARGET_EVENTS;
941 this.evWin = SINGLE_TOUCH_WINDOW_EVENTS;
942 this.started = false;
944 Input.apply(this, arguments);
947 inherit(SingleTouchInput, Input, {
948 handler: function TEhandler(ev) {
949 var type = SINGLE_TOUCH_INPUT_MAP[ev.type];
951 // should we handle the touch events?
952 if (type === INPUT_START) {
960 var touches = normalizeSingleTouches.call(this, ev, type);
962 // when done, reset the started state
963 if (type & (INPUT_END | INPUT_CANCEL) && touches[0].length - touches[1].length === 0) {
964 this.started = false;
967 this.callback(this.manager, type, {
968 pointers: touches[0],
969 changedPointers: touches[1],
970 pointerType: INPUT_TYPE_TOUCH,
979 * @param {Number} type flag
980 * @returns {undefined|Array} [all, changed]
982 function normalizeSingleTouches(ev, type) {
983 var all = toArray(ev.touches);
984 var changed = toArray(ev.changedTouches);
986 if (type & (INPUT_END | INPUT_CANCEL)) {
987 all = uniqueArray(all.concat(changed), 'identifier', true);
990 return [all, changed];
993 var TOUCH_INPUT_MAP = {
994 touchstart: INPUT_START,
995 touchmove: INPUT_MOVE,
997 touchcancel: INPUT_CANCEL
1000 var TOUCH_TARGET_EVENTS = 'touchstart touchmove touchend touchcancel';
1003 * Multi-user touch events input
1007 function TouchInput() {
1008 this.evTarget = TOUCH_TARGET_EVENTS;
1009 this.targetIds = {};
1011 Input.apply(this, arguments);
1014 inherit(TouchInput, Input, {
1015 handler: function MTEhandler(ev) {
1016 var type = TOUCH_INPUT_MAP[ev.type];
1017 var touches = getTouches.call(this, ev, type);
1022 this.callback(this.manager, type, {
1023 pointers: touches[0],
1024 changedPointers: touches[1],
1025 pointerType: INPUT_TYPE_TOUCH,
1032 * @this {TouchInput}
1033 * @param {Object} ev
1034 * @param {Number} type flag
1035 * @returns {undefined|Array} [all, changed]
1037 function getTouches(ev, type) {
1038 var allTouches = toArray(ev.touches);
1039 var targetIds = this.targetIds;
1041 // when there is only one touch, the process can be simplified
1042 if (type & (INPUT_START | INPUT_MOVE) && allTouches.length === 1) {
1043 targetIds[allTouches[0].identifier] = true;
1044 return [allTouches, allTouches];
1049 changedTouches = toArray(ev.changedTouches),
1050 changedTargetTouches = [],
1051 target = this.target;
1053 // get target touches from touches
1054 targetTouches = allTouches.filter(function(touch) {
1055 return hasParent(touch.target, target);
1059 if (type === INPUT_START) {
1061 while (i < targetTouches.length) {
1062 targetIds[targetTouches[i].identifier] = true;
1067 // filter changed touches to only contain touches that exist in the collected target ids
1069 while (i < changedTouches.length) {
1070 if (targetIds[changedTouches[i].identifier]) {
1071 changedTargetTouches.push(changedTouches[i]);
1074 // cleanup removed touches
1075 if (type & (INPUT_END | INPUT_CANCEL)) {
1076 delete targetIds[changedTouches[i].identifier];
1081 if (!changedTargetTouches.length) {
1086 // merge targetTouches with changedTargetTouches so it contains ALL touches, including 'end' and 'cancel'
1087 uniqueArray(targetTouches.concat(changedTargetTouches), 'identifier', true),
1088 changedTargetTouches
1093 * Combined touch and mouse input
1095 * Touch has a higher priority then mouse, and while touching no mouse events are allowed.
1096 * This because touch devices also emit mouse events while doing a touch.
1101 function TouchMouseInput() {
1102 Input.apply(this, arguments);
1104 var handler = bindFn(this.handler, this);
1105 this.touch = new TouchInput(this.manager, handler);
1106 this.mouse = new MouseInput(this.manager, handler);
1109 inherit(TouchMouseInput, Input, {
1111 * handle mouse and touch events
1112 * @param {Hammer} manager
1113 * @param {String} inputEvent
1114 * @param {Object} inputData
1116 handler: function TMEhandler(manager, inputEvent, inputData) {
1117 var isTouch = (inputData.pointerType == INPUT_TYPE_TOUCH),
1118 isMouse = (inputData.pointerType == INPUT_TYPE_MOUSE);
1120 // when we're in a touch event, so block all upcoming mouse events
1121 // most mobile browser also emit mouseevents, right after touchstart
1123 this.mouse.allow = false;
1124 } else if (isMouse && !this.mouse.allow) {
1128 // reset the allowMouse when we're done
1129 if (inputEvent & (INPUT_END | INPUT_CANCEL)) {
1130 this.mouse.allow = true;
1133 this.callback(manager, inputEvent, inputData);
1137 * remove the event listeners
1139 destroy: function destroy() {
1140 this.touch.destroy();
1141 this.mouse.destroy();
1145 var PREFIXED_TOUCH_ACTION = prefixed(TEST_ELEMENT.style, 'touchAction');
1146 var NATIVE_TOUCH_ACTION = PREFIXED_TOUCH_ACTION !== undefined;
1148 // magical touchAction value
1149 var TOUCH_ACTION_COMPUTE = 'compute';
1150 var TOUCH_ACTION_AUTO = 'auto';
1151 var TOUCH_ACTION_MANIPULATION = 'manipulation'; // not implemented
1152 var TOUCH_ACTION_NONE = 'none';
1153 var TOUCH_ACTION_PAN_X = 'pan-x';
1154 var TOUCH_ACTION_PAN_Y = 'pan-y';
1158 * sets the touchAction property or uses the js alternative
1159 * @param {Manager} manager
1160 * @param {String} value
1163 function TouchAction(manager, value) {
1164 this.manager = manager;
1168 TouchAction.prototype = {
1170 * set the touchAction value on the element or enable the polyfill
1171 * @param {String} value
1173 set: function(value) {
1174 // find out the touch-action by the event handlers
1175 if (value == TOUCH_ACTION_COMPUTE) {
1176 value = this.compute();
1179 if (NATIVE_TOUCH_ACTION && this.manager.element.style) {
1180 this.manager.element.style[PREFIXED_TOUCH_ACTION] = value;
1182 this.actions = value.toLowerCase().trim();
1186 * just re-set the touchAction value
1188 update: function() {
1189 this.set(this.manager.options.touchAction);
1193 * compute the value for the touchAction property based on the recognizer's settings
1194 * @returns {String} value
1196 compute: function() {
1198 each(this.manager.recognizers, function(recognizer) {
1199 if (boolOrFn(recognizer.options.enable, [recognizer])) {
1200 actions = actions.concat(recognizer.getTouchAction());
1203 return cleanTouchActions(actions.join(' '));
1207 * this method is called on each input cycle and provides the preventing of the browser behavior
1208 * @param {Object} input
1210 preventDefaults: function(input) {
1211 // not needed with native support for the touchAction property
1212 if (NATIVE_TOUCH_ACTION) {
1216 var srcEvent = input.srcEvent;
1217 var direction = input.offsetDirection;
1219 // if the touch action did prevented once this session
1220 if (this.manager.session.prevented) {
1221 srcEvent.preventDefault();
1225 var actions = this.actions;
1226 var hasNone = inStr(actions, TOUCH_ACTION_NONE);
1227 var hasPanY = inStr(actions, TOUCH_ACTION_PAN_Y);
1228 var hasPanX = inStr(actions, TOUCH_ACTION_PAN_X);
1231 //do not prevent defaults if this is a tap gesture
1233 var isTapPointer = input.pointers.length === 1;
1234 var isTapMovement = input.distance < 2;
1235 var isTapTouchTime = input.deltaTime < 250;
1237 if (isTapPointer && isTapMovement && isTapTouchTime) {
1242 if (hasPanX && hasPanY) {
1243 // `pan-x pan-y` means browser handles all scrolling/panning, do not prevent
1248 (hasPanY && direction & DIRECTION_HORIZONTAL) ||
1249 (hasPanX && direction & DIRECTION_VERTICAL)) {
1250 return this.preventSrc(srcEvent);
1255 * call preventDefault to prevent the browser's default behavior (scrolling in most cases)
1256 * @param {Object} srcEvent
1258 preventSrc: function(srcEvent) {
1259 this.manager.session.prevented = true;
1260 srcEvent.preventDefault();
1265 * when the touchActions are collected they are not a valid value, so we need to clean things up. *
1266 * @param {String} actions
1269 function cleanTouchActions(actions) {
1271 if (inStr(actions, TOUCH_ACTION_NONE)) {
1272 return TOUCH_ACTION_NONE;
1275 var hasPanX = inStr(actions, TOUCH_ACTION_PAN_X);
1276 var hasPanY = inStr(actions, TOUCH_ACTION_PAN_Y);
1278 // if both pan-x and pan-y are set (different recognizers
1279 // for different directions, e.g. horizontal pan but vertical swipe?)
1280 // we need none (as otherwise with pan-x pan-y combined none of these
1281 // recognizers will work, since the browser would handle all panning
1282 if (hasPanX && hasPanY) {
1283 return TOUCH_ACTION_NONE;
1287 if (hasPanX || hasPanY) {
1288 return hasPanX ? TOUCH_ACTION_PAN_X : TOUCH_ACTION_PAN_Y;
1292 if (inStr(actions, TOUCH_ACTION_MANIPULATION)) {
1293 return TOUCH_ACTION_MANIPULATION;
1296 return TOUCH_ACTION_AUTO;
1300 * Recognizer flow explained; *
1301 * All recognizers have the initial state of POSSIBLE when a input session starts.
1302 * The definition of a input session is from the first input until the last input, with all it's movement in it. *
1303 * Example session for mouse-input: mousedown -> mousemove -> mouseup
1305 * On each recognizing cycle (see Manager.recognize) the .recognize() method is executed
1306 * which determines with state it should be.
1308 * If the recognizer has the state FAILED, CANCELLED or RECOGNIZED (equals ENDED), it is reset to
1309 * POSSIBLE to give it another change on the next cycle.
1313 * +-----+---------------+
1317 * Failed Cancelled |
1326 var STATE_POSSIBLE = 1;
1327 var STATE_BEGAN = 2;
1328 var STATE_CHANGED = 4;
1329 var STATE_ENDED = 8;
1330 var STATE_RECOGNIZED = STATE_ENDED;
1331 var STATE_CANCELLED = 16;
1332 var STATE_FAILED = 32;
1336 * Every recognizer needs to extend from this class.
1338 * @param {Object} options
1340 function Recognizer(options) {
1341 this.options = assign({}, this.defaults, options || {});
1343 this.id = uniqueId();
1345 this.manager = null;
1347 // default is enable true
1348 this.options.enable = ifUndefined(this.options.enable, true);
1350 this.state = STATE_POSSIBLE;
1352 this.simultaneous = {};
1353 this.requireFail = [];
1356 Recognizer.prototype = {
1365 * @param {Object} options
1366 * @return {Recognizer}
1368 set: function(options) {
1369 assign(this.options, options);
1371 // also update the touchAction, in case something changed about the directions/enabled state
1372 this.manager && this.manager.touchAction.update();
1377 * recognize simultaneous with an other recognizer.
1378 * @param {Recognizer} otherRecognizer
1379 * @returns {Recognizer} this
1381 recognizeWith: function(otherRecognizer) {
1382 if (invokeArrayArg(otherRecognizer, 'recognizeWith', this)) {
1386 var simultaneous = this.simultaneous;
1387 otherRecognizer = getRecognizerByNameIfManager(otherRecognizer, this);
1388 if (!simultaneous[otherRecognizer.id]) {
1389 simultaneous[otherRecognizer.id] = otherRecognizer;
1390 otherRecognizer.recognizeWith(this);
1396 * drop the simultaneous link. it doesnt remove the link on the other recognizer.
1397 * @param {Recognizer} otherRecognizer
1398 * @returns {Recognizer} this
1400 dropRecognizeWith: function(otherRecognizer) {
1401 if (invokeArrayArg(otherRecognizer, 'dropRecognizeWith', this)) {
1405 otherRecognizer = getRecognizerByNameIfManager(otherRecognizer, this);
1406 delete this.simultaneous[otherRecognizer.id];
1411 * recognizer can only run when an other is failing
1412 * @param {Recognizer} otherRecognizer
1413 * @returns {Recognizer} this
1415 requireFailure: function(otherRecognizer) {
1416 if (invokeArrayArg(otherRecognizer, 'requireFailure', this)) {
1420 var requireFail = this.requireFail;
1421 otherRecognizer = getRecognizerByNameIfManager(otherRecognizer, this);
1422 if (inArray(requireFail, otherRecognizer) === -1) {
1423 requireFail.push(otherRecognizer);
1424 otherRecognizer.requireFailure(this);
1430 * drop the requireFailure link. it does not remove the link on the other recognizer.
1431 * @param {Recognizer} otherRecognizer
1432 * @returns {Recognizer} this
1434 dropRequireFailure: function(otherRecognizer) {
1435 if (invokeArrayArg(otherRecognizer, 'dropRequireFailure', this)) {
1439 otherRecognizer = getRecognizerByNameIfManager(otherRecognizer, this);
1440 var index = inArray(this.requireFail, otherRecognizer);
1442 this.requireFail.splice(index, 1);
1448 * has require failures boolean
1449 * @returns {boolean}
1451 hasRequireFailures: function() {
1452 return this.requireFail.length > 0;
1456 * if the recognizer can recognize simultaneous with an other recognizer
1457 * @param {Recognizer} otherRecognizer
1458 * @returns {Boolean}
1460 canRecognizeWith: function(otherRecognizer) {
1461 return !!this.simultaneous[otherRecognizer.id];
1465 * You should use `tryEmit` instead of `emit` directly to check
1466 * that all the needed recognizers has failed before emitting.
1467 * @param {Object} input
1469 emit: function(input) {
1471 var state = this.state;
1473 function emit(event) {
1474 self.manager.emit(event, input);
1477 // 'panstart' and 'panmove'
1478 if (state < STATE_ENDED) {
1479 emit(self.options.event + stateStr(state));
1482 emit(self.options.event); // simple 'eventName' events
1484 if (input.additionalEvent) { // additional event(panleft, panright, pinchin, pinchout...)
1485 emit(input.additionalEvent);
1488 // panend and pancancel
1489 if (state >= STATE_ENDED) {
1490 emit(self.options.event + stateStr(state));
1495 * Check that all the require failure recognizers has failed,
1496 * if true, it emits a gesture event,
1497 * otherwise, setup the state to FAILED.
1498 * @param {Object} input
1500 tryEmit: function(input) {
1501 if (this.canEmit()) {
1502 return this.emit(input);
1504 // it's failing anyway
1505 this.state = STATE_FAILED;
1510 * @returns {boolean}
1512 canEmit: function() {
1514 while (i < this.requireFail.length) {
1515 if (!(this.requireFail[i].state & (STATE_FAILED | STATE_POSSIBLE))) {
1524 * update the recognizer
1525 * @param {Object} inputData
1527 recognize: function(inputData) {
1528 // make a new copy of the inputData
1529 // so we can change the inputData without messing up the other recognizers
1530 var inputDataClone = assign({}, inputData);
1532 // is is enabled and allow recognizing?
1533 if (!boolOrFn(this.options.enable, [this, inputDataClone])) {
1535 this.state = STATE_FAILED;
1539 // reset when we've reached the end
1540 if (this.state & (STATE_RECOGNIZED | STATE_CANCELLED | STATE_FAILED)) {
1541 this.state = STATE_POSSIBLE;
1544 this.state = this.process(inputDataClone);
1546 // the recognizer has recognized a gesture
1547 // so trigger an event
1548 if (this.state & (STATE_BEGAN | STATE_CHANGED | STATE_ENDED | STATE_CANCELLED)) {
1549 this.tryEmit(inputDataClone);
1554 * return the state of the recognizer
1555 * the actual recognizing happens in this method
1557 * @param {Object} inputData
1558 * @returns {Const} STATE
1560 process: function(inputData) { }, // jshint ignore:line
1563 * return the preferred touch-action
1567 getTouchAction: function() { },
1570 * called when the gesture isn't allowed to recognize
1571 * like when another is being recognized or it is disabled
1574 reset: function() { }
1578 * get a usable string, used as event postfix
1579 * @param {Const} state
1580 * @returns {String} state
1582 function stateStr(state) {
1583 if (state & STATE_CANCELLED) {
1585 } else if (state & STATE_ENDED) {
1587 } else if (state & STATE_CHANGED) {
1589 } else if (state & STATE_BEGAN) {
1596 * direction cons to string
1597 * @param {Const} direction
1600 function directionStr(direction) {
1601 if (direction == DIRECTION_DOWN) {
1603 } else if (direction == DIRECTION_UP) {
1605 } else if (direction == DIRECTION_LEFT) {
1607 } else if (direction == DIRECTION_RIGHT) {
1614 * get a recognizer by name if it is bound to a manager
1615 * @param {Recognizer|String} otherRecognizer
1616 * @param {Recognizer} recognizer
1617 * @returns {Recognizer}
1619 function getRecognizerByNameIfManager(otherRecognizer, recognizer) {
1620 var manager = recognizer.manager;
1622 return manager.get(otherRecognizer);
1624 return otherRecognizer;
1628 * This recognizer is just used as a base for the simple attribute recognizers.
1630 * @extends Recognizer
1632 function AttrRecognizer() {
1633 Recognizer.apply(this, arguments);
1636 inherit(AttrRecognizer, Recognizer, {
1639 * @memberof AttrRecognizer
1650 * Used to check if it the recognizer receives valid input, like input.distance > 10.
1651 * @memberof AttrRecognizer
1652 * @param {Object} input
1653 * @returns {Boolean} recognized
1655 attrTest: function(input) {
1656 var optionPointers = this.options.pointers;
1657 return optionPointers === 0 || input.pointers.length === optionPointers;
1661 * Process the input and return the state for the recognizer
1662 * @memberof AttrRecognizer
1663 * @param {Object} input
1664 * @returns {*} State
1666 process: function(input) {
1667 var state = this.state;
1668 var eventType = input.eventType;
1670 var isRecognized = state & (STATE_BEGAN | STATE_CHANGED);
1671 var isValid = this.attrTest(input);
1673 // on cancel input and we've recognized before, return STATE_CANCELLED
1674 if (isRecognized && (eventType & INPUT_CANCEL || !isValid)) {
1675 return state | STATE_CANCELLED;
1676 } else if (isRecognized || isValid) {
1677 if (eventType & INPUT_END) {
1678 return state | STATE_ENDED;
1679 } else if (!(state & STATE_BEGAN)) {
1682 return state | STATE_CHANGED;
1684 return STATE_FAILED;
1690 * Recognized when the pointer is down and moved in the allowed direction.
1692 * @extends AttrRecognizer
1694 function PanRecognizer() {
1695 AttrRecognizer.apply(this, arguments);
1701 inherit(PanRecognizer, AttrRecognizer, {
1704 * @memberof PanRecognizer
1710 direction: DIRECTION_ALL
1713 getTouchAction: function() {
1714 var direction = this.options.direction;
1716 if (direction & DIRECTION_HORIZONTAL) {
1717 actions.push(TOUCH_ACTION_PAN_Y);
1719 if (direction & DIRECTION_VERTICAL) {
1720 actions.push(TOUCH_ACTION_PAN_X);
1725 directionTest: function(input) {
1726 var options = this.options;
1727 var hasMoved = true;
1728 var distance = input.distance;
1729 var direction = input.direction;
1730 var x = input.deltaX;
1731 var y = input.deltaY;
1734 if (!(direction & options.direction)) {
1735 if (options.direction & DIRECTION_HORIZONTAL) {
1736 direction = (x === 0) ? DIRECTION_NONE : (x < 0) ? DIRECTION_LEFT : DIRECTION_RIGHT;
1737 hasMoved = x != this.pX;
1738 distance = Math.abs(input.deltaX);
1740 direction = (y === 0) ? DIRECTION_NONE : (y < 0) ? DIRECTION_UP : DIRECTION_DOWN;
1741 hasMoved = y != this.pY;
1742 distance = Math.abs(input.deltaY);
1745 input.direction = direction;
1746 return hasMoved && distance > options.threshold && direction & options.direction;
1749 attrTest: function(input) {
1750 return AttrRecognizer.prototype.attrTest.call(this, input) &&
1751 (this.state & STATE_BEGAN || (!(this.state & STATE_BEGAN) && this.directionTest(input)));
1754 emit: function(input) {
1756 this.pX = input.deltaX;
1757 this.pY = input.deltaY;
1759 var direction = directionStr(input.direction);
1762 input.additionalEvent = this.options.event + direction;
1764 this._super.emit.call(this, input);
1770 * Recognized when two or more pointers are moving toward (zoom-in) or away from each other (zoom-out).
1772 * @extends AttrRecognizer
1774 function PinchRecognizer() {
1775 AttrRecognizer.apply(this, arguments);
1778 inherit(PinchRecognizer, AttrRecognizer, {
1781 * @memberof PinchRecognizer
1789 getTouchAction: function() {
1790 return [TOUCH_ACTION_NONE];
1793 attrTest: function(input) {
1794 return this._super.attrTest.call(this, input) &&
1795 (Math.abs(input.scale - 1) > this.options.threshold || this.state & STATE_BEGAN);
1798 emit: function(input) {
1799 if (input.scale !== 1) {
1800 var inOut = input.scale < 1 ? 'in' : 'out';
1801 input.additionalEvent = this.options.event + inOut;
1803 this._super.emit.call(this, input);
1809 * Recognized when the pointer is down for x ms without any movement.
1811 * @extends Recognizer
1813 function PressRecognizer() {
1814 Recognizer.apply(this, arguments);
1820 inherit(PressRecognizer, Recognizer, {
1823 * @memberof PressRecognizer
1828 time: 251, // minimal time of the pointer to be pressed
1829 threshold: 9 // a minimal movement is ok, but keep it low
1832 getTouchAction: function() {
1833 return [TOUCH_ACTION_AUTO];
1836 process: function(input) {
1837 var options = this.options;
1838 var validPointers = input.pointers.length === options.pointers;
1839 var validMovement = input.distance < options.threshold;
1840 var validTime = input.deltaTime > options.time;
1842 this._input = input;
1844 // we only allow little movement
1845 // and we've reached an end event, so a tap is possible
1846 if (!validMovement || !validPointers || (input.eventType & (INPUT_END | INPUT_CANCEL) && !validTime)) {
1848 } else if (input.eventType & INPUT_START) {
1850 this._timer = setTimeoutContext(function() {
1851 this.state = STATE_RECOGNIZED;
1853 }, options.time, this);
1854 } else if (input.eventType & INPUT_END) {
1855 return STATE_RECOGNIZED;
1857 return STATE_FAILED;
1861 clearTimeout(this._timer);
1864 emit: function(input) {
1865 if (this.state !== STATE_RECOGNIZED) {
1869 if (input && (input.eventType & INPUT_END)) {
1870 this.manager.emit(this.options.event + 'up', input);
1872 this._input.timeStamp = now();
1873 this.manager.emit(this.options.event, this._input);
1880 * Recognized when two or more pointer are moving in a circular motion.
1882 * @extends AttrRecognizer
1884 function RotateRecognizer() {
1885 AttrRecognizer.apply(this, arguments);
1888 inherit(RotateRecognizer, AttrRecognizer, {
1891 * @memberof RotateRecognizer
1899 getTouchAction: function() {
1900 return [TOUCH_ACTION_NONE];
1903 attrTest: function(input) {
1904 return this._super.attrTest.call(this, input) &&
1905 (Math.abs(input.rotation) > this.options.threshold || this.state & STATE_BEGAN);
1911 * Recognized when the pointer is moving fast (velocity), with enough distance in the allowed direction.
1913 * @extends AttrRecognizer
1915 function SwipeRecognizer() {
1916 AttrRecognizer.apply(this, arguments);
1919 inherit(SwipeRecognizer, AttrRecognizer, {
1922 * @memberof SwipeRecognizer
1928 direction: DIRECTION_HORIZONTAL | DIRECTION_VERTICAL,
1932 getTouchAction: function() {
1933 return PanRecognizer.prototype.getTouchAction.call(this);
1936 attrTest: function(input) {
1937 var direction = this.options.direction;
1940 if (direction & (DIRECTION_HORIZONTAL | DIRECTION_VERTICAL)) {
1941 velocity = input.overallVelocity;
1942 } else if (direction & DIRECTION_HORIZONTAL) {
1943 velocity = input.overallVelocityX;
1944 } else if (direction & DIRECTION_VERTICAL) {
1945 velocity = input.overallVelocityY;
1948 return this._super.attrTest.call(this, input) &&
1949 direction & input.offsetDirection &&
1950 input.distance > this.options.threshold &&
1951 input.maxPointers == this.options.pointers &&
1952 abs(velocity) > this.options.velocity && input.eventType & INPUT_END;
1955 emit: function(input) {
1956 var direction = directionStr(input.offsetDirection);
1958 this.manager.emit(this.options.event + direction, input);
1961 this.manager.emit(this.options.event, input);
1966 * A tap is ecognized when the pointer is doing a small tap/click. Multiple taps are recognized if they occur
1967 * between the given interval and position. The delay option can be used to recognize multi-taps without firing
1970 * The eventData from the emitted event contains the property `tapCount`, which contains the amount of
1971 * multi-taps being recognized.
1973 * @extends Recognizer
1975 function TapRecognizer() {
1976 Recognizer.apply(this, arguments);
1978 // previous time and center,
1979 // used for tap counting
1981 this.pCenter = false;
1988 inherit(TapRecognizer, Recognizer, {
1991 * @memberof PinchRecognizer
1997 interval: 300, // max time between the multi-tap taps
1998 time: 250, // max time of the pointer to be down (like finger on the screen)
1999 threshold: 9, // a minimal movement is ok, but keep it low
2000 posThreshold: 10 // a multi-tap can be a bit off the initial position
2003 getTouchAction: function() {
2004 return [TOUCH_ACTION_MANIPULATION];
2007 process: function(input) {
2008 var options = this.options;
2010 var validPointers = input.pointers.length === options.pointers;
2011 var validMovement = input.distance < options.threshold;
2012 var validTouchTime = input.deltaTime < options.time;
2016 if ((input.eventType & INPUT_START) && (this.count === 0)) {
2017 return this.failTimeout();
2020 // we only allow little movement
2021 // and we've reached an end event, so a tap is possible
2022 if (validMovement && validTouchTime && validPointers) {
2023 if (input.eventType != INPUT_END) {
2024 return this.failTimeout();
2027 var validInterval = this.pTime ? (input.timeStamp - this.pTime < options.interval) : true;
2028 var validMultiTap = !this.pCenter || getDistance(this.pCenter, input.center) < options.posThreshold;
2030 this.pTime = input.timeStamp;
2031 this.pCenter = input.center;
2033 if (!validMultiTap || !validInterval) {
2039 this._input = input;
2041 // if tap count matches we have recognized it,
2042 // else it has began recognizing...
2043 var tapCount = this.count % options.taps;
2044 if (tapCount === 0) {
2045 // no failing requirements, immediately trigger the tap event
2046 // or wait as long as the multitap interval to trigger
2047 if (!this.hasRequireFailures()) {
2048 return STATE_RECOGNIZED;
2050 this._timer = setTimeoutContext(function() {
2051 this.state = STATE_RECOGNIZED;
2053 }, options.interval, this);
2058 return STATE_FAILED;
2061 failTimeout: function() {
2062 this._timer = setTimeoutContext(function() {
2063 this.state = STATE_FAILED;
2064 }, this.options.interval, this);
2065 return STATE_FAILED;
2069 clearTimeout(this._timer);
2073 if (this.state == STATE_RECOGNIZED) {
2074 this._input.tapCount = this.count;
2075 this.manager.emit(this.options.event, this._input);
2081 * Simple way to create a manager with a default set of recognizers.
2082 * @param {HTMLElement} element
2083 * @param {Object} [options]
2086 function Hammer(element, options) {
2087 options = options || {};
2088 options.recognizers = ifUndefined(options.recognizers, Hammer.defaults.preset);
2089 return new Manager(element, options);
2095 Hammer.VERSION = '2.0.6';
2103 * set if DOM events are being triggered.
2104 * But this is slower and unused by simple implementations, so disabled by default.
2111 * The value for the touchAction property/fallback.
2112 * When set to `compute` it will magically set the correct value based on the added recognizers.
2116 touchAction: TOUCH_ACTION_COMPUTE,
2125 * EXPERIMENTAL FEATURE -- can be removed/changed
2126 * Change the parent input target element.
2127 * If Null, then it is being set the to main element.
2128 * @type {Null|EventTarget}
2134 * force an input class
2135 * @type {Null|Function}
2141 * Default recognizer setup when calling `Hammer()`
2142 * When creating a new Manager these will be skipped.
2146 // RecognizerClass, options, [recognizeWith, ...], [requireFailure, ...]
2147 [RotateRecognizer, {enable: false}],
2148 [PinchRecognizer, {enable: false}, ['rotate']],
2149 [SwipeRecognizer, {direction: DIRECTION_HORIZONTAL}],
2150 [PanRecognizer, {direction: DIRECTION_HORIZONTAL}, ['swipe']],
2152 [TapRecognizer, {event: 'doubletap', taps: 2}, ['tap']],
2157 * Some CSS properties can be used to improve the working of Hammer.
2158 * Add them to this method and they will be set when creating a new Manager.
2163 * Disables text selection to improve the dragging gesture. Mainly for desktop browsers.
2170 * Disable the Windows Phone grippers when pressing an element.
2174 touchSelect: 'none',
2177 * Disables the default callout shown when you touch and hold a touch target.
2178 * On iOS, when you touch and hold a touch target such as a link, Safari displays
2179 * a callout containing information about the link. This property allows you to disable that callout.
2183 touchCallout: 'none',
2186 * Specifies whether zooming is enabled. Used by IE10>
2190 contentZooming: 'none',
2193 * Specifies that an entire element should be draggable instead of its contents. Mainly for desktop browsers.
2200 * Overrides the highlight color shown when the user taps a link or a JavaScript
2201 * clickable element in iOS. This property obeys the alpha value, if specified.
2203 * @default 'rgba(0,0,0,0)'
2205 tapHighlightColor: 'rgba(0,0,0,0)'
2210 var FORCED_STOP = 2;
2214 * @param {HTMLElement} element
2215 * @param {Object} [options]
2218 function Manager(element, options) {
2219 this.options = assign({}, Hammer.defaults, options || {});
2221 this.options.inputTarget = this.options.inputTarget || element;
2225 this.recognizers = [];
2227 this.element = element;
2228 this.input = createInputInstance(this);
2229 this.touchAction = new TouchAction(this, this.options.touchAction);
2231 toggleCssProps(this, true);
2233 each(this.options.recognizers, function(item) {
2234 var recognizer = this.add(new (item[0])(item[1]));
2235 item[2] && recognizer.recognizeWith(item[2]);
2236 item[3] && recognizer.requireFailure(item[3]);
2240 Manager.prototype = {
2243 * @param {Object} options
2244 * @returns {Manager}
2246 set: function(options) {
2247 assign(this.options, options);
2249 // Options that need a little more setup
2250 if (options.touchAction) {
2251 this.touchAction.update();
2253 if (options.inputTarget) {
2254 // Clean up existing event listeners and reinitialize
2255 this.input.destroy();
2256 this.input.target = options.inputTarget;
2263 * stop recognizing for this session.
2264 * This session will be discarded, when a new [input]start event is fired.
2265 * When forced, the recognizer cycle is stopped immediately.
2266 * @param {Boolean} [force]
2268 stop: function(force) {
2269 this.session.stopped = force ? FORCED_STOP : STOP;
2273 * run the recognizers!
2274 * called by the inputHandler function on every movement of the pointers (touches)
2275 * it walks through all the recognizers and tries to detect the gesture that is being made
2276 * @param {Object} inputData
2278 recognize: function(inputData) {
2279 var session = this.session;
2280 if (session.stopped) {
2284 // run the touch-action polyfill
2285 this.touchAction.preventDefaults(inputData);
2288 var recognizers = this.recognizers;
2290 // this holds the recognizer that is being recognized.
2291 // so the recognizer's state needs to be BEGAN, CHANGED, ENDED or RECOGNIZED
2292 // if no recognizer is detecting a thing, it is set to `null`
2293 var curRecognizer = session.curRecognizer;
2295 // reset when the last recognizer is recognized
2296 // or when we're in a new session
2297 if (!curRecognizer || (curRecognizer && curRecognizer.state & STATE_RECOGNIZED)) {
2298 curRecognizer = session.curRecognizer = null;
2302 while (i < recognizers.length) {
2303 recognizer = recognizers[i];
2305 // find out if we are allowed try to recognize the input for this one.
2306 // 1. allow if the session is NOT forced stopped (see the .stop() method)
2307 // 2. allow if we still haven't recognized a gesture in this session, or the this recognizer is the one
2308 // that is being recognized.
2309 // 3. allow if the recognizer is allowed to run simultaneous with the current recognized recognizer.
2310 // this can be setup with the `recognizeWith()` method on the recognizer.
2311 if (session.stopped !== FORCED_STOP && ( // 1
2312 !curRecognizer || recognizer == curRecognizer || // 2
2313 recognizer.canRecognizeWith(curRecognizer))) { // 3
2314 recognizer.recognize(inputData);
2319 // if the recognizer has been recognizing the input as a valid gesture, we want to store this one as the
2320 // current active recognizer. but only if we don't already have an active recognizer
2321 if (!curRecognizer && recognizer.state & (STATE_BEGAN | STATE_CHANGED | STATE_ENDED)) {
2322 curRecognizer = session.curRecognizer = recognizer;
2329 * get a recognizer by its event name.
2330 * @param {Recognizer|String} recognizer
2331 * @returns {Recognizer|Null}
2333 get: function(recognizer) {
2334 if (recognizer instanceof Recognizer) {
2338 var recognizers = this.recognizers;
2339 for (var i = 0; i < recognizers.length; i++) {
2340 if (recognizers[i].options.event == recognizer) {
2341 return recognizers[i];
2348 * add a recognizer to the manager
2349 * existing recognizers with the same event name will be removed
2350 * @param {Recognizer} recognizer
2351 * @returns {Recognizer|Manager}
2353 add: function(recognizer) {
2354 if (invokeArrayArg(recognizer, 'add', this)) {
2359 var existing = this.get(recognizer.options.event);
2361 this.remove(existing);
2364 this.recognizers.push(recognizer);
2365 recognizer.manager = this;
2367 this.touchAction.update();
2372 * remove a recognizer by name or instance
2373 * @param {Recognizer|String} recognizer
2374 * @returns {Manager}
2376 remove: function(recognizer) {
2377 if (invokeArrayArg(recognizer, 'remove', this)) {
2381 recognizer = this.get(recognizer);
2383 // let's make sure this recognizer exists
2385 var recognizers = this.recognizers;
2386 var index = inArray(recognizers, recognizer);
2389 recognizers.splice(index, 1);
2390 this.touchAction.update();
2399 * @param {String} events
2400 * @param {Function} handler
2401 * @returns {EventEmitter} this
2403 on: function(events, handler) {
2404 var handlers = this.handlers;
2405 each(splitStr(events), function(event) {
2406 handlers[event] = handlers[event] || [];
2407 handlers[event].push(handler);
2413 * unbind event, leave emit blank to remove all handlers
2414 * @param {String} events
2415 * @param {Function} [handler]
2416 * @returns {EventEmitter} this
2418 off: function(events, handler) {
2419 var handlers = this.handlers;
2420 each(splitStr(events), function(event) {
2422 delete handlers[event];
2424 handlers[event] && handlers[event].splice(inArray(handlers[event], handler), 1);
2431 * emit event to the listeners
2432 * @param {String} event
2433 * @param {Object} data
2435 emit: function(event, data) {
2436 // we also want to trigger dom events
2437 if (this.options.domEvents) {
2438 triggerDomEvent(event, data);
2441 // no handlers, so skip it all
2442 var handlers = this.handlers[event] && this.handlers[event].slice();
2443 if (!handlers || !handlers.length) {
2448 data.preventDefault = function() {
2449 data.srcEvent.preventDefault();
2453 while (i < handlers.length) {
2460 * destroy the manager and unbinds all events
2461 * it doesn't unbind dom events, that is the user own responsibility
2463 destroy: function() {
2464 this.element && toggleCssProps(this, false);
2468 this.input.destroy();
2469 this.element = null;
2474 * add/remove the css properties as defined in manager.options.cssProps
2475 * @param {Manager} manager
2476 * @param {Boolean} add
2478 function toggleCssProps(manager, add) {
2479 var element = manager.element;
2480 if (!element.style) {
2483 each(manager.options.cssProps, function(value, name) {
2484 element.style[prefixed(element.style, name)] = add ? value : '';
2490 * @param {String} event
2491 * @param {Object} data
2493 function triggerDomEvent(event, data) {
2494 var gestureEvent = document.createEvent('Event');
2495 gestureEvent.initEvent(event, true, true);
2496 gestureEvent.gesture = data;
2497 data.target.dispatchEvent(gestureEvent);
2501 INPUT_START: INPUT_START,
2502 INPUT_MOVE: INPUT_MOVE,
2503 INPUT_END: INPUT_END,
2504 INPUT_CANCEL: INPUT_CANCEL,
2506 STATE_POSSIBLE: STATE_POSSIBLE,
2507 STATE_BEGAN: STATE_BEGAN,
2508 STATE_CHANGED: STATE_CHANGED,
2509 STATE_ENDED: STATE_ENDED,
2510 STATE_RECOGNIZED: STATE_RECOGNIZED,
2511 STATE_CANCELLED: STATE_CANCELLED,
2512 STATE_FAILED: STATE_FAILED,
2514 DIRECTION_NONE: DIRECTION_NONE,
2515 DIRECTION_LEFT: DIRECTION_LEFT,
2516 DIRECTION_RIGHT: DIRECTION_RIGHT,
2517 DIRECTION_UP: DIRECTION_UP,
2518 DIRECTION_DOWN: DIRECTION_DOWN,
2519 DIRECTION_HORIZONTAL: DIRECTION_HORIZONTAL,
2520 DIRECTION_VERTICAL: DIRECTION_VERTICAL,
2521 DIRECTION_ALL: DIRECTION_ALL,
2525 TouchAction: TouchAction,
2527 TouchInput: TouchInput,
2528 MouseInput: MouseInput,
2529 PointerEventInput: PointerEventInput,
2530 TouchMouseInput: TouchMouseInput,
2531 SingleTouchInput: SingleTouchInput,
2533 Recognizer: Recognizer,
2534 AttrRecognizer: AttrRecognizer,
2537 Swipe: SwipeRecognizer,
2538 Pinch: PinchRecognizer,
2539 Rotate: RotateRecognizer,
2540 Press: PressRecognizer,
2542 on: addEventListeners,
2543 off: removeEventListeners,
2553 // this prevents errors when Hammer is loaded in the presence of an AMD
2554 // style loader but by script tag, not by the loader.
2555 var freeGlobal = (typeof window !== 'undefined' ? window : (typeof self !== 'undefined' ? self : {})); // jshint ignore:line
2556 freeGlobal.Hammer = Hammer;
2558 if (typeof define === 'function' && define.amd) {
2562 } else if (typeof module != 'undefined' && module.exports) {
2563 module.exports = Hammer;
2565 window[exportName] = Hammer;
2568 })(window, document, 'Hammer');