-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsTouchLayer.min.js
1 lines (1 loc) · 6.87 KB
/
jsTouchLayer.min.js
1
var TouchLayer_Controller=function(){var a=[],b=function(b){return a[b]!==undefined},c={touchstart:"ontouchstart"in document.documentElement?"touchstart":"mousedown",touchmove:"ontouchmove"in document.documentElement?"touchmove":"mousemove",touchend:"ontouchend"in document.documentElement?"touchend":"mouseup"};return{bind:function(a,b,d,e){var f=!1;if(!a||!b||!d)return;e!==undefined&&(f=e),c[b]!==undefined&&a.addEventListener(c[b],d,f)},unbind:function(a,b,d){if(!a||!b||!d)return;c[b]!==undefined&&a.removeEventListener(c[b],d)},fire:function(a,c,d){if(!a||!c)return;d.type=a,b(a)||c(d)},lock:function(b){if(b.length>0)for(var c=0;c<b.length;c++)a[b[c]]||a.push(b[c])},unlock:function(b){var c=[];if(b.length>0){for(var d=0;d<a.length;d++)b[a[d]]||c.push(a[d]);a=c}},makeReturnData:function(a,b,c){return{originalEvent:a,target:b,data:c}}}},TouchLayer_TapController=function(a){if(!a.eventName||!a.el||!a.callback)return;var b=new TouchLayer_Controller,c=!1,d=!1,e=400,f=800,g=1e3,h=10,i=null,j=null,k=null,l=null,m=!1,n=!0;a.preventDefault!==undefined&&(n=a.preventDefault);var o=function(c){if(a.eventName==="tap"){var d=b.makeReturnData(c,a.el);b.fire("tap",a.callback,d)}c.event&&(c=c.event);var e=(c.changedTouches?c.changedTouches[0]:c).target;if(!e.disabled&&this.fireClickEvent){var f=document.createEvent("MouseEvent");f.initMouseEvent("click",c.bubbles,c.cancelable,document.defaultView,c.detail,c.screenX,c.screenY,c.clientX,c.clientY,c.ctrlKey,c.altKey,c.shiftKey,c.metaKey,c.metaKey,c.button,c.relatedTarget),f.isSimulated=!0,e.dispatchEvent(f)}},p=function(a){var b=0,c=0;return a.touches!==undefined?(b=a.touches[0].pageX,c=a.touches[0].pageY):a.pageX&&a.pageY&&(b=a.pageX,c=a.pageY),Math.abs(b-k)>=h||Math.abs(c-l)>=h},q=function(c){c.originalEvent&&(c=c.originalEvent),n&&(c.preventDefault(),c.stopPropagation()),c.touches!==undefined?(k=c.touches[0].pageX,l=c.touches[0].pageY):c.pageX&&c.pageY&&(k=c.pageX,l=c.pageY),m=!1;if(a.eventName==="tapstart"){var d=b.makeReturnData(c,a.el);b.fire("tapstart",a.callback,d)}a.eventName==="taphold"&&(j=setTimeout(function(){var d=b.makeReturnData(c,a.el);b.fire("taphold",a.callback,d),j=null},g))},r=function(c){c.originalEvent&&(c=c.originalEvent),n&&(c.preventDefault(),c.stopPropagation());if(p(c)&&!m&&k+l>0){if(a.eventName==="tapcancel"){var d=b.makeReturnData(c,a.el);b.fire("tapcancel",a.callback,d)}j&&(clearTimeout(j),j=null),m=!0}},s=function(c){c.originalEvent&&(c=c.originalEvent),n&&(c.preventDefault(),c.stopPropagation());if(!m){o(c);if(i&&c.timeStamp-i<=f){i=null;if(a.eventName==="doubletap"){var g=b.makeReturnData(c,a.el);b.fire("doubletap",a.callback,g)}}else i=c.timeStamp;if(e&&!d){if(a.eventName==="singletap"){var h=b.makeReturnData(c,a.el);b.fire("singletap",a.callback,h)}d=!0,setTimeout(function(){d=!1},e)}j&&(clearTimeout(j),j=null)}m=!0};return b.bind(a.el,"touchstart",q,c),b.bind(a.el,"touchmove",r,c),b.bind(a.el,"touchend",s,c),{unbind:function(){b.unbind(a.el,"touchstart",q),b.unbind(a.el,"touchmove",r),b.unbind(a.el,"touchend",s)}}},TouchLayer_DragController=function(a){if(!a.eventName||!a.el||!a.callback)return;var b=new TouchLayer_Controller,c=!1,d=30,e=!0,f=!0,g=0,h=0,i=0,j=0,k="",l="",m=!1,n=!1,o=!0;a.preventDefault!==undefined&&(o=a.preventDefault);var p=function(a){return e&&a.absDeltaX>=d||f&&a.absDeltaY>=d},q=function(a){a.originalEvent&&(a=a.originalEvent),o&&(a.preventDefault(),a.stopPropagation()),a.touches!==undefined?(g=h=a.touches[0].pageX,i=j=a.touches[0].pageY):a.pageX&&a.pageY&&(g=h=a.pageX,i=j=a.pageY),k=l=a.timeStamp,l=a.timeStamp,m=!1},r=function(c){if(g&&i){c.originalEvent&&(c=c.originalEvent),o&&(c.preventDefault(),c.stopPropagation());var d={startX:g,startY:i,prevX:h,prevY:j,direction:null,deltaY:0,deltaX:0,absDeltaY:0,absDeltaX:0,deltaTime:c.timeStamp-k};c.touches?(d.deltaY=c.touches[0].pageY-i,d.deltaX=c.touches[0].pageX-g):c.pageX&&c.pageY&&(d.deltaY=c.pageY-i,d.deltaX=c.pageX-g),d.direction=d.deltaY<0?"up":"down";if(d!==undefined){d.absDeltaY=Math.abs(d.deltaY),d.absDeltaX=Math.abs(d.deltaX);if(!m&&(!c.touches||c.touches.length<3)&&p(d)){m=!0;if(a.eventName==="dragstart"){var e=b.makeReturnData(c,a.el,d);b.fire("dragstart",a.callback,e)}}if(m){var f=null;a.eventName==="drag"&&(!c.touches||c.touches.length<2)?(f=b.makeReturnData(c,a.el,d),b.fire("drag",a.callback,f)):a.eventName==="twofingerdrag"&&c.touches&&c.touches.length==2&&(f=b.makeReturnData(c,a.el,d),b.fire("twofingerdrag",a.callback,f)),c.touches?(h=c.touches[0].pageX,j=c.touches[0].pageY):c.pageX&&c.pageY&&(h=c.pageX,j=c.pageY)}}}},s=function(c){c.originalEvent&&(c=c.originalEvent),o&&(c.preventDefault(),c.stopPropagation());if(m&&a.eventName==="dragend"){var d=b.makeReturnData(c,a.el);b.fire("dragend",a.callback,d)}m=!1,n=!1,g=i=null};return b.bind(a.el,"touchstart",q,c),b.bind(a.el,"touchmove",r,c),b.bind(a.el,"touchend",s,c),{unbind:function(){b.unbind(a.el,"touchstart",q),b.unbind(a.el,"touchmove",r),b.unbind(a.el,"touchend",s)}}},TouchLayer_SwipeController=function(a){if(!a.eventName||!a.el||!a.callback)return;var b=new TouchLayer_Controller,c=!1,d=null,e=null,f=null,g=105,h=1e3,i=!1,j=!0,k=!1;a.preventDefault!==undefined&&(j=a.preventDefault),a.swipeVertical!==undefined&&(k=a.swipeVertical);var l=function(a){a.originalEvent&&(a=a.originalEvent),j&&(a.preventDefault(),a.stopPropagation()),a.touches?(e=a.touches[0].pageX,f=a.touches[0].pageY):a.pageX&&a.pageY&&(e=a.pageX,f=a.pageY),d=a.timeStamp,i=!1},m=function(c){if(e&&f){c.originalEvent&&(c=c.originalEvent),j&&(c.preventDefault(),c.stopPropagation());var l=null,m=null;c.touches?(m=c.touches[0].pageY-f,l=c.touches[0].pageX-e):c.pageX&&c.pageY&&(m=c.pageY-f,l=c.pageX-e);if(l&&m){var n=Math.abs(m),o=Math.abs(l),p=c.timeStamp-d,q=null,r={};p>h?i=!0:!i&&o>g&&o>n?(r={direction:l<0?"left":"right",distance:o,deltaTime:p,deltaX:l,deltaY:m},a.eventName==="swipe"&&(q=b.makeReturnData(c,a.el,r),b.fire("swipe",a.callback,q),j&&(c.preventDefault(),c.stopPropagation())),i=!0):!i&&k&&n>g&&n>o&&(r={direction:m<0?"up":"down",distance:n,deltaTime:p,deltaX:l,deltaY:m},a.eventName==="swipe"&&(q=b.makeReturnData(c,a.el,r),b.fire("swipe",a.callback,q),j&&(c.preventDefault(),c.stopPropagation())),i=!0)}}},n=function(){i=!0,e=f=null};return b.bind(a.el,"touchstart",l,c),b.bind(a.el,"touchmove",m,c),b.bind(a.el,"touchend",n,c),{unbind:function(){b.unbind(a.el,"touchstart",l),b.unbind(a.el,"touchmove",m),b.unbind(a.el,"touchend",n)}}},TouchLayer=function(){var a={tapstart:TouchLayer_TapController,tapcancel:TouchLayer_TapController,tap:TouchLayer_TapController,doubletap:TouchLayer_TapController,taphold:TouchLayer_TapController,singletap:TouchLayer_TapController,drag:TouchLayer_DragController,twofingerdrag:TouchLayer_DragController,dragstart:TouchLayer_DragController,dragend:TouchLayer_DragController,swipe:TouchLayer_SwipeController},b=[];return{bind:function(c,d,e,f){if(!c||!d||!e)return;f||(f={});if(a[c]){f.eventName=c,f.el=d,f.callback=e;var g=b.length;return b[g]=new a[c](f),g}},unbind:function(a){b[a]!==null&&(b[a].unbind(),b[a]=null)}}}