forked from mattbryson/TouchSwipe-Jquery-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.touchSwipe.min.js
1 lines (1 loc) · 8.63 KB
/
jquery.touchSwipe.min.js
1
(function(d){var m="left",l="right",c="up",t="down",b="in",u="out",j="none",p="auto",i="swipe",q="pinch",v="tap",x="horizontal",r="vertical",g="all",e="start",h="move",f="end",n="cancel",a="ontouchstart" in window,w="TouchSwipe";var k={fingers:1,threshold:75,pinchThreshold:20,maxTimeThreshold:null,fingerReleaseThreshold:250,swipe:null,swipeLeft:null,swipeRight:null,swipeUp:null,swipeDown:null,swipeStatus:null,pinchIn:null,pinchOut:null,pinchStatus:null,click:null,tap:null,triggerOnTouchEnd:true,triggerOnTouchLeave:false,allowPageScroll:"auto",fallbackToMouseEvents:true,excludedElements:"button, input, select, textarea, a, .noSwipe"};d.fn.swipe=function(A){var z=d(this),y=z.data(w);if(y&&typeof A==="string"){if(y[A]){return y[A].apply(this,Array.prototype.slice.call(arguments,1))}else{d.error("Method "+A+" does not exist on jQuery.swipe")}}else{if(!y&&(typeof A==="object"||!A)){return s.apply(this,arguments)}}return z};d.fn.swipe.defaults=k;d.fn.swipe.phases={PHASE_START:e,PHASE_MOVE:h,PHASE_END:f,PHASE_CANCEL:n};d.fn.swipe.directions={LEFT:m,RIGHT:l,UP:c,DOWN:t,IN:b,OUT:u};d.fn.swipe.pageScroll={NONE:j,HORIZONTAL:x,VERTICAL:r,AUTO:p};d.fn.swipe.fingers={ONE:1,TWO:2,THREE:3,ALL:g};function s(y){if(y&&(y.allowPageScroll===undefined&&(y.swipe!==undefined||y.swipeStatus!==undefined))){y.allowPageScroll=j}if(y.click!==undefined&&y.tap===undefined){y.tap=y.click}if(!y){y={}}y=d.extend({},d.fn.swipe.defaults,y);return this.each(function(){var A=d(this);var z=A.data(w);if(!z){z=new o(this,y);A.data(w,z)}})}function o(R,ae){var aG=(a||!ae.fallbackToMouseEvents),ax=aG?"touchstart":"mousedown",T=aG?"touchmove":"mousemove",at=aG?"touchend":"mouseup",D=aG?null:"mouseleave",Q="touchcancel";var ab=0,M=null,af=0,aC=0,A=0,ah=1,ay=0,aI=0;var H=d(R);var N="start";var aF=0;var ag=null;var I=0,X=0,aB=0,aK=0;try{H.bind(ax,aq);H.bind(Q,L)}catch(aD){d.error("events not supported "+ax+","+Q+" on jQuery.swipe")}this.enable=function(){H.bind(ax,aq);H.bind(Q,L);return H};this.disable=function(){P();return H};this.destroy=function(){P();H.data(w,null);return H};function aq(aN){if(W()){return}if(d(aN.target).closest(ae.excludedElements,H).length>0){return}var aO=aN.originalEvent?aN.originalEvent:aN;var aM,aL=a?aO.touches[0]:aO;N=e;if(a){aF=aO.touches.length}else{aN.preventDefault()}ab=0;M=null;aI=null;af=0;aC=0;A=0;ah=1;ay=0;ag=S();z();if(!a||(aF===ae.fingers||ae.fingers===g)||an()){aJ(0,aL);I=B();if(aF==2){aJ(1,aO.touches[1]);aC=A=Y(ag[0].start,ag[1].start)}if(ae.swipeStatus||ae.pinchStatus){aM=aE(aO,N)}}else{aM=false}if(aM===false){N=n;aE(aO,N);return aM}else{ai(true)}}function O(aO){var aR=aO.originalEvent?aO.originalEvent:aO;if(N===f||N===n||ad()){return}var aN,aM=a?aR.touches[0]:aR;var aP=U(aM);X=B();if(a){aF=aR.touches.length}N=h;if(aF==2){if(aC==0){aJ(1,aR.touches[1]);aC=A=Y(ag[0].start,ag[1].start)}else{U(aR.touches[1]);A=Y(ag[0].end,ag[1].end);aI=am(ag[0].end,ag[1].end)}ah=y(aC,A);ay=Math.abs(aC-A)}if((aF===ae.fingers||ae.fingers===g)||!a||an()){M=ap(aP.start,aP.end);C(aO,M);ab=G(aP.start,aP.end);af=K();if(ae.swipeStatus||ae.pinchStatus){aN=aE(aR,N)}if(!ae.triggerOnTouchEnd||ae.triggerOnTouchLeave){var aL=true;if(ae.triggerOnTouchLeave){var aQ=ar(this);aL=aA(aP.end,aQ)}if(!ae.triggerOnTouchEnd&&aL){N=aH(h)}else{if(ae.triggerOnTouchLeave&&!aL){N=aH(f)}}if(N==n||N==f){aE(aR,N)}}}else{N=n;aE(aR,N)}if(aN===false){N=n;aE(aR,N)}}function Z(aN){var aP=aN.originalEvent;if(a){if(aP.touches.length>0){au();return true}}if(ad()){aF=aK}aN.preventDefault();X=B();if(ae.triggerOnTouchEnd||(ae.triggerOnTouchEnd==false&&N===h)){N=f;var aM=((aF===ae.fingers||ae.fingers===g)||!a);var aL=ag[0].end.x!==0;var aO=aM&&aL&&(al()||az());if(aO){aE(aP,N)}else{N=n;aE(aP,N)}}else{if(!ae.triggerOnTouchEnd&&aw()){N=f;ak(aP,N,v)}else{if(N===h){N=n;aE(aP,N)}}}ai(false)}function L(){aF=0;X=0;I=0;aC=0;A=0;ah=1;z();ai(false)}function V(aL){var aM=aL.originalEvent;if(ae.triggerOnTouchLeave){N=aH(f);aE(aM,N)}}function P(){H.unbind(ax,aq);H.unbind(Q,L);H.unbind(T,O);H.unbind(at,Z);if(D){H.unbind(D,V)}ai(false)}function aH(aO){var aN=aO;var aM=ao();var aL=ac();if(!aM){aN=n}else{if(aL&&aO==h&&(!ae.triggerOnTouchEnd||ae.triggerOnTouchLeave)){aN=f}else{if(!aL&&aO==f&&ae.triggerOnTouchLeave){aN=n}}}return aN}function aE(aN,aL){var aM=undefined;if(aa()){aM=ak(aN,aL,i)}if(an()&&aM!==false){aM=ak(aN,aL,q)}if(aw()&&aM!==false){aM=ak(aN,aL,v)}if(aL===n){L(aN)}if(aL===f){if(a){if(aN.touches.length==0){L(aN)}}else{L(aN)}}return aM}function ak(aO,aL,aN){var aM=undefined;if(aN==i){H.trigger("swipeStatus",[aL,M||null,ab||0,af||0,aF]);if(ae.swipeStatus){aM=ae.swipeStatus.call(H,aO,aL,M||null,ab||0,af||0,aF);if(aM===false){return false}}if(aL==f&&az()){H.trigger("swipe",[M,ab,af,aF]);if(ae.swipe){aM=ae.swipe.call(H,aO,M,ab,af,aF);if(aM===false){return false}}switch(M){case m:H.trigger("swipeLeft",[M,ab,af,aF]);if(ae.swipeLeft){aM=ae.swipeLeft.call(H,aO,M,ab,af,aF)}break;case l:H.trigger("swipeRight",[M,ab,af,aF]);if(ae.swipeRight){aM=ae.swipeRight.call(H,aO,M,ab,af,aF)}break;case c:H.trigger("swipeUp",[M,ab,af,aF]);if(ae.swipeUp){aM=ae.swipeUp.call(H,aO,M,ab,af,aF)}break;case t:H.trigger("swipeDown",[M,ab,af,aF]);if(ae.swipeDown){aM=ae.swipeDown.call(H,aO,M,ab,af,aF)}break}}}if(aN==q){H.trigger("pinchStatus",[aL,aI||null,ay||0,af||0,aF,ah]);if(ae.pinchStatus){aM=ae.pinchStatus.call(H,aO,aL,aI||null,ay||0,af||0,aF,ah);if(aM===false){return false}}if(aL==f&&al()){switch(aI){case b:H.trigger("pinchIn",[aI||null,ay||0,af||0,aF,ah]);if(ae.pinchIn){aM=ae.pinchIn.call(H,aO,aI||null,ay||0,af||0,aF,ah)}break;case u:H.trigger("pinchOut",[aI||null,ay||0,af||0,aF,ah]);if(ae.pinchOut){aM=ae.pinchOut.call(H,aO,aI||null,ay||0,af||0,aF,ah)}break}}}if(aN==v){if(aL===n||aL===f){if((aF===1||!a)&&(isNaN(ab)||ab===0)){H.trigger("tap",[aO.target]);if(ae.tap){aM=ae.tap.call(H,aO,aO.target)}}}}return aM}function ac(){if(ae.threshold!==null){return ab>=ae.threshold}return true}function aj(){if(ae.pinchThreshold!==null){return ay>=ae.pinchThreshold}return true}function ao(){var aL;if(ae.maxTimeThreshold){if(af>=ae.maxTimeThreshold){aL=false}else{aL=true}}else{aL=true}return aL}function C(aL,aM){if(ae.allowPageScroll===j||an()){aL.preventDefault()}else{var aN=ae.allowPageScroll===p;switch(aM){case m:if((ae.swipeLeft&&aN)||(!aN&&ae.allowPageScroll!=x)){aL.preventDefault()}break;case l:if((ae.swipeRight&&aN)||(!aN&&ae.allowPageScroll!=x)){aL.preventDefault()}break;case c:if((ae.swipeUp&&aN)||(!aN&&ae.allowPageScroll!=r)){aL.preventDefault()}break;case t:if((ae.swipeDown&&aN)||(!aN&&ae.allowPageScroll!=r)){aL.preventDefault()}break}}}function al(){return aj()}function an(){return !!(ae.pinchStatus||ae.pinchIn||ae.pinchOut)}function av(){return !!(al()&&an())}function az(){var aL=ao();var aN=ac();var aM=aN&&aL;return aM}function aa(){return !!(ae.swipe||ae.swipeStatus||ae.swipeLeft||ae.swipeRight||ae.swipeUp||ae.swipeDown)}function E(){return !!(az()&&aa())}function aw(){return !!(ae.tap)}function au(){aB=B();aK=event.touches.length+1}function z(){aB=0;aK=0}function ad(){var aL=false;if(aB){var aM=B()-aB;if(aM<=ae.fingerReleaseThreshold){aL=true}}return aL}function W(){return !!(H.data(w+"_intouch")===true)}function ai(aL){if(aL===true){H.bind(T,O);H.bind(at,Z);if(D){H.bind(D,V)}}else{H.unbind(T,O,false);H.unbind(at,Z,false);if(D){H.unbind(D,V,false)}}H.data(w+"_intouch",aL===true)}function aJ(aM,aL){var aN=aL.identifier!==undefined?aL.identifier:0;ag[aM].identifier=aN;ag[aM].start.x=ag[aM].end.x=aL.pageX||aL.clientX;ag[aM].start.y=ag[aM].end.y=aL.pageY||aL.clientY;return ag[aM]}function U(aL){var aN=aL.identifier!==undefined?aL.identifier:0;var aM=J(aN);aM.end.x=aL.pageX||aL.clientX;aM.end.y=aL.pageY||aL.clientY;return aM}function J(aM){for(var aL=0;aL<ag.length;aL++){if(ag[aL].identifier==aM){return ag[aL]}}}function S(){var aL=[];for(var aM=0;aM<=5;aM++){aL.push({start:{x:0,y:0},end:{x:0,y:0},identifier:0})}return aL}function K(){return X-I}function Y(aO,aN){var aM=Math.abs(aO.x-aN.x);var aL=Math.abs(aO.y-aN.y);return Math.round(Math.sqrt(aM*aM+aL*aL))}function y(aL,aM){var aN=(aM/aL)*1;return aN.toFixed(2)}function am(){if(ah<1){return u}else{return b}}function G(aM,aL){return Math.round(Math.sqrt(Math.pow(aL.x-aM.x,2)+Math.pow(aL.y-aM.y,2)))}function F(aO,aM){var aL=aO.x-aM.x;var aQ=aM.y-aO.y;var aN=Math.atan2(aQ,aL);var aP=Math.round(aN*180/Math.PI);if(aP<0){aP=360-Math.abs(aP)}return aP}function ap(aM,aL){var aN=F(aM,aL);if((aN<=45)&&(aN>=0)){return m}else{if((aN<=360)&&(aN>=315)){return m}else{if((aN>=135)&&(aN<=225)){return l}else{if((aN>45)&&(aN<135)){return t}else{return c}}}}}function B(){var aL=new Date();return aL.getTime()}function ar(aL){aL=d(aL);var aN=aL.offset();var aM={left:aN.left,right:aN.left+aL.outerWidth(),top:aN.top,bottom:aN.top+aL.outerHeight()};return aM}function aA(aL,aM){return(aL.x>aM.left&&aL.x<aM.right&&aL.y>aM.top&&aL.y<aM.bottom)}}})(jQuery);