primevue
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primevue) [ • 10.8 kB
JavaScript
this.primevue=this.primevue||{},this.primevue.virtualscroller=function(t){"use strict";var s={name:"VirtualScroller",emits:["update:numToleratedItems","scroll-index-change","lazy-load"],props:{items:{type:Array,default:null},itemSize:{type:[Number,Array],default:null},scrollHeight:null,scrollWidth:null,orientation:{type:String,default:"vertical"},numToleratedItems:{type:Number,default:null},delay:{type:Number,default:0},lazy:{type:Boolean,default:!1},showLoader:{type:Boolean,default:!1},loading:{type:Boolean,default:!1},style:null,class:null,disabled:{type:Boolean,default:!1}},data(){return{first:this.isBoth()?{rows:0,cols:0}:0,last:this.isBoth()?{rows:0,cols:0}:0,numItemsInViewport:this.isBoth()?{rows:0,cols:0}:0,lastScrollPos:this.isBoth()?{top:0,left:0}:0,d_numToleratedItems:this.numToleratedItems,d_loading:this.loading,loaderArr:null}},element:null,content:null,spacer:null,scrollTimeout:null,mounted(){this.init()},watch:{numToleratedItems(t){this.d_numToleratedItems=t},loading(t){this.d_loading=t},items(t,s){s&&s.length===(t||[]).length||this.init()}},methods:{init(){this.disabled||(this.setSize(),this.calculateOptions(),this.setSpacerSize())},getLast(t,s){return this.items?Math.min(s?this.items[0].length:this.items.length,t):0},calculateOptions(){const t=this.isBoth(),s=this.isHorizontal(),e=this.first,i=this.itemSize,o=this.getContentPadding(),l=this.element?this.element.offsetWidth-o.left:0,n=this.element?this.element.offsetHeight-o.top:0,r=(t,s)=>Math.ceil(t/(s||t)),a=t?{rows:r(n,i[0]),cols:r(l,i[1])}:r(s?l:n,i);let h=this.d_numToleratedItems||Math.ceil((t?a.rows:a)/2);const c=(t,s,e)=>this.getLast(t+s+(t<h?2:3)*h,e),d=t?{rows:c(e.rows,a.rows),cols:c(e.cols,a.cols,!0)}:c(e,a);this.d_numToleratedItems=h,this.$emit("update:numToleratedItems",this.d_numToleratedItems),this.last=d,this.numItemsInViewport=a,this.showLoader&&(this.$slots&&this.$slots.loader?this.loaderArr=Array.from({length:t?a.rows:a}):this.loaderArr=Array.from({length:1})),this.lazy&&this.$emit("lazy-load",{first:e,last:d})},getContentPadding(){if(this.content){const t=getComputedStyle(this.content),s=parseInt(parseFloat(t.paddingLeft.slice(0,-2)),10),e=parseInt(parseFloat(t.paddingRight.slice(0,-2)),10),i=parseInt(parseFloat(t.paddingTop.slice(0,-2)),10),o=parseInt(parseFloat(t.paddingBottom.slice(0,-2)),10);return{left:s,right:e,top:i,bottom:o,x:s+e,y:i+o}}return{left:0,right:0,top:0,bottom:0,x:0,y:0}},setSize(){if(this.element){const t=this.isBoth(),s=this.isHorizontal(),e=this.element.parentElement,i=this.scrollWidth||`${this.element.offsetWidth||e.offsetWidth}px`,o=this.scrollHeight||`${this.element.offsetHeight||e.offsetHeight}px`,l=(t,s)=>this.element.style[t]=s;t?(l("height",o),l("width",i)):s?l("width",i):l("height",o)}},setSpacerSize(){const t=this.items;if(this.spacer&&t){const s=this.isBoth(),e=this.isHorizontal(),i=this.itemSize,o=this.getContentPadding(),l=(t,s,e,i=0)=>this.spacer.style[t]=(s||[]).length*e+i+"px";s?(l("height",t[0],i[0],o.y),l("width",t[1],i[1],o.x)):e?l("width",t,i,o.x):l("height",t,i,o.y)}},setContentPosition(t){if(this.content){const s=this.isBoth(),e=this.isHorizontal(),i=this.content,o=t?t.first:this.first,l=this.itemSize,n=(t,s)=>t*s,r=(t=0,s=0)=>i.style.transform=`translate3d(${t}px, ${s}px, 0)`;if(s)r(n(o.cols,l[1]),n(o.rows,l[0]));else{const t=n(o,l);e?r(t,0):r(0,t)}}},onScrollPositionChange(t){const s=t.target,e=this.isBoth(),i=this.isHorizontal(),o=this.itemSize,l=this.getContentPadding(),n=(t,s)=>t?t>s?t-s:t:0,r=(t,s)=>Math.floor(t/(s||t)),a=(t,s,e,i,o)=>t<=this.d_numToleratedItems?this.d_numToleratedItems:o?e-i-this.d_numToleratedItems:s+this.d_numToleratedItems-1,h=(t,s,e,i,o,l)=>t<=this.d_numToleratedItems?0:l?t<s?e:t-this.d_numToleratedItems:t>s?e:t-2*this.d_numToleratedItems,c=(t,s,e,i,o)=>{let l=s+i+2*this.d_numToleratedItems;return t>=this.d_numToleratedItems&&(l+=this.d_numToleratedItems+1),this.getLast(l,o)},d=n(s.scrollTop,l.top),m=n(s.scrollLeft,l.left);let f=0,p=this.last,u=!1;if(e){const t=this.lastScrollPos.top<=d,s=this.lastScrollPos.left<=m,e={rows:r(d,o[0]),cols:r(m,o[1])},i={rows:a(e.rows,this.first.rows,this.last.rows,this.numItemsInViewport.rows,t),cols:a(e.cols,this.first.cols,this.last.cols,this.numItemsInViewport.cols,s)};f={rows:h(e.rows,i.rows,this.first.rows,this.last.rows,this.numItemsInViewport.rows,t),cols:h(e.cols,i.cols,this.first.cols,this.last.cols,this.numItemsInViewport.cols,s)},p={rows:c(e.rows,f.rows,this.last.rows,this.numItemsInViewport.rows),cols:c(e.cols,f.cols,this.last.cols,this.numItemsInViewport.cols,!0)},u=f.rows!==this.first.rows||f.cols!==this.first.cols||p.rows!==this.last.rows||p.cols!==this.last.cols,this.lastScrollPos={top:d,left:m}}else{const t=i?m:d,s=this.lastScrollPos<=t,e=r(t,o);f=h(e,a(e,this.first,this.last,this.numItemsInViewport,s),this.first,this.last,this.numItemsInViewport,s),p=c(e,f,this.last,this.numItemsInViewport),u=f!==this.first||p!==this.last,this.lastScrollPos=t}return{first:f,last:p,isRangeChanged:u}},onScrollChange(t){const{first:s,last:e,isRangeChanged:i}=this.onScrollPositionChange(t);if(i){const t={first:s,last:e};this.setContentPosition(t),this.lazy&&this.$emit("lazy-load",{first:s,last:e}),this.first=s,this.last=e,this.$emit("scroll-index-change",{first:s,last:e})}},onScroll(t){if(this.delay&&!this.lazy){if(this.scrollTimeout&&clearTimeout(this.scrollTimeout),!this.d_loading&&this.showLoader){const{isRangeChanged:s}=this.onScrollPositionChange(t);s&&(this.d_loading=!0)}this.scrollTimeout=setTimeout((()=>{this.onScrollChange(t),this.d_loading&&this.showLoader&&!this.lazy&&(this.d_loading=!1)}),this.delay)}else this.onScrollChange(t)},getOptions(t){let s=this.items.length,e=this.isBoth()?this.first.rows+t:this.first+t;return{index:e,count:s,first:0===e,last:e===s-1,even:e%2==0,odd:e%2!=0}},getLoaderOptions(t){let s=this.loaderArr.length;return{loading:this.d_loading,first:0===t,last:t===s-1,even:t%2==0,odd:t%2!=0}},isHorizontal(){return"horizontal"===this.orientation},isBoth(){return"both"===this.orientation},scrollTo(t){this.element&&this.element.scrollTo(t)},scrollToIndex(t,s="auto"){const e=this.isBoth(),i=this.isHorizontal(),o=this.itemSize,l=this.getContentPadding(),n=(t=0)=>t<=this.d_numToleratedItems?0:t,r=(t,s,e)=>t*s+e,a=(t=0,e=0)=>this.scrollTo({left:t,top:e,behavior:s});if(e){const s={rows:n(t[0]),cols:n(t[1])};s.rows===this.first.rows&&s.cols===this.first.cols||a(r(s.cols,o[1],l.left),r(s.rows,o[0],l.top))}else{const s=n(t);s!==this.first&&(i?a(r(s,o,l.left),0):a(0,r(s,o,l.top))),this.first=s}},scrollInView(t,s,e="auto"){if(s){const i=this.isBoth(),o=this.isHorizontal(),{first:l,viewport:n}=this.getRenderedRange(),r=this.itemSize,a=(t=0,s=0)=>this.scrollTo({left:t,top:s,behavior:e}),h="to-end"===s;if("to-start"===s){if(i)n.first.rows-l.rows>t[0]?a(n.first.cols*r[1],(n.first.rows-1)*r[0]):n.first.cols-l.cols>t[1]&&a((n.first.cols-1)*r[1],n.first.rows*r[0]);else if(n.first-l>t){const t=(n.first-1)*r;o?a(t,0):a(0,t)}}else if(h)if(i)n.last.rows-l.rows<=t[0]+1?a(n.first.cols*r[1],(n.first.rows+1)*r[0]):n.last.cols-l.cols<=t[1]+1&&a((n.first.cols+1)*r[1],n.first.rows*r[0]);else if(n.last-l<=t+1){const t=(n.first+1)*r;o?a(t,0):a(0,t)}}else this.scrollToIndex(t,e)},getRenderedRange(){const t=this.isBoth(),s=this.isHorizontal(),e=this.itemSize,i=(t,s)=>Math.floor(t/(s||t));let o=this.first,l=0;if(this.element){const n=this.element.scrollTop,r=this.element.scrollLeft;if(t)o={rows:i(n,e[0]),cols:i(r,e[1])},l={rows:o.rows+this.numItemsInViewport.rows,cols:o.cols+this.numItemsInViewport.cols};else{o=i(s?r:n,e),l=o+this.numItemsInViewport}}return{first:this.first,last:this.last,viewport:{first:o,last:l}}},elementRef(t){this.element=t},contentRef(t){this.content=t},spacerRef(t){this.spacer=t}},computed:{containerClass(){return["p-virtualscroller",{"p-both-scroll":this.isBoth(),"p-horizontal-scroll":this.isHorizontal()},this.class]},loaderClass(){return["p-virtualscroller-loader",{"p-component-overlay":!this.$slots.loader}]},loadedItems(){const t=this.items;if(t&&!this.d_loading){return this.isBoth()?t.slice(this.first.rows,this.last.rows).map((t=>t.slice(this.first.cols,this.last.cols))):t.slice(this.first,this.last).map((t=>t))}return[]}}};const e=t.createVNode("i",{class:"p-virtualscroller-loading-icon pi pi-spinner pi-spin"},null,-1);return function(t,s){void 0===s&&(s={});var e=s.insertAt;if(t&&"undefined"!=typeof document){var i=document.head||document.getElementsByTagName("head")[0],o=document.createElement("style");o.type="text/css","top"===e&&i.firstChild?i.insertBefore(o,i.firstChild):i.appendChild(o),o.styleSheet?o.styleSheet.cssText=t:o.appendChild(document.createTextNode(t))}}("\n.p-virtualscroller {\n position: relative;\n overflow: auto;\n contain: strict;\n -webkit-transform: translateZ(0);\n transform: translateZ(0);\n will-change: scroll-position;\n}\n.p-virtualscroller-content {\n position: absolute;\n top: 0;\n left: 0;\n contain: content;\n min-height: 100%;\n min-width: 100%;\n will-change: transform;\n}\n.p-virtualscroller-spacer {\n position: absolute;\n top: 0;\n left: 0;\n height: 1px;\n width: 1px;\n -webkit-transform-origin: 0 0;\n transform-origin: 0 0;\n pointer-events: none;\n}\n.p-virtualscroller .p-virtualscroller-loader {\n position: sticky;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n}\n.p-virtualscroller-loader.p-component-overlay {\n display: -webkit-box;\n display: -ms-flexbox;\n display: flex;\n -webkit-box-align: center;\n -ms-flex-align: center;\n align-items: center;\n -webkit-box-pack: center;\n -ms-flex-pack: center;\n justify-content: center;\n}\n"),s.render=function(s,i,o,l,n,r){return o.disabled?(t.openBlock(),t.createBlock(t.Fragment,{key:1},[t.renderSlot(s.$slots,"default"),t.renderSlot(s.$slots,"content",{items:o.items})],64)):(t.openBlock(),t.createBlock("div",{key:0,ref:r.elementRef,class:r.containerClass,style:o.style,onScroll:i[1]||(i[1]=(...t)=>r.onScroll&&r.onScroll(...t))},[t.renderSlot(s.$slots,"content",{styleClass:"p-virtualscroller-content",contentRef:r.contentRef,items:r.loadedItems,getItemOptions:r.getOptions},(()=>[t.createVNode("div",{ref:r.contentRef,class:"p-virtualscroller-content"},[(t.openBlock(!0),t.createBlock(t.Fragment,null,t.renderList(r.loadedItems,((e,i)=>t.renderSlot(s.$slots,"item",{key:i,item:e,options:r.getOptions(i)}))),128))],512)])),t.createVNode("div",{ref:r.spacerRef,class:"p-virtualscroller-spacer"},null,512),n.d_loading?(t.openBlock(),t.createBlock("div",{key:0,class:r.loaderClass},[(t.openBlock(!0),t.createBlock(t.Fragment,null,t.renderList(n.loaderArr,((i,o)=>t.renderSlot(s.$slots,"loader",{key:o,options:r.getLoaderOptions(o)},(()=>[e])))),128))],2)):t.createCommentVNode("",!0)],38))},s}(Vue);