UNPKG

primeng

Version:

PrimeNG is an open source UI library for Angular 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 PrimeB

1 lines 89 kB
{"version":3,"file":"primeng-scroller.mjs","sources":["../../src/scroller/style/scrollerstyle.ts","../../src/scroller/scroller.ts","../../src/scroller/primeng-scroller.ts"],"sourcesContent":["import { Injectable } from '@angular/core';\nimport { BaseStyle } from 'primeng/base';\n\nconst theme = ({ dt }) => `\n.p-virtualscroller {\n position: relative;\n overflow: auto;\n contain: strict;\n transform: translateZ(0);\n will-change: scroll-position;\n outline: 0 none;\n}\n\n.p-virtualscroller-content {\n position: absolute;\n top: 0;\n left: 0;\n min-height: 100%;\n min-width: 100%;\n will-change: transform;\n}\n\n.p-virtualscroller-spacer {\n position: absolute;\n top: 0;\n left: 0;\n height: 1px;\n width: 1px;\n transform-origin: 0 0;\n pointer-events: none;\n}\n\n.p-virtualscroller-loader {\n position: sticky;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: ${dt('virtualscroller.loader.mask.background')};\n color: ${dt('virtualscroller.loader.mask.color')};\n}\n\n.p-virtualscroller-loader-mask {\n display: flex;\n align-items: center;\n justify-content: center;\n}\n\n.p-virtualscroller-loading-icon {\n font-size: ${dt('virtualscroller.loader.icon.size')};\n width: ${dt('virtualscroller.loader.icon.size')};\n height: ${dt('virtualscroller.loader.icon.size')};\n}\n\n.p-virtualscroller-horizontal > .p-virtualscroller-content {\n display: flex;\n}\n\n.p-virtualscroller-inline .p-virtualscroller-content {\n position: static;\n}\n`;\n\n@Injectable()\nexport class ScrollerStyle extends BaseStyle {\n name = 'virtualscroller';\n\n theme = theme;\n}\n\n/**\n *\n * VirtualScroller is a performant approach to handle huge data efficiently.\n *\n * [Live Demo](https://www.primeng.org/scroller/)\n *\n * @module scrollerstyle\n *\n */\nexport enum ScrollerClasses {\n /**\n * Class name of the root element\n */\n root = 'p-virtualscroller',\n /**\n * Class name of the content element\n */\n content = 'p-virtualscroller-content',\n /**\n * Class name of the spacer element\n */\n spacer = 'p-virtualscroller-spacer',\n /**\n * Class name of the loader element\n */\n loader = 'p-virtualscroller-loader',\n /**\n * Class name of the loading icon element\n */\n loadingIcon = 'p-virtualscroller-loading-icon'\n}\n\nexport interface ScrollerStyle extends BaseStyle {}\n","import { CommonModule, isPlatformBrowser } from '@angular/common';\nimport {\n AfterContentInit,\n AfterViewChecked,\n ChangeDetectionStrategy,\n Component,\n ContentChild,\n ContentChildren,\n ElementRef,\n EventEmitter,\n HostBinding,\n inject,\n Input,\n NgModule,\n NgZone,\n OnDestroy,\n OnInit,\n Output,\n QueryList,\n SimpleChanges,\n TemplateRef,\n ViewChild,\n ViewEncapsulation\n} from '@angular/core';\nimport { findSingle, getHeight, getWidth, isTouchDevice, isVisible } from '@primeuix/utils';\nimport { PrimeTemplate, ScrollerOptions, SharedModule } from 'primeng/api';\nimport { BaseComponent } from 'primeng/basecomponent';\nimport { SpinnerIcon } from 'primeng/icons';\nimport { Nullable, VoidListener } from 'primeng/ts-helpers';\nimport { ScrollerLazyLoadEvent, ScrollerScrollEvent, ScrollerScrollIndexChangeEvent, ScrollerToType } from './scroller.interface';\nimport { ScrollerStyle } from './style/scrollerstyle';\n\n/**\n * Scroller is a performance-approach to handle huge data efficiently.\n * @group Components\n */\n@Component({\n selector: 'p-scroller, p-virtualscroller, p-virtual-scroller, p-virtualScroller',\n imports: [CommonModule, SpinnerIcon, SharedModule],\n standalone: true,\n template: `\n <ng-container *ngIf=\"!_disabled; else disabledContainer\">\n <div\n #element\n [attr.id]=\"_id\"\n [attr.tabindex]=\"tabindex\"\n [ngStyle]=\"_style\"\n [class]=\"_styleClass\"\n [ngClass]=\"{\n 'p-virtualscroller': true,\n 'p-virtualscroller-inline': inline,\n 'p-virtualscroller-both p-both-scroll': both,\n 'p-virtualscroller-horizontal p-horizontal-scroll': horizontal\n }\"\n (scroll)=\"onContainerScroll($event)\"\n [attr.data-pc-name]=\"'scroller'\"\n [attr.data-pc-section]=\"'root'\"\n >\n <ng-container *ngIf=\"contentTemplate || _contentTemplate; else buildInContent\">\n <ng-container *ngTemplateOutlet=\"contentTemplate || _contentTemplate; context: { $implicit: loadedItems, options: getContentOptions() }\"></ng-container>\n </ng-container>\n <ng-template #buildInContent>\n <div #content [class]=\"contentStyleClass\" [ngClass]=\"{ 'p-virtualscroller-content': true, 'p-virtualscroller-loading ': d_loading }\" [style]=\"contentStyle\" [attr.data-pc-section]=\"'content'\">\n <ng-container *ngFor=\"let item of loadedItems; let index = index; trackBy: _trackBy\">\n <ng-container *ngTemplateOutlet=\"itemTemplate || _itemTemplate; context: { $implicit: item, options: getOptions(index) }\"></ng-container>\n </ng-container>\n </div>\n </ng-template>\n <div *ngIf=\"_showSpacer\" class=\"p-virtualscroller-spacer\" [ngStyle]=\"spacerStyle\" [attr.data-pc-section]=\"'spacer'\"></div>\n <div *ngIf=\"!loaderDisabled && _showLoader && d_loading\" class=\"p-virtualscroller-loader\" [ngClass]=\"{ 'p-virtualscroller-loader-mask': !loaderTemplate }\" [attr.data-pc-section]=\"'loader'\">\n <ng-container *ngIf=\"loaderTemplate || _loaderTemplate; else buildInLoader\">\n <ng-container *ngFor=\"let item of loaderArr; let index = index\">\n <ng-container\n *ngTemplateOutlet=\"\n loaderTemplate || _loaderTemplate;\n context: {\n options: getLoaderOptions(index, both && { numCols: numItemsInViewport.cols })\n }\n \"\n ></ng-container>\n </ng-container>\n </ng-container>\n <ng-template #buildInLoader>\n <ng-container *ngIf=\"loaderIconTemplate || _loaderIconTemplate; else buildInLoaderIcon\">\n <ng-container *ngTemplateOutlet=\"loaderIconTemplate || _loaderIconTemplate; context: { options: { styleClass: 'p-virtualscroller-loading-icon' } }\"></ng-container>\n </ng-container>\n <ng-template #buildInLoaderIcon>\n <SpinnerIcon [styleClass]=\"'p-virtualscroller-loading-icon pi-spin'\" [attr.data-pc-section]=\"'loadingIcon'\" />\n </ng-template>\n </ng-template>\n </div>\n </div>\n </ng-container>\n <ng-template #disabledContainer>\n <ng-content></ng-content>\n <ng-container *ngIf=\"contentTemplate || _contentTemplate\">\n <ng-container *ngTemplateOutlet=\"contentTemplate || _contentTemplate; context: { $implicit: items, options: { rows: _items, columns: loadedColumns } }\"></ng-container>\n </ng-container>\n </ng-template>\n `,\n changeDetection: ChangeDetectionStrategy.Default,\n encapsulation: ViewEncapsulation.None,\n providers: [ScrollerStyle]\n})\nexport class Scroller extends BaseComponent implements OnInit, AfterContentInit, AfterViewChecked, OnDestroy {\n /**\n * Unique identifier of the element.\n * @group Props\n */\n @Input() get id(): string | undefined {\n return this._id;\n }\n set id(val: string | undefined) {\n this._id = val;\n }\n /**\n * Inline style of the component.\n * @group Props\n */\n @Input() get style(): any {\n return this._style;\n }\n set style(val: any) {\n this._style = val;\n }\n /**\n * Style class of the element.\n * @group Props\n */\n @Input() get styleClass(): string | undefined {\n return this._styleClass;\n }\n set styleClass(val: string | undefined) {\n this._styleClass = val;\n }\n /**\n * Index of the element in tabbing order.\n * @group Props\n */\n @Input() get tabindex() {\n return this._tabindex;\n }\n set tabindex(val: number) {\n this._tabindex = val;\n }\n /**\n * An array of objects to display.\n * @group Props\n */\n @Input() get items(): any[] | undefined | null {\n return this._items;\n }\n set items(val: any[] | undefined | null) {\n this._items = val;\n }\n /**\n * The height/width of item according to orientation.\n * @group Props\n */\n @Input() get itemSize(): number[] | number {\n return this._itemSize;\n }\n set itemSize(val: number[] | number) {\n this._itemSize = val;\n }\n /**\n * Height of the scroll viewport.\n * @group Props\n */\n @Input() get scrollHeight(): string | undefined {\n return this._scrollHeight;\n }\n set scrollHeight(val: string | undefined) {\n this._scrollHeight = val;\n }\n /**\n * Width of the scroll viewport.\n * @group Props\n */\n @Input() get scrollWidth(): string | undefined {\n return this._scrollWidth;\n }\n set scrollWidth(val: string | undefined) {\n this._scrollWidth = val;\n }\n /**\n * The orientation of scrollbar.\n * @group Props\n */\n @Input() get orientation(): 'vertical' | 'horizontal' | 'both' {\n return this._orientation;\n }\n set orientation(val: 'vertical' | 'horizontal' | 'both') {\n this._orientation = val;\n }\n /**\n * Used to specify how many items to load in each load method in lazy mode.\n * @group Props\n */\n @Input() get step(): number {\n return this._step;\n }\n set step(val: number) {\n this._step = val;\n }\n /**\n * Delay in scroll before new data is loaded.\n * @group Props\n */\n @Input() get delay() {\n return this._delay;\n }\n set delay(val: number) {\n this._delay = val;\n }\n /**\n * Delay after window's resize finishes.\n * @group Props\n */\n @Input() get resizeDelay() {\n return this._resizeDelay;\n }\n set resizeDelay(val: number) {\n this._resizeDelay = val;\n }\n /**\n * Used to append each loaded item to top without removing any items from the DOM. Using very large data may cause the browser to crash.\n * @group Props\n */\n @Input() get appendOnly(): boolean {\n return this._appendOnly;\n }\n set appendOnly(val: boolean) {\n this._appendOnly = val;\n }\n /**\n * Specifies whether the scroller should be displayed inline or not.\n * @group Props\n */\n @Input() get inline() {\n return this._inline;\n }\n set inline(val: boolean) {\n this._inline = val;\n }\n /**\n * Defines if data is loaded and interacted with in lazy manner.\n * @group Props\n */\n @Input() get lazy() {\n return this._lazy;\n }\n set lazy(val: boolean) {\n this._lazy = val;\n }\n /**\n * If disabled, the scroller feature is eliminated and the content is displayed directly.\n * @group Props\n */\n @Input() get disabled() {\n return this._disabled;\n }\n set disabled(val: boolean) {\n this._disabled = val;\n }\n /**\n * Used to implement a custom loader instead of using the loader feature in the scroller.\n * @group Props\n */\n @Input() get loaderDisabled() {\n return this._loaderDisabled;\n }\n set loaderDisabled(val: boolean) {\n this._loaderDisabled = val;\n }\n /**\n * Columns to display.\n * @group Props\n */\n @Input() get columns(): any[] | undefined | null {\n return this._columns;\n }\n set columns(val: any[] | undefined | null) {\n this._columns = val;\n }\n /**\n * Used to implement a custom spacer instead of using the spacer feature in the scroller.\n * @group Props\n */\n @Input() get showSpacer() {\n return this._showSpacer;\n }\n set showSpacer(val: boolean) {\n this._showSpacer = val;\n }\n /**\n * Defines whether to show loader.\n * @group Props\n */\n @Input() get showLoader() {\n return this._showLoader;\n }\n set showLoader(val: boolean) {\n this._showLoader = val;\n }\n /**\n * Determines how many additional elements to add to the DOM outside of the view. According to the scrolls made up and down, extra items are added in a certain algorithm in the form of multiples of this number. Default value is half the number of items shown in the view.\n * @group Props\n */\n @Input() get numToleratedItems() {\n return this._numToleratedItems;\n }\n set numToleratedItems(val: number) {\n this._numToleratedItems = val;\n }\n /**\n * Defines whether the data is loaded.\n * @group Props\n */\n @Input() get loading(): boolean | undefined {\n return this._loading;\n }\n set loading(val: boolean | undefined) {\n this._loading = val;\n }\n /**\n * Defines whether to dynamically change the height or width of scrollable container.\n * @group Props\n */\n @Input() get autoSize(): boolean {\n return this._autoSize;\n }\n set autoSize(val: boolean) {\n this._autoSize = val;\n }\n /**\n * Function to optimize the dom operations by delegating to ngForTrackBy, default algoritm checks for object identity.\n * @group Props\n */\n @Input() get trackBy(): Function {\n return this._trackBy;\n }\n set trackBy(val: Function) {\n this._trackBy = val;\n }\n /**\n * Defines whether to use the scroller feature. The properties of scroller component can be used like an object in it.\n * @group Props\n */\n @Input() get options(): ScrollerOptions | undefined {\n return this._options;\n }\n set options(val: ScrollerOptions | undefined) {\n this._options = val;\n\n if (val && typeof val === 'object') {\n Object.entries(val).forEach(([k, v]) => this[`_${k}`] !== v && (this[`_${k}`] = v));\n Object.entries(val).forEach(([k, v]) => this[`${k}`] !== v && (this[`${k}`] = v));\n }\n }\n /**\n * Callback to invoke in lazy mode to load new data.\n * @param {ScrollerLazyLoadEvent} event - Custom lazy load event.\n * @group Emits\n */\n @Output() onLazyLoad: EventEmitter<ScrollerLazyLoadEvent> = new EventEmitter<ScrollerLazyLoadEvent>();\n /**\n * Callback to invoke when scroll position changes.\n * @param {ScrollerScrollEvent} event - Custom scroll event.\n * @group Emits\n */\n @Output() onScroll: EventEmitter<ScrollerScrollEvent> = new EventEmitter<ScrollerScrollEvent>();\n /**\n * Callback to invoke when scroll position and item's range in view changes.\n * @param {ScrollerScrollEvent} event - Custom scroll index change event.\n * @group Emits\n */\n @Output() onScrollIndexChange: EventEmitter<ScrollerScrollIndexChangeEvent> = new EventEmitter<ScrollerScrollIndexChangeEvent>();\n\n @ViewChild('element') elementViewChild: Nullable<ElementRef>;\n\n @ViewChild('content') contentViewChild: Nullable<ElementRef>;\n\n @HostBinding('style.height') height: string;\n\n _id: string | undefined;\n\n _style: { [klass: string]: any } | null | undefined;\n\n _styleClass: string | undefined;\n\n _tabindex: number = 0;\n\n _items: any[] | undefined | null;\n\n _itemSize: number | number[] = 0;\n\n _scrollHeight: string | undefined;\n\n _scrollWidth: string | undefined;\n\n _orientation: 'vertical' | 'horizontal' | 'both' = 'vertical';\n\n _step: number = 0;\n\n _delay: number = 0;\n\n _resizeDelay: number = 10;\n\n _appendOnly: boolean = false;\n\n _inline: boolean = false;\n\n _lazy: boolean = false;\n\n _disabled: boolean = false;\n\n _loaderDisabled: boolean = false;\n\n _columns: any[] | undefined | null;\n\n _showSpacer: boolean = true;\n\n _showLoader: boolean = false;\n\n _numToleratedItems: any;\n\n _loading: boolean | undefined;\n\n _autoSize: boolean = false;\n\n _trackBy: any;\n\n _options: ScrollerOptions | undefined;\n\n d_loading: boolean = false;\n\n d_numToleratedItems: any;\n\n contentEl: any;\n /**\n * Content template of the component.\n * @group Templates\n */\n @ContentChild('content', { descendants: false }) contentTemplate: Nullable<TemplateRef<any>>;\n\n /**\n * Item template of the component.\n * @group Templates\n */\n @ContentChild('item', { descendants: false }) itemTemplate: Nullable<TemplateRef<any>>;\n\n /**\n * Loader template of the component.\n * @group Templates\n */\n @ContentChild('loader', { descendants: false }) loaderTemplate: Nullable<TemplateRef<any>>;\n\n /**\n * Loader icon template of the component.\n * @group Templates\n */\n @ContentChild('loadericon', { descendants: false }) loaderIconTemplate: Nullable<TemplateRef<any>>;\n\n @ContentChildren(PrimeTemplate) templates: Nullable<QueryList<PrimeTemplate>>;\n\n _contentTemplate: TemplateRef<any> | undefined;\n\n _itemTemplate: TemplateRef<any> | undefined;\n\n _loaderTemplate: TemplateRef<any> | undefined;\n\n _loaderIconTemplate: TemplateRef<any> | undefined;\n\n first: any = 0;\n\n last: any = 0;\n\n page: number = 0;\n\n isRangeChanged: boolean = false;\n\n numItemsInViewport: any = 0;\n\n lastScrollPos: any = 0;\n\n lazyLoadState: any = {};\n\n loaderArr: any[] = [];\n\n spacerStyle: { [klass: string]: any } | null | undefined = {};\n\n contentStyle: { [klass: string]: any } | null | undefined = {};\n\n scrollTimeout: any;\n\n resizeTimeout: any;\n\n initialized: boolean = false;\n\n windowResizeListener: VoidListener;\n\n defaultWidth: number | undefined;\n\n defaultHeight: number | undefined;\n\n defaultContentWidth: number | undefined;\n\n defaultContentHeight: number | undefined;\n\n _contentStyleClass: any;\n\n get contentStyleClass() {\n return this._contentStyleClass;\n }\n\n set contentStyleClass(val) {\n this._contentStyleClass = val;\n }\n\n get vertical() {\n return this._orientation === 'vertical';\n }\n\n get horizontal() {\n return this._orientation === 'horizontal';\n }\n\n get both() {\n return this._orientation === 'both';\n }\n\n get loadedItems() {\n if (this._items && !this.d_loading) {\n if (this.both) return this._items.slice(this._appendOnly ? 0 : this.first.rows, this.last.rows).map((item) => (this._columns ? item : item.slice(this._appendOnly ? 0 : this.first.cols, this.last.cols)));\n else if (this.horizontal && this._columns) return this._items;\n else return this._items.slice(this._appendOnly ? 0 : this.first, this.last);\n }\n\n return [];\n }\n\n get loadedRows() {\n return this.d_loading ? (this._loaderDisabled ? this.loaderArr : []) : this.loadedItems;\n }\n\n get loadedColumns() {\n if (this._columns && (this.both || this.horizontal)) {\n return this.d_loading && this._loaderDisabled ? (this.both ? this.loaderArr[0] : this.loaderArr) : this._columns.slice(this.both ? this.first.cols : this.first, this.both ? this.last.cols : this.last);\n }\n\n return this._columns;\n }\n\n _componentStyle = inject(ScrollerStyle);\n\n constructor(private zone: NgZone) {\n super();\n }\n\n ngOnInit() {\n super.ngOnInit();\n this.setInitialState();\n }\n\n ngOnChanges(simpleChanges: SimpleChanges) {\n super.ngOnChanges(simpleChanges);\n let isLoadingChanged = false;\n if (this.scrollHeight == '100%') {\n this.height = '100%';\n }\n if (simpleChanges.loading) {\n const { previousValue, currentValue } = simpleChanges.loading;\n\n if (this.lazy && previousValue !== currentValue && currentValue !== this.d_loading) {\n this.d_loading = currentValue;\n isLoadingChanged = true;\n }\n }\n\n if (simpleChanges.orientation) {\n this.lastScrollPos = this.both ? { top: 0, left: 0 } : 0;\n }\n\n if (simpleChanges.numToleratedItems) {\n const { previousValue, currentValue } = simpleChanges.numToleratedItems;\n\n if (previousValue !== currentValue && currentValue !== this.d_numToleratedItems) {\n this.d_numToleratedItems = currentValue;\n }\n }\n\n if (simpleChanges.options) {\n const { previousValue, currentValue } = simpleChanges.options;\n\n if (this.lazy && previousValue?.loading !== currentValue?.loading && currentValue?.loading !== this.d_loading) {\n this.d_loading = currentValue.loading;\n isLoadingChanged = true;\n }\n\n if (previousValue?.numToleratedItems !== currentValue?.numToleratedItems && currentValue?.numToleratedItems !== this.d_numToleratedItems) {\n this.d_numToleratedItems = currentValue.numToleratedItems;\n }\n }\n\n if (this.initialized) {\n const isChanged = !isLoadingChanged && (simpleChanges.items?.previousValue?.length !== simpleChanges.items?.currentValue?.length || simpleChanges.itemSize || simpleChanges.scrollHeight || simpleChanges.scrollWidth);\n\n if (isChanged) {\n this.init();\n this.calculateAutoSize();\n }\n }\n }\n\n ngAfterContentInit() {\n (this.templates as QueryList<PrimeTemplate>).forEach((item) => {\n switch (item.getType()) {\n case 'content':\n this._contentTemplate = item.template;\n break;\n\n case 'item':\n this._itemTemplate = item.template;\n break;\n\n case 'loader':\n this._loaderTemplate = item.template;\n break;\n\n case 'loadericon':\n this._loaderIconTemplate = item.template;\n break;\n\n default:\n this._itemTemplate = item.template;\n break;\n }\n });\n }\n\n ngAfterViewInit() {\n super.ngAfterViewInit();\n Promise.resolve().then(() => {\n this.viewInit();\n });\n }\n\n ngAfterViewChecked() {\n if (!this.initialized) {\n this.viewInit();\n }\n }\n\n ngOnDestroy() {\n this.unbindResizeListener();\n\n this.contentEl = null;\n this.initialized = false;\n super.ngOnDestroy();\n }\n\n viewInit() {\n if (isPlatformBrowser(this.platformId) && !this.initialized) {\n if (isVisible(this.elementViewChild?.nativeElement)) {\n this.setInitialState();\n this.setContentEl(this.contentEl);\n this.init();\n\n this.defaultWidth = getWidth(this.elementViewChild?.nativeElement);\n this.defaultHeight = getHeight(this.elementViewChild?.nativeElement);\n this.defaultContentWidth = getWidth(this.contentEl);\n this.defaultContentHeight = getHeight(this.contentEl);\n this.initialized = true;\n }\n }\n }\n\n init() {\n if (!this._disabled) {\n this.setSize();\n this.calculateOptions();\n this.setSpacerSize();\n this.bindResizeListener();\n\n this.cd.detectChanges();\n }\n }\n\n setContentEl(el?: HTMLElement) {\n this.contentEl = el || this.contentViewChild?.nativeElement || findSingle(this.elementViewChild?.nativeElement, '.p-virtualscroller-content');\n }\n\n setInitialState() {\n this.first = this.both ? { rows: 0, cols: 0 } : 0;\n this.last = this.both ? { rows: 0, cols: 0 } : 0;\n this.numItemsInViewport = this.both ? { rows: 0, cols: 0 } : 0;\n this.lastScrollPos = this.both ? { top: 0, left: 0 } : 0;\n this.d_loading = this._loading || false;\n this.d_numToleratedItems = this._numToleratedItems;\n this.loaderArr = [];\n }\n\n getElementRef() {\n return this.elementViewChild;\n }\n\n getPageByFirst(first?: any) {\n return Math.floor(((first ?? this.first) + this.d_numToleratedItems * 4) / (this._step || 1));\n }\n\n isPageChanged(first?: any) {\n return this._step ? this.page !== this.getPageByFirst(first ?? this.first) : true;\n }\n\n scrollTo(options: ScrollToOptions) {\n // this.lastScrollPos = this.both ? { top: 0, left: 0 } : 0;\n this.elementViewChild?.nativeElement?.scrollTo(options);\n }\n\n scrollToIndex(index: number | number[], behavior: ScrollBehavior = 'auto') {\n const valid = this.both ? (index as number[]).every((i) => i > -1) : (index as number) > -1;\n\n if (valid) {\n const first = this.first;\n const { scrollTop = 0, scrollLeft = 0 } = this.elementViewChild?.nativeElement;\n const { numToleratedItems } = this.calculateNumItems();\n const contentPos = this.getContentPosition();\n const itemSize = this.itemSize;\n const calculateFirst = (_index = 0, _numT) => (_index <= _numT ? 0 : _index);\n const calculateCoord = (_first, _size, _cpos) => _first * _size + _cpos;\n const scrollTo = (left = 0, top = 0) => this.scrollTo({ left, top, behavior });\n let newFirst = this.both ? { rows: 0, cols: 0 } : 0;\n let isRangeChanged = false,\n isScrollChanged = false;\n\n if (this.both) {\n newFirst = {\n rows: calculateFirst(index[0], numToleratedItems[0]),\n cols: calculateFirst(index[1], numToleratedItems[1])\n };\n scrollTo(calculateCoord(newFirst.cols, itemSize[1], contentPos.left), calculateCoord(newFirst.rows, itemSize[0], contentPos.top));\n isScrollChanged = this.lastScrollPos.top !== scrollTop || this.lastScrollPos.left !== scrollLeft;\n isRangeChanged = newFirst.rows !== first.rows || newFirst.cols !== first.cols;\n } else {\n newFirst = calculateFirst(index as number, numToleratedItems);\n this.horizontal ? scrollTo(calculateCoord(newFirst, itemSize, contentPos.left), scrollTop) : scrollTo(scrollLeft, calculateCoord(newFirst, itemSize, contentPos.top));\n isScrollChanged = this.lastScrollPos !== (this.horizontal ? scrollLeft : scrollTop);\n isRangeChanged = newFirst !== first;\n }\n\n this.isRangeChanged = isRangeChanged;\n isScrollChanged && (this.first = newFirst);\n }\n }\n\n scrollInView(index: number, to: ScrollerToType, behavior: ScrollBehavior = 'auto') {\n if (to) {\n const { first, viewport } = this.getRenderedRange();\n const scrollTo = (left = 0, top = 0) => this.scrollTo({ left, top, behavior });\n const isToStart = to === 'to-start';\n const isToEnd = to === 'to-end';\n\n if (isToStart) {\n if (this.both) {\n if (viewport.first.rows - first.rows > (<any>index)[0]) {\n scrollTo(viewport.first.cols * (<number[]>this._itemSize)[1], (viewport.first.rows - 1) * (<number[]>this._itemSize)[0]);\n } else if (viewport.first.cols - first.cols > (<any>index)[1]) {\n scrollTo((viewport.first.cols - 1) * (<number[]>this._itemSize)[1], viewport.first.rows * (<number[]>this._itemSize)[0]);\n }\n } else {\n if (viewport.first - first > index) {\n const pos = (viewport.first - 1) * <number>this._itemSize;\n this.horizontal ? scrollTo(pos, 0) : scrollTo(0, pos);\n }\n }\n } else if (isToEnd) {\n if (this.both) {\n if (viewport.last.rows - first.rows <= (<any>index)[0] + 1) {\n scrollTo(viewport.first.cols * (<number[]>this._itemSize)[1], (viewport.first.rows + 1) * (<number[]>this._itemSize)[0]);\n } else if (viewport.last.cols - first.cols <= (<any>index)[1] + 1) {\n scrollTo((viewport.first.cols + 1) * (<number[]>this._itemSize)[1], viewport.first.rows * (<number[]>this._itemSize)[0]);\n }\n } else {\n if (viewport.last - first <= index + 1) {\n const pos = (viewport.first + 1) * <number>this._itemSize;\n this.horizontal ? scrollTo(pos, 0) : scrollTo(0, pos);\n }\n }\n }\n } else {\n this.scrollToIndex(index, behavior);\n }\n }\n\n getRenderedRange() {\n const calculateFirstInViewport = (_pos: number, _size: number) => (_size || _pos ? Math.floor(_pos / (_size || _pos)) : 0);\n\n let firstInViewport = this.first;\n let lastInViewport: any = 0;\n\n if (this.elementViewChild?.nativeElement) {\n const { scrollTop, scrollLeft } = this.elementViewChild.nativeElement;\n\n if (this.both) {\n firstInViewport = {\n rows: calculateFirstInViewport(scrollTop, (<number[]>this._itemSize)[0]),\n cols: calculateFirstInViewport(scrollLeft, (<number[]>this._itemSize)[1])\n };\n lastInViewport = {\n rows: firstInViewport.rows + this.numItemsInViewport.rows,\n cols: firstInViewport.cols + this.numItemsInViewport.cols\n };\n } else {\n const scrollPos = this.horizontal ? scrollLeft : scrollTop;\n firstInViewport = calculateFirstInViewport(scrollPos, <number>this._itemSize);\n lastInViewport = firstInViewport + this.numItemsInViewport;\n }\n }\n\n return {\n first: this.first,\n last: this.last,\n viewport: {\n first: firstInViewport,\n last: lastInViewport\n }\n };\n }\n\n calculateNumItems() {\n const contentPos = this.getContentPosition();\n const contentWidth = (this.elementViewChild?.nativeElement ? this.elementViewChild.nativeElement.offsetWidth - contentPos.left : 0) || 0;\n const contentHeight = (this.elementViewChild?.nativeElement ? this.elementViewChild.nativeElement.offsetHeight - contentPos.top : 0) || 0;\n const calculateNumItemsInViewport = (_contentSize: number, _itemSize: number) => (_itemSize || _contentSize ? Math.ceil(_contentSize / (_itemSize || _contentSize)) : 0);\n const calculateNumToleratedItems = (_numItems: number) => Math.ceil(_numItems / 2);\n const numItemsInViewport: any = this.both\n ? {\n rows: calculateNumItemsInViewport(contentHeight, (<number[]>this._itemSize)[0]),\n cols: calculateNumItemsInViewport(contentWidth, (<number[]>this._itemSize)[1])\n }\n : calculateNumItemsInViewport(this.horizontal ? contentWidth : contentHeight, <number>this._itemSize);\n\n const numToleratedItems = this.d_numToleratedItems || (this.both ? [calculateNumToleratedItems(numItemsInViewport.rows), calculateNumToleratedItems(numItemsInViewport.cols)] : calculateNumToleratedItems(numItemsInViewport));\n\n return { numItemsInViewport, numToleratedItems };\n }\n\n calculateOptions() {\n const { numItemsInViewport, numToleratedItems } = this.calculateNumItems();\n const calculateLast = (_first: number, _num: number, _numT: number, _isCols: boolean = false) => this.getLast(_first + _num + (_first < _numT ? 2 : 3) * _numT, _isCols);\n const first = this.first;\n const last = this.both\n ? {\n rows: calculateLast(this.first.rows, numItemsInViewport.rows, numToleratedItems[0]),\n cols: calculateLast(this.first.cols, numItemsInViewport.cols, numToleratedItems[1], true)\n }\n : calculateLast(this.first, numItemsInViewport, numToleratedItems);\n\n this.last = last;\n this.numItemsInViewport = numItemsInViewport;\n this.d_numToleratedItems = numToleratedItems;\n\n if (this.showLoader) {\n this.loaderArr = this.both ? Array.from({ length: numItemsInViewport.rows }).map(() => Array.from({ length: numItemsInViewport.cols })) : Array.from({ length: numItemsInViewport });\n }\n\n if (this._lazy) {\n Promise.resolve().then(() => {\n this.lazyLoadState = {\n first: this._step ? (this.both ? { rows: 0, cols: first.cols } : 0) : first,\n last: Math.min(this._step ? this._step : this.last, (<any[]>this.items).length)\n };\n\n this.handleEvents('onLazyLoad', this.lazyLoadState);\n });\n }\n }\n\n calculateAutoSize() {\n if (this._autoSize && !this.d_loading) {\n Promise.resolve().then(() => {\n if (this.contentEl) {\n this.contentEl.style.minHeight = this.contentEl.style.minWidth = 'auto';\n this.contentEl.style.position = 'relative';\n (<ElementRef>this.elementViewChild).nativeElement.style.contain = 'none';\n\n const [contentWidth, contentHeight] = [getWidth(this.contentEl), getHeight(this.contentEl)];\n contentWidth !== this.defaultContentWidth && ((<ElementRef>this.elementViewChild).nativeElement.style.width = '');\n contentHeight !== this.defaultContentHeight && ((<ElementRef>this.elementViewChild).nativeElement.style.height = '');\n\n const [width, height] = [getWidth((<ElementRef>this.elementViewChild).nativeElement), getHeight((<ElementRef>this.elementViewChild).nativeElement)];\n (this.both || this.horizontal) && ((<ElementRef>this.elementViewChild).nativeElement.style.width = width < <number>this.defaultWidth ? width + 'px' : this._scrollWidth || this.defaultWidth + 'px');\n (this.both || this.vertical) && ((<ElementRef>this.elementViewChild).nativeElement.style.height = height < <number>this.defaultHeight ? height + 'px' : this._scrollHeight || this.defaultHeight + 'px');\n\n this.contentEl.style.minHeight = this.contentEl.style.minWidth = '';\n this.contentEl.style.position = '';\n (<ElementRef>this.elementViewChild).nativeElement.style.contain = '';\n }\n });\n }\n }\n\n getLast(last = 0, isCols = false) {\n return this._items ? Math.min(isCols ? (this._columns || this._items[0]).length : this._items.length, last) : 0;\n }\n\n getContentPosition() {\n if (this.contentEl) {\n const style = getComputedStyle(this.contentEl);\n const left = parseFloat(style.paddingLeft) + Math.max(parseFloat(style.left) || 0, 0);\n const right = parseFloat(style.paddingRight) + Math.max(parseFloat(style.right) || 0, 0);\n const top = parseFloat(style.paddingTop) + Math.max(parseFloat(style.top) || 0, 0);\n const bottom = parseFloat(style.paddingBottom) + Math.max(parseFloat(style.bottom) || 0, 0);\n\n return { left, right, top, bottom, x: left + right, y: top + bottom };\n }\n\n return { left: 0, right: 0, top: 0, bottom: 0, x: 0, y: 0 };\n }\n\n setSize() {\n if (this.elementViewChild?.nativeElement) {\n const parentElement = this.elementViewChild.nativeElement.parentElement.parentElement;\n const width = this._scrollWidth || `${this.elementViewChild.nativeElement.offsetWidth || parentElement.offsetWidth}px`;\n const height = this._scrollHeight || `${this.elementViewChild.nativeElement.offsetHeight || parentElement.offsetHeight}px`;\n const setProp = (_name: string, _value: any) => ((<ElementRef>this.elementViewChild).nativeElement.style[_name] = _value);\n\n if (this.both || this.horizontal) {\n setProp('height', height);\n setProp('width', width);\n } else {\n setProp('height', height);\n }\n }\n }\n\n setSpacerSize() {\n if (this._items) {\n const contentPos = this.getContentPosition();\n const setProp = (_name: string, _value: any, _size: number, _cpos: number = 0) =>\n (this.spacerStyle = {\n ...this.spacerStyle,\n ...{ [`${_name}`]: (_value || []).length * _size + _cpos + 'px' }\n });\n\n if (this.both) {\n setProp('height', this._items, (<number[]>this._itemSize)[0], contentPos.y);\n setProp('width', this._columns || this._items[1], (<number[]>this._itemSize)[1], contentPos.x);\n } else {\n this.horizontal ? setProp('width', this._columns || this._items, <number>this._itemSize, contentPos.x) : setProp('height', this._items, <number>this._itemSize, contentPos.y);\n }\n }\n }\n\n setContentPosition(pos: any) {\n if (this.contentEl && !this._appendOnly) {\n const first = pos ? pos.first : this.first;\n const calculateTranslateVal = (_first: number, _size: number) => _first * _size;\n const setTransform = (_x = 0, _y = 0) => (this.contentStyle = { ...this.contentStyle, ...{ transform: `translate3d(${_x}px, ${_y}px, 0)` } });\n\n if (this.both) {\n setTransform(calculateTranslateVal(first.cols, (<number[]>this._itemSize)[1]), calculateTranslateVal(first.rows, (<number[]>this._itemSize)[0]));\n } else {\n const translateVal = calculateTranslateVal(first, <number>this._itemSize);\n this.horizontal ? setTransform(translateVal, 0) : setTransform(0, translateVal);\n }\n }\n }\n\n onScrollPositionChange(event: Event) {\n const target = event.target;\n const contentPos = this.getContentPosition();\n const calculateScrollPos = (_pos: number, _cpos: number) => (_pos ? (_pos > _cpos ? _pos - _cpos : _pos) : 0);\n const calculateCurrentIndex = (_pos: number, _size: number) => (_size || _pos ? Math.floor(_pos / (_size || _pos)) : 0);\n const calculateTriggerIndex = (_currentIndex: number, _first: number, _last: number, _num: number, _numT: number, _isScrollDownOrRight: any) => {\n return _currentIndex <= _numT ? _numT : _isScrollDownOrRight ? _last - _num - _numT : _first + _numT - 1;\n };\n const calculateFirst = (_currentIndex: number, _triggerIndex: number, _first: number, _last: number, _num: number, _numT: number, _isScrollDownOrRight: any) => {\n if (_currentIndex <= _numT) return 0;\n else return Math.max(0, _isScrollDownOrRight ? (_currentIndex < _triggerIndex ? _first : _currentIndex - _numT) : _currentIndex > _triggerIndex ? _first : _currentIndex - 2 * _numT);\n };\n const calculateLast = (_currentIndex: number, _first: number, _last: number, _num: number, _numT: number, _isCols = false) => {\n let lastValue = _first + _num + 2 * _numT;\n\n if (_currentIndex >= _numT) {\n lastValue += _numT + 1;\n }\n\n return this.getLast(lastValue, _isCols);\n };\n\n const scrollTop = calculateScrollPos((<HTMLElement>target).scrollTop, contentPos.top);\n const scrollLeft = calculateScrollPos((<HTMLElement>target).scrollLeft, contentPos.left);\n\n let newFirst = this.both ? { rows: 0, cols: 0 } : 0;\n let newLast = this.last;\n let isRangeChanged = false;\n let newScrollPos = this.lastScrollPos;\n\n if (this.both) {\n const isScrollDown = this.lastScrollPos.top <= scrollTop;\n const isScrollRight = this.lastScrollPos.left <= scrollLeft;\n\n if (!this._appendOnly || (this._appendOnly && (isScrollDown || isScrollRight))) {\n const currentIndex = {\n rows: calculateCurrentIndex(scrollTop, (<number[]>this._itemSize)[0]),\n cols: calculateCurrentIndex(scrollLeft, (<number[]>this._itemSize)[1])\n };\n const triggerIndex = {\n rows: calculateTriggerIndex(currentIndex.rows, this.first.rows, this.last.rows, this.numItemsInViewport.rows, this.d_numToleratedItems[0], isScrollDown),\n cols: calculateTriggerIndex(currentIndex.cols, this.first.cols, this.last.cols, this.numItemsInViewport.cols, this.d_numToleratedItems[1], isScrollRight)\n };\n\n newFirst = {\n rows: calculateFirst(currentIndex.rows, triggerIndex.rows, this.first.rows, this.last.rows, this.numItemsInViewport.rows, this.d_numToleratedItems[0], isScrollDown),\n cols: calculateFirst(currentIndex.cols, triggerIndex.cols, this.first.cols, this.last.cols, this.numItemsInViewport.cols, this.d_numToleratedItems[1], isScrollRight)\n };\n newLast = {\n rows: calculateLast(currentIndex.rows, newFirst.rows, this.last.rows, this.numItemsInViewport.rows, this.d_numToleratedItems[0]),\n cols: calculateLast(currentIndex.cols, newFirst.cols, this.last.cols, this.numItemsInViewport.cols, this.d_numToleratedItems[1], true)\n };\n\n isRangeChanged = newFirst.rows !== this.first.rows || newLast.rows !== this.last.rows || newFirst.cols !== this.first.cols || newLast.cols !== this.last.cols || this.isRangeChanged;\n newScrollPos = { top: scrollTop, left: scrollLeft };\n }\n } else {\n const scrollPos = this.horizontal ? scrollLeft : scrollTop;\n const isScrollDownOrRight = this.lastScrollPos <= scrollPos;\n\n if (!this._appendOnly || (this._appendOnly && isScrollDownOrRight)) {\n const currentIndex = calculateCurrentIndex(scrollPos, <number>this._itemSize);\n const triggerIndex = calculateTriggerIndex(currentIndex, this.first, this.last, this.numItemsInViewport, this.d_numToleratedItems, isScrollDownOrRight);\n\n newFirst = calculateFirst(currentIndex, triggerIndex, this.first, this.last, this.numItemsInViewport, this.d_numToleratedItems, isScrollDownOrRight);\n newLast = calculateLast(currentIndex, newFirst, this.last, this.numItemsInViewport, this.d_numToleratedItems);\n isRangeChanged = newFirst !== this.first || newLast !== this.last || this.isRangeChanged;\n newScrollPos = scrollPos;\n }\n }\n\n return {\n first: newFirst,\n last: newLast,\n isRangeChanged,\n scrollPos: newScrollPos\n };\n }\n\n onScrollChange(event: Event) {\n const { first, last, isRangeChanged, scrollPos } = this.onScrollPositionChange(event);\n\n if (isRangeChanged) {\n const newState = { first, last };\n\n this.setContentPosition(newState);\n\n this.first = first;\n this.last = last;\n this.lastScrollPos = scrollPos;\n\n this.handleEvents('onScrollIndexChange', newState);\n\n if (this._lazy && this.isPageChanged(first)) {\n const lazyLoadState = {\n first: this._step ? Math.min(this.getPageByFirst(first) * this._step, (<any[]>this.items).length - this._step) : first,\n last: Math.min(this._step ? (this.getPageByFirst(first) + 1) * this._step : last, (<any[]>this.items).length)\n };\n const isLazyStateChanged = this.lazyLoadState.first !== lazyLoadState.first || this.lazyLoadState.last !== lazyLoadState.last;\n\n isLazyStateChanged && this.handleEvents('onLazyLoad', lazyLoadState);\n this.lazyLoadState = lazyLoadState;\n }\n }\n }\n\n onContainerScroll(event: Event) {\n this.handleEvents('onScroll', { originalEvent: event });\n\n if (this._delay && this.isPageChanged()) {\n if (this.scrollTimeout) {\n clearTimeout(this.scrollTimeout);\n }\n\n if (!this.d_loading && this.showLoader) {\n const { isRangeChanged } = this.onScrollPositionChange(event);\n const changed = isRangeChanged || (this._step ? this.isPageChanged() : false);\n\n if (changed) {\n this.d_loading = true;\n\n this.cd.detectChanges();\n }\n }\n\n this.scrollTimeout = setTimeout(() => {\n this.onScrollChange(event);\n\n if (this.d_loading && this.showLoader && (!this._lazy || this._loading === undefined)) {\n this.d_loading = false;\n this.page = this.getPageByFirst();\n }\n this.cd.detectChanges();\n }, this._delay);\n } else {\n !this.d_loading && this.onScrollChange(event);\n }\n }\n\n bindResizeListener() {\n if (isPlatformBrowser(this.platformId)) {\n if (!this.windowResizeListener) {\n this.zone.runOutsideAngular(() => {\n const window = this.document.defaultView as Window;\n const event = isTouchDevice() ? 'orientationchange' : 'resize';\n this.windowResizeListener = this.renderer.listen(window, event, this.onWindowResize.bind(this));\n });\n }\n }\n }\n\n unbindResizeListener() {\n if (this.windowResizeListener) {\n this.windowResizeListener();\n this.windowResizeListener = null;\n }\n }\n\n onWindowResize() {\n if (this.resizeTimeout) {\n clearTimeout(this.resizeTimeout);\n }\n\n this.resizeTimeout = setTimeout(() => {\n if (isVisible(this.elementViewChild?.nativeElement)) {\n const [width, height] = [getWidth(this.elementViewChild?.nativeElement), getHeight(this.elementViewChild?.nativeElement)];\n const [isDiffWidth, isDiffHeight] = [width !== this.defaultWidth, height !== this.defaultHeight];\n const reinit = this.both ? isDiffWidth || isDiffHeight : this.horizontal ? isDiffWidth : this.vertical ? isDiffHeight : false;\n\n reinit &&\n this.zone.run(() => {\n this.d_numToleratedItems = this._numToleratedItems;\n this.defaultWidth = width;\n this.defaultHeight = height;\n this.defaultContentWidth = getWidth(this.contentEl);\n this.defaultContentHeight = getHeight(this.contentEl);\n\n this.init();\n });\n }\n }, this._resizeDelay);\n }\n\n handleEvents(name: string, params: any) {\n //@ts-ignore\n return this.options && (<any>this.options)[name] ? (<any>this.options)[name](params) : this[name].emit(params);\n }\n\n getContentOptions() {\n return {\n contentStyleClass: `p-virtualscroller-content ${this.d_loading ? 'p-virtualscroller-loading' : ''}`,\n items: this.loadedItems,\n getItemOptions: (index: number) => this.getOptions(index),\n loading: this.d_loading,\n getLoaderOptions: (index: number, options?: any) => this.getLoaderOptions(index, options),\n itemSize: this._itemSize,\n rows: this.loadedRows,\n columns: this.loadedColumns,\n spacerStyle: this.spacerStyle,\n contentStyle: this.contentStyle,\n vertical: this.vertical,\n horizontal: this.horizontal,\n both: this.both\n };\n }\n\n getOptions(renderedIndex: number) {\n const count = (this._items || []).length;\n const index = this.both ? this.first.rows + renderedIndex : this.first + renderedIndex;\n\n return {\n index,\n count,\n first: index === 0,\n last: index === count - 1,\n even: index % 2 === 0,\n odd: inde