UNPKG

primeng

Version:

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Join the chat at https://gitter.im/primefaces/primeng](https://badges.gitter.im/primefaces/primeng.svg)](https://gitter.im/primefaces/primeng?ut

231 lines 11.2 kB
"use strict"; 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; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); var core_1 = require("@angular/core"); var common_1 = require("@angular/common"); var forms_1 = require("@angular/forms"); var dropdown_1 = require("../dropdown/dropdown"); var shared_1 = require("../common/shared"); var Paginator = /** @class */ (function () { function Paginator() { this.pageLinkSize = 5; this.onPageChange = new core_1.EventEmitter(); this.alwaysShow = true; this._totalRecords = 0; this._first = 0; this._rows = 0; } Paginator.prototype.ngOnInit = function () { this.updatePaginatorState(); }; Object.defineProperty(Paginator.prototype, "totalRecords", { get: function () { return this._totalRecords; }, set: function (val) { this._totalRecords = val; this.updatePageLinks(); }, enumerable: true, configurable: true }); Object.defineProperty(Paginator.prototype, "first", { get: function () { return this._first; }, set: function (val) { this._first = val; this.updatePageLinks(); }, enumerable: true, configurable: true }); Object.defineProperty(Paginator.prototype, "rows", { get: function () { return this._rows; }, set: function (val) { this._rows = val; this.updatePageLinks(); }, enumerable: true, configurable: true }); Object.defineProperty(Paginator.prototype, "rowsPerPageOptions", { get: function () { return this._rowsPerPageOptions; }, set: function (val) { this._rowsPerPageOptions = val; if (this._rowsPerPageOptions) { this.rowsPerPageItems = []; for (var _i = 0, _a = this._rowsPerPageOptions; _i < _a.length; _i++) { var opt = _a[_i]; this.rowsPerPageItems.push({ label: String(opt), value: opt }); } } }, enumerable: true, configurable: true }); Paginator.prototype.isFirstPage = function () { return this.getPage() === 0; }; Paginator.prototype.isLastPage = function () { return this.getPage() === this.getPageCount() - 1; }; Paginator.prototype.getPageCount = function () { return Math.ceil(this.totalRecords / this.rows) || 1; }; Paginator.prototype.calculatePageLinkBoundaries = function () { var numberOfPages = this.getPageCount(), visiblePages = Math.min(this.pageLinkSize, numberOfPages); //calculate range, keep current in middle if necessary var start = Math.max(0, Math.ceil(this.getPage() - ((visiblePages) / 2))), end = Math.min(numberOfPages - 1, start + visiblePages - 1); //check when approaching to last page var delta = this.pageLinkSize - (end - start + 1); start = Math.max(0, start - delta); return [start, end]; }; Paginator.prototype.updatePageLinks = function () { this.pageLinks = []; var boundaries = this.calculatePageLinkBoundaries(), start = boundaries[0], end = boundaries[1]; for (var i = start; i <= end; i++) { this.pageLinks.push(i + 1); } }; Paginator.prototype.changePage = function (p) { var pc = this.getPageCount(); if (p >= 0 && p < pc) { this.first = this.rows * p; var state = { page: p, first: this.first, rows: this.rows, pageCount: pc }; this.updatePageLinks(); this.onPageChange.emit(state); this.updatePaginatorState(); } }; Paginator.prototype.getPage = function () { return Math.floor(this.first / this.rows); }; Paginator.prototype.changePageToFirst = function (event) { if (!this.isFirstPage()) { this.changePage(0); } event.preventDefault(); }; Paginator.prototype.changePageToPrev = function (event) { this.changePage(this.getPage() - 1); event.preventDefault(); }; Paginator.prototype.changePageToNext = function (event) { this.changePage(this.getPage() + 1); event.preventDefault(); }; Paginator.prototype.changePageToLast = function (event) { if (!this.isLastPage()) { this.changePage(this.getPageCount() - 1); } event.preventDefault(); }; Paginator.prototype.onPageLinkClick = function (event, page) { this.changePage(page); event.preventDefault(); }; Paginator.prototype.onRppChange = function (event) { this.changePage(this.getPage()); }; Paginator.prototype.updatePaginatorState = function () { this.paginatorState = { page: this.getPage(), rows: this.rows, first: this.first, totalRecords: this.totalRecords }; }; __decorate([ core_1.Input(), __metadata("design:type", Number) ], Paginator.prototype, "pageLinkSize", void 0); __decorate([ core_1.Output(), __metadata("design:type", core_1.EventEmitter) ], Paginator.prototype, "onPageChange", void 0); __decorate([ core_1.Input(), __metadata("design:type", Object) ], Paginator.prototype, "style", void 0); __decorate([ core_1.Input(), __metadata("design:type", String) ], Paginator.prototype, "styleClass", void 0); __decorate([ core_1.Input(), __metadata("design:type", Boolean) ], Paginator.prototype, "alwaysShow", void 0); __decorate([ core_1.Input(), __metadata("design:type", core_1.TemplateRef) ], Paginator.prototype, "templateLeft", void 0); __decorate([ core_1.Input(), __metadata("design:type", core_1.TemplateRef) ], Paginator.prototype, "templateRight", void 0); __decorate([ core_1.Input(), __metadata("design:type", Object) ], Paginator.prototype, "dropdownAppendTo", void 0); __decorate([ core_1.Input(), __metadata("design:type", Number), __metadata("design:paramtypes", [Number]) ], Paginator.prototype, "totalRecords", null); __decorate([ core_1.Input(), __metadata("design:type", Number), __metadata("design:paramtypes", [Number]) ], Paginator.prototype, "first", null); __decorate([ core_1.Input(), __metadata("design:type", Number), __metadata("design:paramtypes", [Number]) ], Paginator.prototype, "rows", null); __decorate([ core_1.Input(), __metadata("design:type", Array), __metadata("design:paramtypes", [Array]) ], Paginator.prototype, "rowsPerPageOptions", null); Paginator = __decorate([ core_1.Component({ selector: 'p-paginator', template: "\n <div [class]=\"styleClass\" [ngStyle]=\"style\" [ngClass]=\"'ui-paginator ui-widget ui-widget-header ui-unselectable-text ui-helper-clearfix'\"\n *ngIf=\"alwaysShow ? true : (pageLinks && pageLinks.length > 1)\">\n <div class=\"ui-paginator-left-content\" *ngIf=\"templateLeft\">\n <ng-container *ngTemplateOutlet=\"templateLeft; context: {$implicit: paginatorState}\"></ng-container>\n </div>\n <a href=\"#\" class=\"ui-paginator-first ui-paginator-element ui-state-default ui-corner-all\"\n (click)=\"changePageToFirst($event)\" [ngClass]=\"{'ui-state-disabled':isFirstPage()}\" [tabindex]=\"isFirstPage() ? -1 : null\">\n <span class=\"ui-paginator-icon pi pi-step-backward\"></span>\n </a>\n <a href=\"#\" class=\"ui-paginator-prev ui-paginator-element ui-state-default ui-corner-all\"\n (click)=\"changePageToPrev($event)\" [ngClass]=\"{'ui-state-disabled':isFirstPage()}\" [tabindex]=\"isFirstPage() ? -1 : null\">\n <span class=\"ui-paginator-icon pi pi-caret-left\"></span>\n </a>\n <span class=\"ui-paginator-pages\">\n <a href=\"#\" *ngFor=\"let pageLink of pageLinks\" class=\"ui-paginator-page ui-paginator-element ui-state-default ui-corner-all\"\n (click)=\"onPageLinkClick($event, pageLink - 1)\" [ngClass]=\"{'ui-state-active': (pageLink-1 == getPage())}\">{{pageLink}}</a>\n </span>\n <a href=\"#\" class=\"ui-paginator-next ui-paginator-element ui-state-default ui-corner-all\"\n (click)=\"changePageToNext($event)\" [ngClass]=\"{'ui-state-disabled':isLastPage()}\" [tabindex]=\"isLastPage() ? -1 : null\">\n <span class=\"ui-paginator-icon pi pi-caret-right\"></span>\n </a>\n <a href=\"#\" class=\"ui-paginator-last ui-paginator-element ui-state-default ui-corner-all\"\n (click)=\"changePageToLast($event)\" [ngClass]=\"{'ui-state-disabled':isLastPage()}\" [tabindex]=\"isLastPage() ? -1 : null\">\n <span class=\"ui-paginator-icon pi pi-step-forward\"></span>\n </a>\n <p-dropdown [options]=\"rowsPerPageItems\" [(ngModel)]=\"rows\" *ngIf=\"rowsPerPageOptions\" \n (onChange)=\"onRppChange($event)\" [autoWidth]=\"false\" [appendTo]=\"dropdownAppendTo\"></p-dropdown>\n <div class=\"ui-paginator-right-content\" *ngIf=\"templateRight\">\n <ng-container *ngTemplateOutlet=\"templateRight; context: {$implicit: paginatorState}\"></ng-container>\n </div>\n </div>\n " }) ], Paginator); return Paginator; }()); exports.Paginator = Paginator; var PaginatorModule = /** @class */ (function () { function PaginatorModule() { } PaginatorModule = __decorate([ core_1.NgModule({ imports: [common_1.CommonModule, dropdown_1.DropdownModule, forms_1.FormsModule, shared_1.SharedModule], exports: [Paginator, dropdown_1.DropdownModule, forms_1.FormsModule, shared_1.SharedModule], declarations: [Paginator] }) ], PaginatorModule); return PaginatorModule; }()); exports.PaginatorModule = PaginatorModule; //# sourceMappingURL=paginator.js.map