ngx-bootstrap
Version:
Native Angular Bootstrap Components
249 lines • 9.93 kB
JavaScript
import { Component, ElementRef, Renderer2, Input, Output, EventEmitter, forwardRef, ChangeDetectorRef } 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,
// tslint:disable-next-line
useExisting: forwardRef(function () { return PagerComponent; }),
multi: true
};
var PagerComponent = /** @class */ (function () {
function PagerComponent(renderer, elementRef, paginationConfig, changeDetection) {
this.renderer = renderer;
this.elementRef = elementRef;
this.changeDetection = changeDetection;
/** 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", {
get: /** maximum number of items per page. If value less than 1 will display all items on one page */
function () {
return this._itemsPerPage;
},
set: function (v) {
this._itemsPerPage = v;
this.totalPages = this.calculateTotalPages();
},
enumerable: true,
configurable: true
});
Object.defineProperty(PagerComponent.prototype, "totalItems", {
get: /** total number of items in all pages */
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;
this.changeDetection.markForCheck();
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 () {
if (typeof window !== 'undefined') {
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
// Create page object used in template
PagerComponent.prototype.makePage =
// Create page object used in template
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
// base class
PagerComponent.prototype.calculateTotalPages =
// base class
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: "<ul class=\"pager\"> <li [class.disabled]=\"noPrevious()\" [class.previous]=\"align\" [ngClass]=\"{'pull-right': align, 'float-right': align}\" class=\"{{ pageBtnClass }}\"> <a href (click)=\"selectPage(page - 1, $event)\">{{ getText('previous') }}</a> </li> <li [class.disabled]=\"noNext()\" [class.next]=\"align\" [ngClass]=\"{'pull-right': align, 'float-right': align}\" class=\"{{ pageBtnClass }}\"> <a href (click)=\"selectPage(page + 1, $event)\">{{ getText('next') }}</a> </li> </ul> ",
providers: [PAGER_CONTROL_VALUE_ACCESSOR]
},] },
];
/** @nocollapse */
PagerComponent.ctorParameters = function () { return [
{ type: Renderer2, },
{ type: ElementRef, },
{ type: PaginationConfig, },
{ type: ChangeDetectorRef, },
]; };
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;
}());
export { PagerComponent };
//# sourceMappingURL=pager.component.js.map