universalviewer
Version:
The Universal Viewer is a community-developed open source project on a mission to help you share your 📚📜📰📽️📻🗿 with the 🌎
2 lines (1 loc) • 10.2 kB
JavaScript
import{a as O,b as T,c as L,e as x,f as V,l as k}from"./chunk-VNJCZCB4.js";import"./chunk-L67DAW3A.js";var w=function(r,t,e,n){return new(e||(e=Promise))(function(s,i){function o(a){try{u(n.next(a))}catch(c){i(c)}}function l(a){try{u(n.throw(a))}catch(c){i(c)}}function u(a){a.done?s(a.value):new e(function(c){c(a.value)}).then(o,l)}u((n=n.apply(r,t||[])).next())})},H=function(r,t){var e={label:0,sent:function(){if(i[0]&1)throw i[1];return i[1]},trys:[],ops:[]},n,s,i,o;return o={next:l(0),throw:l(1),return:l(2)},typeof Symbol=="function"&&(o[Symbol.iterator]=function(){return this}),o;function l(a){return function(c){return u([a,c])}}function u(a){if(n)throw new TypeError("Generator is already executing.");for(;e;)try{if(n=1,s&&(i=a[0]&2?s.return:a[0]?s.throw||((i=s.return)&&i.call(s),0):s.next)&&!(i=i.call(s,a[1])).done)return i;switch(s=0,i&&(a=[a[0]&2,i.value]),a[0]){case 0:case 1:i=a;break;case 4:return e.label++,{value:a[1],done:!1};case 5:e.label++,s=a[1],a=[0];continue;case 7:a=e.ops.pop(),e.trys.pop();continue;default:if(i=e.trys,!(i=i.length>0&&i[i.length-1])&&(a[0]===6||a[0]===2)){e=0;continue}if(a[0]===3&&(!i||a[1]>i[0]&&a[1]<i[3])){e.label=a[1];break}if(a[0]===6&&e.label<i[1]){e.label=i[1],i=a;break}if(i&&e.label<i[2]){e.label=i[2],e.ops.push(a);break}i[2]&&e.ops.pop(),e.trys.pop();continue}a=t.call(r,e)}catch(c){a=[6,c],s=0}finally{n=i=0}if(a[0]&5)throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}},E="item",I="header",C="footer",P=0,N=1,_=2,S=2,R=function(r,t,e,n){for(var s=0,i=r;s<i.length;s++){var o=i[s];o.change=P,o.d=!0}for(var l=[],u=n.offset+n.length,a=function(h){var v=e[h],p=r.find(function(D){return D.d&&D.cell===v});if(p){var b=t[h];b!==p.top&&(p.top=b,p.change=N),p.d=!1}else l.push(v)},c=n.offset;c<u;c++)a(c);for(var g=r.filter(function(h){return h.d}),d=function(h){var v=g.find(function(b){return b.d&&b.cell.type===h.type}),p=h.i;v?(v.d=!1,v.change=_,v.cell=h,v.top=t[p]):r.push({d:!1,cell:h,visible:!0,change:_,top:t[p]})},y=0,f=l;y<f.length;y++){var m=f[y];d(m)}r.filter(function(h){return h.d&&h.top!==-9999}).forEach(function(h){h.change=N,h.top=-9999})},F=function(r,t,e,n){for(var s=Array.from(r.children).filter(function(d){return d.tagName!=="TEMPLATE"}),i=s.length,o,l=0;l<e.length;l++){var u=e[l],a=u.cell;if(u.change===_){if(l<i)o=s[l],t(o,a,l);else{var c=A(r,a.type);o=t(c,a,l)||c,o.classList.add("virtual-item"),r.appendChild(o)}o.$ionCell=a}else o=s[l];u.change!==P&&(o.style.transform="translate3d(0,"+u.top+"px,0)");var g=a.visible;u.visible!==g&&(g?o.classList.remove("virtual-loading"):o.classList.add("virtual-loading"),u.visible=g),a.reads>0&&(n(a,o),a.reads--)}},A=function(r,t){var e=M(r,t);return e&&r.ownerDocument?r.ownerDocument.importNode(e.content,!0).children[0]:null},M=function(r,t){switch(t){case E:return r.querySelector("template:not([name])");case I:return r.querySelector("template[name=header]");case C:return r.querySelector("template[name=footer]")}},j=function(r,t,e){return{top:Math.max(r-e,0),bottom:r+t+e}},G=function(r,t,e){for(var n=t.top,s=t.bottom,i=0;i<r.length&&!(r[i]>n);i++);for(var o=Math.max(i-e-1,0);i<r.length&&!(r[i]>=s);i++);var l=Math.min(i+e,r.length),u=l-o;return{offset:o,length:u}},q=function(r,t,e){var n=e.offset+e.length;return r<=n||t.offset!==e.offset||t.length!==e.length},z=function(r,t){var e=r.length>0?r[r.length-1].index:0;return t===0?0:t===e+1?r.length:r.findIndex(function(n){return n.index===t})},Y=function(r,t,e){if(e===0&&t.length>=r.length)return t;for(var n=0;n<t.length;n++)r[n+e]=t[n];return r},U=function(r,t,e,n,s,i,o,l,u,a,c,g){for(var d=[],y=g+c,f=c;f<y;f++){var m=r[f];if(s){var h=s(m,f,r);h!=null&&d.push({i:a++,type:I,value:h,index:f,height:e?e(h,f):o,reads:e?0:S,visible:!!e})}if(d.push({i:a++,type:E,value:m,index:f,height:t?t(m,f):u,reads:t?0:S,visible:!!t}),i){var h=i(m,f,r);h!=null&&d.push({i:a++,type:C,value:h,index:f,height:n?n(h,f):l,reads:n?0:S,visible:!!n})}}return d},$=function(r,t,e){for(var n=r[e],s=e;s<r.length;s++)r[s]=n,n+=t[s].height;return n},B=function(r,t){if(!r)return new Uint32Array(t);if(r.length===t)return r;if(t>r.length){var e=new Uint32Array(t);return e.set(r),e}else return r.subarray(0,t)},J=function(r,t,e){var n=t.find(function(s){return s.type===E&&s.index===r});return n?e[n.i]:-1},W=function(){function r(t){var e=this;O(this,t),this.range={offset:0,length:0},this.viewportHeight=0,this.cells=[],this.virtualDom=[],this.isEnabled=!1,this.viewportOffset=0,this.currentScrollTop=0,this.indexDirty=0,this.lastItemLen=0,this.totalHeight=0,this.approxItemHeight=45,this.approxHeaderHeight=30,this.approxFooterHeight=30,this.onScroll=function(){e.updateVirtualScroll()}}return r.prototype.itemsChanged=function(){this.calcCells(),this.updateVirtualScroll()},r.prototype.connectedCallback=function(){return w(this,void 0,void 0,function(){var t,e;return H(this,function(n){switch(n.label){case 0:return t=this.el.closest("ion-content"),t?(e=this,[4,t.getScrollElement()]):(console.error("<ion-virtual-scroll> must be used inside an <ion-content>"),[2]);case 1:return e.scrollEl=n.sent(),this.contentEl=t,this.calcCells(),this.updateState(),[2]}})})},r.prototype.componentDidUpdate=function(){this.updateState()},r.prototype.disconnectedCallback=function(){this.scrollEl=void 0},r.prototype.onResize=function(){this.calcCells(),this.updateVirtualScroll()},r.prototype.positionForItem=function(t){return Promise.resolve(J(t,this.cells,this.getHeightIndex()))},r.prototype.checkRange=function(t,e){return e===void 0&&(e=-1),w(this,void 0,void 0,function(){var n,s,i;return H(this,function(o){return this.items?(n=e===-1?this.items.length-t:e,s=z(this.cells,t),i=U(this.items,this.itemHeight,this.headerHeight,this.footerHeight,this.headerFn,this.footerFn,this.approxHeaderHeight,this.approxFooterHeight,this.approxItemHeight,s,t,n),this.cells=Y(this.cells,i,s),this.lastItemLen=this.items.length,this.indexDirty=Math.max(t-1,0),this.scheduleUpdate(),[2]):[2]})})},r.prototype.checkEnd=function(){return w(this,void 0,void 0,function(){return H(this,function(t){return this.items&&this.checkRange(this.lastItemLen),[2]})})},r.prototype.updateVirtualScroll=function(){!this.isEnabled||!this.scrollEl||(this.timerUpdate&&(clearTimeout(this.timerUpdate),this.timerUpdate=void 0),T(this.readVS.bind(this)),L(this.writeVS.bind(this)))},r.prototype.readVS=function(){for(var t=this,e=t.contentEl,n=t.scrollEl,s=t.el,i=0,o=s;o&&o!==e;)i+=o.offsetTop,o=o.parentElement;this.viewportOffset=i,n&&(this.viewportHeight=n.offsetHeight,this.currentScrollTop=n.scrollTop)},r.prototype.writeVS=function(){var t=this.indexDirty,e=this.currentScrollTop-this.viewportOffset,n=j(e,this.viewportHeight,100),s=this.getHeightIndex(),i=G(s,n,2),o=q(t,this.range,i);o&&(this.range=i,R(this.virtualDom,s,this.cells,i),this.nodeRender?F(this.el,this.nodeRender,this.virtualDom,this.updateCellHeight.bind(this)):this.domRender?this.domRender(this.virtualDom):this.renderItem&&this.el.forceUpdate())},r.prototype.updateCellHeight=function(t,e){var n=this,s=function(){if(e.$ionCell===t){var i=window.getComputedStyle(e),o=e.offsetHeight+parseFloat(i.getPropertyValue("margin-bottom"));n.setCellHeight(t,o)}};e&&e.componentOnReady?e.componentOnReady().then(s):s()},r.prototype.setCellHeight=function(t,e){var n=t.i;t===this.cells[n]&&(t.height!==e||t.visible!==!0)&&(t.visible=!0,t.height=e,this.indexDirty=Math.min(this.indexDirty,n),this.scheduleUpdate())},r.prototype.scheduleUpdate=function(){var t=this;clearTimeout(this.timerUpdate),this.timerUpdate=setTimeout(function(){return t.updateVirtualScroll()},100)},r.prototype.updateState=function(){var t=!!(this.scrollEl&&this.cells);t!==this.isEnabled&&(this.enableScrollEvents(t),t&&this.updateVirtualScroll())},r.prototype.calcCells=function(){this.items&&(this.lastItemLen=this.items.length,this.cells=U(this.items,this.itemHeight,this.headerHeight,this.footerHeight,this.headerFn,this.footerFn,this.approxHeaderHeight,this.approxFooterHeight,this.approxItemHeight,0,0,this.lastItemLen),this.indexDirty=0)},r.prototype.getHeightIndex=function(){return this.indexDirty!==1/0&&this.calcHeightIndex(this.indexDirty),this.heightIndex},r.prototype.calcHeightIndex=function(t){t===void 0&&(t=0),this.heightIndex=B(this.heightIndex,this.cells.length),this.totalHeight=$(this.heightIndex,this.cells,t),this.indexDirty=1/0},r.prototype.enableScrollEvents=function(t){var e=this;this.rmEvent&&(this.rmEvent(),this.rmEvent=void 0);var n=this.scrollEl;n&&(this.isEnabled=t,n.addEventListener("scroll",this.onScroll),this.rmEvent=function(){n.removeEventListener("scroll",e.onScroll)})},r.prototype.renderVirtualNode=function(t){var e=t.cell,n=e.type,s=e.value,i=e.index;switch(n){case E:return this.renderItem(s,i);case I:return this.renderHeader(s,i);case C:return this.renderFooter(s,i)}},r.prototype.render=function(){var t=this;return x(V,{style:{height:this.totalHeight+"px"}},this.renderItem&&x(K,{dom:this.virtualDom},this.virtualDom.map(function(e){return t.renderVirtualNode(e)})))},Object.defineProperty(r.prototype,"el",{get:function(){return k(this)},enumerable:!0,configurable:!0}),Object.defineProperty(r,"watchers",{get:function(){return{itemHeight:["itemsChanged"],headerHeight:["itemsChanged"],footerHeight:["itemsChanged"],items:["itemsChanged"]}},enumerable:!0,configurable:!0}),Object.defineProperty(r,"style",{get:function(){return"ion-virtual-scroll{display:block;position:relative;width:100%;contain:strict;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}ion-virtual-scroll>.virtual-loading{opacity:0}ion-virtual-scroll>.virtual-item{position:absolute!important;top:0!important;right:0!important;left:0!important;-webkit-transition-duration:0ms;transition-duration:0ms;will-change:transform}"},enumerable:!0,configurable:!0}),r}(),K=function(r,t,e){var n=r.dom;return e.map(t,function(s,i){var o=n[i],l=s.vattrs||{},u=l.class||"";return u+="virtual-item ",o.visible||(u+="virtual-loading"),Object.assign(Object.assign({},s),{vattrs:Object.assign(Object.assign({},l),{class:u,style:Object.assign(Object.assign({},l.style),{transform:"translate3d(0,"+o.top+"px,0)"})})})})};export{W as ion_virtual_scroll};