@better-scroll/infinity
Version:
The ability to inject a infinity load for BetterScroll.
2 lines (1 loc) • 10.8 kB
JavaScript
!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(n,i){var s,r,a,h={label:0,sent:function(){if(1&a[0])throw a[1];return a[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(o){return function(t){var e=[o,t];if(s)throw new TypeError("Generator is already executing.");for(;h;)try{if(s=1,r&&(a=2&e[0]?r.return:e[0]?r.throw||((a=r.return)&&a.call(r),0):r.next)&&!(a=a.call(r,e[1])).done)return a;switch(r=0,(e=a?[2&e[0],a.value]:e)[0]){case 0:case 1:a=e;break;case 4:return h.label++,{value:e[1],done:!1};case 5:h.label++,r=e[1],e=[0];continue;case 7:e=h.ops.pop(),h.trys.pop();continue;default:if(!(a=0<(a=h.trys).length&&a[a.length-1])&&(6===e[0]||2===e[0])){h=0;continue}if(3===e[0]&&(!a||e[1]>a[0]&&e[1]<a[3]))h.label=e[1];else if(6===e[0]&&h.label<a[1])h.label=a[1],a=e;else{if(!(a&&h.label<a[2])){a[2]&&h.ops.pop(),h.trys.pop();continue}h.label=a[2],h.ops.push(e)}}e=i.call(n,h)}catch(t){e=[6,t],r=0}finally{s=a=0}if(5&e[0])throw e[1];return{value:e[0]?e[1]:void 0,done:!0}}}}var n="undefined"!=typeof window,s=n&&navigator.userAgent.toLowerCase();if(s&&/wechatdevtools/.test(s),s&&s.indexOf("android"),"string"==typeof s)(s=/os (\d\d?_\d(_\d)?)/.exec(s))&&(13===(s=s[1].split("_").map(function(t){return parseInt(t,10)}))[0]&&s[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)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()+"-":"",s=l("transform"),r=l("transition");n&&l("perspective")in a;var d=s,p=r,c=(l("transitionTimingFunction"),l("transitionDuration"),l("transitionDelay"),l("transformOrigin"),l("transitionEnd"),l("transitionProperty"),f.prototype.calculate=function(t,e){var o=t-this.lastPos,o=(this.lastPos=t,this.getDirection(o)),n=this.calculateIndex(0,t,e),t=this.calculateIndex(n,t+this.wrapperHeight,e);return 1===o?(n-=10,t+=30):(n-=30,t+=10),{start:n=n<0?0:n,end:t}},f.prototype.getDirection=function(t){var e;if(0<t)e=1;else{if(!(t<0))return this.lastDirection;e=0}return this.lastDirection=e},f.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},f.prototype.resetState=function(){this.lastDirection=1,this.lastPos=0},f);function f(t,e){this.wrapperHeight=t,this.tombstoneHeight=e,this.lastDirection=1,this.lastPos=0}var y=function(){this.data=null,this.dom=null,this.tombstone=null,this.width=0,this.height=0,this.pos=0},m=(v.prototype.update=function(o){return t(this,void 0,void 0,function(){var e;return i(this,function(t){return(o=this.hasMore?o:Math.min(o,this.list.length))>this.list.length&&(e=o-this.list.length,this.addEmptyData(e)),[2,this.checkToFetch(o)]})})},v.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},v.prototype.addEmptyData=function(t){for(var e=0;e<t;e++)this.list.push(new y);return this.list},v.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]}})})},v.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]:(e=n-this.loadedNum,[4,this.fetch(e)]):[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])}})})},v.prototype.getList=function(){return this.list},v.prototype.resetState=function(){this.loadedNum=0,this.fetching=!1,this.hasMore=!0,this.list=[]},v);function v(t,e,o){this.fetchFn=e,this.onFetchFinish=o,this.loadedNum=0,this.fetching=!1,this.hasMore=!0,this.list=t||[]}b.isTombstone=function(t){return!(!t||!t.classList)&&t.classList.contains("tombstone")},b.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))},b.prototype.getOne=function(){var t,e=this.cached.pop();return e?((t=e.style).display="",t.opacity="1",t[d]="",t[p]="",e):this.create()},b.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},b.prototype.recycleOne=function(t){return this.cached.push(t),this.cached};var g=b;function b(t){this.create=t,this.cached=[],this.width=0,this.height=0,this.initialed=!1,this.getSize()}T.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}},T.prototype.collectUnusedDom=function(t,e,o){for(var n,i=0;i<t.length;i++)i===e?i=o-1:t[i].dom&&(n=t[i].dom,g.isTombstone(n)?(this.tombstone.recycleOne(n),n.style.display="none"):this.unusedDom.push(n),t[i].dom=null);return t},T.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(!g.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)}},T.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)},T.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}},T.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(!g.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}},T.prototype.removeTombstone=function(){for(var t=this.content.querySelectorAll(".tombstone"),e=t.length-1;0<=e;e--)this.content.removeChild(t[e])},T.prototype.setContent=function(t){t!==this.content&&(this.content=t)},T.prototype.destroy=function(){this.removeTombstone(),this.timers.forEach(function(t){clearTimeout(t)})},T.prototype.resetState=function(){this.destroy(),this.timers=[],this.unusedDom=[]};var w=T;function T(t,e,o){this.renderFn=e,this.tombstone=o,this.unusedDom=[],this.timers=[],this.setContent(t)}function D(t){this.scroll=t,this.start=0,this.end=0,this.init()}return D.prototype.init=function(){var e=this,t=(this.handleOptions(),this.options),o=t.fetch,n=t.render,t=t.createTombstone,t=(this.tombstone=new g(t),this.indexCalculator=new c(this.scroll.scroller.scrollBehaviorY.wrapperSize,this.tombstone.height),this.domManager=new w(this.scroll.scroller.content,n,this.tombstone),this.dataManager=new m([],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})}),this.scroll.scroller.scrollBehaviorY);t.hooks.on(t.hooks.eventTypes.computeBoundary,this.modifyBoundary,this),this.update({y:0})},D.prototype.modifyBoundary=function(t){t.maxScrollPos=-2e3},D.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},D.prototype.update=function(t){var t=Math.round(-t.y),t=this.indexCalculator.calculate(t,this.dataManager.getList()),e=t.start,t=t.end;this.start=e,this.end=t,this.dataManager.update(t),this.updateDom(this.dataManager.getList())},D.prototype.onFetchFinish=function(t,e){t=this.updateDom(t).end;return e||(this.domManager.removeTombstone(),this.scroll.scroller.animater.stop(),this.scroll.resetPosition()),t},D.prototype.updateDom=function(t){var t=this.domManager.update(t,this.start,this.end),e=t.end,o=t.startPos,n=t.endPos,t=t.startDelta;return t&&(this.scroll.minScrollY=t),n>this.scroll.maxScrollY&&(this.scroll.maxScrollY=-(n-this.scroll.scroller.scrollBehaviorY.wrapperSize)),{end:e,startPos:o,endPos:n}},D.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)},D.pluginName="infinity",D});