173 lines
6.3 KiB
JavaScript
173 lines
6.3 KiB
JavaScript
import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ElementRef, Input, ContentChild, ContentChildren, ViewChild, Output, NgModule } from '@angular/core';
|
|
import { CommonModule } from '@angular/common';
|
|
import { Header, Footer, PrimeTemplate, SharedModule } from 'primeng/api';
|
|
import { CdkVirtualScrollViewport, ScrollingModule } from '@angular/cdk/scrolling';
|
|
|
|
class VirtualScroller {
|
|
constructor(el) {
|
|
this.el = el;
|
|
this.trackBy = (index, item) => item;
|
|
this.onLazyLoad = new EventEmitter();
|
|
this._totalRecords = 0;
|
|
this.page = 0;
|
|
this._first = 0;
|
|
this.loadedPages = [];
|
|
}
|
|
get totalRecords() {
|
|
return this._totalRecords;
|
|
}
|
|
set totalRecords(val) {
|
|
this._totalRecords = val;
|
|
console.log("totalRecords is deprecated, provide a value with the length of virtual items instead.");
|
|
}
|
|
get first() {
|
|
return this._first;
|
|
}
|
|
set first(val) {
|
|
this._first = val;
|
|
console.log("first property is deprecated, use scrollToIndex function to scroll a specific item.");
|
|
}
|
|
get cache() {
|
|
return this._cache;
|
|
}
|
|
set cache(val) {
|
|
this._cache = val;
|
|
console.log("cache is deprecated as it is always on.");
|
|
}
|
|
ngAfterContentInit() {
|
|
this.templates.forEach((item) => {
|
|
switch (item.getType()) {
|
|
case 'item':
|
|
this.itemTemplate = item.template;
|
|
break;
|
|
case 'loadingItem':
|
|
this.loadingItemTemplate = item.template;
|
|
break;
|
|
case 'header':
|
|
this.headerTemplate = item.template;
|
|
break;
|
|
case 'footer':
|
|
this.footerTemplate = item.template;
|
|
break;
|
|
default:
|
|
this.itemTemplate = item.template;
|
|
break;
|
|
}
|
|
});
|
|
}
|
|
onScrollIndexChange(index) {
|
|
if (this.lazy) {
|
|
let pageRange = this.createPageRange(Math.floor(index / this.rows));
|
|
pageRange.forEach(page => this.loadPage(page));
|
|
}
|
|
}
|
|
createPageRange(page) {
|
|
let range = [];
|
|
if (page !== 0) {
|
|
range.push(page - 1);
|
|
}
|
|
range.push(page);
|
|
if (page !== (Math.ceil(this.value.length / this.rows) - 1)) {
|
|
range.push(page + 1);
|
|
}
|
|
return range;
|
|
}
|
|
loadPage(page) {
|
|
if (!this.loadedPages.includes(page)) {
|
|
this.onLazyLoad.emit({ first: this.rows * page, rows: this.rows });
|
|
this.loadedPages.push(page);
|
|
}
|
|
}
|
|
getBlockableElement() {
|
|
return this.el.nativeElement.children[0];
|
|
}
|
|
//@deprecated
|
|
scrollTo(index, mode) {
|
|
this.scrollToIndex(index, mode);
|
|
}
|
|
scrollToIndex(index, mode) {
|
|
if (this.viewport) {
|
|
this.viewport.scrollToIndex(index, mode);
|
|
}
|
|
}
|
|
clearCache() {
|
|
this.loadedPages = [];
|
|
}
|
|
ngOnChanges(simpleChange) {
|
|
if (simpleChange.value) {
|
|
if (!this.lazy) {
|
|
this.clearCache();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
VirtualScroller.decorators = [
|
|
{ type: Component, args: [{
|
|
selector: 'p-virtualScroller',
|
|
template: `
|
|
<div [ngClass]="'p-virtualscroller p-component'" [ngStyle]="style" [class]="styleClass">
|
|
<div class="p-virtualscroller-header" *ngIf="header || headerTemplate">
|
|
<ng-content select="p-header"></ng-content>
|
|
<ng-container *ngTemplateOutlet="headerTemplate"></ng-container>
|
|
</div>
|
|
<div #content class="p-virtualscroller-content">
|
|
<div class="p-virtualscroller-list">
|
|
<cdk-virtual-scroll-viewport #viewport [ngStyle]="{'height': scrollHeight}" [itemSize]="itemSize" [minBufferPx]="minBufferPx" [maxBufferPx]="maxBufferPx" (scrolledIndexChange)="onScrollIndexChange($event)">
|
|
<ng-container *cdkVirtualFor="let item of value; trackBy: trackBy; let i = index; let c = count; let f = first; let l = last; let e = even; let o = odd;">
|
|
<div [ngStyle]="{'height': itemSize + 'px'}" class="p-virtualscroller-item">
|
|
<ng-container *ngTemplateOutlet="item ? itemTemplate : loadingItemTemplate; context: {$implicit: item, index: i, count: c, first: f, last: l, even: e, odd: o}"></ng-container>
|
|
</div>
|
|
</ng-container>
|
|
</cdk-virtual-scroll-viewport>
|
|
</div>
|
|
</div>
|
|
<div class="p-virtualscroller-footer" *ngIf="footer || footerTemplate">
|
|
<ng-content select="p-footer"></ng-content>
|
|
<ng-container *ngTemplateOutlet="footerTemplate"></ng-container>
|
|
</div>
|
|
</div>
|
|
`,
|
|
changeDetection: ChangeDetectionStrategy.Default,
|
|
encapsulation: ViewEncapsulation.None
|
|
},] }
|
|
];
|
|
VirtualScroller.ctorParameters = () => [
|
|
{ type: ElementRef }
|
|
];
|
|
VirtualScroller.propDecorators = {
|
|
value: [{ type: Input }],
|
|
itemSize: [{ type: Input }],
|
|
style: [{ type: Input }],
|
|
styleClass: [{ type: Input }],
|
|
scrollHeight: [{ type: Input }],
|
|
lazy: [{ type: Input }],
|
|
rows: [{ type: Input }],
|
|
minBufferPx: [{ type: Input }],
|
|
maxBufferPx: [{ type: Input }],
|
|
trackBy: [{ type: Input }],
|
|
header: [{ type: ContentChild, args: [Header,] }],
|
|
footer: [{ type: ContentChild, args: [Footer,] }],
|
|
templates: [{ type: ContentChildren, args: [PrimeTemplate,] }],
|
|
viewport: [{ type: ViewChild, args: [CdkVirtualScrollViewport,] }],
|
|
onLazyLoad: [{ type: Output }],
|
|
totalRecords: [{ type: Input }],
|
|
first: [{ type: Input }],
|
|
cache: [{ type: Input }]
|
|
};
|
|
class VirtualScrollerModule {
|
|
}
|
|
VirtualScrollerModule.decorators = [
|
|
{ type: NgModule, args: [{
|
|
imports: [CommonModule, ScrollingModule],
|
|
exports: [VirtualScroller, SharedModule, ScrollingModule],
|
|
declarations: [VirtualScroller]
|
|
},] }
|
|
];
|
|
|
|
/**
|
|
* Generated bundle index. Do not edit.
|
|
*/
|
|
|
|
export { VirtualScroller, VirtualScrollerModule };
|
|
//# sourceMappingURL=primeng-virtualscroller.js.map
|