text: import t,{Component as e}from"react";import{ObjectUtils as o,classNames as s}from"primereact/utils";function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){for(var o=0;o<e.length;o++){var s=e[o];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(t,s.key,s)}}function n(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function l(t,e){return l=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},l(t,e)}function a(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&l(t,e)}function c(t){return c="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},c(t)}function h(t,e){if(e&&("object"===c(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return n(t)}function p(t){return p=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)},p(t)}function u(t,e,o){return e in t?Object.defineProperty(t,e,{value:o,enumerable:!0,configurable:!0,writable:!0}):t[e]=o,t}function f(t,e){var o=Object.keys(t);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(t);e&&(s=s.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),o.push.apply(o,s)}return o}function d(t){for(var e=1;e<arguments.length;e++){var o=null!=arguments[e]?arguments[e]:{};e%2?f(Object(o),!0).forEach((function(e){u(t,e,o[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(o)):f(Object(o)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(o,e))}))}return t}function m(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}var v=function(l){a(w,e);var c,u,f,v,g,y=(c=w,u=m(),function(){var t,e=p(c);if(u){var o=p(this).constructor;t=Reflect.construct(e,arguments,o)}else t=e.apply(this,arguments);return h(this,t)});function w(t){var e;r(this,w);var o=(e=y.call(this,t)).isBoth();return e.state={first:o?{rows:0,cols:0}:0,last:o?{rows:0,cols:0}:0,numItemsInViewport:o?{rows:0,cols:0}:0,numToleratedItems:t.numToleratedItems,loading:t.loading,loaderArr:[]},e.onScroll=e.onScroll.bind(n(e)),e.lastScrollPos=o?{top:0,left:0}:0,e}return f=w,v=[{key:"scrollTo",value:function(t){this.el&&this.el.scrollTo(t)}},{key:"scrollToIndex",value:function(t){var e=this,o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"auto",s=this.isBoth(),r=this.isHorizontal(),i=this.state.first,n=this.calculateNumItems(),l=n.numToleratedItems,a=this.props.itemSize,c=this.getContentPosition(),h=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,e=arguments.length>1?arguments[1]:void 0;return t<=e?0:t},p=function(t,e,o){return t*e+o},u=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,s=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return e.scrollTo({left:t,top:s,behavior:o})};if(s){var f={rows:h(t[0],l[0]),cols:h(t[1],l[1])};f.rows===i.rows&&f.cols===i.cols||(u(p(f.cols,a[1],c.left),p(f.rows,a[0],c.top)),this.setState({first:f}))}else{var d=h(t,l);d!==i&&(r?u(p(d,a,c.left),0):u(0,p(d,a,c.top)),this.setState({first:d}))}}},{key:"scrollInView",value:function(t,e){var o=this,s=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"auto";if(e){var r=this.isBoth(),i=this.isHorizontal(),n=this.getRenderedRange(),l=n.first,a=n.viewport,c=this.props.itemSize,h=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return o.scrollTo({left:t,top:e,behavior:s})},p="to-start"===e,u="to-end"===e;if(p){if(r)a.first.rows-l.rows>t[0]?h(a.first.cols*c,(a.first.rows-1)*c):a.first.cols-l.cols>t[1]&&h((a.first.cols-1)*c,a.first.rows*c);else if(a.first-l>t){var f=(a.first-1)*c;i?h(f,0):h(0,f)}}else if(u)if(r)a.last.rows-l.rows<=t[0]+1?h(a.first.cols*c,(a.first.rows+1)*c):a.last.cols-l.cols<=t[1]+1&&h((a.first.cols+1)*c,a.first.rows*c);else if(a.last-l<=t+1){var d=(a.first+1)*c;i?h(d,0):h(0,d)}}else this.scrollToIndex(t,s)}},{key:"getRows",value:function(){return this.state.loading?this.props.loaderDisabled?this.state.loaderArr:[]:this.loadedItems()}},{key:"getColumns",value:function(){if(this.props.columns){var t=this.isBoth(),e=this.isHorizontal();if(t||e)return this.state.loading&&this.props.loaderDisabled?t?this.state.loaderArr[0]:this.state.loaderArr:this.props.columns.slice(t?this.state.first.cols:this.state.first,t?this.state.last.cols:this.state.last)}return this.props.columns}},{key:"getRenderedRange",value:function(){var t=this.isBoth(),e=this.isHorizontal(),o=this.state,s=o.first,r=o.last,i=o.numItemsInViewport,n=this.props.itemSize,l=function(t,e){return Math.floor(t/(e||t))},a=s,c=0;if(this.el){var h=this.el.scrollTop,p=this.el.scrollLeft;c=t?{rows:(a={rows:l(h,n[0]),cols:l(p,n[1])}).rows+i.rows,cols:a.cols+i.cols}:(a=l(e?p:h,n))+i}return{first:s,last:r,viewport:{first:a,last:c}}}},{key:"isVertical",value:function(){return"vertical"===this.props.orientation}},{key:"isHorizontal",value:function(){return"horizontal"===this.props.orientation}},{key:"isBoth",value:function(){return"both"===this.props.orientation}},{key:"calculateNumItems",value:function(){var t=this.isBoth(),e=this.isHorizontal(),o=this.props.itemSize,s=this.getContentPosition(),r=this.el?this.el.offsetWidth-s.left:0,i=this.el?this.el.offsetHeight-s.top:0,n=function(t,e){return Math.ceil(t/(e||t))},l=function(t){return Math.ceil(t/2)},a=t?{rows:n(i,o[0]),cols:n(r,o[1])}:n(e?r:i,o);return{numItemsInViewport:a,numToleratedItems:this.state.numToleratedItems||(t?[l(a.rows),l(a.cols)]:l(a))}}},{key:"calculateOptions",value:function(){var t=this,e=this.isBoth(),o=this.state.first,s=this.calculateNumItems(),r=s.numItemsInViewport,i=s.numToleratedItems,n=function(e,o,s,r){return t.getLast(e+o+(e<s?2:3)*s,r)},l=e?{rows:n(o.rows,r.rows,i[0]),cols:n(o.cols,r.cols,i[1],!0)}:n(o,r,i),a={numItemsInViewport:r,last:l,numToleratedItems:i};this.props.showLoader&&(a.loaderArr=e?Array.from({length:r.rows}).map((function(){return Array.from({length:r.cols})})):Array.from({length:r})),this.setState(a,(function(){t.props.lazy&&t.props.onLazyLoad&&t.props.onLazyLoad({first:t.state.first,last:t.state.last})}))}},{key:"getLast",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,e=arguments.length>1?arguments[1]:void 0;return this.props.items?Math.min(e?(this.props.columns||this.props.items[0]).length:this.props.items.length,t):0}},{key:"getContentPosition",value:function(){if(this.content){var t=getComputedStyle(this.content),e=parseInt(t.paddingLeft,10)+Math.max(parseInt(t.left,10),0),o=parseInt(t.paddingRight,10)+Math.max(parseInt(t.right,10),0),s=parseInt(t.paddingTop,10)+Math.max(parseInt(t.top,10),0),r=parseInt(t.paddingBottom,10)+Math.max(parseInt(t.bottom,10),0);return{left:e,right:o,top:s,bottom:r,x:e+o,y:s+r}}return{left:0,right:0,top:0,bottom:0,x:0,y:0}}},{key:"setSize",value:function(){var t=this;if(this.el){var e=this.isBoth(),o=this.isHorizontal(),s=this.el.parentElement,r=this.props.scrollWidth||"".concat(this.el.offsetWidth||s.offsetWidth,"px"),i=this.props.scrollHeight||"".concat(this.el.offsetHeight||s.offsetHeight,"px"),n=function(e,o){return t.el.style[e]=o};e||o?(n("height",i),n("width",r)):n("height",i)}}},{key:"setSpacerSize",value:function(){var t=this,e=this.props.items;if(this.spacer&&e){var o=this.isBoth(),s=this.isHorizontal(),r=this.props.itemSize,i=this.getContentPosition(),n=function(e,o,s){var r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0;return t.spacer.style[e]=(o||[]).length*s+r+"px"};o?(n("height",e,r[0],i.y),n("width",this.props.columns||e[1],r[1],i.x)):s?n("width",this.props.columns||e,r,i.x):n("height",e,r,i.y)}}},{key:"setContentPosition",value:function(t){var e=this;if(this.content){var o=this.isBoth(),s=this.isHorizontal(),r=t?t.first:this.state.first,i=this.props.itemSize,n=function(t,e){return t*e},l=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;e.sticky&&(e.sticky.style.top="-".concat(o,"px")),e.content.style.transform="translate3d(".concat(t,"px, ").concat(o,"px, 0)")};if(o)l(n(r.cols,i[1]),n(r.rows,i[0]));else{var a=n(r,i);s?l(a,0):l(0,a)}}}},{key:"onScrollPositionChange",value:function(t){var e=this,o=t.target,s=this.isBoth(),r=this.isHorizontal(),i=this.state,n=i.first,l=i.last,a=i.numItemsInViewport,c=i.numToleratedItems,h=this.props.itemSize,p=this.getContentPosition(),u=function(t,e){return t?t>e?t-e:t:0},f=function(t,e){return Math.floor(t/(e||t))},d=function(t,e,o,s,r,i){return t<=r?r:i?o-s-r:e+r-1},m=function(t,e,o,s,r,i,n){return t<=i?0:Math.max(0,n?t<e?o:t-i:t>e?o:t-2*i)},v=function(t,o,s,r,i,n){var l=o+r+2*i;return t>=i&&(l+=i+1),e.getLast(l,n)},g=u(o.scrollTop,p.top),y=u(o.scrollLeft,p.left),w=0,S=l,b=!1;if(s){var I=this.lastScrollPos.top<=g,k=this.lastScrollPos.left<=y,z={rows:f(g,h[0]),cols:f(y,h[1])},O={rows:d(z.rows,n.rows,l.rows,a.rows,c[0],I),cols:d(z.cols,n.cols,l.cols,a.cols,c[1],k)};w={rows:m(z.rows,O.rows,n.rows,0,0,c[0],I),cols:m(z.cols,O.cols,n.cols,0,0,c[1],k)},S={rows:v(z.rows,w.rows,0,a.rows,c[0]),cols:v(z.cols,w.cols,0,a.cols,c[1],!0)},b=w.rows!==n.rows&&S.rows!==l.rows||w.cols!==n.cols&&S.cols!==l.cols,this.lastScrollPos={top:g,left:y}}else{var T=r?y:g,P=this.lastScrollPos<=T,C=f(T,h);S=v(C,w=m(C,d(C,n,l,a,c,P),n,0,0,c,P),0,a,c),b=w!==n&&S!==l,this.lastScrollPos=T}return{first:w,last:S,isRangeChanged:b}}},{key:"onScrollChange",value:function(t){var e=this,o=this.onScrollPositionChange(t);if(o.isRangeChanged){var s={first:o.first,last:o.last};this.setContentPosition(s),this.setState(s,(function(){e.props.onScrollIndexChange&&e.props.onScrollIndexChange(s),e.props.lazy&&e.props.onLazyLoad&&e.props.onLazyLoad(s)}))}}},{key:"onScroll",value:function(t){var e=this;this.props.onScroll&&this.props.onScroll(t),this.props.delay?(this.scrollTimeout&&clearTimeout(this.scrollTimeout),!this.state.loading&&this.props.showLoader&&this.onScrollPositionChange(t).isRangeChanged&&this.setState({loading:!0}),this.scrollTimeout=setTimeout((function(){e.onScrollChange(t),e.state.loading&&e.props.showLoader&&!e.props.lazy&&e.setState({loading:!1})}),this.props.delay)):this.onScrollChange(t)}},{key:"getOptions",value:function(t){var e=this.state.first,o=(this.props.items||[]).length,s=this.isBoth()?e.rows+t:e+t;return{index:s,count:o,first:0===s,last:s===o-1,even:s%2==0,odd:s%2!=0,props:this.props}}},{key:"loaderOptions",value:function(t,e){var o=this.state.loaderArr.length;return d({index:t,count:o,first:0===t,last:t===o-1,even:t%2==0,odd:t%2!=0,props:this.props},e)}},{key:"loadedItems",value:function(){var t=this,e=this.props.items;if(e&&!this.state.loading){var o=this.isBoth(),s=this.isHorizontal(),r=this.state,i=r.first,n=r.last;return o?e.slice(i.rows,n.rows).map((function(e){return t.props.columns?e:e.slice(i.cols,n.cols)})):s&&this.props.columns?e:e.slice(i,n)}return[]}},{key:"isPropChanged",value:function(t){var e=this;return["itemSize","scrollHeight"].some((function(s){return!o.equals(t[s],e.props[s])}))}},{key:"init",value:function(){this.setSize(),this.calculateOptions(),this.setSpacerSize()}},{key:"componentDidMount",value:function(){this.init()}},{key:"componentDidUpdate",value:function(t,e){t.items&&t.items.length===(this.props.items||[]).length&&!this.isPropChanged(t)||this.init(),this.props.lazy&&t.loading!==this.props.loading&&this.state.loading!==this.props.loading&&this.setState({loading:this.props.loading}),t.orientation!==this.props.orientation&&(this.lastScrollPos=this.isBoth()?{top:0,left:0}:0)}},{key:"renderLoaderItem",value:function(e){var s=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=this.loaderOptions(e,s),i=o.getJSXElement(this.props.loadingTemplate,r);return t.createElement(t.Fragment,{key:e},i)}},{key:"renderLoader",value:function(){var e=this;if(!this.props.loaderDisabled&&this.props.showLoader&&this.state.loading){var o=s("p-virtualscroller-loader",{"p-component-overlay":!this.props.loadingTemplate}),r=t.createElement("i",{className:"p-virtualscroller-loading-icon pi pi-spinner pi-spin"});if(this.props.loadingTemplate){var i=this.isBoth(),n=this.state.numItemsInViewport;r=this.state.loaderArr.map((function(t,o){return e.renderLoaderItem(o,i&&{numCols:n.cols})}))}return t.createElement("div",{className:o},r)}return null}},{key:"renderSpacer",value:function(){var e=this;return this.props.showSpacer?t.createElement("div",{ref:function(t){return e.spacer=t},className:"p-virtualscroller-spacer"}):null}},{key:"renderItem",value:function(e,s){var r=this.getOptions(s),i=o.getJSXElement(this.props.itemTemplate,e,r);return t.createElement(t.Fragment,{key:r.index},i)}},{key:"renderItems",value:function(t){var e=this;return t.map((function(t,o){return e.renderItem(t,o)}))}},{key:"renderContent",value:function(){var e=this,r=this.loadedItems(),i=this.renderItems(r),n=s("p-virtualscroller-content",{"p-virtualscroller-loading":this.state.loading}),l=t.createElement("div",{className:n,ref:function(t){return e.content=t}},i);if(this.props.contentTemplate){var a={className:n,contentRef:function(t){return e.content=t},spacerRef:function(t){return e.spacer=t},stickyRef:function(t){return e.sticky=t},items:r,getItemOptions:function(t){return e.getOptions(t)},children:i,element:l,props:this.props,loading:this.state.loading,getLoaderOptions:function(t,o){return e.loaderOptions(t,o)},loadingTemplate:this.props.loadingTemplate,itemSize:this.props.itemSize,rows:this.getRows(),columns:this.getColumns(),vertical:this.isVertical(),horizontal:this.isHorizontal(),both:this.isBoth()};return o.getJSXElement(this.props.contentTemplate,a)}return l}},{key:"render",value:function(){var e=this;if(this.props.disabled){var r=o.getJSXElement(this.props.contentTemplate,{items:this.props.items,rows:this.props.items,columns:this.props.columns});return t.createElement(t.Fragment,null,this.props.children,r)}var i=this.isBoth(),n=this.isHorizontal(),l=s("p-virtualscroller",{"p-both-scroll":i,"p-horizontal-scroll":n},this.props.className),a=this.renderLoader(),c=this.renderContent(),h=this.renderSpacer();return t.createElement("div",{ref:function(t){return e.el=t},className:l,tabIndex:0,style:this.props.style,onScroll:this.onScroll},c,h,a)}}],v&&i(f.prototype,v),g&&i(f,g),Object.defineProperty(f,"prototype",{writable:!1}),w}();u(v,"defaultProps",{id:null,style:null,className:null,items:null,itemSize:0,scrollHeight:null,scrollWidth:null,orientation:"vertical",numToleratedItems:null,delay:0,lazy:!1,disabled:!1,loaderDisabled:!1,columns:null,loading:!1,showSpacer:!0,showLoader:!1,loadingTemplate:null,itemTemplate:null,contentTemplate:null,onScroll:null,onScrollIndexChange:null,onLazyLoad:null});export{v as VirtualScroller};
  