UNPKG

angular-9-datatable

Version:

DataTable component for Angular 9 framework

348 lines (339 loc) 13.5 kB
import { __decorate, __metadata, __param } from 'tslib'; import { EventEmitter, IterableDiffers, Input, Output, Directive, Component, Optional, NgModule } from '@angular/core'; import { includes, orderBy, slice, min } from 'lodash'; import { ReplaySubject } from 'rxjs'; import { CommonModule } from '@angular/common'; let DataTable = class DataTable { constructor(differs) { this.differs = differs; this.inputData = []; this.sortBy = ""; this.sortOrder = "asc"; this.sortByChange = new EventEmitter(); this.sortOrderChange = new EventEmitter(); this.rowsOnPage = 1000; this.activePage = 1; this.mustRecalculateData = false; this.onSortChange = new ReplaySubject(1); this.onPageChange = new EventEmitter(); this.diff = differs.find([]).create(null); } getSort() { return { sortBy: this.sortBy, sortOrder: this.sortOrder }; } setSort(sortBy, sortOrder) { if (this.sortBy !== sortBy || this.sortOrder !== sortOrder) { this.sortBy = sortBy; this.sortOrder = includes(["asc", "desc"], sortOrder) ? sortOrder : "asc"; this.mustRecalculateData = true; this.onSortChange.next({ sortBy: sortBy, sortOrder: sortOrder }); this.sortByChange.emit(this.sortBy); this.sortOrderChange.emit(this.sortOrder); } } getPage() { return { activePage: this.activePage, rowsOnPage: this.rowsOnPage, dataLength: this.inputData.length }; } setPage(activePage, rowsOnPage) { if (this.rowsOnPage !== rowsOnPage || this.activePage !== activePage) { this.activePage = this.activePage !== activePage ? activePage : this.calculateNewActivePage(this.rowsOnPage, rowsOnPage); this.rowsOnPage = rowsOnPage; this.mustRecalculateData = true; this.onPageChange.emit({ activePage: this.activePage, rowsOnPage: this.rowsOnPage, dataLength: this.inputData ? this.inputData.length : 0 }); } } calculateNewActivePage(previousRowsOnPage, currentRowsOnPage) { let firstRowOnPage = (this.activePage - 1) * previousRowsOnPage + 1; let newActivePage = Math.ceil(firstRowOnPage / currentRowsOnPage); return newActivePage; } recalculatePage() { let lastPage = Math.ceil(this.inputData.length / this.rowsOnPage); this.activePage = lastPage < this.activePage ? lastPage : this.activePage; this.activePage = this.activePage || 1; this.onPageChange.emit({ activePage: this.activePage, rowsOnPage: this.rowsOnPage, dataLength: this.inputData.length }); } ngOnChanges(changes) { if (changes["rowsOnPage"]) { this.rowsOnPage = changes["rowsOnPage"].previousValue; this.setPage(this.activePage, changes["rowsOnPage"].currentValue); this.mustRecalculateData = true; } if (changes["sortBy"] || changes["sortOrder"]) { if (!includes(["asc", "desc"], this.sortOrder)) { console.warn("angular2-datatable: value for input mfSortOrder must be one of ['asc', 'desc'], but is:", this.sortOrder); this.sortOrder = "asc"; } if (this.sortBy) { this.onSortChange.next({ sortBy: this.sortBy, sortOrder: this.sortOrder }); } this.mustRecalculateData = true; } if (changes["inputData"]) { this.inputData = changes["inputData"].currentValue || []; this.recalculatePage(); this.mustRecalculateData = true; } } ngDoCheck() { let changes = this.diff.diff(this.inputData); if (changes) { this.recalculatePage(); this.mustRecalculateData = true; } if (this.mustRecalculateData) { this.fillData(); this.mustRecalculateData = false; } } fillData() { this.activePage = this.activePage; this.rowsOnPage = this.rowsOnPage; let offset = (this.activePage - 1) * this.rowsOnPage; let data = this.inputData; var sortBy = this.sortBy; if (typeof sortBy === 'string' || sortBy instanceof String) { data = orderBy(data, this.caseInsensitiveIteratee(sortBy), this.sortOrder == 'asc' ? 'asc' : 'desc'); } else { data = orderBy(data, sortBy, this.sortOrder == 'asc' ? 'asc' : 'desc'); } data = slice(data, offset, offset + this.rowsOnPage); this.data = data; } caseInsensitiveIteratee(sortBy) { return (row) => { var value = row; for (let sortByProperty of sortBy.split('.')) { if (value) { value = value[sortByProperty]; } } if (value && typeof value === 'string' || value instanceof String) { return value.toLowerCase(); } return value; }; } }; DataTable.ctorParameters = () => [ { type: IterableDiffers } ]; __decorate([ Input("mfData"), __metadata("design:type", Array) ], DataTable.prototype, "inputData", void 0); __decorate([ Input("mfSortBy"), __metadata("design:type", Object) ], DataTable.prototype, "sortBy", void 0); __decorate([ Input("mfSortOrder"), __metadata("design:type", Object) ], DataTable.prototype, "sortOrder", void 0); __decorate([ Output("mfSortByChange"), __metadata("design:type", Object) ], DataTable.prototype, "sortByChange", void 0); __decorate([ Output("mfSortOrderChange"), __metadata("design:type", Object) ], DataTable.prototype, "sortOrderChange", void 0); __decorate([ Input("mfRowsOnPage"), __metadata("design:type", Object) ], DataTable.prototype, "rowsOnPage", void 0); __decorate([ Input("mfActivePage"), __metadata("design:type", Object) ], DataTable.prototype, "activePage", void 0); DataTable = __decorate([ Directive({ selector: 'table[mfData]', exportAs: 'mfDataTable' }), __metadata("design:paramtypes", [IterableDiffers]) ], DataTable); let DefaultSorter = class DefaultSorter { constructor(mfTable) { this.mfTable = mfTable; this.isSortedByMeAsc = false; this.isSortedByMeDesc = false; } ngOnInit() { this.mfTable.onSortChange.subscribe((event) => { this.isSortedByMeAsc = (event.sortBy == this.sortBy && event.sortOrder == "asc"); this.isSortedByMeDesc = (event.sortBy == this.sortBy && event.sortOrder == "desc"); }); } sort() { if (this.isSortedByMeAsc) { this.mfTable.setSort(this.sortBy, "desc"); } else { this.mfTable.setSort(this.sortBy, "asc"); } } }; DefaultSorter.ctorParameters = () => [ { type: DataTable } ]; __decorate([ Input("by"), __metadata("design:type", String) ], DefaultSorter.prototype, "sortBy", void 0); DefaultSorter = __decorate([ Component({ selector: "mfDefaultSorter", template: ` <a style="cursor: pointer" (click)="sort()" class="text-nowrap"> <ng-content></ng-content> <span *ngIf="isSortedByMeAsc" class="glyphicon glyphicon-triangle-top fa fa-sort-up" aria-hidden="true"></span> <span *ngIf="isSortedByMeDesc" class="glyphicon glyphicon-triangle-bottom fa fa-sort-down" aria-hidden="true"></span> </a>` }), __metadata("design:paramtypes", [DataTable]) ], DefaultSorter); let Paginator = class Paginator { constructor(injectMfTable) { this.injectMfTable = injectMfTable; this.dataLength = 0; this.onPageChangeSubscriber = (event) => { this.activePage = event.activePage; this.rowsOnPage = event.rowsOnPage; this.dataLength = event.dataLength; this.lastPage = Math.ceil(this.dataLength / this.rowsOnPage); }; } ngOnChanges(changes) { this.mfTable = this.inputMfTable || this.injectMfTable; this.onPageChangeSubscriber(this.mfTable.getPage()); this.mfTable.onPageChange.subscribe(this.onPageChangeSubscriber); } setPage(pageNumber) { this.mfTable.setPage(pageNumber, this.rowsOnPage); } setRowsOnPage(rowsOnPage) { this.mfTable.setPage(this.activePage, rowsOnPage); } }; Paginator.ctorParameters = () => [ { type: DataTable, decorators: [{ type: Optional }] } ]; __decorate([ Input("mfTable"), __metadata("design:type", DataTable) ], Paginator.prototype, "inputMfTable", void 0); Paginator = __decorate([ Component({ selector: "mfPaginator", template: `<ng-content></ng-content>` }), __param(0, Optional()), __metadata("design:paramtypes", [DataTable]) ], Paginator); let BootstrapPaginator = class BootstrapPaginator { constructor() { this.rowsOnPageSet = []; this.minRowsOnPage = 0; } ngOnChanges(changes) { if (changes.rowsOnPageSet) { this.minRowsOnPage = min(this.rowsOnPageSet); } } }; __decorate([ Input("rowsOnPageSet"), __metadata("design:type", Object) ], BootstrapPaginator.prototype, "rowsOnPageSet", void 0); __decorate([ Input("mfTable"), __metadata("design:type", DataTable) ], BootstrapPaginator.prototype, "mfTable", void 0); BootstrapPaginator = __decorate([ Component({ selector: "mfBootstrapPaginator", template: ` <mfPaginator #p [mfTable]="mfTable"> <ul class="pagination" *ngIf="p.dataLength > p.rowsOnPage"> <li class="page-item" [class.disabled]="p.activePage <= 1" (click)="p.setPage(1)"> <a class="page-link" style="cursor: pointer">&laquo;</a> </li> <li class="page-item" *ngIf="p.activePage > 4 && p.activePage + 1 > p.lastPage" (click)="p.setPage(p.activePage - 4)"> <a class="page-link" style="cursor: pointer">{{p.activePage-4}}</a> </li> <li class="page-item" *ngIf="p.activePage > 3 && p.activePage + 2 > p.lastPage" (click)="p.setPage(p.activePage - 3)"> <a class="page-link" style="cursor: pointer">{{p.activePage-3}}</a> </li> <li class="page-item" *ngIf="p.activePage > 2" (click)="p.setPage(p.activePage - 2)"> <a class="page-link" style="cursor: pointer">{{p.activePage-2}}</a> </li> <li class="page-item" *ngIf="p.activePage > 1" (click)="p.setPage(p.activePage - 1)"> <a class="page-link" style="cursor: pointer">{{p.activePage-1}}</a> </li> <li class="page-item active"> <a class="page-link" style="cursor: pointer">{{p.activePage}}</a> </li> <li class="page-item" *ngIf="p.activePage + 1 <= p.lastPage" (click)="p.setPage(p.activePage + 1)"> <a class="page-link" style="cursor: pointer">{{p.activePage+1}}</a> </li> <li class="page-item" *ngIf="p.activePage + 2 <= p.lastPage" (click)="p.setPage(p.activePage + 2)"> <a class="page-link" style="cursor: pointer">{{p.activePage+2}}</a> </li> <li class="page-item" *ngIf="p.activePage + 3 <= p.lastPage && p.activePage < 3" (click)="p.setPage(p.activePage + 3)"> <a class="page-link" style="cursor: pointer">{{p.activePage+3}}</a> </li> <li class="page-item" *ngIf="p.activePage + 4 <= p.lastPage && p.activePage < 2" (click)="p.setPage(p.activePage + 4)"> <a class="page-link" style="cursor: pointer">{{p.activePage+4}}</a> </li> <li class="page-item" [class.disabled]="p.activePage >= p.lastPage" (click)="p.setPage(p.lastPage)"> <a class="page-link" style="cursor: pointer">&raquo;</a> </li> </ul> <ul class="pagination pull-right float-sm-right" *ngIf="p.dataLength > minRowsOnPage"> <li class="page-item" *ngFor="let rows of rowsOnPageSet" [class.active]="p.rowsOnPage===rows" (click)="p.setRowsOnPage(rows)"> <a class="page-link" style="cursor: pointer">{{rows}}</a> </li> </ul> </mfPaginator> ` }) ], BootstrapPaginator); let NgxDataTableModule = class NgxDataTableModule { }; NgxDataTableModule = __decorate([ NgModule({ imports: [ CommonModule ], declarations: [ DataTable, DefaultSorter, Paginator, BootstrapPaginator ], exports: [ DataTable, DefaultSorter, Paginator, BootstrapPaginator ] }) ], NgxDataTableModule); /* * Public API Surface of data-table */ /** * Generated bundle index. Do not edit. */ export { BootstrapPaginator, DataTable, DefaultSorter, NgxDataTableModule, Paginator }; //# sourceMappingURL=angular-9-datatable.js.map