UNPKG

primeng

Version:

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![npm version](https://badge.fury.io/js/primeng.svg)](https://badge.fury.io/js/primeng) [![Build Status](https://travis-ci.org/primefaces/primeng.

182 lines (178 loc) 6.7 kB
import { EventEmitter, ElementRef, Input, ContentChild, ContentChildren, ViewChild, Output, Component, ChangeDetectionStrategy, NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { Header, Footer, PrimeTemplate, SharedModule } from 'primeng/api'; import { ScrollingModule } from '@angular/cdk/scrolling'; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; let VirtualScroller = class VirtualScroller { constructor(el) { this.el = el; this.cache = true; this.first = 0; this.trackBy = (index, item) => item; this.onLazyLoad = new EventEmitter(); this._totalRecords = 0; this.lazyValue = []; this.page = 0; } get totalRecords() { return this._totalRecords; } set totalRecords(val) { this._totalRecords = val; this.lazyValue = Array.from({ length: this._totalRecords }); this.onLazyLoad.emit(this.createLazyLoadMetadata()); this.first = 0; this.scrollTo(0); } get value() { return this.lazy ? this.lazyValue : this._value; } set value(val) { if (this.lazy) { if (val) { let arr = this.cache ? [...this.lazyValue] : Array.from({ length: this._totalRecords }); for (let i = this.first, j = 0; i < (this.first + this.rows); i++, j++) { arr[i] = val[j]; } this.lazyValue = arr; } } else { this._value = val; } } ngAfterContentInit() { this.templates.forEach((item) => { switch (item.getType()) { case 'item': this.itemTemplate = item.template; break; case 'loadingItem': this.loadingItemTemplate = item.template; break; default: this.itemTemplate = item.template; break; } }); } onScrollIndexChange(index) { let p = Math.floor(index / this.rows); if (p !== this.page) { this.page = p; this.first = this.page * this.rows; this.onLazyLoad.emit(this.createLazyLoadMetadata()); } } createLazyLoadMetadata() { return { first: this.first, rows: this.rows }; } getBlockableElement() { return this.el.nativeElement.children[0]; } scrollTo(index) { if (this.viewPortViewChild && this.viewPortViewChild['elementRef'] && this.viewPortViewChild['elementRef'].nativeElement) { this.viewPortViewChild['elementRef'].nativeElement.scrollTop = index * this.itemSize; } } }; VirtualScroller.ctorParameters = () => [ { type: ElementRef } ]; __decorate([ Input() ], VirtualScroller.prototype, "itemSize", void 0); __decorate([ Input() ], VirtualScroller.prototype, "style", void 0); __decorate([ Input() ], VirtualScroller.prototype, "styleClass", void 0); __decorate([ Input() ], VirtualScroller.prototype, "scrollHeight", void 0); __decorate([ Input() ], VirtualScroller.prototype, "lazy", void 0); __decorate([ Input() ], VirtualScroller.prototype, "cache", void 0); __decorate([ Input() ], VirtualScroller.prototype, "rows", void 0); __decorate([ Input() ], VirtualScroller.prototype, "first", void 0); __decorate([ Input() ], VirtualScroller.prototype, "trackBy", void 0); __decorate([ ContentChild(Header) ], VirtualScroller.prototype, "header", void 0); __decorate([ ContentChild(Footer) ], VirtualScroller.prototype, "footer", void 0); __decorate([ ContentChildren(PrimeTemplate) ], VirtualScroller.prototype, "templates", void 0); __decorate([ ViewChild('viewport') ], VirtualScroller.prototype, "viewPortViewChild", void 0); __decorate([ Output() ], VirtualScroller.prototype, "onLazyLoad", void 0); __decorate([ Input() ], VirtualScroller.prototype, "totalRecords", null); __decorate([ Input() ], VirtualScroller.prototype, "value", null); VirtualScroller = __decorate([ Component({ selector: 'p-virtualScroller', template: ` <div [ngClass]="'ui-virtualscroller ui-widget'" [ngStyle]="style" [class]="styleClass"> <div class="ui-virtualscroller-header ui-widget-header ui-corner-top" *ngIf="header"> <ng-content select="p-header"></ng-content> </div> <div #content class="ui-virtualscroller-content ui-widget-content"> <ul class="ui-virtualscroller-list"> <cdk-virtual-scroll-viewport #viewport [ngStyle]="{'height': scrollHeight}" [itemSize]="itemSize" (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; "> <li [ngStyle]="{'height': itemSize + 'px'}"> <ng-container *ngTemplateOutlet="item ? itemTemplate : loadingItemTemplate; context: {$implicit: item, index: i, count: c, first: f, last: l, even: e, odd: o}"></ng-container> </li> </ng-container> </cdk-virtual-scroll-viewport> </ul> </div> <div class="ui-virtualscroller-footer ui-widget-header ui-corner-bottom" *ngIf="footer"> <ng-content select="p-footer"></ng-content> </div> </div> `, changeDetection: ChangeDetectionStrategy.Default }) ], VirtualScroller); let VirtualScrollerModule = class VirtualScrollerModule { }; VirtualScrollerModule = __decorate([ NgModule({ imports: [CommonModule, ScrollingModule], exports: [VirtualScroller, SharedModule, ScrollingModule], declarations: [VirtualScroller] }) ], VirtualScrollerModule); /** * Generated bundle index. Do not edit. */ export { VirtualScroller, VirtualScrollerModule }; //# sourceMappingURL=primeng-virtualscroller.js.map