UNPKG

angular-9-datatable

Version:

DataTable component for Angular 9 framework

320 lines (311 loc) 15.4 kB
import { __values, __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'; var DataTable = /** @class */ (function () { function DataTable(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); } DataTable.prototype.getSort = function () { return { sortBy: this.sortBy, sortOrder: this.sortOrder }; }; DataTable.prototype.setSort = function (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); } }; DataTable.prototype.getPage = function () { return { activePage: this.activePage, rowsOnPage: this.rowsOnPage, dataLength: this.inputData.length }; }; DataTable.prototype.setPage = function (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 }); } }; DataTable.prototype.calculateNewActivePage = function (previousRowsOnPage, currentRowsOnPage) { var firstRowOnPage = (this.activePage - 1) * previousRowsOnPage + 1; var newActivePage = Math.ceil(firstRowOnPage / currentRowsOnPage); return newActivePage; }; DataTable.prototype.recalculatePage = function () { var 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 }); }; DataTable.prototype.ngOnChanges = function (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; } }; DataTable.prototype.ngDoCheck = function () { var changes = this.diff.diff(this.inputData); if (changes) { this.recalculatePage(); this.mustRecalculateData = true; } if (this.mustRecalculateData) { this.fillData(); this.mustRecalculateData = false; } }; DataTable.prototype.fillData = function () { this.activePage = this.activePage; this.rowsOnPage = this.rowsOnPage; var offset = (this.activePage - 1) * this.rowsOnPage; var 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; }; DataTable.prototype.caseInsensitiveIteratee = function (sortBy) { return function (row) { var e_1, _a; var value = row; try { for (var _b = __values(sortBy.split('.')), _c = _b.next(); !_c.done; _c = _b.next()) { var sortByProperty = _c.value; if (value) { value = value[sortByProperty]; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } if (value && typeof value === 'string' || value instanceof String) { return value.toLowerCase(); } return value; }; }; DataTable.ctorParameters = function () { return [ { 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); return DataTable; }()); var DefaultSorter = /** @class */ (function () { function DefaultSorter(mfTable) { this.mfTable = mfTable; this.isSortedByMeAsc = false; this.isSortedByMeDesc = false; } DefaultSorter.prototype.ngOnInit = function () { var _this = this; this.mfTable.onSortChange.subscribe(function (event) { _this.isSortedByMeAsc = (event.sortBy == _this.sortBy && event.sortOrder == "asc"); _this.isSortedByMeDesc = (event.sortBy == _this.sortBy && event.sortOrder == "desc"); }); }; DefaultSorter.prototype.sort = function () { if (this.isSortedByMeAsc) { this.mfTable.setSort(this.sortBy, "desc"); } else { this.mfTable.setSort(this.sortBy, "asc"); } }; DefaultSorter.ctorParameters = function () { return [ { type: DataTable } ]; }; __decorate([ Input("by"), __metadata("design:type", String) ], DefaultSorter.prototype, "sortBy", void 0); DefaultSorter = __decorate([ Component({ selector: "mfDefaultSorter", template: "\n <a style=\"cursor: pointer\" (click)=\"sort()\" class=\"text-nowrap\">\n <ng-content></ng-content>\n <span *ngIf=\"isSortedByMeAsc\" class=\"glyphicon glyphicon-triangle-top fa fa-sort-up\" aria-hidden=\"true\"></span>\n <span *ngIf=\"isSortedByMeDesc\" class=\"glyphicon glyphicon-triangle-bottom fa fa-sort-down\" aria-hidden=\"true\"></span>\n </a>" }), __metadata("design:paramtypes", [DataTable]) ], DefaultSorter); return DefaultSorter; }()); var Paginator = /** @class */ (function () { function Paginator(injectMfTable) { var _this = this; this.injectMfTable = injectMfTable; this.dataLength = 0; this.onPageChangeSubscriber = function (event) { _this.activePage = event.activePage; _this.rowsOnPage = event.rowsOnPage; _this.dataLength = event.dataLength; _this.lastPage = Math.ceil(_this.dataLength / _this.rowsOnPage); }; } Paginator.prototype.ngOnChanges = function (changes) { this.mfTable = this.inputMfTable || this.injectMfTable; this.onPageChangeSubscriber(this.mfTable.getPage()); this.mfTable.onPageChange.subscribe(this.onPageChangeSubscriber); }; Paginator.prototype.setPage = function (pageNumber) { this.mfTable.setPage(pageNumber, this.rowsOnPage); }; Paginator.prototype.setRowsOnPage = function (rowsOnPage) { this.mfTable.setPage(this.activePage, rowsOnPage); }; Paginator.ctorParameters = function () { return [ { 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); return Paginator; }()); var BootstrapPaginator = /** @class */ (function () { function BootstrapPaginator() { this.rowsOnPageSet = []; this.minRowsOnPage = 0; } BootstrapPaginator.prototype.ngOnChanges = function (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: "\n <mfPaginator #p [mfTable]=\"mfTable\">\n <ul class=\"pagination\" *ngIf=\"p.dataLength > p.rowsOnPage\">\n <li class=\"page-item\" [class.disabled]=\"p.activePage <= 1\" (click)=\"p.setPage(1)\">\n <a class=\"page-link\" style=\"cursor: pointer\">&laquo;</a>\n </li>\n <li class=\"page-item\" *ngIf=\"p.activePage > 4 && p.activePage + 1 > p.lastPage\" (click)=\"p.setPage(p.activePage - 4)\">\n <a class=\"page-link\" style=\"cursor: pointer\">{{p.activePage-4}}</a>\n </li>\n <li class=\"page-item\" *ngIf=\"p.activePage > 3 && p.activePage + 2 > p.lastPage\" (click)=\"p.setPage(p.activePage - 3)\">\n <a class=\"page-link\" style=\"cursor: pointer\">{{p.activePage-3}}</a>\n </li>\n <li class=\"page-item\" *ngIf=\"p.activePage > 2\" (click)=\"p.setPage(p.activePage - 2)\">\n <a class=\"page-link\" style=\"cursor: pointer\">{{p.activePage-2}}</a>\n </li>\n <li class=\"page-item\" *ngIf=\"p.activePage > 1\" (click)=\"p.setPage(p.activePage - 1)\">\n <a class=\"page-link\" style=\"cursor: pointer\">{{p.activePage-1}}</a>\n </li>\n <li class=\"page-item active\">\n <a class=\"page-link\" style=\"cursor: pointer\">{{p.activePage}}</a>\n </li>\n <li class=\"page-item\" *ngIf=\"p.activePage + 1 <= p.lastPage\" (click)=\"p.setPage(p.activePage + 1)\">\n <a class=\"page-link\" style=\"cursor: pointer\">{{p.activePage+1}}</a>\n </li>\n <li class=\"page-item\" *ngIf=\"p.activePage + 2 <= p.lastPage\" (click)=\"p.setPage(p.activePage + 2)\">\n <a class=\"page-link\" style=\"cursor: pointer\">{{p.activePage+2}}</a>\n </li>\n <li class=\"page-item\" *ngIf=\"p.activePage + 3 <= p.lastPage && p.activePage < 3\" (click)=\"p.setPage(p.activePage + 3)\">\n <a class=\"page-link\" style=\"cursor: pointer\">{{p.activePage+3}}</a>\n </li>\n <li class=\"page-item\" *ngIf=\"p.activePage + 4 <= p.lastPage && p.activePage < 2\" (click)=\"p.setPage(p.activePage + 4)\">\n <a class=\"page-link\" style=\"cursor: pointer\">{{p.activePage+4}}</a>\n </li>\n <li class=\"page-item\" [class.disabled]=\"p.activePage >= p.lastPage\" (click)=\"p.setPage(p.lastPage)\">\n <a class=\"page-link\" style=\"cursor: pointer\">&raquo;</a>\n </li>\n </ul>\n <ul class=\"pagination pull-right float-sm-right\" *ngIf=\"p.dataLength > minRowsOnPage\">\n <li class=\"page-item\" *ngFor=\"let rows of rowsOnPageSet\" [class.active]=\"p.rowsOnPage===rows\" (click)=\"p.setRowsOnPage(rows)\">\n <a class=\"page-link\" style=\"cursor: pointer\">{{rows}}</a>\n </li>\n </ul>\n </mfPaginator>\n " }) ], BootstrapPaginator); return BootstrapPaginator; }()); var NgxDataTableModule = /** @class */ (function () { function NgxDataTableModule() { } NgxDataTableModule = __decorate([ NgModule({ imports: [ CommonModule ], declarations: [ DataTable, DefaultSorter, Paginator, BootstrapPaginator ], exports: [ DataTable, DefaultSorter, Paginator, BootstrapPaginator ] }) ], NgxDataTableModule); return 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