ng-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
381 lines (375 loc) • 17.4 kB
JavaScript
import { __decorate, __metadata } from 'tslib';
import { EventEmitter, Component, ViewEncapsulation, ChangeDetectionStrategy, ChangeDetectorRef, Output, Input, ViewChild, NgModule } from '@angular/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { toNumber, isInteger, InputBoolean, InputNumber } from 'ng-zorro-antd/core';
import { NzI18nService, NzI18nModule } from 'ng-zorro-antd/i18n';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { NzSelectModule } from 'ng-zorro-antd/select';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @record
*/
function PaginationItemRenderContext() { }
if (false) {
/** @type {?} */
PaginationItemRenderContext.prototype.$implicit;
/** @type {?} */
PaginationItemRenderContext.prototype.page;
}
class NzPaginationComponent {
/**
* @param {?} i18n
* @param {?} cdr
*/
constructor(i18n, cdr) {
this.i18n = i18n;
this.cdr = cdr;
// tslint:disable-next-line:no-any
this.locale = {};
this.firstIndex = 1;
this.pages = [];
this.$destroy = new Subject();
this.nzPageSizeChange = new EventEmitter();
this.nzPageIndexChange = new EventEmitter();
this.nzInTable = false;
this.nzSize = 'default';
this.nzPageSizeOptions = [10, 20, 30, 40];
this.nzDisabled = false;
this.nzShowSizeChanger = false;
this.nzHideOnSinglePage = false;
this.nzShowQuickJumper = false;
this.nzSimple = false;
this.nzTotal = 0;
this.nzPageIndex = 1;
this.nzPageSize = 10;
}
/**
* @return {?}
*/
get itemRender() {
return this.nzItemRender || this.nzItemRenderChild;
}
/**
* @param {?} value
* @return {?}
*/
validatePageIndex(value) {
if (value > this.lastIndex) {
return this.lastIndex;
}
else if (value < this.firstIndex) {
return this.firstIndex;
}
else {
return value;
}
}
/**
* @param {?} page
* @return {?}
*/
updatePageIndexValue(page) {
this.nzPageIndex = page;
this.nzPageIndexChange.emit(this.nzPageIndex);
this.buildIndexes();
}
/**
* @param {?} value
* @return {?}
*/
isPageIndexValid(value) {
return this.validatePageIndex(value) === value;
}
/**
* @param {?} index
* @return {?}
*/
jumpPage(index) {
if (index !== this.nzPageIndex && !this.nzDisabled) {
/** @type {?} */
const pageIndex = this.validatePageIndex(index);
if (pageIndex !== this.nzPageIndex) {
this.updatePageIndexValue(pageIndex);
}
}
}
/**
* @param {?} diff
* @return {?}
*/
jumpDiff(diff) {
this.jumpPage(this.nzPageIndex + diff);
}
/**
* @param {?} $event
* @return {?}
*/
onPageSizeChange($event) {
this.nzPageSize = $event;
this.nzPageSizeChange.emit($event);
this.buildIndexes();
if (this.nzPageIndex > this.lastIndex) {
this.updatePageIndexValue(this.lastIndex);
}
}
/**
* @param {?} _
* @param {?} input
* @param {?} clearInputValue
* @return {?}
*/
handleKeyDown(_, input, clearInputValue) {
/** @type {?} */
const target = input;
/** @type {?} */
const page = toNumber(target.value, this.nzPageIndex);
if (isInteger(page) && this.isPageIndexValid(page) && page !== this.nzPageIndex) {
this.updatePageIndexValue(page);
}
if (clearInputValue) {
target.value = '';
}
else {
target.value = `${this.nzPageIndex}`;
}
}
/**
* generate indexes list
* @return {?}
*/
buildIndexes() {
/** @type {?} */
const pages = [];
if (this.lastIndex <= 9) {
for (let i = 2; i <= this.lastIndex - 1; i++) {
pages.push(i);
}
}
else {
/** @type {?} */
const current = +this.nzPageIndex;
/** @type {?} */
let left = Math.max(2, current - 2);
/** @type {?} */
let right = Math.min(current + 2, this.lastIndex - 1);
if (current - 1 <= 2) {
right = 5;
}
if (this.lastIndex - current <= 2) {
left = this.lastIndex - 4;
}
for (let i = left; i <= right; i++) {
pages.push(i);
}
}
this.pages = pages;
this.cdr.markForCheck();
}
/**
* @return {?}
*/
get lastIndex() {
return Math.ceil(this.nzTotal / this.nzPageSize);
}
/**
* @return {?}
*/
get isLastIndex() {
return this.nzPageIndex === this.lastIndex;
}
/**
* @return {?}
*/
get isFirstIndex() {
return this.nzPageIndex === this.firstIndex;
}
/**
* @return {?}
*/
get ranges() {
return [(this.nzPageIndex - 1) * this.nzPageSize + 1, Math.min(this.nzPageIndex * this.nzPageSize, this.nzTotal)];
}
/**
* @return {?}
*/
get showAddOption() {
return this.nzPageSizeOptions.indexOf(this.nzPageSize) === -1;
}
/**
* @return {?}
*/
ngOnInit() {
this.i18n.localeChange.pipe(takeUntil(this.$destroy)).subscribe((/**
* @return {?}
*/
() => {
this.locale = this.i18n.getLocaleData('Pagination');
this.cdr.markForCheck();
}));
}
/**
* @return {?}
*/
ngOnDestroy() {
this.$destroy.next();
this.$destroy.complete();
}
/**
* @param {?} changes
* @return {?}
*/
ngOnChanges(changes) {
if (changes.nzTotal || changes.nzPageSize || changes.nzPageIndex) {
this.buildIndexes();
}
}
}
NzPaginationComponent.decorators = [
{ type: Component, args: [{
selector: 'nz-pagination',
exportAs: 'nzPagination',
preserveWhitespaces: false,
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
template: "<ng-template #renderItemTemplate let-type let-page=\"page\">\n <a class=\"ant-pagination-item-link\" *ngIf=\"type==='pre'\">\n <i nz-icon nzType=\"left\"></i>\n </a>\n <a class=\"ant-pagination-item-link\" *ngIf=\"type==='next'\">\n <i nz-icon nzType=\"right\"></i>\n </a>\n <a *ngIf=\"type=='page'\">{{ page }}</a>\n</ng-template>\n<ng-container *ngIf=\"(nzHideOnSinglePage && (nzTotal > nzPageSize)) || (nzTotal && !nzHideOnSinglePage)\">\n <ul class=\"ant-pagination\"\n [class.ant-table-pagination]=\"nzInTable\"\n [class.ant-pagination-simple]=\"nzSimple\"\n [class.ant-pagination-disabled]=\"nzDisabled\"\n [class.mini]=\"(nzSize === 'small') && !nzSimple\">\n <ng-container *ngIf=\"nzSimple; else normalTemplate\">\n <li class=\"ant-pagination-prev\"\n [attr.title]=\"locale.prev_page\"\n [class.ant-pagination-disabled]=\"isFirstIndex\"\n (click)=\"jumpDiff(-1)\">\n <ng-template [ngTemplateOutlet]=\"itemRender\" [ngTemplateOutletContext]=\"{ $implicit: 'pre'}\"></ng-template>\n </li>\n <li [attr.title]=\"nzPageIndex+'/'+lastIndex\" class=\"ant-pagination-simple-pager\">\n <input #simplePagerInput\n [disabled]=\"nzDisabled\"\n [value]=\"nzPageIndex\"\n (keydown.enter)=\"handleKeyDown($event,simplePagerInput,false)\"\n size=\"3\">\n <span class=\"ant-pagination-slash\">\uFF0F</span>\n {{ lastIndex }}\n </li>\n <li class=\"ant-pagination-next\"\n [attr.title]=\"locale.next_page\"\n [class.ant-pagination-disabled]=\"isLastIndex\"\n (click)=\"jumpDiff(1)\">\n <ng-template [ngTemplateOutlet]=\"itemRender\" [ngTemplateOutletContext]=\"{ $implicit: 'next'}\"></ng-template>\n </li>\n </ng-container>\n <ng-template #normalTemplate>\n <li class=\"ant-pagination-total-text\" *ngIf=\"nzShowTotal\">\n <ng-template [ngTemplateOutlet]=\"nzShowTotal\"\n [ngTemplateOutletContext]=\"{ $implicit: nzTotal,range:ranges }\"></ng-template>\n </li>\n <li class=\"ant-pagination-prev\"\n [attr.title]=\"locale.prev_page\"\n [class.ant-pagination-disabled]=\"isFirstIndex\"\n (click)=\"jumpDiff(-1)\">\n <ng-template [ngTemplateOutlet]=\"itemRender\" [ngTemplateOutletContext]=\"{ $implicit: 'pre'}\"></ng-template>\n </li>\n <li class=\"ant-pagination-item\"\n [attr.title]=\"firstIndex\"\n [class.ant-pagination-item-active]=\"isFirstIndex\"\n (click)=\"jumpPage(firstIndex)\">\n <ng-template [ngTemplateOutlet]=\"itemRender\"\n [ngTemplateOutletContext]=\"{ $implicit: 'page',page: firstIndex }\"></ng-template>\n </li>\n <li class=\"ant-pagination-jump-prev\"\n *ngIf=\"(lastIndex > 9) && (nzPageIndex - 3 > firstIndex)\"\n [attr.title]=\"locale.prev_5\"\n (click)=\"jumpDiff(-5)\">\n <a class=\"ant-pagination-item-link\">\n <div class=\"ant-pagination-item-container\">\n <i nz-icon nzType=\"double-left\" class=\"ant-pagination-item-link-icon\"></i>\n <span class=\"ant-pagination-item-ellipsis\">\u2022\u2022\u2022</span>\n </div>\n </a>\n </li>\n <li class=\"ant-pagination-item\"\n *ngFor=\"let page of pages\"\n [attr.title]=\"page\"\n [class.ant-pagination-item-active]=\"nzPageIndex === page\"\n (click)=\"jumpPage(page)\">\n <ng-template [ngTemplateOutlet]=\"itemRender\"\n [ngTemplateOutletContext]=\"{ $implicit: 'page',page: page }\"></ng-template>\n </li>\n <li class=\"ant-pagination-jump-next ant-pagination-item-link-icon\"\n [attr.title]=\"locale.next_5\"\n (click)=\"jumpDiff(5)\"\n *ngIf=\"(lastIndex > 9) && (nzPageIndex + 3 < lastIndex)\">\n <a class=\"ant-pagination-item-link\">\n <div class=\"ant-pagination-item-container\">\n <i nz-icon nzType=\"double-right\" class=\"ant-pagination-item-link-icon\"></i>\n <span class=\"ant-pagination-item-ellipsis\">\u2022\u2022\u2022</span>\n </div>\n </a>\n </li>\n <li class=\"ant-pagination-item\"\n [attr.title]=\"lastIndex\"\n (click)=\"jumpPage(lastIndex)\"\n *ngIf=\"(lastIndex > 0) && (lastIndex !== firstIndex)\"\n [class.ant-pagination-item-active]=\"isLastIndex\">\n <ng-template [ngTemplateOutlet]=\"itemRender\"\n [ngTemplateOutletContext]=\"{ $implicit: 'page',page: lastIndex }\"></ng-template>\n </li>\n <li class=\"ant-pagination-next\"\n [title]=\"locale.next_page\"\n [class.ant-pagination-disabled]=\"isLastIndex\"\n (click)=\"jumpDiff(1)\">\n <ng-template [ngTemplateOutlet]=\"itemRender\" [ngTemplateOutletContext]=\"{ $implicit: 'next'}\"></ng-template>\n </li>\n <div class=\"ant-pagination-options\" *ngIf=\"nzShowQuickJumper || nzShowSizeChanger\">\n <nz-select class=\"ant-pagination-options-size-changer\"\n *ngIf=\"nzShowSizeChanger\"\n [nzDisabled]=\"nzDisabled\"\n [nzSize]=\"nzSize\"\n [ngModel]=\"nzPageSize\"\n (ngModelChange)=\"onPageSizeChange($event)\">\n <nz-option *ngFor=\"let option of nzPageSizeOptions\"\n [nzLabel]=\"option + ' ' + locale.items_per_page\"\n [nzValue]=\"option\">\n </nz-option>\n <nz-option *ngIf=\"showAddOption\"\n [nzLabel]=\"nzPageSize + ' ' + locale.items_per_page\"\n [nzValue]=\"nzPageSize\">\n </nz-option>\n </nz-select>\n <div class=\"ant-pagination-options-quick-jumper\" *ngIf=\"nzShowQuickJumper\">\n {{ locale.jump_to }}\n <input #quickJumperInput\n [disabled]=\"nzDisabled\"\n (keydown.enter)=\"handleKeyDown($event,quickJumperInput,true)\">\n {{ locale.page }}\n </div>\n </div>\n </ng-template>\n </ul>\n</ng-container>\n"
}] }
];
/** @nocollapse */
NzPaginationComponent.ctorParameters = () => [
{ type: NzI18nService },
{ type: ChangeDetectorRef }
];
NzPaginationComponent.propDecorators = {
nzPageSizeChange: [{ type: Output }],
nzPageIndexChange: [{ type: Output }],
nzShowTotal: [{ type: Input }],
nzInTable: [{ type: Input }],
nzSize: [{ type: Input }],
nzPageSizeOptions: [{ type: Input }],
nzItemRender: [{ type: Input }],
nzItemRenderChild: [{ type: ViewChild, args: ['renderItemTemplate', { static: true },] }],
nzDisabled: [{ type: Input }],
nzShowSizeChanger: [{ type: Input }],
nzHideOnSinglePage: [{ type: Input }],
nzShowQuickJumper: [{ type: Input }],
nzSimple: [{ type: Input }],
nzTotal: [{ type: Input }],
nzPageIndex: [{ type: Input }],
nzPageSize: [{ type: Input }]
};
__decorate([
InputBoolean(),
__metadata("design:type", Object)
], NzPaginationComponent.prototype, "nzDisabled", void 0);
__decorate([
InputBoolean(),
__metadata("design:type", Object)
], NzPaginationComponent.prototype, "nzShowSizeChanger", void 0);
__decorate([
InputBoolean(),
__metadata("design:type", Object)
], NzPaginationComponent.prototype, "nzHideOnSinglePage", void 0);
__decorate([
InputBoolean(),
__metadata("design:type", Object)
], NzPaginationComponent.prototype, "nzShowQuickJumper", void 0);
__decorate([
InputBoolean(),
__metadata("design:type", Object)
], NzPaginationComponent.prototype, "nzSimple", void 0);
__decorate([
InputNumber(),
__metadata("design:type", Object)
], NzPaginationComponent.prototype, "nzTotal", void 0);
__decorate([
InputNumber(),
__metadata("design:type", Object)
], NzPaginationComponent.prototype, "nzPageIndex", void 0);
__decorate([
InputNumber(),
__metadata("design:type", Object)
], NzPaginationComponent.prototype, "nzPageSize", void 0);
if (false) {
/** @type {?} */
NzPaginationComponent.prototype.locale;
/** @type {?} */
NzPaginationComponent.prototype.firstIndex;
/** @type {?} */
NzPaginationComponent.prototype.pages;
/**
* @type {?}
* @private
*/
NzPaginationComponent.prototype.$destroy;
/** @type {?} */
NzPaginationComponent.prototype.nzPageSizeChange;
/** @type {?} */
NzPaginationComponent.prototype.nzPageIndexChange;
/** @type {?} */
NzPaginationComponent.prototype.nzShowTotal;
/** @type {?} */
NzPaginationComponent.prototype.nzInTable;
/** @type {?} */
NzPaginationComponent.prototype.nzSize;
/** @type {?} */
NzPaginationComponent.prototype.nzPageSizeOptions;
/** @type {?} */
NzPaginationComponent.prototype.nzItemRender;
/** @type {?} */
NzPaginationComponent.prototype.nzItemRenderChild;
/** @type {?} */
NzPaginationComponent.prototype.nzDisabled;
/** @type {?} */
NzPaginationComponent.prototype.nzShowSizeChanger;
/** @type {?} */
NzPaginationComponent.prototype.nzHideOnSinglePage;
/** @type {?} */
NzPaginationComponent.prototype.nzShowQuickJumper;
/** @type {?} */
NzPaginationComponent.prototype.nzSimple;
/** @type {?} */
NzPaginationComponent.prototype.nzTotal;
/** @type {?} */
NzPaginationComponent.prototype.nzPageIndex;
/** @type {?} */
NzPaginationComponent.prototype.nzPageSize;
/**
* @type {?}
* @private
*/
NzPaginationComponent.prototype.i18n;
/**
* @type {?}
* @private
*/
NzPaginationComponent.prototype.cdr;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NzPaginationModule {
}
NzPaginationModule.decorators = [
{ type: NgModule, args: [{
declarations: [NzPaginationComponent],
exports: [NzPaginationComponent],
imports: [CommonModule, FormsModule, NzSelectModule, NzI18nModule, NzIconModule]
},] }
];
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { NzPaginationComponent, NzPaginationModule };
//# sourceMappingURL=ng-zorro-antd-pagination.js.map