forked from maxzhang/touchbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
touchbox.min.js
1 lines (1 loc) · 14.5 KB
/
touchbox.min.js
1
!function(t){function e(e){return t.MSPointerEvent?"MSPointer"+e.charAt(9).toUpperCase()+e.substr(10):e}function n(t){return"[object Function]"===m.call(t)}function i(t){return"[object Array]"===m.call(t)}function o(t){return"undefined"==typeof t}function r(t){return"string"==typeof t}function s(t,e){if(t&&e)if(i(t))for(var n=0,o=t.length;o>n&&e.call(null,t[n],n,t)!==!1;n++);else for(var r in t)if(e.call(null,r,t[r])===!1)break}function a(t,e){if(t&&e&&"object"==typeof e){var n,i,o;for(n in e)t[n]=e[n];if(x)for(i=x.length;i--;)o=x[i],e.hasOwnProperty(o)&&(t[o]=e[o])}return t}function c(e){var n,i=t.getComputedStyle(e)[L.transform];return i&&(n=i.match(/^matrix(3d)?(.*)$/i))&&n&&n[2]?parseInt(n[2].replace(/ /g,"").split(",")[n[1]?13:5],10):0}function l(){return t.innerHeight>t.innerWidth}function h(e,n){return function(){var i=g.call(arguments,0),o=l();e.lastOrientation!==o&&(e.lastOrientation=o,e.apply(n||t,i))}}function u(t,e,n){var i=this,o=function(){t.transitionTimer&&clearTimeout(t.transitionTimer),t.transitionTimer=null,t.removeEventListener(L.transitionEndEvent,r,!1)},r=function(){o(),n&&n.call(i)};o(),t.addEventListener(L.transitionEndEvent,r,!1),t.transitionTimer=setTimeout(r,e+50)}var p=t.document,v=t.navigator.userAgent.toLowerCase(),d="ontouchstart"in t,f=t.PointerEvent||t.MSPointerEvent,m=Object.prototype.toString,g=Array.prototype.slice,x=["hasOwnProperty","valueOf","isPrototypeOf","propertyIsEnumerable","toLocaleString","toString","constructor"],y={wp:function(){return-1!==v.indexOf("windows phone ")?/\bwindows phone (?:os )?([0-9.]+)/:-1!==v.indexOf("xblwp")?/\bxblwp([0-9.]+)/:-1!==v.indexOf("zunewp")?/\bzunewp([0-9.]+)/:/\bwindows phone/}().test(v),ios:function(){return/\bcpu(?: iphone)? os /.test(v)?/\bcpu(?: iphone)? os ([0-9._]+)/:-1!==v.indexOf("iph os ")?/\biph os ([0-9_]+)/:/\bios\b/}().test(v),android:function(){return-1!==v.indexOf("android")?/\bandroid[ \/-]?([0-9.x]+)?/:-1!==v.indexOf("adr")?-1!==v.indexOf("mqqbrowser")?/\badr[ ]\(linux; u; ([0-9.]+)?/:/\badr(?:[ ]([0-9.]+))?/:/\bandroid/}().test(v)},E=function(){return{start:d?"touchstart":f?e("pointerdown"):"mousedown",move:d?"touchmove":f?e("pointermove"):"mousemove",end:d?"touchend":f?e("pointerup"):"mouseup",cancel:d?"touchcancel":f?e("pointercancel"):"mousecancel"}}(),L=function(){var t=p.createElement("div").style,e=function(){for(var e,n="webkitT,t,msT,MozT,OT".split(","),i=0,o=n.length;o>i;i++)if(e=n[i]+"ransform",e in t)return n[i].substr(0,n[i].length-1);return!1}(),n=e?"-"+e.toLowerCase()+"-":"",i=function(t){return""===e?t:(t=t.charAt(0).toUpperCase()+t.substr(1),e+t)},o=i("transform"),r=i("transition"),s=i("transitionProperty"),a=i("transitionDuration"),c=i("transformOrigin"),l=i("transitionTimingFunction"),h=i("transitionDelay"),u=function(){return"webkit"==e||"O"===e?e.toLowerCase()+"TransitionEnd":"transitionend"}(),v=i("animation"),d=i("animationName"),f=i("animationDuration"),m=i("animationTimingFunction"),g=i("animationDelay");return t=null,{propPrefix:e,cssPrefix:n,transform:o,transition:r,transitionProperty:s,transitionDuration:a,transformOrigin:c,transitionTimingFunction:l,transitionDelay:h,transitionEndEvent:u,animation:v,animationName:d,animationDuration:f,animationTimingFunction:m,animationDelay:g}}(),C=function(){function e(t){return"string"==typeof t}function n(t){return"[object Function]"===r.call(t)}function i(t){return"[object Array]"===r.call(t)}function o(t,e,n,i){return function(){return t.removeListener(e,n,i),n.apply(i,arguments)}}var r=Object.prototype.toString,s=/^(?:scope|delay|buffer|single|stopEvent|preventDefault|stopPropagation|normalized|args|delegate)$/,a=function(e,n){this.events={},this.defaultScope=n||t,e&&this.addListener(e)};return a.prototype={constructor:a,addEvents:function(t){var n,i,o=this.events;if(e(t))for(n=arguments,i=n.length;i--;)o[n[i]]=o[n[i]]||[];else for(i in t)o[i]=o[i]||[]},addListener:function(t,i,o,r){if(e(t)){var a=this.events;t=t.toLowerCase(),a[t]=a[t]||[],o=o||this.defaultScope,r=r||{},a[t].push({single:r.single,fireFn:i,listenerFn:this.createListener(t,i,o,r),scope:o})}else{var c,l;o=t.scope;for(l in t)s.test(l)||(c=t[l],n(c)?this.addListener(l,c,o):this.addListener(l,c.fn,c.scope||o))}},removeListener:function(t,e,n){t=t.toLowerCase();var o=this.events[t];if(i(o)){n=n||this.defaultScope;for(var r=0,s=o.length;s>r;r++)if(o[r].fireFn==e&&n==o[r].scope)return void o.splice(r,1)}},clearListeners:function(t){this.events[t.toLowerCase()]=[]},purgeListeners:function(){for(var t in this.events)this.clearListeners(t)},hasListener:function(t){var e=this.events[t.toLowerCase()];return i(e)&&e.length>0},fireEvent:function(t){var e=this.events[t.toLowerCase()];if(i(e)){var n,o=Array.prototype.slice.call(arguments,1),r=e.length,s=0;if(r>0)for(;r>s;s++)if(n=e[s],n&&(n.single===!0&&s--,n.listenerFn.apply(n.scope,o)===!1))return!1}},createListener:function(t,e,n,i){var r=e;return i=i||{},i.single&&(r=o(this,t,e,n)),r}},a.prototype.on=a.prototype.addListener,a.prototype.off=a.prototype.removeListener,a}(),b=function(t,e){var n={itemSelector:"",active:0,loop:!1,animation:"flow",duration:400,lockScreen:"off",rotateBody:"",beforeSlide:null,onSlide:null,onResize:null,scope:this};t&&!e&&(e=t,t=null),t?(t=r(t)?p.querySelector(t):t,t.parentNode.style.overflow="hidden"):t=p.body,t.style.overflow="hidden",this.ct=t,this.options=a(n,e),this.getLength()<3&&(this.options.loop=!1),this.initEvents(),this.onOrientationChange(),this.resize(),this.to(this.options.active,!0)};b.prototype={initEvents:function(){this.ct.addEventListener(E.start,this,!1),this.onOrientationChangeProxy=h(this.onOrientationChange,this),t.addEventListener("orientationchange",this.onOrientationChangeProxy,!1),t.addEventListener("resize",this.onOrientationChangeProxy,!1),t.addEventListener("resize",this,!1),this.ee=new C(null,this.options.scope),this.options.beforeSlide&&this.ee.on("beforeslide",this.options.beforeSlide),this.options.onSlide&&this.ee.on("slide",this.options.onSlide),this.options.onResize&&this.ee.on("resize",this.options.onResize)},on:function(){return this.ee.on.apply(this.ee,arguments),this},off:function(){return this.ee.off.apply(this.ee,arguments),this},getItems:function(){var t=this.options.itemSelector;return g.call(t?this.ct.querySelectorAll(t):this.ct.children,0)},getItem:function(t){return this.getItems()[t]},getActive:function(){return this.getItems()[this.active]},getLength:function(){return this.getItems().length},getLast:function(){return this.getLength()-1},getContext:function(t){var e,n,i=this.getLast();return o(t)&&(t=this.active),e=t-1,n=t+1,0>e&&(e=this.options.loop?i:-1),n>i&&(n=this.options.loop?0:-1),{prev:e,next:n,active:t}},resize:function(e,n){return e=e||t.innerWidth,n=n||t.innerHeight,this.ct.style.width=e+"px",this.ct.style.height=n+"px",s(g.call(this.getItems(),0),function(t){t.style.width=e+"px",t.style.height=n+"px"}),this.ee.fireEvent("resize",e,n),this},onResize:function(){var e=t.innerWidth,n=t.innerHeight;(this.lastWidth!=e||this.lastHeight!=n)&&(this.lastWidth=e,this.lastHeight=n,this.resize(e,n))},onOrientationChange:function(){var t=this.options.lockScreen;if("off"!=t){var e=l(),n=this.getLockRotateEl();"landscape"==t&&e?n.style.display="":"portrait"!=t||e?(this.onResize(),n.style.display="none"):n.style.display=""}else this.onResize()},getLockRotateEl:function(){if(!this.lockRotateEl){var t=this.options.rotateBody,e="display:none;position:fixed;left:0;top:0;bottom:0;right:0;z-index:9999998;width:100%;height:100%;",i=p.createElement("div");i.id="touchBoxRotate",i.style.cssText=e,t&&(i.innerHTML=n(t)?t():t),p.body.appendChild(i),this.lockRotateEl=i}return this.lockRotateEl},getAnimation:function(){return b.animations[this.options.animation]},setItemShow:function(t,e,n,i){if(e>-1){var o=this.getItem(e),r=this.getAnimation();r.touchStart.call(this,t,e,n,i),o.style[L.transitionTimingFunction]="ease-in-out",o.style[L.transitionDuration]="0ms"}},setItemHide:function(t,e){if(t>-1){var n=this.getItem(t);n.style[L.transform]="translate3d(0,"+e+"px,0)",n.style[L.transitionDuration]="0ms"}},onTouchStart:function(t){var e=this;if(e.sliding)return t.preventDefault(),void t.stopPropagation();if(clearTimeout(e.prepareNextViewTimer),e.ct.removeEventListener(E.move,e,!1),e.ct.removeEventListener(E.end,e,!1),e.ct.removeEventListener(E.cancel,e,!1),e.ct.addEventListener(E.move,e,!1),e.ct.addEventListener(E.end,e,!1),e.ct.addEventListener(E.cancel,e,!1),delete e.vertical,e.ee.fireEvent("touchstart",e.active)!==!1){var n=t.touches?t.touches[0]:t,i=e.getContext(),o=e.ct.offsetHeight;i.prev>-1&&e.setItemShow("prev",i.prev,-o,i),i.next>-1&&e.setItemShow("next",i.next,o,i),e.setItemShow("active",i.active,0,i),e.touchCoords={},e.touchCoords.startX=n.pageX,e.touchCoords.startY=n.pageY,e.touchCoords.timeStamp=t.timeStamp}},onTouchMove:function(t){var e=this;if(e.touchCoords&&!e.sliding){var n=t.touches?t.touches[0]:t;e.touchCoords.stopX=n.pageX,e.touchCoords.stopY=n.pageY;var i=e.touchCoords.startX-e.touchCoords.stopX,r=e.touchCoords.startY-e.touchCoords.stopY,s=Math.abs(i),a=Math.abs(r);if(o(e.vertical)){if(0!==r&&t.preventDefault(),!(a>s))return e.vertical=!1,void delete e.touchCoords;if(e.vertical=!0,0!==r&&(t.preventDefault(),e.ee.fireEvent("touchmove",e.active,r)===!1))return}else if(e.vertical&&0!==r&&(t.preventDefault(),e.ee.fireEvent("touchmove",e.active,r)===!1))return;var c,l=e.getContext(),h=e.ct.offsetHeight;h>a&&(c=e.getAnimation(),l.prev>-1&&c.touchMove.call(e,"prev",l.prev,-h-r,l),(e.options.loop||0>r&&l.prev>-1||r>0&&l.next>-1)&&c.touchMove.call(e,"active",l.active,-r,l),l.next>-1&&c.touchMove.call(e,"next",l.next,h-r,l))}},onTouchEnd:function(t){var e=this;e.ct.removeEventListener(E.move,e,!1),e.ct.removeEventListener(E.end,e,!1),e.ct.removeEventListener(E.cancel,e,!1);var n,i,o,r=e.getContext(),s=e.ct.offsetHeight;return!e.touchCoords||e.sliding?(e.setItemHide(r.prev,-s),void e.setItemHide(r.next,s)):(n=e.touchCoords.startY-e.touchCoords.stopY,i=Math.abs(n),isNaN(i)||0===i||e.ee.fireEvent("touchend",e.active,n)===!1||!isNaN(i)&&i>0&&(i>s&&(i=s),o=i>=80||t.timeStamp-e.touchCoords.timeStamp<200?e.touchCoords.startY>e.touchCoords.stopY&&r.next>-1?r.next:e.touchCoords.startY<e.touchCoords.stopY&&r.prev>-1?r.prev:r.active:r.active,o==r.active&&0===c(e.getItem(o))?(e.setItemHide(r.prev,-s),e.setItemHide(r.next,s)):(e.setItemHide(e.touchCoords.startY>e.touchCoords.stopY?r.prev:r.next,-s),e.to(o,!1,!0))),void delete e.touchCoords)},to:function(t,e,n){var i=this;if(!i.sliding){var o,r,s=i.active,a=i.getLast(),l=i.ct.offsetHeight,h=i.getContext(),u=function(r){n||(r?i.setItemShow("prev",t,-l,h):i.setItemShow("next",t,l,h),o>-1&&i.setItemShow("active",o,0,h)),i.slide(o,t,r,e)};return t>-1&&a>=t&&t!=s&&i.ee.fireEvent("beforeslide",t,s)!==!1?(o=s,r=a>1?s>t&&a>s||t==a-1&&s==a||t==a&&0===s:s>t,u(r)):(c(i.getItem(s))>0?(o=h.prev,r=!1):(o=h.next,r=!0),t=s,u(r)),i}},slide:function(t,e,n,i){var o,r,s,a,l=this,h=l.ct.offsetHeight,p=l.options.duration,v="0ms",d=l.getAnimation();l.sliding=!0;var f=function(t,e){t.removeEventListener(L.transitionEndEvent,e,!1)},m=function(){o&&(f(o,m),o.style.position="absolute",o.style[L.transitionDuration]=v)},g=function(){clearTimeout(l.resetSlideTimeout),l.resetSlideTimeout=null,f(r,g),r.style.position="relative",o&&(o.style.zIndex="11"),r.style.zIndex="12",r.style[L.transitionDuration]=v,l.lastActive=l.active,l.active=e,l.sliding=!1,l.ee.fireEvent("slide",e,l.lastActive),l.prepareNextViewTimer=setTimeout(function(){var t=l.getContext(),e=l.ct.offsetHeight;t.prev>-1&&l.setItemShow("prev",t.prev,-e,t),t.next>-1&&l.setItemShow("next",t.next,e,t)},100)},x=l.getContext();l.active==e?n?l.setItemHide(x.prev,-h):l.setItemHide(x.next,h):n?l.setItemHide(x.next,h):l.setItemHide(x.prev,-h),t>-1&&(o=l.getItem(t)),r=l.getItem(e),s=c(r),a=i?0:d.duration?d.duration.call(l,o,r,t,e,n):Math.round(Math.abs(s)/h*p),o&&(f(o,m),o.style[L.transitionDuration]=a+"ms"),f(r,g),r.style[L.transitionDuration]=a+"ms",setTimeout(function(){i||(o&&u(o,a,m),u(r,a,g)),o&&d.touchEnd.call(l,"active",t,s>0?-h:h,n),d.touchEnd.call(l,s>0?"next":"prev",e,0,n),i?(m(),g()):l.resetSlideTimeout=setTimeout(function(){m(),g()},a+400)},y.android?50:10)},prev:function(){var t=this.getContext();return t.prev>-1&&this.to(t.prev),this},next:function(){var t=this.getContext();return t.next>-1&&this.to(t.next),this},handleEvent:function(t){switch(t.type){case E.start:this.onTouchStart(t);break;case E.move:this.onTouchMove(t);break;case E.end:case E.cancel:this.onTouchEnd(t);break;case"resize":this.onResize(t)}},destroy:function(){this.destroyed||(this.destroyed=!0,this.ct.removeEventListener(E.start,this,!1),this.ct.removeEventListener(E.move,this,!1),this.ct.removeEventListener(E.end,this,!1),this.ct.removeEventListener(E.cancel,this,!1),t.removeEventListener("orientationchange",this.onOrientationChangeProxy,!1),t.removeEventListener("resize",this.onOrientationChangeProxy,!1),t.removeEventListener("resize",this,!1),this.lockRotateEl&&(this.lockRotateEl.parentNode.removeChild(this.lockRotateEl),this.lockRotateEl=null),this.ct=null)}},b.animations={slide:{touchStart:function(t,e,n){var i=this.getItem(e);"prev"===t||"next"===t?(i.style.position="absolute",i.style.left="0",i.style.top="0",i.style.zIndex="11"):(i.style.position="relative",i.style.zIndex="12"),i.style[L.transform]="translate3d(0px,"+n+"px,0px)"},touchMove:function(t,e,n){var i=this.getItem(e);this.options.loop||(0===e&&n>0||e==this.getLast()&&0>n)&&(n/=4),i.style[L.transform]="translate3d(0px,"+n+"px,0px)"},touchEnd:function(t,e,n){var i=this.getItem(e);i.style[L.transform]="translate3d(0px,"+n+"px,0px)"}},flow:{touchStart:function(t,e,n){var i=this.getItem(e);"prev"===t?(i.style.position="absolute",i.style.left="0",i.style.top="0",i.style.zIndex="11",i.style[L.transform]="translate3d(0px,"+n/4+"px,0px)"):"next"===t?(i.style.position="absolute",i.style.left="0",i.style.top="0",i.style.zIndex="13",i.style[L.transform]="translate3d(0px,"+n+"px,0px)"):(i.style.position="relative",i.style.zIndex="12",i.style[L.transform]="translate3d(0px,"+n+"px,0px)")},touchMove:function(t,e,n,i){var o=this.getItem(e),r=c(this.getItem(i.active));"prev"===t?o.style[L.transform]="translate3d(0px,"+n/4+"px,0px)":"next"===t?o.style[L.transform]="translate3d(0px,"+n+"px,0px)":(r>0?this.options.loop||(0===e&&n>0||e==this.getLast()&&0>n)&&(n/=4):n/=4,o.style[L.transform]="translate3d(0px,"+n+"px,0px)")},touchEnd:function(t,e,n,i){var o=this.getItem(e);o.style[L.transform]=!i&&"active"===t&&0>n?"translate3d(0px,"+n/4+"px,0px)":"translate3d(0px,"+n+"px,0px)"},duration:function(t,e,n,i){var o,r=this.ct.offsetHeight,s=c(e),a=0;return t&&(a=c(t)),o=0>s&&(this.options.loop||i!==this.getLast())?r-Math.abs(a):Math.abs(s),Math.round(o/r*this.options.duration)}}},b.EventEmitter=C,t.TouchBox=b}(window);