UNPKG

ng2-bootstrap

Version:
230 lines 9.38 kB
import { Component, ElementRef, Renderer, Input, Output, EventEmitter, forwardRef } from '@angular/core'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; import { PaginationConfig } from './pagination.config'; export var PAGER_CONTROL_VALUE_ACCESSOR = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(function () { return PagerComponent; }), multi: true }; var PAGER_TEMPLATE = "\n <ul class=\"pager\">\n <li [class.disabled]=\"noPrevious()\" [class.previous]=\"align\" [ngClass]=\"{'pull-right': align}\" class=\"{{ pageBtnClass }}\">\n <a href (click)=\"selectPage(page - 1, $event)\">{{getText('previous')}}</a>\n </li>\n <li [class.disabled]=\"noNext()\" [class.next]=\"align\" [ngClass]=\"{'pull-right': align}\" class=\"{{ pageBtnClass }}\">\n <a href (click)=\"selectPage(page + 1, $event)\">{{getText('next')}}</a>\n </li>\n </ul>\n"; export var PagerComponent = (function () { function PagerComponent(renderer, elementRef, paginationConfig) { /** fired when total pages count changes, $event:number equals to total pages count */ this.numPages = new EventEmitter(); /** fired when page was changed, $event:{page, itemsPerPage} equals to object with current page index and number of items per page */ this.pageChanged = new EventEmitter(); this.onChange = Function.prototype; this.onTouched = Function.prototype; this.inited = false; this._page = 1; this.renderer = renderer; this.elementRef = elementRef; if (!this.config) { this.configureOptions(Object.assign({}, paginationConfig.main, paginationConfig.pager)); } } Object.defineProperty(PagerComponent.prototype, "itemsPerPage", { /** maximum number of items per page. If value less than 1 will display all items on one page */ get: function () { return this._itemsPerPage; }, set: function (v) { this._itemsPerPage = v; this.totalPages = this.calculateTotalPages(); }, enumerable: true, configurable: true }); Object.defineProperty(PagerComponent.prototype, "totalItems", { /** total number of items in all pages */ get: function () { return this._totalItems; }, set: function (v) { this._totalItems = v; this.totalPages = this.calculateTotalPages(); }, enumerable: true, configurable: true }); Object.defineProperty(PagerComponent.prototype, "totalPages", { get: function () { return this._totalPages; }, set: function (v) { this._totalPages = v; this.numPages.emit(v); if (this.inited) { this.selectPage(this.page); } }, enumerable: true, configurable: true }); Object.defineProperty(PagerComponent.prototype, "page", { get: function () { return this._page; }, set: function (value) { var _previous = this._page; this._page = (value > this.totalPages) ? this.totalPages : (value || 1); if (_previous === this._page || typeof _previous === 'undefined') { return; } this.pageChanged.emit({ page: this._page, itemsPerPage: this.itemsPerPage }); }, enumerable: true, configurable: true }); PagerComponent.prototype.configureOptions = function (config) { this.config = Object.assign({}, config); }; PagerComponent.prototype.ngOnInit = function () { this.classMap = this.elementRef.nativeElement.getAttribute('class') || ''; // watch for maxSize this.maxSize = typeof this.maxSize !== 'undefined' ? this.maxSize : this.config.maxSize; this.rotate = typeof this.rotate !== 'undefined' ? this.rotate : this.config.rotate; this.boundaryLinks = typeof this.boundaryLinks !== 'undefined' ? this.boundaryLinks : this.config.boundaryLinks; this.directionLinks = typeof this.directionLinks !== 'undefined' ? this.directionLinks : this.config.directionLinks; this.pageBtnClass = typeof this.pageBtnClass !== 'undefined' ? this.pageBtnClass : this.config.pageBtnClass; // base class this.itemsPerPage = typeof this.itemsPerPage !== 'undefined' ? this.itemsPerPage : this.config.itemsPerPage; this.totalPages = this.calculateTotalPages(); // this class this.pages = this.getPages(this.page, this.totalPages); this.inited = true; }; PagerComponent.prototype.writeValue = function (value) { this.page = value; this.pages = this.getPages(this.page, this.totalPages); }; PagerComponent.prototype.getText = function (key) { return this[key + 'Text'] || this.config[key + 'Text']; }; PagerComponent.prototype.noPrevious = function () { return this.page === 1; }; PagerComponent.prototype.noNext = function () { return this.page === this.totalPages; }; PagerComponent.prototype.registerOnChange = function (fn) { this.onChange = fn; }; PagerComponent.prototype.registerOnTouched = function (fn) { this.onTouched = fn; }; PagerComponent.prototype.selectPage = function (page, event) { if (event) { event.preventDefault(); } if (!this.disabled) { if (event && event.target) { var target = event.target; target.blur(); } this.writeValue(page); this.onChange(this.page); } }; // Create page object used in template PagerComponent.prototype.makePage = function (num, text, active) { return { text: text, number: num, active: active }; }; PagerComponent.prototype.getPages = function (currentPage, totalPages) { var pages = []; // Default page limits var startPage = 1; var endPage = totalPages; var isMaxSized = typeof this.maxSize !== 'undefined' && this.maxSize < totalPages; // recompute if maxSize if (isMaxSized) { if (this.rotate) { // Current page is displayed in the middle of the visible ones startPage = Math.max(currentPage - Math.floor(this.maxSize / 2), 1); endPage = startPage + this.maxSize - 1; // Adjust if limit is exceeded if (endPage > totalPages) { endPage = totalPages; startPage = endPage - this.maxSize + 1; } } else { // Visible pages are paginated with maxSize startPage = ((Math.ceil(currentPage / this.maxSize) - 1) * this.maxSize) + 1; // Adjust last page if limit is exceeded endPage = Math.min(startPage + this.maxSize - 1, totalPages); } } // Add page number links for (var num = startPage; num <= endPage; num++) { var page = this.makePage(num, num.toString(), num === currentPage); pages.push(page); } // Add links to move between page sets if (isMaxSized && !this.rotate) { if (startPage > 1) { var previousPageSet = this.makePage(startPage - 1, '...', false); pages.unshift(previousPageSet); } if (endPage < totalPages) { var nextPageSet = this.makePage(endPage + 1, '...', false); pages.push(nextPageSet); } } return pages; }; // base class PagerComponent.prototype.calculateTotalPages = function () { var totalPages = this.itemsPerPage < 1 ? 1 : Math.ceil(this.totalItems / this.itemsPerPage); return Math.max(totalPages || 0, 1); }; PagerComponent.decorators = [ { type: Component, args: [{ selector: 'pager', template: PAGER_TEMPLATE, providers: [PAGER_CONTROL_VALUE_ACCESSOR] },] }, ]; /** @nocollapse */ PagerComponent.ctorParameters = function () { return [ { type: Renderer, }, { type: ElementRef, }, { type: PaginationConfig, }, ]; }; PagerComponent.propDecorators = { 'align': [{ type: Input },], 'maxSize': [{ type: Input },], 'boundaryLinks': [{ type: Input },], 'directionLinks': [{ type: Input },], 'firstText': [{ type: Input },], 'previousText': [{ type: Input },], 'nextText': [{ type: Input },], 'lastText': [{ type: Input },], 'rotate': [{ type: Input },], 'pageBtnClass': [{ type: Input },], 'disabled': [{ type: Input },], 'numPages': [{ type: Output },], 'pageChanged': [{ type: Output },], 'itemsPerPage': [{ type: Input },], 'totalItems': [{ type: Input },], }; return PagerComponent; }()); //# sourceMappingURL=pager.component.js.map