import { Component, Input, Output, EventEmitter, OnChanges } from '@angular/core';
import { RdComponent } from '../../base/rdComponent';
import { RdLib } from '../../base/rdLib';
@Component({
selector: 'rd-paging',
template: `
`
})
export class Paging extends RdComponent implements OnChanges {
@Input("rd-current-page") currentPage: number;
@Output("rd-current-pageChange") currentPageChange: EventEmitter = new EventEmitter();
@Output("rd-change") changeEvent: EventEmitter = new EventEmitter();
@Input("rd-item-count") itemCount: number;
@Input("rd-page-size") pageSize: number;
@Input("rd-additional-page-size") additionalPageSizeList: Array;
@Output("rd-page-sizeChange") pageSizeChange: EventEmitter = new EventEmitter();
translateEn = RdLib.localization.translateEn;
base: number = 1;
totalPageCount: number;
groupSize: number = 5;
pageToShowGroupSize: number = 5;
currentGroup: number = 1;
pageSizeOptions: Array = [5, 10, 20, 30, 50, 100];
minPageSize = 5;
ngOnChanges(changes) {
if (changes.itemCount || changes.pageSize) {
if (this.additionalPageSizeList) this.pageSizeOptions = Array.from(new Set([...this.pageSizeOptions, ...this.additionalPageSizeList]));
this.totalPageCount = Math.ceil(this.itemCount / this.pageSize);
if (this.totalPageCount < this.pageToShowGroupSize) this.pageToShowGroupSize = this.totalPageCount;
this.refreshPageItems(this.currentPage);
}
}
gotoPage(page: number) {
if (!page) page = 1;
if (page > this.totalPageCount || page < 1 || isNaN(page)) return;
this.currentPage = page;
var group = Math.ceil(page / this.groupSize);
if (this.currentGroup != group) {
this.currentGroup = group;
this.refreshPageItems(page, group);
}
this.currentPageChange.emit(page);
this.changeEvent.emit(page);
}
refreshPageItems(page: number, group?: number) {
if (!group) group = Math.ceil(page / this.groupSize);
this.base = this.groupSize * (group - 1) + 1;
if (this.base + this.groupSize - 1 > this.totalPageCount)
this.pageToShowGroupSize = this.totalPageCount % this.groupSize;
else this.pageToShowGroupSize = 5;
}
emitPageSize(i) {
this.pageSize = i;
this.pageSizeChange.emit(i);
}
}