/*! hammer.js - v2.0.7 - 2016-04-22 * http://hammerjs.github.io/ * * copyright (c) 2016 jorik tangelder; * licensed under the mit license */ (function(window, document, exportname, undefined) { 'use strict'; var vendor_prefixes = ['', 'webkit', 'moz', 'ms', 'ms', 'o']; var test_element = document.createelement('div'); var type_function = 'function'; var round = math.round; var abs = math.abs; var now = date.now; /** * set a timeout with a given scope * @param {function} fn * @param {number} timeout * @param {object} context * @returns {number} */ function settimeoutcontext(fn, timeout, context) { return settimeout(bindfn(fn, context), timeout); } /** * if the argument is an array, we want to execute the fn on each entry * if it aint an array we don't want to do a thing. * this is used by all the methods that accept a single and array argument. * @param {*|array} arg * @param {string} fn * @param {object} [context] * @returns {boolean} */ function invokearrayarg(arg, fn, context) { if (array.isarray(arg)) { each(arg, context[fn], context); return true; } return false; } /** * walk objects and arrays * @param {object} obj * @param {function} iterator * @param {object} context */ function each(obj, iterator, context) { var i; if (!obj) { return; } if (obj.foreach) { obj.foreach(iterator, context); } else if (obj.length !== undefined) { i = 0; while (i < obj.length) { iterator.call(context, obj[i], i, obj); i++; } } else { for (i in obj) { obj.hasownproperty(i) && iterator.call(context, obj[i], i, obj); } } } /** * wrap a method with a deprecation warning and stack trace * @param {function} method * @param {string} name * @param {string} message * @returns {function} a new function wrapping the supplied method. */ function deprecate(method, name, message) { var deprecationmessage = 'deprecated method: ' + name + '\n' + message + ' at \n'; return function() { var e = new error('get-stack-trace'); var stack = e && e.stack ? e.stack.replace(/^[^\(]+?[\n$]/gm, '') .replace(/^\s+at\s+/gm, '') .replace(/^object.\s*\(/gm, '{anonymous}()@') : 'unknown stack trace'; var log = window.console && (window.console.warn || window.console.log); if (log) { log.call(window.console, deprecationmessage, stack); } return method.apply(this, arguments); }; } /** * extend object. * means that properties in dest will be overwritten by the ones in src. * @param {object} target * @param {...object} objects_to_assign * @returns {object} target */ var assign; if (typeof object.assign !== 'function') { assign = function assign(target) { if (target === undefined || target === null) { throw new typeerror('cannot convert undefined or null to object'); } var output = object(target); for (var index = 1; index < arguments.length; index++) { var source = arguments[index]; if (source !== undefined && source !== null) { for (var nextkey in source) { if (source.hasownproperty(nextkey)) { output[nextkey] = source[nextkey]; } } } } return output; }; } else { assign = object.assign; } /** * extend object. * means that properties in dest will be overwritten by the ones in src. * @param {object} dest * @param {object} src * @param {boolean} [merge=false] * @returns {object} dest */ var extend = deprecate(function extend(dest, src, merge) { var keys = object.keys(src); var i = 0; while (i < keys.length) { if (!merge || (merge && dest[keys[i]] === undefined)) { dest[keys[i]] = src[keys[i]]; } i++; } return dest; }, 'extend', 'use `assign`.'); /** * merge the values from src in the dest. * means that properties that exist in dest will not be overwritten by src * @param {object} dest * @param {object} src * @returns {object} dest */ var merge = deprecate(function merge(dest, src) { return extend(dest, src, true); }, 'merge', 'use `assign`.'); /** * simple class inheritance * @param {function} child * @param {function} base * @param {object} [properties] */ function inherit(child, base, properties) { var basep = base.prototype, childp; childp = child.prototype = object.create(basep); childp.constructor = child; childp._super = basep; if (properties) { assign(childp, properties); } } /** * simple function bind * @param {function} fn * @param {object} context * @returns {function} */ function bindfn(fn, context) { return function boundfn() { return fn.apply(context, arguments); }; } /** * let a boolean value also be a function that must return a boolean * this first item in args will be used as the context * @param {boolean|function} val * @param {array} [args] * @returns {boolean} */ function boolorfn(val, args) { if (typeof val == type_function) { return val.apply(args ? args[0] || undefined : undefined, args); } return val; } /** * use the val2 when val1 is undefined * @param {*} val1 * @param {*} val2 * @returns {*} */ function ifundefined(val1, val2) { return (val1 === undefined) ? val2 : val1; } /** * addeventlistener with multiple events at once * @param {eventtarget} target * @param {string} types * @param {function} handler */ function addeventlisteners(target, types, handler) { each(splitstr(types), function(type) { target.addeventlistener(type, handler, false); }); } /** * removeeventlistener with multiple events at once * @param {eventtarget} target * @param {string} types * @param {function} handler */ function removeeventlisteners(target, types, handler) { each(splitstr(types), function(type) { target.removeeventlistener(type, handler, false); }); } /** * find if a node is in the given parent * @method hasparent * @param {htmlelement} node * @param {htmlelement} parent * @return {boolean} found */ function hasparent(node, parent) { while (node) { if (node == parent) { return true; } node = node.parentnode; } return false; } /** * small indexof wrapper * @param {string} str * @param {string} find * @returns {boolean} found */ function instr(str, find) { return str.indexof(find) > -1; } /** * split string on whitespace * @param {string} str * @returns {array} words */ function splitstr(str) { return str.trim().split(/\s+/g); } /** * find if a array contains the object using indexof or a simple polyfill * @param {array} src * @param {string} find * @param {string} [findbykey] * @return {boolean|number} false when not found, or the index */ function inarray(src, find, findbykey) { if (src.indexof && !findbykey) { return src.indexof(find); } else { var i = 0; while (i < src.length) { if ((findbykey && src[i][findbykey] == find) || (!findbykey && src[i] === find)) { return i; } i++; } return -1; } } /** * convert array-like objects to real arrays * @param {object} obj * @returns {array} */ function toarray(obj) { return array.prototype.slice.call(obj, 0); } /** * unique array with objects based on a key (like 'id') or just by the array's value * @param {array} src [{id:1},{id:2},{id:1}] * @param {string} [key] * @param {boolean} [sort=false] * @returns {array} [{id:1},{id:2}] */ function uniquearray(src, key, sort) { var results = []; var values = []; var i = 0; while (i < src.length) { var val = key ? src[i][key] : src[i]; if (inarray(values, val) < 0) { results.push(src[i]); } values[i] = val; i++; } if (sort) { if (!key) { results = results.sort(); } else { results = results.sort(function sortuniquearray(a, b) { return a[key] > b[key]; }); } } return results; } /** * get the prefixed property * @param {object} obj * @param {string} property * @returns {string|undefined} prefixed */ function prefixed(obj, property) { var prefix, prop; var camelprop = property[0].touppercase() + property.slice(1); var i = 0; while (i < vendor_prefixes.length) { prefix = vendor_prefixes[i]; prop = (prefix) ? prefix + camelprop : property; if (prop in obj) { return prop; } i++; } return undefined; } /** * get a unique id * @returns {number} uniqueid */ var _uniqueid = 1; function uniqueid() { return _uniqueid++; } /** * get the window object of an element * @param {htmlelement} element * @returns {documentview|window} */ function getwindowforelement(element) { var doc = element.ownerdocument || element; return (doc.defaultview || doc.parentwindow || window); } var mobile_regex = /mobile|tablet|ip(ad|hone|od)|android/i; var support_touch = ('ontouchstart' in window); var support_pointer_events = prefixed(window, 'pointerevent') !== undefined; var support_only_touch = support_touch && mobile_regex.test(navigator.useragent); var input_type_touch = 'touch'; var input_type_pen = 'pen'; var input_type_mouse = 'mouse'; var input_type_kinect = 'kinect'; var compute_interval = 25; var input_start = 1; var input_move = 2; var input_end = 4; var input_cancel = 8; var direction_none = 1; var direction_left = 2; var direction_right = 4; var direction_up = 8; var direction_down = 16; var direction_horizontal = direction_left | direction_right; var direction_vertical = direction_up | direction_down; var direction_all = direction_horizontal | direction_vertical; var props_xy = ['x', 'y']; var props_client_xy = ['clientx', 'clienty']; /** * create new input type manager * @param {manager} manager * @param {function} callback * @returns {input} * @constructor */ function input(manager, callback) { var self = this; this.manager = manager; this.callback = callback; this.element = manager.element; this.target = manager.options.inputtarget; // smaller wrapper around the handler, for the scope and the enabled state of the manager, // so when disabled the input events are completely bypassed. this.domhandler = function(ev) { if (boolorfn(manager.options.enable, [manager])) { self.handler(ev); } }; this.init(); } input.prototype = { /** * should handle the inputevent data and trigger the callback * @virtual */ handler: function() { }, /** * bind the events */ init: function() { this.evel && addeventlisteners(this.element, this.evel, this.domhandler); this.evtarget && addeventlisteners(this.target, this.evtarget, this.domhandler); this.evwin && addeventlisteners(getwindowforelement(this.element), this.evwin, this.domhandler); }, /** * unbind the events */ destroy: function() { this.evel && removeeventlisteners(this.element, this.evel, this.domhandler); this.evtarget && removeeventlisteners(this.target, this.evtarget, this.domhandler); this.evwin && removeeventlisteners(getwindowforelement(this.element), this.evwin, this.domhandler); } }; /** * create new input type manager * called by the manager constructor * @param {hammer} manager * @returns {input} */ function createinputinstance(manager) { var type; var inputclass = manager.options.inputclass; if (inputclass) { type = inputclass; } else if (support_pointer_events) { type = pointereventinput; } else if (support_only_touch) { type = touchinput; } else if (!support_touch) { type = mouseinput; } else { type = touchmouseinput; } return new (type)(manager, inputhandler); } /** * handle input events * @param {manager} manager * @param {string} eventtype * @param {object} input */ function inputhandler(manager, eventtype, input) { var pointerslen = input.pointers.length; var changedpointerslen = input.changedpointers.length; var isfirst = (eventtype & input_start && (pointerslen - changedpointerslen === 0)); var isfinal = (eventtype & (input_end | input_cancel) && (pointerslen - changedpointerslen === 0)); input.isfirst = !!isfirst; input.isfinal = !!isfinal; if (isfirst) { manager.session = {}; } // source event is the normalized value of the domevents // like 'touchstart, mouseup, pointerdown' input.eventtype = eventtype; // compute scale, rotation etc computeinputdata(manager, input); // emit secret event manager.emit('hammer.input', input); manager.recognize(input); manager.session.previnput = input; } /** * extend the data with some usable properties like scale, rotate, velocity etc * @param {object} manager * @param {object} input */ function computeinputdata(manager, input) { var session = manager.session; var pointers = input.pointers; var pointerslength = pointers.length; // store the first input to calculate the distance and direction if (!session.firstinput) { session.firstinput = simplecloneinputdata(input); } // to compute scale and rotation we need to store the multiple touches if (pointerslength > 1 && !session.firstmultiple) { session.firstmultiple = simplecloneinputdata(input); } else if (pointerslength === 1) { session.firstmultiple = false; } var firstinput = session.firstinput; var firstmultiple = session.firstmultiple; var offsetcenter = firstmultiple ? firstmultiple.center : firstinput.center; var center = input.center = getcenter(pointers); input.timestamp = now(); input.deltatime = input.timestamp - firstinput.timestamp; input.angle = getangle(offsetcenter, center); input.distance = getdistance(offsetcenter, center); computedeltaxy(session, input); input.offsetdirection = getdirection(input.deltax, input.deltay); var overallvelocity = getvelocity(input.deltatime, input.deltax, input.deltay); input.overallvelocityx = overallvelocity.x; input.overallvelocityy = overallvelocity.y; input.overallvelocity = (abs(overallvelocity.x) > abs(overallvelocity.y)) ? overallvelocity.x : overallvelocity.y; input.scale = firstmultiple ? getscale(firstmultiple.pointers, pointers) : 1; input.rotation = firstmultiple ? getrotation(firstmultiple.pointers, pointers) : 0; input.maxpointers = !session.previnput ? input.pointers.length : ((input.pointers.length > session.previnput.maxpointers) ? input.pointers.length : session.previnput.maxpointers); computeintervalinputdata(session, input); // find the correct target var target = manager.element; if (hasparent(input.srcevent.target, target)) { target = input.srcevent.target; } input.target = target; } function computedeltaxy(session, input) { var center = input.center; var offset = session.offsetdelta || {}; var prevdelta = session.prevdelta || {}; var previnput = session.previnput || {}; if (input.eventtype === input_start || previnput.eventtype === input_end) { prevdelta = session.prevdelta = { x: previnput.deltax || 0, y: previnput.deltay || 0 }; offset = session.offsetdelta = { x: center.x, y: center.y }; } input.deltax = prevdelta.x + (center.x - offset.x); input.deltay = prevdelta.y + (center.y - offset.y); } /** * velocity is calculated every x ms * @param {object} session * @param {object} input */ function computeintervalinputdata(session, input) { var last = session.lastinterval || input, deltatime = input.timestamp - last.timestamp, velocity, velocityx, velocityy, direction; if (input.eventtype != input_cancel && (deltatime > compute_interval || last.velocity === undefined)) { var deltax = input.deltax - last.deltax; var deltay = input.deltay - last.deltay; var v = getvelocity(deltatime, deltax, deltay); velocityx = v.x; velocityy = v.y; velocity = (abs(v.x) > abs(v.y)) ? v.x : v.y; direction = getdirection(deltax, deltay); session.lastinterval = input; } else { // use latest velocity info if it doesn't overtake a minimum period velocity = last.velocity; velocityx = last.velocityx; velocityy = last.velocityy; direction = last.direction; } input.velocity = velocity; input.velocityx = velocityx; input.velocityy = velocityy; input.direction = direction; } /** * create a simple clone from the input used for storage of firstinput and firstmultiple * @param {object} input * @returns {object} clonedinputdata */ function simplecloneinputdata(input) { // make a simple copy of the pointers because we will get a reference if we don't // we only need clientxy for the calculations var pointers = []; var i = 0; while (i < input.pointers.length) { pointers[i] = { clientx: round(input.pointers[i].clientx), clienty: round(input.pointers[i].clienty) }; i++; } return { timestamp: now(), pointers: pointers, center: getcenter(pointers), deltax: input.deltax, deltay: input.deltay }; } /** * get the center of all the pointers * @param {array} pointers * @return {object} center contains `x` and `y` properties */ function getcenter(pointers) { var pointerslength = pointers.length; // no need to loop when only one touch if (pointerslength === 1) { return { x: round(pointers[0].clientx), y: round(pointers[0].clienty) }; } var x = 0, y = 0, i = 0; while (i < pointerslength) { x += pointers[i].clientx; y += pointers[i].clienty; i++; } return { x: round(x / pointerslength), y: round(y / pointerslength) }; } /** * calculate the velocity between two points. unit is in px per ms. * @param {number} deltatime * @param {number} x * @param {number} y * @return {object} velocity `x` and `y` */ function getvelocity(deltatime, x, y) { return { x: x / deltatime || 0, y: y / deltatime || 0 }; } /** * get the direction between two points * @param {number} x * @param {number} y * @return {number} direction */ function getdirection(x, y) { if (x === y) { return direction_none; } if (abs(x) >= abs(y)) { return x < 0 ? direction_left : direction_right; } return y < 0 ? direction_up : direction_down; } /** * calculate the absolute distance between two points * @param {object} p1 {x, y} * @param {object} p2 {x, y} * @param {array} [props] containing x and y keys * @return {number} distance */ function getdistance(p1, p2, props) { if (!props) { props = props_xy; } var x = p2[props[0]] - p1[props[0]], y = p2[props[1]] - p1[props[1]]; return math.sqrt((x * x) + (y * y)); } /** * calculate the angle between two coordinates * @param {object} p1 * @param {object} p2 * @param {array} [props] containing x and y keys * @return {number} angle */ function getangle(p1, p2, props) { if (!props) { props = props_xy; } var x = p2[props[0]] - p1[props[0]], y = p2[props[1]] - p1[props[1]]; return math.atan2(y, x) * 180 / math.pi; } /** * calculate the rotation degrees between two pointersets * @param {array} start array of pointers * @param {array} end array of pointers * @return {number} rotation */ function getrotation(start, end) { return getangle(end[1], end[0], props_client_xy) + getangle(start[1], start[0], props_client_xy); } /** * calculate the scale factor between two pointersets * no scale is 1, and goes down to 0 when pinched together, and bigger when pinched out * @param {array} start array of pointers * @param {array} end array of pointers * @return {number} scale */ function getscale(start, end) { return getdistance(end[0], end[1], props_client_xy) / getdistance(start[0], start[1], props_client_xy); } var mouse_input_map = { mousedown: input_start, mousemove: input_move, mouseup: input_end }; var mouse_element_events = 'mousedown'; var mouse_window_events = 'mousemove mouseup'; /** * mouse events input * @constructor * @extends input */ function mouseinput() { this.evel = mouse_element_events; this.evwin = mouse_window_events; this.pressed = false; // mousedown state input.apply(this, arguments); } inherit(mouseinput, input, { /** * handle mouse events * @param {object} ev */ handler: function mehandler(ev) { var eventtype = mouse_input_map[ev.type]; // on start we want to have the left mouse button down if (eventtype & input_start && ev.button === 0) { this.pressed = true; } if (eventtype & input_move && ev.which !== 1) { eventtype = input_end; } // mouse must be down if (!this.pressed) { return; } if (eventtype & input_end) { this.pressed = false; } this.callback(this.manager, eventtype, { pointers: [ev], changedpointers: [ev], pointertype: input_type_mouse, srcevent: ev }); } }); var pointer_input_map = { pointerdown: input_start, pointermove: input_move, pointerup: input_end, pointercancel: input_cancel, pointerout: input_cancel }; // in ie10 the pointer types is defined as an enum var ie10_pointer_type_enum = { 2: input_type_touch, 3: input_type_pen, 4: input_type_mouse, 5: input_type_kinect // see https://twitter.com/jacobrossi/status/480596438489890816 }; var pointer_element_events = 'pointerdown'; var pointer_window_events = 'pointermove pointerup pointercancel'; // ie10 has prefixed support, and case-sensitive if (window.mspointerevent && !window.pointerevent) { pointer_element_events = 'mspointerdown'; pointer_window_events = 'mspointermove mspointerup mspointercancel'; } /** * pointer events input * @constructor * @extends input */ function pointereventinput() { this.evel = pointer_element_events; this.evwin = pointer_window_events; input.apply(this, arguments); this.store = (this.manager.session.pointerevents = []); } inherit(pointereventinput, input, { /** * handle mouse events * @param {object} ev */ handler: function pehandler(ev) { var store = this.store; var removepointer = false; var eventtypenormalized = ev.type.tolowercase().replace('ms', ''); var eventtype = pointer_input_map[eventtypenormalized]; var pointertype = ie10_pointer_type_enum[ev.pointertype] || ev.pointertype; var istouch = (pointertype == input_type_touch); // get index of the event in the store var storeindex = inarray(store, ev.pointerid, 'pointerid'); // start and mouse must be down if (eventtype & input_start && (ev.button === 0 || istouch)) { if (storeindex < 0) { store.push(ev); storeindex = store.length - 1; } } else if (eventtype & (input_end | input_cancel)) { removepointer = true; } // it not found, so the pointer hasn't been down (so it's probably a hover) if (storeindex < 0) { return; } // update the event in the store store[storeindex] = ev; this.callback(this.manager, eventtype, { pointers: store, changedpointers: [ev], pointertype: pointertype, srcevent: ev }); if (removepointer) { // remove from the store store.splice(storeindex, 1); } } }); var single_touch_input_map = { touchstart: input_start, touchmove: input_move, touchend: input_end, touchcancel: input_cancel }; var single_touch_target_events = 'touchstart'; var single_touch_window_events = 'touchstart touchmove touchend touchcancel'; /** * touch events input * @constructor * @extends input */ function singletouchinput() { this.evtarget = single_touch_target_events; this.evwin = single_touch_window_events; this.started = false; input.apply(this, arguments); } inherit(singletouchinput, input, { handler: function tehandler(ev) { var type = single_touch_input_map[ev.type]; // should we handle the touch events? if (type === input_start) { this.started = true; } if (!this.started) { return; } var touches = normalizesingletouches.call(this, ev, type); // when done, reset the started state if (type & (input_end | input_cancel) && touches[0].length - touches[1].length === 0) { this.started = false; } this.callback(this.manager, type, { pointers: touches[0], changedpointers: touches[1], pointertype: input_type_touch, srcevent: ev }); } }); /** * @this {touchinput} * @param {object} ev * @param {number} type flag * @returns {undefined|array} [all, changed] */ function normalizesingletouches(ev, type) { var all = toarray(ev.touches); var changed = toarray(ev.changedtouches); if (type & (input_end | input_cancel)) { all = uniquearray(all.concat(changed), 'identifier', true); } return [all, changed]; } var touch_input_map = { touchstart: input_start, touchmove: input_move, touchend: input_end, touchcancel: input_cancel }; var touch_target_events = 'touchstart touchmove touchend touchcancel'; /** * multi-user touch events input * @constructor * @extends input */ function touchinput() { this.evtarget = touch_target_events; this.targetids = {}; input.apply(this, arguments); } inherit(touchinput, input, { handler: function mtehandler(ev) { var type = touch_input_map[ev.type]; var touches = gettouches.call(this, ev, type); if (!touches) { return; } this.callback(this.manager, type, { pointers: touches[0], changedpointers: touches[1], pointertype: input_type_touch, srcevent: ev }); } }); /** * @this {touchinput} * @param {object} ev * @param {number} type flag * @returns {undefined|array} [all, changed] */ function gettouches(ev, type) { var alltouches = toarray(ev.touches); var targetids = this.targetids; // when there is only one touch, the process can be simplified if (type & (input_start | input_move) && alltouches.length === 1) { targetids[alltouches[0].identifier] = true; return [alltouches, alltouches]; } var i, targettouches, changedtouches = toarray(ev.changedtouches), changedtargettouches = [], target = this.target; // get target touches from touches targettouches = alltouches.filter(function(touch) { return hasparent(touch.target, target); }); // collect touches if (type === input_start) { i = 0; while (i < targettouches.length) { targetids[targettouches[i].identifier] = true; i++; } } // filter changed touches to only contain touches that exist in the collected target ids i = 0; while (i < changedtouches.length) { if (targetids[changedtouches[i].identifier]) { changedtargettouches.push(changedtouches[i]); } // cleanup removed touches if (type & (input_end | input_cancel)) { delete targetids[changedtouches[i].identifier]; } i++; } if (!changedtargettouches.length) { return; } return [ // merge targettouches with changedtargettouches so it contains all touches, including 'end' and 'cancel' uniquearray(targettouches.concat(changedtargettouches), 'identifier', true), changedtargettouches ]; } /** * combined touch and mouse input * * touch has a higher priority then mouse, and while touching no mouse events are allowed. * this because touch devices also emit mouse events while doing a touch. * * @constructor * @extends input */ var dedup_timeout = 2500; var dedup_distance = 25; function touchmouseinput() { input.apply(this, arguments); var handler = bindfn(this.handler, this); this.touch = new touchinput(this.manager, handler); this.mouse = new mouseinput(this.manager, handler); this.primarytouch = null; this.lasttouches = []; } inherit(touchmouseinput, input, { /** * handle mouse and touch events * @param {hammer} manager * @param {string} inputevent * @param {object} inputdata */ handler: function tmehandler(manager, inputevent, inputdata) { var istouch = (inputdata.pointertype == input_type_touch), ismouse = (inputdata.pointertype == input_type_mouse); if (ismouse && inputdata.sourcecapabilities && inputdata.sourcecapabilities.firestouchevents) { return; } // when we're in a touch event, record touches to de-dupe synthetic mouse event if (istouch) { recordtouches.call(this, inputevent, inputdata); } else if (ismouse && issyntheticevent.call(this, inputdata)) { return; } this.callback(manager, inputevent, inputdata); }, /** * remove the event listeners */ destroy: function destroy() { this.touch.destroy(); this.mouse.destroy(); } }); function recordtouches(eventtype, eventdata) { if (eventtype & input_start) { this.primarytouch = eventdata.changedpointers[0].identifier; setlasttouch.call(this, eventdata); } else if (eventtype & (input_end | input_cancel)) { setlasttouch.call(this, eventdata); } } function setlasttouch(eventdata) { var touch = eventdata.changedpointers[0]; if (touch.identifier === this.primarytouch) { var lasttouch = {x: touch.clientx, y: touch.clienty}; this.lasttouches.push(lasttouch); var lts = this.lasttouches; var removelasttouch = function() { var i = lts.indexof(lasttouch); if (i > -1) { lts.splice(i, 1); } }; settimeout(removelasttouch, dedup_timeout); } } function issyntheticevent(eventdata) { var x = eventdata.srcevent.clientx, y = eventdata.srcevent.clienty; for (var i = 0; i < this.lasttouches.length; i++) { var t = this.lasttouches[i]; var dx = math.abs(x - t.x), dy = math.abs(y - t.y); if (dx <= dedup_distance && dy <= dedup_distance) { return true; } } return false; } var prefixed_touch_action = prefixed(test_element.style, 'touchaction'); var native_touch_action = prefixed_touch_action !== undefined; // magical touchaction value var touch_action_compute = 'compute'; var touch_action_auto = 'auto'; var touch_action_manipulation = 'manipulation'; // not implemented var touch_action_none = 'none'; var touch_action_pan_x = 'pan-x'; var touch_action_pan_y = 'pan-y'; var touch_action_map = gettouchactionprops(); /** * touch action * sets the touchaction property or uses the js alternative * @param {manager} manager * @param {string} value * @constructor */ function touchaction(manager, value) { this.manager = manager; this.set(value); } touchaction.prototype = { /** * set the touchaction value on the element or enable the polyfill * @param {string} value */ set: function(value) { // find out the touch-action by the event handlers if (value == touch_action_compute) { value = this.compute(); } if (native_touch_action && this.manager.element.style && touch_action_map[value]) { this.manager.element.style[prefixed_touch_action] = value; } this.actions = value.tolowercase().trim(); }, /** * just re-set the touchaction value */ update: function() { this.set(this.manager.options.touchaction); }, /** * compute the value for the touchaction property based on the recognizer's settings * @returns {string} value */ compute: function() { var actions = []; each(this.manager.recognizers, function(recognizer) { if (boolorfn(recognizer.options.enable, [recognizer])) { actions = actions.concat(recognizer.gettouchaction()); } }); return cleantouchactions(actions.join(' ')); }, /** * this method is called on each input cycle and provides the preventing of the browser behavior * @param {object} input */ preventdefaults: function(input) { var srcevent = input.srcevent; var direction = input.offsetdirection; // if the touch action did prevented once this session if (this.manager.session.prevented) { srcevent.preventdefault(); return; } var actions = this.actions; var hasnone = instr(actions, touch_action_none) && !touch_action_map[touch_action_none]; var haspany = instr(actions, touch_action_pan_y) && !touch_action_map[touch_action_pan_y]; var haspanx = instr(actions, touch_action_pan_x) && !touch_action_map[touch_action_pan_x]; if (hasnone) { //do not prevent defaults if this is a tap gesture var istappointer = input.pointers.length === 1; var istapmovement = input.distance < 2; var istaptouchtime = input.deltatime < 250; if (istappointer && istapmovement && istaptouchtime) { return; } } if (haspanx && haspany) { // `pan-x pan-y` means browser handles all scrolling/panning, do not prevent return; } if (hasnone || (haspany && direction & direction_horizontal) || (haspanx && direction & direction_vertical)) { return this.preventsrc(srcevent); } }, /** * call preventdefault to prevent the browser's default behavior (scrolling in most cases) * @param {object} srcevent */ preventsrc: function(srcevent) { this.manager.session.prevented = true; srcevent.preventdefault(); } }; /** * when the touchactions are collected they are not a valid value, so we need to clean things up. * * @param {string} actions * @returns {*} */ function cleantouchactions(actions) { // none if (instr(actions, touch_action_none)) { return touch_action_none; } var haspanx = instr(actions, touch_action_pan_x); var haspany = instr(actions, touch_action_pan_y); // if both pan-x and pan-y are set (different recognizers // for different directions, e.g. horizontal pan but vertical swipe?) // we need none (as otherwise with pan-x pan-y combined none of these // recognizers will work, since the browser would handle all panning if (haspanx && haspany) { return touch_action_none; } // pan-x or pan-y if (haspanx || haspany) { return haspanx ? touch_action_pan_x : touch_action_pan_y; } // manipulation if (instr(actions, touch_action_manipulation)) { return touch_action_manipulation; } return touch_action_auto; } function gettouchactionprops() { if (!native_touch_action) { return false; } var touchmap = {}; var csssupports = window.css && window.css.supports; ['auto', 'manipulation', 'pan-y', 'pan-x', 'pan-x pan-y', 'none'].foreach(function(val) { // if css.supports is not supported but there is native touch-action assume it supports // all values. this is the case for ie 10 and 11. touchmap[val] = csssupports ? window.css.supports('touch-action', val) : true; }); return touchmap; } /** * recognizer flow explained; * * all recognizers have the initial state of possible when a input session starts. * the definition of a input session is from the first input until the last input, with all it's movement in it. * * example session for mouse-input: mousedown -> mousemove -> mouseup * * on each recognizing cycle (see manager.recognize) the .recognize() method is executed * which determines with state it should be. * * if the recognizer has the state failed, cancelled or recognized (equals ended), it is reset to * possible to give it another change on the next cycle. * * possible * | * +-----+---------------+ * | | * +-----+-----+ | * | | | * failed cancelled | * +-------+------+ * | | * recognized began * | * changed * | * ended/recognized */ var state_possible = 1; var state_began = 2; var state_changed = 4; var state_ended = 8; var state_recognized = state_ended; var state_cancelled = 16; var state_failed = 32; /** * recognizer * every recognizer needs to extend from this class. * @constructor * @param {object} options */ function recognizer(options) { this.options = assign({}, this.defaults, options || {}); this.id = uniqueid(); this.manager = null; // default is enable true this.options.enable = ifundefined(this.options.enable, true); this.state = state_possible; this.simultaneous = {}; this.requirefail = []; } recognizer.prototype = { /** * @virtual * @type {object} */ defaults: {}, /** * set options * @param {object} options * @return {recognizer} */ set: function(options) { assign(this.options, options); // also update the touchaction, in case something changed about the directions/enabled state this.manager && this.manager.touchaction.update(); return this; }, /** * recognize simultaneous with an other recognizer. * @param {recognizer} otherrecognizer * @returns {recognizer} this */ recognizewith: function(otherrecognizer) { if (invokearrayarg(otherrecognizer, 'recognizewith', this)) { return this; } var simultaneous = this.simultaneous; otherrecognizer = getrecognizerbynameifmanager(otherrecognizer, this); if (!simultaneous[otherrecognizer.id]) { simultaneous[otherrecognizer.id] = otherrecognizer; otherrecognizer.recognizewith(this); } return this; }, /** * drop the simultaneous link. it doesnt remove the link on the other recognizer. * @param {recognizer} otherrecognizer * @returns {recognizer} this */ droprecognizewith: function(otherrecognizer) { if (invokearrayarg(otherrecognizer, 'droprecognizewith', this)) { return this; } otherrecognizer = getrecognizerbynameifmanager(otherrecognizer, this); delete this.simultaneous[otherrecognizer.id]; return this; }, /** * recognizer can only run when an other is failing * @param {recognizer} otherrecognizer * @returns {recognizer} this */ requirefailure: function(otherrecognizer) { if (invokearrayarg(otherrecognizer, 'requirefailure', this)) { return this; } var requirefail = this.requirefail; otherrecognizer = getrecognizerbynameifmanager(otherrecognizer, this); if (inarray(requirefail, otherrecognizer) === -1) { requirefail.push(otherrecognizer); otherrecognizer.requirefailure(this); } return this; }, /** * drop the requirefailure link. it does not remove the link on the other recognizer. * @param {recognizer} otherrecognizer * @returns {recognizer} this */ droprequirefailure: function(otherrecognizer) { if (invokearrayarg(otherrecognizer, 'droprequirefailure', this)) { return this; } otherrecognizer = getrecognizerbynameifmanager(otherrecognizer, this); var index = inarray(this.requirefail, otherrecognizer); if (index > -1) { this.requirefail.splice(index, 1); } return this; }, /** * has require failures boolean * @returns {boolean} */ hasrequirefailures: function() { return this.requirefail.length > 0; }, /** * if the recognizer can recognize simultaneous with an other recognizer * @param {recognizer} otherrecognizer * @returns {boolean} */ canrecognizewith: function(otherrecognizer) { return !!this.simultaneous[otherrecognizer.id]; }, /** * you should use `tryemit` instead of `emit` directly to check * that all the needed recognizers has failed before emitting. * @param {object} input */ emit: function(input) { var self = this; var state = this.state; function emit(event) { self.manager.emit(event, input); } // 'panstart' and 'panmove' if (state < state_ended) { emit(self.options.event + statestr(state)); } emit(self.options.event); // simple 'eventname' events if (input.additionalevent) { // additional event(panleft, panright, pinchin, pinchout...) emit(input.additionalevent); } // panend and pancancel if (state >= state_ended) { emit(self.options.event + statestr(state)); } }, /** * check that all the require failure recognizers has failed, * if true, it emits a gesture event, * otherwise, setup the state to failed. * @param {object} input */ tryemit: function(input) { if (this.canemit()) { return this.emit(input); } // it's failing anyway this.state = state_failed; }, /** * can we emit? * @returns {boolean} */ canemit: function() { var i = 0; while (i < this.requirefail.length) { if (!(this.requirefail[i].state & (state_failed | state_possible))) { return false; } i++; } return true; }, /** * update the recognizer * @param {object} inputdata */ recognize: function(inputdata) { // make a new copy of the inputdata // so we can change the inputdata without messing up the other recognizers var inputdataclone = assign({}, inputdata); // is is enabled and allow recognizing? if (!boolorfn(this.options.enable, [this, inputdataclone])) { this.reset(); this.state = state_failed; return; } // reset when we've reached the end if (this.state & (state_recognized | state_cancelled | state_failed)) { this.state = state_possible; } this.state = this.process(inputdataclone); // the recognizer has recognized a gesture // so trigger an event if (this.state & (state_began | state_changed | state_ended | state_cancelled)) { this.tryemit(inputdataclone); } }, /** * return the state of the recognizer * the actual recognizing happens in this method * @virtual * @param {object} inputdata * @returns {const} state */ process: function(inputdata) { }, // jshint ignore:line /** * return the preferred touch-action * @virtual * @returns {array} */ gettouchaction: function() { }, /** * called when the gesture isn't allowed to recognize * like when another is being recognized or it is disabled * @virtual */ reset: function() { } }; /** * get a usable string, used as event postfix * @param {const} state * @returns {string} state */ function statestr(state) { if (state & state_cancelled) { return 'cancel'; } else if (state & state_ended) { return 'end'; } else if (state & state_changed) { return 'move'; } else if (state & state_began) { return 'start'; } return ''; } /** * direction cons to string * @param {const} direction * @returns {string} */ function directionstr(direction) { if (direction == direction_down) { return 'down'; } else if (direction == direction_up) { return 'up'; } else if (direction == direction_left) { return 'left'; } else if (direction == direction_right) { return 'right'; } return ''; } /** * get a recognizer by name if it is bound to a manager * @param {recognizer|string} otherrecognizer * @param {recognizer} recognizer * @returns {recognizer} */ function getrecognizerbynameifmanager(otherrecognizer, recognizer) { var manager = recognizer.manager; if (manager) { return manager.get(otherrecognizer); } return otherrecognizer; } /** * this recognizer is just used as a base for the simple attribute recognizers. * @constructor * @extends recognizer */ function attrrecognizer() { recognizer.apply(this, arguments); } inherit(attrrecognizer, recognizer, { /** * @namespace * @memberof attrrecognizer */ defaults: { /** * @type {number} * @default 1 */ pointers: 1 }, /** * used to check if it the recognizer receives valid input, like input.distance > 10. * @memberof attrrecognizer * @param {object} input * @returns {boolean} recognized */ attrtest: function(input) { var optionpointers = this.options.pointers; return optionpointers === 0 || input.pointers.length === optionpointers; }, /** * process the input and return the state for the recognizer * @memberof attrrecognizer * @param {object} input * @returns {*} state */ process: function(input) { var state = this.state; var eventtype = input.eventtype; var isrecognized = state & (state_began | state_changed); var isvalid = this.attrtest(input); // on cancel input and we've recognized before, return state_cancelled if (isrecognized && (eventtype & input_cancel || !isvalid)) { return state | state_cancelled; } else if (isrecognized || isvalid) { if (eventtype & input_end) { return state | state_ended; } else if (!(state & state_began)) { return state_began; } return state | state_changed; } return state_failed; } }); /** * pan * recognized when the pointer is down and moved in the allowed direction. * @constructor * @extends attrrecognizer */ function panrecognizer() { attrrecognizer.apply(this, arguments); this.px = null; this.py = null; } inherit(panrecognizer, attrrecognizer, { /** * @namespace * @memberof panrecognizer */ defaults: { event: 'pan', threshold: 10, pointers: 1, direction: direction_all }, gettouchaction: function() { var direction = this.options.direction; var actions = []; if (direction & direction_horizontal) { actions.push(touch_action_pan_y); } if (direction & direction_vertical) { actions.push(touch_action_pan_x); } return actions; }, directiontest: function(input) { var options = this.options; var hasmoved = true; var distance = input.distance; var direction = input.direction; var x = input.deltax; var y = input.deltay; // lock to axis? if (!(direction & options.direction)) { if (options.direction & direction_horizontal) { direction = (x === 0) ? direction_none : (x < 0) ? direction_left : direction_right; hasmoved = x != this.px; distance = math.abs(input.deltax); } else { direction = (y === 0) ? direction_none : (y < 0) ? direction_up : direction_down; hasmoved = y != this.py; distance = math.abs(input.deltay); } } input.direction = direction; return hasmoved && distance > options.threshold && direction & options.direction; }, attrtest: function(input) { return attrrecognizer.prototype.attrtest.call(this, input) && (this.state & state_began || (!(this.state & state_began) && this.directiontest(input))); }, emit: function(input) { this.px = input.deltax; this.py = input.deltay; var direction = directionstr(input.direction); if (direction) { input.additionalevent = this.options.event + direction; } this._super.emit.call(this, input); } }); /** * pinch * recognized when two or more pointers are moving toward (zoom-in) or away from each other (zoom-out). * @constructor * @extends attrrecognizer */ function pinchrecognizer() { attrrecognizer.apply(this, arguments); } inherit(pinchrecognizer, attrrecognizer, { /** * @namespace * @memberof pinchrecognizer */ defaults: { event: 'pinch', threshold: 0, pointers: 2 }, gettouchaction: function() { return [touch_action_none]; }, attrtest: function(input) { return this._super.attrtest.call(this, input) && (math.abs(input.scale - 1) > this.options.threshold || this.state & state_began); }, emit: function(input) { if (input.scale !== 1) { var inout = input.scale < 1 ? 'in' : 'out'; input.additionalevent = this.options.event + inout; } this._super.emit.call(this, input); } }); /** * press * recognized when the pointer is down for x ms without any movement. * @constructor * @extends recognizer */ function pressrecognizer() { recognizer.apply(this, arguments); this._timer = null; this._input = null; } inherit(pressrecognizer, recognizer, { /** * @namespace * @memberof pressrecognizer */ defaults: { event: 'press', pointers: 1, time: 251, // minimal time of the pointer to be pressed threshold: 9 // a minimal movement is ok, but keep it low }, gettouchaction: function() { return [touch_action_auto]; }, process: function(input) { var options = this.options; var validpointers = input.pointers.length === options.pointers; var validmovement = input.distance < options.threshold; var validtime = input.deltatime > options.time; this._input = input; // we only allow little movement // and we've reached an end event, so a tap is possible if (!validmovement || !validpointers || (input.eventtype & (input_end | input_cancel) && !validtime)) { this.reset(); } else if (input.eventtype & input_start) { this.reset(); this._timer = settimeoutcontext(function() { this.state = state_recognized; this.tryemit(); }, options.time, this); } else if (input.eventtype & input_end) { return state_recognized; } return state_failed; }, reset: function() { cleartimeout(this._timer); }, emit: function(input) { if (this.state !== state_recognized) { return; } if (input && (input.eventtype & input_end)) { this.manager.emit(this.options.event + 'up', input); } else { this._input.timestamp = now(); this.manager.emit(this.options.event, this._input); } } }); /** * rotate * recognized when two or more pointer are moving in a circular motion. * @constructor * @extends attrrecognizer */ function rotaterecognizer() { attrrecognizer.apply(this, arguments); } inherit(rotaterecognizer, attrrecognizer, { /** * @namespace * @memberof rotaterecognizer */ defaults: { event: 'rotate', threshold: 0, pointers: 2 }, gettouchaction: function() { return [touch_action_none]; }, attrtest: function(input) { return this._super.attrtest.call(this, input) && (math.abs(input.rotation) > this.options.threshold || this.state & state_began); } }); /** * swipe * recognized when the pointer is moving fast (velocity), with enough distance in the allowed direction. * @constructor * @extends attrrecognizer */ function swiperecognizer() { attrrecognizer.apply(this, arguments); } inherit(swiperecognizer, attrrecognizer, { /** * @namespace * @memberof swiperecognizer */ defaults: { event: 'swipe', threshold: 10, velocity: 0.3, direction: direction_horizontal | direction_vertical, pointers: 1 }, gettouchaction: function() { return panrecognizer.prototype.gettouchaction.call(this); }, attrtest: function(input) { var direction = this.options.direction; var velocity; if (direction & (direction_horizontal | direction_vertical)) { velocity = input.overallvelocity; } else if (direction & direction_horizontal) { velocity = input.overallvelocityx; } else if (direction & direction_vertical) { velocity = input.overallvelocityy; } return this._super.attrtest.call(this, input) && direction & input.offsetdirection && input.distance > this.options.threshold && input.maxpointers == this.options.pointers && abs(velocity) > this.options.velocity && input.eventtype & input_end; }, emit: function(input) { var direction = directionstr(input.offsetdirection); if (direction) { this.manager.emit(this.options.event + direction, input); } this.manager.emit(this.options.event, input); } }); /** * a tap is ecognized when the pointer is doing a small tap/click. multiple taps are recognized if they occur * between the given interval and position. the delay option can be used to recognize multi-taps without firing * a single tap. * * the eventdata from the emitted event contains the property `tapcount`, which contains the amount of * multi-taps being recognized. * @constructor * @extends recognizer */ function taprecognizer() { recognizer.apply(this, arguments); // previous time and center, // used for tap counting this.ptime = false; this.pcenter = false; this._timer = null; this._input = null; this.count = 0; } inherit(taprecognizer, recognizer, { /** * @namespace * @memberof pinchrecognizer */ defaults: { event: 'tap', pointers: 1, taps: 1, interval: 300, // max time between the multi-tap taps time: 250, // max time of the pointer to be down (like finger on the screen) threshold: 9, // a minimal movement is ok, but keep it low posthreshold: 10 // a multi-tap can be a bit off the initial position }, gettouchaction: function() { return [touch_action_manipulation]; }, process: function(input) { var options = this.options; var validpointers = input.pointers.length === options.pointers; var validmovement = input.distance < options.threshold; var validtouchtime = input.deltatime < options.time; this.reset(); if ((input.eventtype & input_start) && (this.count === 0)) { return this.failtimeout(); } // we only allow little movement // and we've reached an end event, so a tap is possible if (validmovement && validtouchtime && validpointers) { if (input.eventtype != input_end) { return this.failtimeout(); } var validinterval = this.ptime ? (input.timestamp - this.ptime < options.interval) : true; var validmultitap = !this.pcenter || getdistance(this.pcenter, input.center) < options.posthreshold; this.ptime = input.timestamp; this.pcenter = input.center; if (!validmultitap || !validinterval) { this.count = 1; } else { this.count += 1; } this._input = input; // if tap count matches we have recognized it, // else it has began recognizing... var tapcount = this.count % options.taps; if (tapcount === 0) { // no failing requirements, immediately trigger the tap event // or wait as long as the multitap interval to trigger if (!this.hasrequirefailures()) { return state_recognized; } else { this._timer = settimeoutcontext(function() { this.state = state_recognized; this.tryemit(); }, options.interval, this); return state_began; } } } return state_failed; }, failtimeout: function() { this._timer = settimeoutcontext(function() { this.state = state_failed; }, this.options.interval, this); return state_failed; }, reset: function() { cleartimeout(this._timer); }, emit: function() { if (this.state == state_recognized) { this._input.tapcount = this.count; this.manager.emit(this.options.event, this._input); } } }); /** * simple way to create a manager with a default set of recognizers. * @param {htmlelement} element * @param {object} [options] * @constructor */ function hammer(element, options) { options = options || {}; options.recognizers = ifundefined(options.recognizers, hammer.defaults.preset); return new manager(element, options); } /** * @const {string} */ hammer.version = '2.0.7'; /** * default settings * @namespace */ hammer.defaults = { /** * set if dom events are being triggered. * but this is slower and unused by simple implementations, so disabled by default. * @type {boolean} * @default false */ domevents: false, /** * the value for the touchaction property/fallback. * when set to `compute` it will magically set the correct value based on the added recognizers. * @type {string} * @default compute */ touchaction: touch_action_compute, /** * @type {boolean} * @default true */ enable: true, /** * experimental feature -- can be removed/changed * change the parent input target element. * if null, then it is being set the to main element. * @type {null|eventtarget} * @default null */ inputtarget: null, /** * force an input class * @type {null|function} * @default null */ inputclass: null, /** * default recognizer setup when calling `hammer()` * when creating a new manager these will be skipped. * @type {array} */ preset: [ // recognizerclass, options, [recognizewith, ...], [requirefailure, ...] [rotaterecognizer, {enable: false}], [pinchrecognizer, {enable: false}, ['rotate']], [swiperecognizer, {direction: direction_horizontal}], [panrecognizer, {direction: direction_horizontal}, ['swipe']], [taprecognizer], [taprecognizer, {event: 'doubletap', taps: 2}, ['tap']], [pressrecognizer] ], /** * some css properties can be used to improve the working of hammer. * add them to this method and they will be set when creating a new manager. * @namespace */ cssprops: { /** * disables text selection to improve the dragging gesture. mainly for desktop browsers. * @type {string} * @default 'none' */ userselect: 'none', /** * disable the windows phone grippers when pressing an element. * @type {string} * @default 'none' */ touchselect: 'none', /** * disables the default callout shown when you touch and hold a touch target. * on ios, when you touch and hold a touch target such as a link, safari displays * a callout containing information about the link. this property allows you to disable that callout. * @type {string} * @default 'none' */ touchcallout: 'none', /** * specifies whether zooming is enabled. used by ie10> * @type {string} * @default 'none' */ contentzooming: 'none', /** * specifies that an entire element should be draggable instead of its contents. mainly for desktop browsers. * @type {string} * @default 'none' */ userdrag: 'none', /** * overrides the highlight color shown when the user taps a link or a javascript * clickable element in ios. this property obeys the alpha value, if specified. * @type {string} * @default 'rgba(0,0,0,0)' */ taphighlightcolor: 'rgba(0,0,0,0)' } }; var stop = 1; var forced_stop = 2; /** * manager * @param {htmlelement} element * @param {object} [options] * @constructor */ function manager(element, options) { this.options = assign({}, hammer.defaults, options || {}); this.options.inputtarget = this.options.inputtarget || element; this.handlers = {}; this.session = {}; this.recognizers = []; this.oldcssprops = {}; this.element = element; this.input = createinputinstance(this); this.touchaction = new touchaction(this, this.options.touchaction); togglecssprops(this, true); each(this.options.recognizers, function(item) { var recognizer = this.add(new (item[0])(item[1])); item[2] && recognizer.recognizewith(item[2]); item[3] && recognizer.requirefailure(item[3]); }, this); } manager.prototype = { /** * set options * @param {object} options * @returns {manager} */ set: function(options) { assign(this.options, options); // options that need a little more setup if (options.touchaction) { this.touchaction.update(); } if (options.inputtarget) { // clean up existing event listeners and reinitialize this.input.destroy(); this.input.target = options.inputtarget; this.input.init(); } return this; }, /** * stop recognizing for this session. * this session will be discarded, when a new [input]start event is fired. * when forced, the recognizer cycle is stopped immediately. * @param {boolean} [force] */ stop: function(force) { this.session.stopped = force ? forced_stop : stop; }, /** * run the recognizers! * called by the inputhandler function on every movement of the pointers (touches) * it walks through all the recognizers and tries to detect the gesture that is being made * @param {object} inputdata */ recognize: function(inputdata) { var session = this.session; if (session.stopped) { return; } // run the touch-action polyfill this.touchaction.preventdefaults(inputdata); var recognizer; var recognizers = this.recognizers; // this holds the recognizer that is being recognized. // so the recognizer's state needs to be began, changed, ended or recognized // if no recognizer is detecting a thing, it is set to `null` var currecognizer = session.currecognizer; // reset when the last recognizer is recognized // or when we're in a new session if (!currecognizer || (currecognizer && currecognizer.state & state_recognized)) { currecognizer = session.currecognizer = null; } var i = 0; while (i < recognizers.length) { recognizer = recognizers[i]; // find out if we are allowed try to recognize the input for this one. // 1. allow if the session is not forced stopped (see the .stop() method) // 2. allow if we still haven't recognized a gesture in this session, or the this recognizer is the one // that is being recognized. // 3. allow if the recognizer is allowed to run simultaneous with the current recognized recognizer. // this can be setup with the `recognizewith()` method on the recognizer. if (session.stopped !== forced_stop && ( // 1 !currecognizer || recognizer == currecognizer || // 2 recognizer.canrecognizewith(currecognizer))) { // 3 recognizer.recognize(inputdata); } else { recognizer.reset(); } // if the recognizer has been recognizing the input as a valid gesture, we want to store this one as the // current active recognizer. but only if we don't already have an active recognizer if (!currecognizer && recognizer.state & (state_began | state_changed | state_ended)) { currecognizer = session.currecognizer = recognizer; } i++; } }, /** * get a recognizer by its event name. * @param {recognizer|string} recognizer * @returns {recognizer|null} */ get: function(recognizer) { if (recognizer instanceof recognizer) { return recognizer; } var recognizers = this.recognizers; for (var i = 0; i < recognizers.length; i++) { if (recognizers[i].options.event == recognizer) { return recognizers[i]; } } return null; }, /** * add a recognizer to the manager * existing recognizers with the same event name will be removed * @param {recognizer} recognizer * @returns {recognizer|manager} */ add: function(recognizer) { if (invokearrayarg(recognizer, 'add', this)) { return this; } // remove existing var existing = this.get(recognizer.options.event); if (existing) { this.remove(existing); } this.recognizers.push(recognizer); recognizer.manager = this; this.touchaction.update(); return recognizer; }, /** * remove a recognizer by name or instance * @param {recognizer|string} recognizer * @returns {manager} */ remove: function(recognizer) { if (invokearrayarg(recognizer, 'remove', this)) { return this; } recognizer = this.get(recognizer); // let's make sure this recognizer exists if (recognizer) { var recognizers = this.recognizers; var index = inarray(recognizers, recognizer); if (index !== -1) { recognizers.splice(index, 1); this.touchaction.update(); } } return this; }, /** * bind event * @param {string} events * @param {function} handler * @returns {eventemitter} this */ on: function(events, handler) { if (events === undefined) { return; } if (handler === undefined) { return; } var handlers = this.handlers; each(splitstr(events), function(event) { handlers[event] = handlers[event] || []; handlers[event].push(handler); }); return this; }, /** * unbind event, leave emit blank to remove all handlers * @param {string} events * @param {function} [handler] * @returns {eventemitter} this */ off: function(events, handler) { if (events === undefined) { return; } var handlers = this.handlers; each(splitstr(events), function(event) { if (!handler) { delete handlers[event]; } else { handlers[event] && handlers[event].splice(inarray(handlers[event], handler), 1); } }); return this; }, /** * emit event to the listeners * @param {string} event * @param {object} data */ emit: function(event, data) { // we also want to trigger dom events if (this.options.domevents) { triggerdomevent(event, data); } // no handlers, so skip it all var handlers = this.handlers[event] && this.handlers[event].slice(); if (!handlers || !handlers.length) { return; } data.type = event; data.preventdefault = function() { data.srcevent.preventdefault(); }; var i = 0; while (i < handlers.length) { handlers[i](data); i++; } }, /** * destroy the manager and unbinds all events * it doesn't unbind dom events, that is the user own responsibility */ destroy: function() { this.element && togglecssprops(this, false); this.handlers = {}; this.session = {}; this.input.destroy(); this.element = null; } }; /** * add/remove the css properties as defined in manager.options.cssprops * @param {manager} manager * @param {boolean} add */ function togglecssprops(manager, add) { var element = manager.element; if (!element.style) { return; } var prop; each(manager.options.cssprops, function(value, name) { prop = prefixed(element.style, name); if (add) { manager.oldcssprops[prop] = element.style[prop]; element.style[prop] = value; } else { element.style[prop] = manager.oldcssprops[prop] || ''; } }); if (!add) { manager.oldcssprops = {}; } } /** * trigger dom event * @param {string} event * @param {object} data */ function triggerdomevent(event, data) { var gestureevent = document.createevent('event'); gestureevent.initevent(event, true, true); gestureevent.gesture = data; data.target.dispatchevent(gestureevent); } assign(hammer, { input_start: input_start, input_move: input_move, input_end: input_end, input_cancel: input_cancel, state_possible: state_possible, state_began: state_began, state_changed: state_changed, state_ended: state_ended, state_recognized: state_recognized, state_cancelled: state_cancelled, state_failed: state_failed, direction_none: direction_none, direction_left: direction_left, direction_right: direction_right, direction_up: direction_up, direction_down: direction_down, direction_horizontal: direction_horizontal, direction_vertical: direction_vertical, direction_all: direction_all, manager: manager, input: input, touchaction: touchaction, touchinput: touchinput, mouseinput: mouseinput, pointereventinput: pointereventinput, touchmouseinput: touchmouseinput, singletouchinput: singletouchinput, recognizer: recognizer, attrrecognizer: attrrecognizer, tap: taprecognizer, pan: panrecognizer, swipe: swiperecognizer, pinch: pinchrecognizer, rotate: rotaterecognizer, press: pressrecognizer, on: addeventlisteners, off: removeeventlisteners, each: each, merge: merge, extend: extend, assign: assign, inherit: inherit, bindfn: bindfn, prefixed: prefixed }); // this prevents errors when hammer is loaded in the presence of an amd // style loader but by script tag, not by the loader. var freeglobal = (typeof window !== 'undefined' ? window : (typeof self !== 'undefined' ? self : {})); // jshint ignore:line freeglobal.hammer = hammer; if (typeof define === 'function' && define.amd) { define(function() { return hammer; }); } else if (typeof module != 'undefined' && module.exports) { module.exports = hammer; } else { window[exportname] = hammer; } })(window, document, 'hammer');