indicators.min.js 8.7 KB

1
  1. !function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i():"function"==typeof define&&define.amd?define(i):(t="undefined"!=typeof globalThis?globalThis:t||self).Indicators=i()}(this,function(){"use strict";var r=function(){return(r=Object.assign||function(t){for(var i,o=1,e=arguments.length;o<e;o++)for(var n in i=arguments[o])Object.prototype.hasOwnProperty.call(i,n)&&(t[n]=i[n]);return t}).apply(this,arguments)};function n(t,i){if(!t)throw new Error("[BScroll] "+i)}var e="undefined"!=typeof window,i=e&&navigator.userAgent.toLowerCase();i&&/wechatdevtools/.test(i),i&&i.indexOf("android"),function(){if("string"==typeof i){var t=/os (\d\d?_\d(_\d)?)/.exec(i);if(!t)return;t=t[1].split("_").map(function(t){return parseInt(t,10)});13===t[0]&&t[1]}}();var s=!1;if(e)try{var t={};Object.defineProperty(t,"passive",{get:function(){s=!0}}),window.addEventListener("test-passive",function(){},t)}catch(t){}function a(){return window.performance&&window.performance.now&&window.performance.timing?window.performance.now()+window.performance.timing.navigationStart:+new Date}function d(t,i,o){return t<i?i:o<t?o:t}var h=e&&document.createElement("div").style,o=function(){if(!e)return!1;for(var t=0,i=[{key:"standard",value:"transform"},{key:"webkit",value:"webkitTransform"},{key:"Moz",value:"MozTransform"},{key:"O",value:"OTransform"},{key:"ms",value:"msTransform"}];t<i.length;t++){var o=i[t];if(void 0!==h[o.value])return o.key}return!1}();function l(t){return!1===o?t:"standard"===o?"transitionEnd"===t?"transitionend":t:o+t.charAt(0).toUpperCase()+t.substr(1)}function c(t,i,o,e){e=s?{passive:!1,capture:!!e}:!!e;t.addEventListener(i,o,e)}function p(t,i,o,e){t.removeEventListener(i,o,{capture:!!e})}o&&"standard"!==o&&o.toLowerCase();var u=l("transform");l("transition");e&&l("perspective")in h;var m=u,f=l("transitionTimingFunction"),v=l("transitionDuration");l("transitionDelay"),l("transformOrigin"),l("transitionEnd"),l("transitionProperty");var y=(g.prototype.destroy=function(){this.removeDOMEvents(),this.events=[]},g.prototype.addDOMEvents=function(){this.handleDOMEvents(c)},g.prototype.removeDOMEvents=function(){this.handleDOMEvents(p)},g.prototype.handleDOMEvents=function(i){var o=this,e=this.wrapper;this.events.forEach(function(t){i(e,t.name,o,!!t.capture)})},g.prototype.handleEvent=function(i){var o=i.type;this.events.some(function(t){return t.name===o&&(t.handler(i),!0)})},g);function g(t,i){this.wrapper=t,this.events=i,this.addDOMEvents()}function S(t){t.preventDefault(),t.stopPropagation()}var w=(x.prototype.handleDOM=function(){var t=this.options,i=t.relationElement,t=t.relationElementHandleElementIndex,t=void 0===t?0:t;this.wrapper=i,this.indicatorEl=this.wrapper.children[t]},x.prototype.handleHooks=function(){var i=this,t=this.scroll,o=t.hooks,e=t.scroller.translater.hooks,t=t.scroller.animater.hooks;this.registerHooks(o,o.eventTypes.refresh,this.refresh),this.registerHooks(e,e.eventTypes.translate,function(t){i.updatePosition(t)}),this.registerHooks(t,t.eventTypes.time,this.transitionTime),this.registerHooks(t,t.eventTypes.timeFunction,this.transitionTimingFunction)},x.prototype.transitionTime=function(t){this.indicatorEl.style[v]=(t=void 0===t?0:t)+"ms"},x.prototype.transitionTimingFunction=function(t){this.indicatorEl.style[f]=t},x.prototype.handleInteractive=function(){!1!==this.options.interactive&&this.registerEvents()},x.prototype.registerHooks=function(t,i,o){t.on(i,o,this),this.hooksFn.push([t,i,o])},x.prototype.registerEvents=function(){var t=this.scroll.options,i=t.disableMouse,o=t.disableTouch,e=[],n=[],t=[];i||(e.push({name:"mousedown",handler:this.start.bind(this)}),n.push({name:"mousemove",handler:this.move.bind(this)}),t.push({name:"mouseup",handler:this.end.bind(this)})),o||(e.push({name:"touchstart",handler:this.start.bind(this)}),n.push({name:"touchmove",handler:this.move.bind(this)}),t.push({name:"touchend",handler:this.end.bind(this)},{name:"touchcancel",handler:this.end.bind(this)})),this.startEventRegister=new y(this.indicatorEl,e),this.moveEventRegister=new y(window,n),this.endEventRegister=new y(window,t)},x.prototype.refresh=function(){var t=this.scroll,i=t.x,o=t.y,e=t.hasHorizontalScroll,n=t.hasVerticalScroll,r=t.maxScrollX,s=t.maxScrollY,a=(c=this.options.ratio,p={ratioX:0,ratioY:0},c&&("number"==typeof c?p.ratioX=p.ratioY=c:"object"==typeof c&&c&&(p.ratioX=c.x||0,p.ratioY=c.y||0)),p),h=a.ratioX,l=a.ratioY,c={width:(t=this.wrapper).clientWidth,height:t.clientHeight},p=c.width,a=c.height,t=function(t){if(t instanceof window.SVGElement){var i=t.getBoundingClientRect();return{top:i.top,left:i.left,width:i.width,height:i.height}}return{top:t.offsetTop,left:t.offsetLeft,width:t.offsetWidth,height:t.offsetHeight}}(this.indicatorEl),c=t.width,t=t.height;e&&(this.maxScrollX=p-c,this.translateXSign=0<this.maxScrollX?-1:1,this.minScrollX=0,this.ratioX=h||Math.abs(this.maxScrollX/r)),n&&(this.maxScrollY=a-t,this.translateYSign=0<this.maxScrollY?-1:1,this.minScrollY=0,this.ratioY=l||Math.abs(this.maxScrollY/s)),this.updatePosition({x:i,y:o})},x.prototype.start=function(t){var i;this.BScrollIsDisabled()||(i=t.touches?t.touches[0]:t,S(t),this.initiated=!0,this.moved=!1,this.lastPointX=i.pageX,this.lastPointY=i.pageY,this.startTime=a(),this.scroll.scroller.hooks.trigger(this.scroll.scroller.hooks.eventTypes.beforeScrollStart))},x.prototype.BScrollIsDisabled=function(){return!this.scroll.enabled},x.prototype.move=function(t){var i,o,e;this.initiated&&(i=(e=t.touches?t.touches[0]:t).pageX,o=e.pageY,S(t),e=i-this.lastPointX,t=o-this.lastPointY,this.lastPointX=i,this.lastPointY=o,this.moved||this.indicatorNotMoved(e,t)||(this.moved=!0,this.scroll.scroller.hooks.trigger(this.scroll.scroller.hooks.eventTypes.scrollStart)),this.moved&&(t=this.getBScrollPosByRatio(this.currentPos,e,t),this.syncBScroll(t)))},x.prototype.end=function(t){var i;this.initiated&&(this.initiated=!1,S(t),this.moved&&(t=(i=this.scroll).x,i=i.y,this.scroll.scroller.hooks.trigger(this.scroll.scroller.hooks.eventTypes.scrollEnd,{x:t,y:i})))},x.prototype.getBScrollPosByRatio=function(t,i,o){var e=t.x,n=t.y,r=this.scroll,s=r.hasHorizontalScroll,a=r.hasVerticalScroll,h=r.minScrollX,l=r.maxScrollX,c=r.minScrollY,p=r.maxScrollY,t=this.scroll,r=t.x,t=t.y;return s&&(i=d(e+i,Math.min(this.minScrollX,this.maxScrollX),Math.max(this.minScrollX,this.maxScrollX)),r=d(Math.round(i/this.ratioX*this.translateXSign),l,h)),a&&(o=d(n+o,Math.min(this.minScrollY,this.maxScrollY),Math.max(this.minScrollY,this.maxScrollY)),t=d(Math.round(o/this.ratioY*this.translateYSign),p,c)),{x:r,y:t}},x.prototype.indicatorNotMoved=function(t,i){var o=this.currentPos,e=o.x,o=o.y,t=e===this.minScrollX&&t<=0||e===this.maxScrollX&&0<=t,i=o===this.minScrollY&&i<=0||o===this.maxScrollY&&0<=i;return t&&i},x.prototype.syncBScroll=function(t){var i=a(),o=this.scroll,e=o.options,n=o.scroller,o=e.probeType,e=e.momentumLimitTime;n.translater.translate(t),i-this.startTime>e&&(this.startTime=i,1===o&&n.hooks.trigger(n.hooks.eventTypes.scroll,t)),1<o&&n.hooks.trigger(n.hooks.eventTypes.scroll,t)},x.prototype.updatePosition=function(t){t=this.getIndicatorPosByRatio(t);this.applyTransformProperty(t),this.currentPos=r({},t)},x.prototype.applyTransformProperty=function(t){var i=this.scroll.options.translateZ,i=["translateX("+t.x+"px)","translateY("+t.y+"px)",""+i];this.indicatorEl.style[m]=i.join(" ")},x.prototype.getIndicatorPosByRatio=function(t){var i=t.x,o=t.y,e=this.scroll,n=e.hasHorizontalScroll,t=e.hasVerticalScroll,e=r({},this.currentPos);return n&&(i=Math.round(this.ratioX*i*this.translateXSign),e.x=d(i,Math.min(this.minScrollX,this.maxScrollX),Math.max(this.minScrollX,this.maxScrollX))),t&&(o=Math.round(this.ratioY*o*this.translateYSign),e.y=d(o,Math.min(this.minScrollY,this.maxScrollY),Math.max(this.minScrollY,this.maxScrollY))),e},x.prototype.destroy=function(){!1!==this.options.interactive&&(this.startEventRegister.destroy(),this.moveEventRegister.destroy(),this.endEventRegister.destroy()),this.hooksFn.forEach(function(t){var i=t[0],o=t[1],t=t[2];i.off(o,t)}),this.hooksFn.length=0},x);function x(t,i){this.scroll=t,this.options=i,this.currentPos={x:0,y:0},this.hooksFn=[],this.handleDOM(),this.handleHooks(),this.handleInteractive()}function E(t){this.scroll=t,this.options=[],this.indicators=[],this.handleOptions(),this.handleHooks()}return E.prototype.handleOptions=function(){var t=this.scroll.options.indicators;n(Array.isArray(t),"'indicators' must be an array.");for(var i=0,o=t;i<o.length;i++){var e=o[i];n(!!e.relationElement,"'relationElement' must be a HTMLElement."),this.createIndicators(e)}},E.prototype.createIndicators=function(t){this.indicators.push(new w(this.scroll,t))},E.prototype.handleHooks=function(){var o=this,t=this.scroll.hooks;t.on(t.eventTypes.destroy,function(){for(var t=0,i=o.indicators;t<i.length;t++)i[t].destroy();o.indicators=[]})},E.pluginName="indicators",E});