UNPKG

primevue

Version:

PrimeVue is an open source UI library for Vue featuring a rich set of 80+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with PrimeBloc

2 lines (1 loc) 18.9 kB
"use strict";var t=require("primevue/icons/spinner"),e=require("primevue/utils"),i=require("primevue/basecomponent"),s=require("primevue/virtualscroller/style"),o=require("vue");function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var l=n(t),r=n(i),a=n(s);function h(t){return h="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},h(t)}function c(t,e){var i=Object.keys(t);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(t);e&&(s=s.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),i.push.apply(i,s)}return i}function d(t){for(var e=1;e<arguments.length;e++){var i=null!=arguments[e]?arguments[e]:{};e%2?c(Object(i),!0).forEach((function(e){u(t,e,i[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(i)):c(Object(i)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(i,e))}))}return t}function u(t,e,i){var s;return(e="symbol"==h(s=m(e,"string"))?s:String(s))in t?Object.defineProperty(t,e,{value:i,enumerable:!0,configurable:!0,writable:!0}):t[e]=i,t}function m(t,e){if("object"!=h(t)||!t)return t;var i=t[Symbol.toPrimitive];if(void 0!==i){var s=i.call(t,e||"default");if("object"!=h(s))return s;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}var f={name:"VirtualScroller",extends:{name:"BaseVirtualScroller",extends:r.default,props:{id:{type:String,default:null},style:null,class:null,items:{type:Array,default:null},itemSize:{type:[Number,Array],default:0},scrollHeight:null,scrollWidth:null,orientation:{type:String,default:"vertical"},numToleratedItems:{type:Number,default:null},delay:{type:Number,default:0},resizeDelay:{type:Number,default:10},lazy:{type:Boolean,default:!1},disabled:{type:Boolean,default:!1},loaderDisabled:{type:Boolean,default:!1},columns:{type:Array,default:null},loading:{type:Boolean,default:!1},showSpacer:{type:Boolean,default:!0},showLoader:{type:Boolean,default:!1},tabindex:{type:Number,default:0},inline:{type:Boolean,default:!1},step:{type:Number,default:0},appendOnly:{type:Boolean,default:!1},autoSize:{type:Boolean,default:!1}},style:a.default,provide:function(){return{$parentInstance:this}},beforeMount:function(){a.default.loadStyle()}},emits:["update:numToleratedItems","scroll","scroll-index-change","lazy-load"],data:function(){return{first:this.isBoth()?{rows:0,cols:0}:0,last:this.isBoth()?{rows:0,cols:0}:0,page: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:[],spacerStyle:{},contentStyle:{}}},element:null,content:null,lastScrollPos:null,scrollTimeout:null,resizeTimeout:null,defaultWidth:0,defaultHeight:0,defaultContentWidth:0,defaultContentHeight:0,isRangeChanged:!1,lazyLoadState:{},resizeListener:null,initialized:!1,watch:{numToleratedItems:function(t){this.d_numToleratedItems=t},loading:function(t,e){this.lazy&&t!==e&&t!==this.d_loading&&(this.d_loading=t)},items:function(t,e){e&&e.length===(t||[]).length||(this.init(),this.calculateAutoSize())},itemSize:function(){this.init(),this.calculateAutoSize()},orientation:function(){this.lastScrollPos=this.isBoth()?{top:0,left:0}:0},scrollHeight:function(){this.init(),this.calculateAutoSize()},scrollWidth:function(){this.init(),this.calculateAutoSize()}},mounted:function(){this.viewInit(),this.lastScrollPos=this.isBoth()?{top:0,left:0}:0,this.lazyLoadState=this.lazyLoadState||{}},updated:function(){!this.initialized&&this.viewInit()},unmounted:function(){this.unbindResizeListener(),this.initialized=!1},methods:{viewInit:function(){e.DomHandler.isVisible(this.element)&&(this.setContentEl(this.content),this.init(),this.bindResizeListener(),this.defaultWidth=e.DomHandler.getWidth(this.element),this.defaultHeight=e.DomHandler.getHeight(this.element),this.defaultContentWidth=e.DomHandler.getWidth(this.content),this.defaultContentHeight=e.DomHandler.getHeight(this.content),this.initialized=!0)},init:function(){this.disabled||(this.setSize(),this.calculateOptions(),this.setSpacerSize())},isVertical:function(){return"vertical"===this.orientation},isHorizontal:function(){return"horizontal"===this.orientation},isBoth:function(){return"both"===this.orientation},scrollTo:function(t){this.lastScrollPos=this.both?{top:0,left:0}:0,this.element&&this.element.scrollTo(t)},scrollToIndex:function(t){var e=this,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"auto",s=this.isBoth(),o=this.isHorizontal(),n=this.first,l=this.calculateNumItems().numToleratedItems,r=this.getContentPosition(),a=this.itemSize,h=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;return t<=(arguments.length>1?arguments[1]:void 0)?0:t},c=function(t,e,i){return t*e+i},d=function(){return e.scrollTo({left:arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,top:arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,behavior:i})},u=s?{rows:0,cols:0}:0,m=!1;s?(d(c((u={rows:h(t[0],l[0]),cols:h(t[1],l[1])}).cols,a[1],r.left),c(u.rows,a[0],r.top)),m=u.rows!==n.rows||u.cols!==n.cols):(u=h(t,l),o?d(c(u,a,r.left),0):d(0,c(u,a,r.top)),m=u!==n),this.isRangeChanged=m,this.first=u},scrollInView:function(t,e){var i=this,s=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"auto";if(e){var o=this.isBoth(),n=this.isHorizontal(),l=this.getRenderedRange(),r=l.first,a=l.viewport,h=function(){return i.scrollTo({left:arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,top:arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,behavior:s})},c="to-end"===e;if("to-start"===e){if(o)a.first.rows-r.rows>t[0]?h(a.first.cols*this.itemSize[1],(a.first.rows-1)*this.itemSize[0]):a.first.cols-r.cols>t[1]&&h((a.first.cols-1)*this.itemSize[1],a.first.rows*this.itemSize[0]);else if(a.first-r>t){var d=(a.first-1)*this.itemSize;n?h(d,0):h(0,d)}}else if(c)if(o)a.last.rows-r.rows<=t[0]+1?h(a.first.cols*this.itemSize[1],(a.first.rows+1)*this.itemSize[0]):a.last.cols-r.cols<=t[1]+1&&h((a.first.cols+1)*this.itemSize[1],a.first.rows*this.itemSize[0]);else if(a.last-r<=t+1){var u=(a.first+1)*this.itemSize;n?h(u,0):h(0,u)}}else this.scrollToIndex(t,s)},getRenderedRange:function(){var t=function(t,e){return Math.floor(t/(e||t))},e=this.first,i=0;if(this.element){var s=this.isBoth(),o=this.isHorizontal(),n=this.element,l=n.scrollTop,r=n.scrollLeft;if(s)i={rows:(e={rows:t(l,this.itemSize[0]),cols:t(r,this.itemSize[1])}).rows+this.numItemsInViewport.rows,cols:e.cols+this.numItemsInViewport.cols};else i=(e=t(o?r:l,this.itemSize))+this.numItemsInViewport}return{first:this.first,last:this.last,viewport:{first:e,last:i}}},calculateNumItems:function(){var t=this.isBoth(),e=this.isHorizontal(),i=this.itemSize,s=this.getContentPosition(),o=this.element?this.element.offsetWidth-s.left:0,n=this.element?this.element.offsetHeight-s.top:0,l=function(t,e){return Math.ceil(t/(e||t))},r=function(t){return Math.ceil(t/2)},a=t?{rows:l(n,i[0]),cols:l(o,i[1])}:l(e?o:n,i);return{numItemsInViewport:a,numToleratedItems:this.d_numToleratedItems||(t?[r(a.rows),r(a.cols)]:r(a))}},calculateOptions:function(){var t=this,e=this.isBoth(),i=this.first,s=this.calculateNumItems(),o=s.numItemsInViewport,n=s.numToleratedItems,l=function(e,i,s){return t.getLast(e+i+(e<s?2:3)*s,arguments.length>3&&void 0!==arguments[3]&&arguments[3])},r=e?{rows:l(i.rows,o.rows,n[0]),cols:l(i.cols,o.cols,n[1],!0)}:l(i,o,n);this.last=r,this.numItemsInViewport=o,this.d_numToleratedItems=n,this.$emit("update:numToleratedItems",this.d_numToleratedItems),this.showLoader&&(this.loaderArr=e?Array.from({length:o.rows}).map((function(){return Array.from({length:o.cols})})):Array.from({length:o})),this.lazy&&Promise.resolve().then((function(){t.lazyLoadState={first:t.step?e?{rows:0,cols:i.cols}:0:i,last:Math.min(t.step?t.step:r,t.items.length)},t.$emit("lazy-load",t.lazyLoadState)}))},calculateAutoSize:function(){var t=this;this.autoSize&&!this.d_loading&&Promise.resolve().then((function(){if(t.content){var i=t.isBoth(),s=t.isHorizontal(),o=t.isVertical();t.content.style.minHeight=t.content.style.minWidth="auto",t.content.style.position="relative",t.element.style.contain="none";var n=[e.DomHandler.getWidth(t.content),e.DomHandler.getHeight(t.content)],l=n[1];n[0]!==t.defaultContentWidth&&(t.element.style.width=""),l!==t.defaultContentHeight&&(t.element.style.height="");var r=[e.DomHandler.getWidth(t.element),e.DomHandler.getHeight(t.element)],a=r[0],h=r[1];(i||s)&&(t.element.style.width=a<t.defaultWidth?a+"px":t.scrollWidth||t.defaultWidth+"px"),(i||o)&&(t.element.style.height=h<t.defaultHeight?h+"px":t.scrollHeight||t.defaultHeight+"px"),t.content.style.minHeight=t.content.style.minWidth="",t.content.style.position="",t.element.style.contain=""}}))},getLast:function(){return this.items?Math.min((arguments.length>1?arguments[1]:void 0)?(this.columns||this.items[0]).length:this.items.length,arguments.length>0&&void 0!==arguments[0]?arguments[0]:0):0},getContentPosition:function(){if(this.content){var t=getComputedStyle(this.content),e=parseFloat(t.paddingLeft)+Math.max(parseFloat(t.left)||0,0),i=parseFloat(t.paddingRight)+Math.max(parseFloat(t.right)||0,0),s=parseFloat(t.paddingTop)+Math.max(parseFloat(t.top)||0,0),o=parseFloat(t.paddingBottom)+Math.max(parseFloat(t.bottom)||0,0);return{left:e,right:i,top:s,bottom:o,x:e+i,y:s+o}}return{left:0,right:0,top:0,bottom:0,x:0,y:0}},setSize:function(){var t=this;if(this.element){var e=this.isBoth(),i=this.isHorizontal(),s=this.element.parentElement,o=this.scrollWidth||"".concat(this.element.offsetWidth||s.offsetWidth,"px"),n=this.scrollHeight||"".concat(this.element.offsetHeight||s.offsetHeight,"px"),l=function(e,i){return t.element.style[e]=i};e||i?(l("height",n),l("width",o)):l("height",n)}},setSpacerSize:function(){var t=this,e=this.items;if(e){var i=this.isBoth(),s=this.isHorizontal(),o=this.getContentPosition(),n=function(e,i,s){var o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0;return t.spacerStyle=d(d({},t.spacerStyle),u({},"".concat(e),(i||[]).length*s+o+"px"))};i?(n("height",e,this.itemSize[0],o.y),n("width",this.columns||e[1],this.itemSize[1],o.x)):s?n("width",this.columns||e,this.itemSize,o.x):n("height",e,this.itemSize,o.y)}},setContentPosition:function(t){var e=this;if(this.content&&!this.appendOnly){var i=this.isBoth(),s=this.isHorizontal(),o=t?t.first:this.first,n=function(t,e){return t*e},l=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return e.contentStyle=d(d({},e.contentStyle),{transform:"translate3d(".concat(t,"px, ").concat(i,"px, 0)")})};if(i)l(n(o.cols,this.itemSize[1]),n(o.rows,this.itemSize[0]));else{var r=n(o,this.itemSize);s?l(r,0):l(0,r)}}},onScrollPositionChange:function(t){var e=this,i=t.target,s=this.isBoth(),o=this.isHorizontal(),n=this.getContentPosition(),l=function(t,e){return t?t>e?t-e:t:0},r=function(t,e){return Math.floor(t/(e||t))},a=function(t,e,i,s,o,n){return t<=o?o:n?i-s-o:e+o-1},h=function(t,e,i,s,o,n,l){return t<=n?0:Math.max(0,l?t<e?i:t-n:t>e?i:t-2*n)},c=function(t,i,s,o,n,l){var r=i+o+2*n;return t>=n&&(r+=n+1),e.getLast(r,l)},d=l(i.scrollTop,n.top),u=l(i.scrollLeft,n.left),m=s?{rows:0,cols:0}:0,f=this.last,p=!1,g=this.lastScrollPos;if(s){var y=this.lastScrollPos.top<=d,v=this.lastScrollPos.left<=u;if(!this.appendOnly||this.appendOnly&&(y||v)){var S={rows:r(d,this.itemSize[0]),cols:r(u,this.itemSize[1])},w={rows:a(S.rows,this.first.rows,this.last.rows,this.numItemsInViewport.rows,this.d_numToleratedItems[0],y),cols:a(S.cols,this.first.cols,this.last.cols,this.numItemsInViewport.cols,this.d_numToleratedItems[1],v)};m={rows:h(S.rows,w.rows,this.first.rows,0,0,this.d_numToleratedItems[0],y),cols:h(S.cols,w.cols,this.first.cols,0,0,this.d_numToleratedItems[1],v)},f={rows:c(S.rows,m.rows,0,this.numItemsInViewport.rows,this.d_numToleratedItems[0]),cols:c(S.cols,m.cols,0,this.numItemsInViewport.cols,this.d_numToleratedItems[1],!0)},p=m.rows!==this.first.rows||f.rows!==this.last.rows||m.cols!==this.first.cols||f.cols!==this.last.cols||this.isRangeChanged,g={top:d,left:u}}}else{var z=o?u:d,I=this.lastScrollPos<=z;if(!this.appendOnly||this.appendOnly&&I){var b=r(z,this.itemSize);f=c(b,m=h(b,a(b,this.first,this.last,this.numItemsInViewport,this.d_numToleratedItems,I),this.first,0,0,this.d_numToleratedItems,I),0,this.numItemsInViewport,this.d_numToleratedItems),p=m!==this.first||f!==this.last||this.isRangeChanged,g=z}}return{first:m,last:f,isRangeChanged:p,scrollPos:g}},onScrollChange:function(t){var e=this.onScrollPositionChange(t),i=e.first,s=e.last,o=e.scrollPos;if(e.isRangeChanged){var n={first:i,last:s};if(this.setContentPosition(n),this.first=i,this.last=s,this.lastScrollPos=o,this.$emit("scroll-index-change",n),this.lazy&&this.isPageChanged(i)){var l={first:this.step?Math.min(this.getPageByFirst(i)*this.step,this.items.length-this.step):i,last:Math.min(this.step?(this.getPageByFirst(i)+1)*this.step:s,this.items.length)};(this.lazyLoadState.first!==l.first||this.lazyLoadState.last!==l.last)&&this.$emit("lazy-load",l),this.lazyLoadState=l}}},onScroll:function(t){var e=this;if(this.$emit("scroll",t),this.delay){if(this.scrollTimeout&&clearTimeout(this.scrollTimeout),this.isPageChanged()){if(!this.d_loading&&this.showLoader)(this.onScrollPositionChange(t).isRangeChanged||!!this.step&&this.isPageChanged())&&(this.d_loading=!0);this.scrollTimeout=setTimeout((function(){e.onScrollChange(t),!e.d_loading||!e.showLoader||e.lazy&&void 0!==e.loading||(e.d_loading=!1,e.page=e.getPageByFirst())}),this.delay)}}else this.onScrollChange(t)},onResize:function(){var t=this;this.resizeTimeout&&clearTimeout(this.resizeTimeout),this.resizeTimeout=setTimeout((function(){if(e.DomHandler.isVisible(t.element)){var i=t.isBoth(),s=t.isVertical(),o=t.isHorizontal(),n=[e.DomHandler.getWidth(t.element),e.DomHandler.getHeight(t.element)],l=n[0],r=n[1],a=l!==t.defaultWidth,h=r!==t.defaultHeight;(i?a||h:o?a:!!s&&h)&&(t.d_numToleratedItems=t.numToleratedItems,t.defaultWidth=l,t.defaultHeight=r,t.defaultContentWidth=e.DomHandler.getWidth(t.content),t.defaultContentHeight=e.DomHandler.getHeight(t.content),t.init())}}),this.resizeDelay)},bindResizeListener:function(){this.resizeListener||(this.resizeListener=this.onResize.bind(this),window.addEventListener("resize",this.resizeListener),window.addEventListener("orientationchange",this.resizeListener))},unbindResizeListener:function(){this.resizeListener&&(window.removeEventListener("resize",this.resizeListener),window.removeEventListener("orientationchange",this.resizeListener),this.resizeListener=null)},getOptions:function(t){var e=(this.items||[]).length,i=this.isBoth()?this.first.rows+t:this.first+t;return{index:i,count:e,first:0===i,last:i===e-1,even:i%2==0,odd:i%2!=0}},getLoaderOptions:function(t,e){var i=this.loaderArr.length;return d({index:t,count:i,first:0===t,last:t===i-1,even:t%2==0,odd:t%2!=0},e)},getPageByFirst:function(t){return Math.floor(((null!=t?t:this.first)+4*this.d_numToleratedItems)/(this.step||1))},isPageChanged:function(t){return!this.step||this.page!==this.getPageByFirst(null!=t?t:this.first)},setContentEl:function(t){this.content=t||this.content||e.DomHandler.findSingle(this.element,'[data-pc-section="content"]')},elementRef:function(t){this.element=t},contentRef:function(t){this.content=t}},computed:{containerClass:function(){return["p-virtualscroller",this.class,{"p-virtualscroller-inline":this.inline,"p-virtualscroller-both p-both-scroll":this.isBoth(),"p-virtualscroller-horizontal p-horizontal-scroll":this.isHorizontal()}]},contentClass:function(){return["p-virtualscroller-content",{"p-virtualscroller-loading":this.d_loading}]},loaderClass:function(){return["p-virtualscroller-loader",{"p-component-overlay":!this.$slots.loader}]},loadedItems:function(){var t=this;return this.items&&!this.d_loading?this.isBoth()?this.items.slice(this.appendOnly?0:this.first.rows,this.last.rows).map((function(e){return t.columns?e:e.slice(t.appendOnly?0:t.first.cols,t.last.cols)})):this.isHorizontal()&&this.columns?this.items:this.items.slice(this.appendOnly?0:this.first,this.last):[]},loadedRows:function(){return this.d_loading?this.loaderDisabled?this.loaderArr:[]:this.loadedItems},loadedColumns:function(){if(this.columns){var t=this.isBoth(),e=this.isHorizontal();if(t||e)return this.d_loading&&this.loaderDisabled?t?this.loaderArr[0]:this.loaderArr:this.columns.slice(t?this.first.cols:this.first,t?this.last.cols:this.last)}return this.columns}},components:{SpinnerIcon:l.default}},p=["tabindex"];f.render=function(t,e,i,s,n,l){var r=o.resolveComponent("SpinnerIcon");return t.disabled?(o.openBlock(),o.createElementBlock(o.Fragment,{key:1},[o.renderSlot(t.$slots,"default"),o.renderSlot(t.$slots,"content",{items:t.items,rows:t.items,columns:l.loadedColumns})],64)):(o.openBlock(),o.createElementBlock("div",o.mergeProps({key:0,ref:l.elementRef,class:l.containerClass,tabindex:t.tabindex,style:t.style,onScroll:e[0]||(e[0]=function(){return l.onScroll&&l.onScroll.apply(l,arguments)})},t.ptm("root"),{"data-pc-name":"virtualscroller"}),[o.renderSlot(t.$slots,"content",{styleClass:l.contentClass,items:l.loadedItems,getItemOptions:l.getOptions,loading:n.d_loading,getLoaderOptions:l.getLoaderOptions,itemSize:t.itemSize,rows:l.loadedRows,columns:l.loadedColumns,contentRef:l.contentRef,spacerStyle:n.spacerStyle,contentStyle:n.contentStyle,vertical:l.isVertical(),horizontal:l.isHorizontal(),both:l.isBoth()},(function(){return[o.createElementVNode("div",o.mergeProps({ref:l.contentRef,class:l.contentClass,style:n.contentStyle},t.ptm("content")),[(o.openBlock(!0),o.createElementBlock(o.Fragment,null,o.renderList(l.loadedItems,(function(e,i){return o.renderSlot(t.$slots,"item",{key:i,item:e,options:l.getOptions(i)})})),128))],16)]})),t.showSpacer?(o.openBlock(),o.createElementBlock("div",o.mergeProps({key:0,class:"p-virtualscroller-spacer",style:n.spacerStyle},t.ptm("spacer")),null,16)):o.createCommentVNode("",!0),!t.loaderDisabled&&t.showLoader&&n.d_loading?(o.openBlock(),o.createElementBlock("div",o.mergeProps({key:1,class:l.loaderClass},t.ptm("loader")),[t.$slots&&t.$slots.loader?(o.openBlock(!0),o.createElementBlock(o.Fragment,{key:0},o.renderList(n.loaderArr,(function(e,i){return o.renderSlot(t.$slots,"loader",{key:i,options:l.getLoaderOptions(i,l.isBoth()&&{numCols:t.d_numItemsInViewport.cols})})})),128)):o.createCommentVNode("",!0),o.renderSlot(t.$slots,"loadingicon",{},(function(){return[o.createVNode(r,o.mergeProps({spin:"",class:"p-virtualscroller-loading-icon"},t.ptm("loadingIcon")),null,16)]}))],16)):o.createCommentVNode("",!0)],16,p))},module.exports=f;