UNPKG

primereact

Version:

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

2 lines (1 loc) 16 kB
this.primereact=this.primereact||{},this.primereact.virtualscroller=function(e,t,r,n){"use strict";function o(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var l=o(t);function i(){return i=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},i.apply(this,arguments)}function s(e){return s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},s(e)}function c(e,t){if("object"!==s(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!==s(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}function u(e){var t=c(e,"string");return"symbol"===s(t)?t:String(t)}function a(e,t,r){return(t=u(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function f(e){if(Array.isArray(e))return e}function m(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,l,i,s=[],c=!0,u=!1;try{if(l=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;c=!1}else for(;!(c=(n=l.call(r)).done)&&(s.push(n.value),s.length!==t);c=!0);}catch(e){u=!0,o=e}finally{try{if(!c&&null!=r.return&&(i=r.return(),Object(i)!==i))return}finally{if(u)throw o}}return s}}function p(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function d(e,t){if(e){if("string"==typeof e)return p(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?p(e,t):void 0}}function g(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function h(e,t){return f(e)||m(e,t)||d(e,t)||g()}var v={defaultProps:{__TYPE:"VirtualScroller",id:null,style:null,className:null,tabIndex:0,items:null,itemSize:0,scrollHeight:null,scrollWidth:null,orientation:"vertical",step:0,numToleratedItems:null,delay:0,resizeDelay:10,appendOnly:!1,inline:!1,lazy:!1,disabled:!1,loaderDisabled:!1,columns:null,loading:void 0,autoSize:!1,showSpacer:!0,showLoader:!1,loadingTemplate:null,loaderIconTemplate:null,itemTemplate:null,contentTemplate:null,onScroll:null,onScrollIndexChange:null,onLazyLoad:null,children:void 0},getProps:function(e){return n.ObjectUtils.getMergedProps(e,v.defaultProps)},getOtherProps:function(e){return n.ObjectUtils.getDiffProps(e,v.defaultProps)}};function y(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function w(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?y(Object(r),!0).forEach((function(t){a(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):y(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}var b=l.memo(l.forwardRef((function(e,t){var o=v.getProps(e),s=r.usePrevious(e)||{},c="vertical"===o.orientation,u="horizontal"===o.orientation,f="both"===o.orientation,m=h(l.useState(f?{rows:0,cols:0}:0),2),p=m[0],d=m[1],g=h(l.useState(f?{rows:0,cols:0}:0),2),y=g[0],b=g[1],S=h(l.useState(0),2),O=S[0],z=S[1],j=h(l.useState(f?{rows:0,cols:0}:0),2),T=j[0],E=j[1],R=h(l.useState(o.numToleratedItems),2),I=R[0],P=R[1],x=h(l.useState(o.loading||!1),2),H=x[0],D=x[1],L=h(l.useState([]),2),M=L[0],U=L[1],N=l.useRef(null),W=l.useRef(null),F=l.useRef(null),A=l.useRef(null),C=l.useRef(f?{top:0,left:0}:0),V=l.useRef(null),k=l.useRef(null),_=l.useRef({}),J=l.useRef({}),X=l.useRef(null),B=l.useRef(null),Y=l.useRef(null),$=l.useRef(null),q=l.useRef(!1),G=l.useRef(null),K=h(r.useResizeListener({listener:function(e){return ge()},when:!o.disabled}),1)[0],Q=h(r.useEventListener({target:"window",type:"orientationchange",listener:function(e){return ge()},when:!o.disabled}),1)[0],Z=function(){return N},ee=function(e){return Math.floor((e+4*I)/(o.step||1))},te=function(e){return!o.step||O!==ee(e)},re=function(e){C.current=f?{top:0,left:0}:0,N.current&&N.current.scrollTo(e)},ne=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"auto",r=ie().numToleratedItems,n=ue(),l=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0;return e<=(arguments.length>1?arguments[1]:void 0)?0:e},i=function(e,t,r){return e*t+r},s=function(){return re({left:arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,top:arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,behavior:t})},c=f?{rows:0,cols:0}:0,a=!1;f?(s(i((c={rows:l(e[0],r[0]),cols:l(e[1],r[1])}).cols,o.itemSize[1],n.left),i(c.rows,o.itemSize[0],n.top)),a=p.rows!==c.rows||p.cols!==c.cols):(c=l(e,r),u?s(i(c,o.itemSize,n.left),0):s(0,i(c,o.itemSize,n.top)),a=p!==c),q.current=a,d(c)},oe=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"auto";if(t){var n=le(),l=n.first,i=n.viewport,s=function(){return re({left:arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,top:arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,behavior:r})},c="to-end"===t;if("to-start"===t){if(f)i.first.rows-l.rows>e[0]?s(i.first.cols*o.itemSize[1],(i.first.rows-1)*o.itemSize[0]):i.first.cols-l.cols>e[1]&&s((i.first.cols-1)*o.itemSize[1],i.first.rows*o.itemSize[0]);else if(i.first-l>e){var a=(i.first-1)*o.itemSize;u?s(a,0):s(0,a)}}else if(c)if(f)i.last.rows-l.rows<=e[0]+1?s(i.first.cols*o.itemSize[1],(i.first.rows+1)*o.itemSize[0]):i.last.cols-l.cols<=e[1]+1&&s((i.first.cols+1)*o.itemSize[1],i.first.rows*o.itemSize[0]);else if(i.last-l<=e+1){var m=(i.first+1)*o.itemSize;u?s(m,0):s(0,m)}}else ne(e,r)},le=function(){var e=function(e,t){return Math.floor(e/(t||e))},t=p,r=0;if(N.current){var n=N.current,l=n.scrollTop,i=n.scrollLeft;if(f)r={rows:(t={rows:e(l,o.itemSize[0]),cols:e(i,o.itemSize[1])}).rows+T.rows,cols:t.cols+T.cols};else r=(t=e(u?i:l,o.itemSize))+T}return{first:p,last:y,viewport:{first:t,last:r}}},ie=function(){var e=ue(),t=N.current?N.current.offsetWidth-e.left:0,r=N.current?N.current.offsetHeight-e.top:0,n=function(e,t){return Math.ceil(e/(t||e))},l=function(e){return Math.ceil(e/2)},i=f?{rows:n(r,o.itemSize[0]),cols:n(t,o.itemSize[1])}:n(u?t:r,o.itemSize);return{numItemsInViewport:i,numToleratedItems:I||(f?[l(i.rows),l(i.cols)]:l(i))}},se=function(e){o.autoSize&&!e&&Promise.resolve().then((function(){if(W.current){W.current.style.minHeight=W.current.style.minWidth="auto",W.current.style.position="relative",N.current.style.contain="none";var e=[n.DomHandler.getWidth(N.current),n.DomHandler.getHeight(N.current)],t=e[0],r=e[1];(f||u)&&(N.current.style.width=(t<X.current?t:o.scrollWidth||X.current)+"px"),(f||c)&&(N.current.style.height=(r<B.current?r:o.scrollHeight||B.current)+"px"),W.current.style.minHeight=W.current.style.minWidth="",W.current.style.position="",N.current.style.contain=""}}))},ce=function(){return o.items?Math.min((arguments.length>1?arguments[1]:void 0)?(o.columns||o.items[0]).length:o.items.length,arguments.length>0&&void 0!==arguments[0]?arguments[0]:0):0},ue=function(){if(W.current){var e=getComputedStyle(W.current),t=parseFloat(e.paddingLeft)+Math.max(parseFloat(e.left)||0,0),r=parseFloat(e.paddingRight)+Math.max(parseFloat(e.right)||0,0),n=parseFloat(e.paddingTop)+Math.max(parseFloat(e.top)||0,0),o=parseFloat(e.paddingBottom)+Math.max(parseFloat(e.bottom)||0,0);return{left:t,right:r,top:n,bottom:o,x:t+r,y:n+o}}return{left:0,right:0,top:0,bottom:0,x:0,y:0}},ae=function(){if(N.current){var e=N.current.parentElement,t=o.scrollWidth||"".concat(N.current.offsetWidth||e.offsetWidth,"px"),r=o.scrollHeight||"".concat(N.current.offsetHeight||e.offsetHeight,"px"),n=function(e,t){return N.current.style[e]=t};f||u?(n("height",r),n("width",t)):n("height",r)}},fe=function(){var e=o.items;if(e){var t=ue(),r=function(e,t,r){var n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0;return J.current=w(w({},J.current),a({},"".concat(e),(t||[]).length*r+n+"px"))};f?(r("height",e,o.itemSize[0],t.y),r("width",o.columns||e[1],o.itemSize[1],t.x)):u?r("width",o.columns||e,o.itemSize,t.x):r("height",e,o.itemSize,t.y)}},me=function(e){if(W.current&&!o.appendOnly){var t=e?e.first:p,r=function(e,t){return e*t},n=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;A.current&&(A.current.style.top="-".concat(t,"px")),_.current=w(w({},_.current),{transform:"translate3d(".concat(e,"px, ").concat(t,"px, 0)")})};if(f)n(r(t.cols,o.itemSize[1]),r(t.rows,o.itemSize[0]));else{var l=r(t,o.itemSize);u?n(l,0):n(0,l)}}},pe=function(e){var t=e.target,r=ue(),n=function(e,t){return e?e>t?e-t:e:0},l=function(e,t){return Math.floor(e/(t||e))},i=function(e,t,r,n,o,l){return e<=o?o:l?r-n-o:t+o-1},s=function(e,t,r,n,o,l,i){return e<=l?0:Math.max(0,i?e<t?r:e-l:e>t?r:e-2*l)},c=function(e,t,r,n,o,l){var i=t+n+2*o;return e>=o&&(i+=o+1),ce(i,l)},a=n(t.scrollTop,r.top),m=n(t.scrollLeft,r.left),d=f?{rows:0,cols:0}:0,g=y,h=!1,v=C.current;if(f){var w=C.current.top<=a,b=C.current.left<=m;if(!o.appendOnly||o.appendOnly&&(w||b)){var S={rows:l(a,o.itemSize[0]),cols:l(m,o.itemSize[1])},O={rows:i(S.rows,p.rows,y.rows,T.rows,I[0],w),cols:i(S.cols,p.cols,y.cols,T.cols,I[1],b)};d={rows:s(S.rows,O.rows,p.rows,0,0,I[0],w),cols:s(S.cols,O.cols,p.cols,0,0,I[1],b)},g={rows:c(S.rows,d.rows,0,T.rows,I[0]),cols:c(S.cols,d.cols,0,T.cols,I[1],!0)},h=d.rows!==p.rows||g.rows!==y.rows||d.cols!==p.cols||g.cols!==y.cols||q.current,v={top:a,left:m}}}else{var z=u?m:a,j=C.current<=z;if(!o.appendOnly||o.appendOnly&&j){var E=l(z,o.itemSize);g=c(E,d=s(E,i(E,p,y,T,I,j),p,0,0,I,j),0,T,I),h=d!==p||g!==y||q.current,v=z}}return{first:d,last:g,isRangeChanged:h,scrollPos:v}},de=function(e){var t=pe(e),r=t.first,n=t.last,l=t.scrollPos;if(t.isRangeChanged){var i={first:r,last:n};if(me(i),d(r),b(n),C.current=l,o.onScrollIndexChange&&o.onScrollIndexChange(i),o.lazy&&te(r)){var s={first:o.step?Math.min(ee(r)*o.step,o.items.length-o.step):r,last:Math.min(o.step?(ee(r)+1)*o.step:n,o.items.length)};(!G.current||G.current.first!==s.first||G.current.last!==s.last)&&o.onLazyLoad&&o.onLazyLoad(s),G.current=s}}},ge=function(){k.current&&clearTimeout(k.current),k.current=setTimeout((function(){if(N.current){var e=[n.DomHandler.getWidth(N.current),n.DomHandler.getHeight(N.current)],t=e[0],r=e[1],l=t!==X.current,i=r!==B.current;(f?l||i:u?l:!!c&&i)&&(P(o.numToleratedItems),X.current=t,B.current=r,Y.current=n.DomHandler.getWidth(W.current),$.current=n.DomHandler.getHeight(W.current))}}),o.resizeDelay)},he=function(e){var t=(o.items||[]).length,r=f?p.rows+e:p+e;return{index:r,count:t,first:0===r,last:r===t-1,even:r%2==0,odd:r%2!=0,props:o}},ve=function(e,t){var r=M.length;return w({index:e,count:r,first:0===e,last:e===r-1,even:e%2==0,odd:e%2!=0,props:o},t)},ye=function(){var e=o.items;return e&&!H?f?e.slice(o.appendOnly?0:p.rows,y.rows).map((function(e){return o.columns?e:e.slice(o.appendOnly?0:p.cols,y.cols)})):u&&o.columns?e:e.slice(o.appendOnly?0:p,y):[]},we=function(){var e,t,r,n,l;o.disabled||(ae(),e=ie(),t=e.numItemsInViewport,r=e.numToleratedItems,n=function(e,t,r){return ce(e+t+(e<r?2:3)*r,arguments.length>3&&void 0!==arguments[3]&&arguments[3])},l=f?{rows:n(p.rows,t.rows,r[0]),cols:n(p.cols,t.cols,r[1],!0)}:n(p,t,r),E(t),P(r),b(l),o.showLoader&&U(f?Array.from({length:t.rows}).map((function(){return Array.from({length:t.cols})})):Array.from({length:t})),o.lazy&&Promise.resolve().then((function(){G.current={first:o.step?f?{rows:0,cols:p.cols}:0:p,last:Math.min(o.step?o.step:l,o.items.length)},o.onLazyLoad&&o.onLazyLoad(G.current)})),fe())};r.useMountEffect((function(){N.current&&n.DomHandler.isVisible(N.current)&&(W.current=W.current||W.current||n.DomHandler.findSingle(N.current,".p-virtualscroller-content"),we(),K(),Q(),X.current=n.DomHandler.getWidth(N.current),B.current=n.DomHandler.getHeight(N.current),Y.current=n.DomHandler.getWidth(W.current),$.current=n.DomHandler.getHeight(W.current))})),r.useUpdateEffect((function(){we()}),[o.itemSize,o.scrollHeight,o.scrollWidth]),r.useUpdateEffect((function(){o.numToleratedItems!==I&&P(o.numToleratedItems)}),[o.numToleratedItems]),r.useUpdateEffect((function(){o.numToleratedItems===I&&we()}),[I]),r.useUpdateEffect((function(){s.items&&s.items.length===(o.items||[]).length||we();var e=H;o.lazy&&s.loading!==o.loading&&o.loading!==H&&(D(o.loading),e=o.loading),se(e)})),r.useUpdateEffect((function(){C.current=f?{top:0,left:0}:0}),[o.orientation]),l.useImperativeHandle(t,(function(){return{props:o,getElementRef:Z,scrollTo:re,scrollToIndex:ne,scrollInView:oe,getRenderedRange:le}}));var be=function(e){var t=ve(e,arguments.length>1&&void 0!==arguments[1]?arguments[1]:{}),r=n.ObjectUtils.getJSXElement(o.loadingTemplate,t);return l.createElement(l.Fragment,{key:e},r)},Se=function(e,t){var r=he(t),i=n.ObjectUtils.getJSXElement(o.itemTemplate,e,r);return l.createElement(l.Fragment,{key:r.index},i)};if(o.disabled){var Oe=n.ObjectUtils.getJSXElement(o.contentTemplate,{items:o.items,rows:o.items,columns:o.columns});return l.createElement(l.Fragment,null,o.children,Oe)}var ze=v.getOtherProps(o),je=n.classNames("p-virtualscroller",{"p-virtualscroller-inline":o.inline,"p-virtualscroller-both p-both-scroll":f,"p-virtualscroller-horizontal p-horizontal-scroll":u},o.className),Te=function(){if(!o.loaderDisabled&&o.showLoader&&H){var e=n.classNames("p-virtualscroller-loader",{"p-component-overlay":!o.loadingTemplate}),t=l.createElement("i",{className:"p-virtualscroller-loading-icon pi pi-spinner pi-spin"});return o.loadingTemplate?t=M.map((function(e,t){return be(t,f&&{numCols:T.cols})})):o.loaderIconTemplate&&(t=n.ObjectUtils.getJSXElement(o.loaderIconTemplate,{className:"p-virtualscroller-loading-icon",element:t,props:o})),l.createElement("div",{className:e},t)}return null}(),Ee=function(){var e=ye().map(Se),t=n.classNames("p-virtualscroller-content",{"p-virtualscroller-loading":H}),r=l.createElement("div",{ref:W,style:_.current,className:t},e);if(o.contentTemplate){var i={style:_.current,className:t,spacerStyle:J.current,contentRef:function(e){return W.current=n.ObjectUtils.getRefElement(e)},spacerRef:function(e){return F.current=n.ObjectUtils.getRefElement(e)},stickyRef:function(e){return A.current=n.ObjectUtils.getRefElement(e)},items:ye(),getItemOptions:function(e){return he(e)},children:e,element:r,props:o,loading:H,getLoaderOptions:function(e,t){return ve(e,t)},loadingTemplate:o.loadingTemplate,itemSize:o.itemSize,rows:H?o.loaderDisabled?M:[]:ye(),columns:o.columns&&f||u?H&&o.loaderDisabled?f?M[0]:M:o.columns.slice(f?p.cols:p,f?y.cols:y):o.columns,vertical:c,horizontal:u,both:f};return n.ObjectUtils.getJSXElement(o.contentTemplate,i)}return r}(),Re=o.showSpacer?l.createElement("div",{ref:F,style:J.current,className:"p-virtualscroller-spacer"}):null;return l.createElement("div",i({ref:N,className:je,tabIndex:o.tabIndex,style:o.style},ze,{onScroll:function(e){o.onScroll&&o.onScroll(e),o.delay?(V.current&&clearTimeout(V.current),te(p)&&(!H&&o.showLoader&&(pe(e).isRangeChanged||o.step&&te(p))&&D(!0),V.current=setTimeout((function(){de(e),!H||!o.showLoader||o.lazy&&void 0!==o.loading||(D(!1),z(ee(p)))}),o.delay))):de(e)}}),Ee,Re,Te)})));return b.displayName="VirtualScroller",e.VirtualScroller=b,Object.defineProperty(e,"__esModule",{value:!0}),e}({},React,primereact.hooks,primereact.utils);