1 import {EventEmitter} from '@angular/core';
4 * Utility class for manage paged information.
10 onChange$: EventEmitter<number>;
13 this.resultCount = null;
14 this.onChange$ = new EventEmitter<number>();
18 this.resultCount = null;
23 if (l !== this.limit) {
29 isFirstPage(): boolean {
30 return this.offset === 0;
33 isLastPage(): boolean {
34 return this.currentPage() === this.pageCount();
37 currentPage(): number {
38 return Math.floor(this.offset / this.limit) + 1;
42 this.setPage(this.currentPage() + 1);
46 this.setPage(this.currentPage() - 1);
50 if (!this.isFirstPage()) {
56 if (!this.isLastPage()) {
57 this.setPage(this.pageCount());
61 setPage(page: number): void {
62 this.offset = (this.limit * (page - 1));
63 this.onChange$.emit(this.offset);
67 if (this.resultCount === null) { return -1; }
68 let pages = this.resultCount / this.limit;
69 if (Math.floor(pages) < pages) {
70 pages = Math.floor(pages) + 1;
75 // Returns a list of pages numbers with @pivot at the center
76 // or as close to center as possible.
77 // @pivot is 1-based for consistency with page numbers.
78 // pageRange(25, 10) => [21,22,...29,30]
79 pageRange(pivot: number, size: number): number[] {
81 const diff = Math.floor(size / 2);
82 let start = pivot <= diff ? 1 : pivot - diff + 1;
84 const pcount = this.pageCount();
86 if (start + size > pcount) {
87 start = pcount - size + 1;
88 if (start < 1) { start = 1; }
91 if (start + size > pcount) {
95 return this.pageList().slice(start - 1, start - 1 + size);
98 pageList(): number[] {
100 for (let i = 1; i <= this.pageCount(); i++) {
106 // Given a zero-based page-specific offset, return the where in the
107 // entire data set the row lives, 1-based for UI friendliness.
108 rowNumber(offset: number): number {
109 return this.offset + offset + 1;