/* AlloyFinger v0.1.6
* By dntzhang
* Github: https://github.com/AlloyTeam/AlloyFinger
*/
(function(){function h(a){return Math.sqrt(a.x*a.x+a.y*a.y)}function d(a,b){var c=new f(a);c.add(b);return c}var f=function(a){this.handlers=[];this.el=a};f.prototype.add=function(a){this.handlers.push(a)};f.prototype.del=function(a){a||(this.handlers=[]);for(var b=this.handlers.length;0<=b;b--)this.handlers[b]===a&&this.handlers.splice(b,1)};f.prototype.dispatch=function(){for(var a=0,b=this.handlers.length;a<b;a++){var c=this.handlers[a];"function"===typeof c&&c.apply(this.el,arguments)}};var k=
function(a,b){this.element="string"==typeof a?document.querySelector(a):a;this.start=this.start.bind(this);this.move=this.move.bind(this);this.end=this.end.bind(this);this.cancel=this.cancel.bind(this);this.element.addEventListener("touchstart",this.start,!1);this.element.addEventListener("touchmove",this.move,!1);this.element.addEventListener("touchend",this.end,!1);this.element.addEventListener("touchcancel",this.cancel,!1);this.preV={x:null,y:null};this.pinchStartLen=null;this.scale=1;this.isDoubleTap=
!1;var c=function(){};this.rotate=d(this.element,b.rotate||c);this.touchStart=d(this.element,b.touchStart||c);this.multipointStart=d(this.element,b.multipointStart||c);this.multipointEnd=d(this.element,b.multipointEnd||c);this.pinch=d(this.element,b.pinch||c);this.swipe=d(this.element,b.swipe||c);this.tap=d(this.element,b.tap||c);this.doubleTap=d(this.element,b.doubleTap||c);this.longTap=d(this.element,b.longTap||c);this.singleTap=d(this.element,b.singleTap||c);this.pressMove=d(this.element,b.pressMove||
c);this.touchMove=d(this.element,b.touchMove||c);this.touchEnd=d(this.element,b.touchEnd||c);this.touchCancel=d(this.element,b.touchCancel||c);this.x1=this.x2=this.y1=this.y2=this.swipeTimeout=this.longTapTimeout=this.singleTapTimeout=this.tapTimeout=this.now=this.last=this.delta=null;this.preTapPosition={x:null,y:null}};k.prototype={start:function(a){if(a.touches){this.now=Date.now();this.x1=a.touches[0].pageX;this.y1=a.touches[0].pageY;this.delta=this.now-(this.last||this.now);this.touchStart.dispatch(a);
null!==this.preTapPosition.x&&(this.isDoubleTap=0<this.delta&&250>=this.delta&&30>Math.abs(this.preTapPosition.x-this.x1)&&30>Math.abs(this.preTapPosition.y-this.y1));this.preTapPosition.x=this.x1;this.preTapPosition.y=this.y1;this.last=this.now;var b=this.preV;if(1<a.touches.length){this._cancelLongTap();this._cancelSingleTap();var c=a.touches[1].pageY-this.y1;b.x=a.touches[1].pageX-this.x1;b.y=c;this.pinchStartLen=h(b);this.multipointStart.dispatch(a)}this.longTapTimeout=setTimeout(function(){this.longTap.dispatch(a)}.bind(this),
750)}},move:function(a){if(a.touches){var b=this.preV,c=a.touches.length,d=a.touches[0].pageX,f=a.touches[0].pageY;this.isDoubleTap=!1;if(1<c){var g={x:a.touches[1].pageX-d,y:a.touches[1].pageY-f};if(null!==b.x){0<this.pinchStartLen&&(a.scale=h(g)/this.pinchStartLen,this.pinch.dispatch(a));var e;e=h(g)*h(b);0===e?e=0:(e=(g.x*b.x+g.y*b.y)/e,1<e&&(e=1),e=Math.acos(e));0<g.x*b.y-b.x*g.y&&(e*=-1);a.angle=180*e/Math.PI;this.rotate.dispatch(a)}b.x=g.x;b.y=g.y}else null!==this.x2?(a.deltaX=d-this.x2,a.deltaY=
f-this.y2):(a.deltaX=0,a.deltaY=0),this.pressMove.dispatch(a);this.touchMove.dispatch(a);this._cancelLongTap();this.x2=d;this.y2=f;1<c&&a.preventDefault()}},end:function(a){if(a.changedTouches){this._cancelLongTap();var b=this;2>a.touches.length&&this.multipointEnd.dispatch(a);this.touchEnd.dispatch(a);this.x2&&30<Math.abs(this.x1-this.x2)||this.y2&&30<Math.abs(this.y1-this.y2)?(a.direction=this._swipeDirection(this.x1,this.x2,this.y1,this.y2),this.swipeTimeout=setTimeout(function(){b.swipe.dispatch(a)},
0)):(this.tapTimeout=setTimeout(function(){b.tap.dispatch(a);b.isDoubleTap&&(b.doubleTap.dispatch(a),clearTimeout(b.singleTapTimeout),b.isDoubleTap=!1)},0),b.isDoubleTap||(b.singleTapTimeout=setTimeout(function(){b.singleTap.dispatch(a)},250)));this.preV.x=0;this.preV.y=0;this.scale=1;this.x1=this.x2=this.y1=this.y2=this.pinchStartLen=null}},cancel:function(a){clearTimeout(this.singleTapTimeout);clearTimeout(this.tapTimeout);clearTimeout(this.longTapTimeout);clearTimeout(this.swipeTimeout);this.touchCancel.dispatch(a)},
_cancelLongTap:function(){clearTimeout(this.longTapTimeout)},_cancelSingleTap:function(){clearTimeout(this.singleTapTimeout)},_swipeDirection:function(a,b,c,d){return Math.abs(a-b)>=Math.abs(c-d)?0<a-b?"Left":"Right":0<c-d?"Up":"Down"},on:function(a,b){this[a]&&this[a].add(b)},off:function(a,b){this[a]&&this[a].del(b)},destroy:function(){this.singleTapTimeout&&clearTimeout(this.singleTapTimeout);this.tapTimeout&&clearTimeout(this.tapTimeout);this.longTapTimeout&&clearTimeout(this.longTapTimeout);
this.swipeTimeout&&clearTimeout(this.swipeTimeout);this.element.removeEventListener("touchstart",this.start);this.element.removeEventListener("touchmove",this.move);this.element.removeEventListener("touchend",this.end);this.element.removeEventListener("touchcancel",this.cancel);this.rotate.del();this.touchStart.del();this.multipointStart.del();this.multipointEnd.del();this.pinch.del();this.swipe.del();this.tap.del();this.doubleTap.del();this.longTap.del();this.singleTap.del();this.pressMove.del();
this.touchMove.del();this.touchEnd.del();this.touchCancel.del();return this.preV=this.pinchStartLen=this.scale=this.isDoubleTap=this.delta=this.last=this.now=this.tapTimeout=this.singleTapTimeout=this.longTapTimeout=this.swipeTimeout=this.x1=this.x2=this.y1=this.y2=this.preTapPosition=this.rotate=this.touchStart=this.multipointStart=this.multipointEnd=this.pinch=this.swipe=this.tap=this.doubleTap=this.longTap=this.singleTap=this.pressMove=this.touchMove=this.touchEnd=this.touchCancel=null}};"undefined"!==
typeof module&&"object"===typeof exports?module.exports=k:window.AlloyFinger=k})();
|