1 |
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).Infinity=e()}(this,function(){"use strict";function e(t){console.error("[BScroll warn]: "+t)}var o=function(){return(o=Object.assign||function(t){for(var e,o=1,n=arguments.length;o<n;o++)for(var i in e=arguments[o])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)};function t(t,r,a,h){return new(a=a||Promise)(function(o,e){function n(t){try{s(h.next(t))}catch(t){e(t)}}function i(t){try{s(h.throw(t))}catch(t){e(t)}}function s(t){var e;t.done?o(t.value):((e=t.value)instanceof a?e:new a(function(t){t(e)})).then(n,i)}s((h=h.apply(t,r||[])).next())})}function i(o,n){var i,s,r,a={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]},t={next:e(0),throw:e(1),return:e(2)};return"function"==typeof Symbol&&(t[Symbol.iterator]=function(){return this}),t;function e(e){return function(t){return function(e){if(i)throw new TypeError("Generator is already executing.");for(;a;)try{if(i=1,s&&(r=2&e[0]?s.return:e[0]?s.throw||((r=s.return)&&r.call(s),0):s.next)&&!(r=r.call(s,e[1])).done)return r;switch(s=0,(e=r?[2&e[0],r.value]:e)[0]){case 0:case 1:r=e;break;case 4:return a.label++,{value:e[1],done:!1};case 5:a.label++,s=e[1],e=[0];continue;case 7:e=a.ops.pop(),a.trys.pop();continue;default:if(!(r=0<(r=a.trys).length&&r[r.length-1])&&(6===e[0]||2===e[0])){a=0;continue}if(3===e[0]&&(!r||e[1]>r[0]&&e[1]<r[3])){a.label=e[1];break}if(6===e[0]&&a.label<r[1]){a.label=r[1],r=e;break}if(r&&a.label<r[2]){a.label=r[2],a.ops.push(e);break}r[2]&&a.ops.pop(),a.trys.pop();continue}e=n.call(o,a)}catch(t){e=[6,t],s=0}finally{i=r=0}if(5&e[0])throw e[1];return{value:e[0]?e[1]:void 0,done:!0}}([e,t])}}}var n="undefined"!=typeof window,s=n&&navigator.userAgent.toLowerCase();s&&/wechatdevtools/.test(s),s&&s.indexOf("android"),function(){if("string"==typeof s){var t=/os (\d\d?_\d(_\d)?)/.exec(s);if(!t)return;t=t[1].split("_").map(function(t){return parseInt(t,10)});13===t[0]&&t[1]}}();if(n)try{var r={};Object.defineProperty(r,"passive",{get:function(){}}),window.addEventListener("test-passive",function(){},r)}catch(t){}var a=n&&document.createElement("div").style,h=function(){if(!n)return!1;for(var t=0,e=[{key:"standard",value:"transform"},{key:"webkit",value:"webkitTransform"},{key:"Moz",value:"MozTransform"},{key:"O",value:"OTransform"},{key:"ms",value:"msTransform"}];t<e.length;t++){var o=e[t];if(void 0!==a[o.value])return o.key}return!1}();function l(t){return!1===h?t:"standard"===h?"transitionEnd"===t?"transitionend":t:h+t.charAt(0).toUpperCase()+t.substr(1)}var u=h&&"standard"!==h?"-"+h.toLowerCase()+"-":"",c=l("transform"),r=l("transition");n&&l("perspective")in a;var d=c,p=r,f=(l("transitionTimingFunction"),l("transitionDuration"),l("transitionDelay"),l("transformOrigin"),l("transitionEnd"),l("transitionProperty"),y.prototype.calculate=function(t,e){var o=t-this.lastPos;this.lastPos=t;var n=this.getDirection(o),o=this.calculateIndex(0,t,e),e=this.calculateIndex(o,t+this.wrapperHeight,e);return 1===n?(o-=10,e+=30):(o-=30,e+=10),{start:o=o<0?0:o,end:e}},y.prototype.getDirection=function(t){var e;if(0<t)e=1;else{if(!(t<0))return this.lastDirection;e=0}return this.lastDirection=e},y.prototype.calculateIndex=function(t,e,o){if(e<=0)return t;for(var n=t,i=o[n]&&-1!==o[n].pos?o[n].pos:0,t=0;n<o.length&&o[n].pos<e;)i=o[n].pos,n++;return n+=t=n===o.length?Math.floor((e-i)/this.tombstoneHeight):t},y.prototype.resetState=function(){this.lastDirection=1,this.lastPos=0},y);function y(t,e){this.wrapperHeight=t,this.tombstoneHeight=e,this.lastDirection=1,this.lastPos=0}var m=function(){this.data=null,this.dom=null,this.tombstone=null,this.width=0,this.height=0,this.pos=0},v=(g.prototype.update=function(o){return t(this,void 0,void 0,function(){var e;return i(this,function(t){return(o=!this.hasMore?Math.min(o,this.list.length):o)>this.list.length&&(e=o-this.list.length,this.addEmptyData(e)),[2,this.checkToFetch(o)]})})},g.prototype.add=function(t){for(var e=0;e<t.length;e++)this.list[this.loadedNum]?this.list[this.loadedNum]=o(o({},this.list[this.loadedNum]),{data:t[e]}):this.list[this.loadedNum]={data:t[e]},this.loadedNum++;return this.list},g.prototype.addEmptyData=function(t){for(var e=0;e<t;e++)this.list.push(new m);return this.list},g.prototype.fetch=function(o){return t(this,void 0,void 0,function(){var e;return i(this,function(t){switch(t.label){case 0:return this.fetching?[2,[]]:(this.fetching=!0,[4,this.fetchFn(o)]);case 1:return e=t.sent(),this.fetching=!1,[2,e]}})})},g.prototype.checkToFetch=function(n){return t(this,void 0,void 0,function(){var e,o;return i(this,function(t){switch(t.label){case 0:return this.hasMore?n<=this.loadedNum?[2]:(o=n-this.loadedNum,[4,this.fetch(o)]):[2];case 1:return(e=t.sent())instanceof Array&&e.length?(this.add(e),o=this.onFetchFinish(this.list,!0),[2,this.checkToFetch(o)]):("boolean"==typeof e&&!1===e&&(this.hasMore=!1,this.list.splice(this.loadedNum),this.onFetchFinish(this.list,!1)),[2])}})})},g.prototype.getList=function(){return this.list},g.prototype.resetState=function(){this.loadedNum=0,this.fetching=!1,this.hasMore=!0,this.list=[]},g);function g(t,e,o){this.fetchFn=e,this.onFetchFinish=o,this.loadedNum=0,this.fetching=!1,this.hasMore=!0,this.list=t||[]}var b=(w.isTombstone=function(t){return!(!t||!t.classList)&&t.classList.contains("tombstone")},w.prototype.getSize=function(){var t;this.initialed||((t=this.create()).style.position="absolute",document.body.appendChild(t),t.style.display="",this.height=t.offsetHeight,this.width=t.offsetWidth,document.body.removeChild(t),this.cached.push(t))},w.prototype.getOne=function(){var t=this.cached.pop();if(t){var e=t.style;return e.display="",e.opacity="1",e[d]="",e[p]="",t}return this.create()},w.prototype.recycle=function(t){for(var e=0,o=t;e<o.length;e++){var n=o[e];n.style.display="none",this.cached.push(n)}return this.cached},w.prototype.recycleOne=function(t){return this.cached.push(t),this.cached},w);function w(t){this.create=t,this.cached=[],this.width=0,this.height=0,this.initialed=!1,this.getSize()}var T=(D.prototype.update=function(t,e,o){e>=t.length&&(e=t.length-1),o>t.length&&(o=t.length),this.collectUnusedDom(t,e,o),this.createDom(t,e,o),this.cacheHeight(t,e,o);t=this.positionDom(t,e,o);return{start:e,startPos:t.startPos,startDelta:t.startDelta,end:o,endPos:t.endPos}},D.prototype.collectUnusedDom=function(t,e,o){for(var n,i=0;i<t.length;i++)i!==e?t[i].dom&&(n=t[i].dom,b.isTombstone(n)?(this.tombstone.recycleOne(n),n.style.display="none"):this.unusedDom.push(n),t[i].dom=null):i=o-1;return t},D.prototype.createDom=function(t,e,o){for(var n=e;n<o;n++){var i=t[n].dom,s=t[n].data;if(i){if(!b.isTombstone(i)||!s)continue;t[n].tombstone=i,t[n].dom=null}(i=s?this.renderFn(s,this.unusedDom.pop()):this.tombstone.getOne()).style.position="absolute",t[n].dom=i,t[n].pos=-1,this.content.appendChild(i)}},D.prototype.cacheHeight=function(t,e,o){for(var n=e;n<o;n++)t[n].data&&!t[n].height&&(t[n].height=t[n].dom.offsetHeight)},D.prototype.positionDom=function(t,e,o){for(var n=this,i=[],s=this.getStartPos(t,e,o),r=s.start,s=s.delta,a=r,h=e;h<o;h++){var l,c=t[h].tombstone;c&&((l=c.style)[p]=u+"transform 200ms, opacity 200ms",l[d]="translateY("+a+"px)",l.opacity="0",t[h].tombstone=null,i.push(c)),t[h].dom&&t[h].pos!==a&&(t[h].dom.style[d]="translateY("+a+"px)",t[h].pos=a),a+=t[h].height||this.tombstone.height}e=window.setTimeout(function(){n.tombstone.recycle(i)},200);return this.timers.push(e),{startPos:r,startDelta:s,endPos:a}},D.prototype.getStartPos=function(t,e,o){if(t[e]&&-1!==t[e].pos)return{start:t[e].pos,delta:0};for(var n=-1===t[0].pos?0:t[0].pos,i=0;i<e;i++)n+=t[i].height||this.tombstone.height;for(var s=n,r=e;r<o;r++)if(!b.isTombstone(t[r].dom)&&-1!==t[r].pos){n=t[r].pos;break}var a=r;if(a<o)for(;e<a;)n-=t[a-1].height,a--;return{start:n,delta:s-n}},D.prototype.removeTombstone=function(){for(var t=this.content.querySelectorAll(".tombstone"),e=t.length-1;0<=e;e--)this.content.removeChild(t[e])},D.prototype.setContent=function(t){t!==this.content&&(this.content=t)},D.prototype.destroy=function(){this.removeTombstone(),this.timers.forEach(function(t){clearTimeout(t)})},D.prototype.resetState=function(){this.destroy(),this.timers=[],this.unusedDom=[]},D);function D(t,e,o){this.renderFn=e,this.tombstone=o,this.unusedDom=[],this.timers=[],this.setContent(t)}function P(t){this.scroll=t,this.start=0,this.end=0,this.init()}return P.prototype.init=function(){var e=this;this.handleOptions();var t=this.options,o=t.fetch,n=t.render,t=t.createTombstone;this.tombstone=new b(t),this.indexCalculator=new f(this.scroll.scroller.scrollBehaviorY.wrapperSize,this.tombstone.height),this.domManager=new T(this.scroll.scroller.content,n,this.tombstone),this.dataManager=new v([],o,this.onFetchFinish.bind(this)),this.scroll.on(this.scroll.eventTypes.destroy,this.destroy,this),this.scroll.on(this.scroll.eventTypes.scroll,this.update,this),this.scroll.on(this.scroll.eventTypes.contentChanged,function(t){e.domManager.setContent(t),e.indexCalculator.resetState(),e.domManager.resetState(),e.dataManager.resetState(),e.update({y:0})});o=this.scroll.scroller.scrollBehaviorY;o.hooks.on(o.hooks.eventTypes.computeBoundary,this.modifyBoundary,this),this.update({y:0})},P.prototype.modifyBoundary=function(t){t.maxScrollPos=-2e3},P.prototype.handleOptions=function(){var t=this.scroll.options.infinity;t&&("function"!=typeof t.fetch&&e("Infinity plugin need fetch Function to new data."),"function"!=typeof t.render&&e("Infinity plugin need render Function to render each item."),"function"!=typeof t.render&&e("Infinity plugin need createTombstone Function to create tombstone."),this.options=t),this.scroll.options.probeType=3},P.prototype.update=function(t){var e=Math.round(-t.y),t=this.indexCalculator.calculate(e,this.dataManager.getList()),e=t.start,t=t.end;this.start=e,this.end=t,this.dataManager.update(t),this.updateDom(this.dataManager.getList())},P.prototype.onFetchFinish=function(t,e){t=this.updateDom(t).end;return e||(this.domManager.removeTombstone(),this.scroll.scroller.animater.stop(),this.scroll.resetPosition()),t},P.prototype.updateDom=function(t){var e=this.domManager.update(t,this.start,this.end),o=e.end,n=e.startPos,t=e.endPos,e=e.startDelta;return e&&(this.scroll.minScrollY=e),t>this.scroll.maxScrollY&&(this.scroll.maxScrollY=-(t-this.scroll.scroller.scrollBehaviorY.wrapperSize)),{end:o,startPos:n,endPos:t}},P.prototype.destroy=function(){for(var t=this.scroll.scroller,e=t.content,t=t.scrollBehaviorY;e.firstChild;)e.removeChild(e.firstChild);this.domManager.destroy(),this.scroll.off("scroll",this.update),this.scroll.off("destroy",this.destroy),t.hooks.off(t.hooks.eventTypes.computeBoundary)},P.pluginName="infinity",P});
|