/
pagination.ts
398 lines (350 loc) · 12.9 KB
/
pagination.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import {
Component,
ContentChild,
Directive,
EventEmitter,
Input,
Output,
OnChanges,
ChangeDetectionStrategy,
SimpleChanges,
TemplateRef
} from '@angular/core';
import {getValueInRange, isNumber} from '../util/util';
import {NgbPaginationConfig} from './pagination-config';
/**
* A context for the
* * `NgbPaginationFirst`
* * `NgbPaginationPrevious`
* * `NgbPaginationNext`
* * `NgbPaginationLast`
* * `NgbPaginationEllipsis`
*
* link templates in case you want to override one.
*
* @since 4.1.0
*/
export interface NgbPaginationLinkContext {
/**
* The currently selected page number
*/
currentPage: number;
/**
* If `true`, the current link is disabled
*/
disabled: boolean;
}
/**
* A context for the `NgbPaginationNumber` link template in case you want to override one.
*
* Extends `NgbPaginationLinkContext`.
*
* @since 4.1.0
*/
export interface NgbPaginationNumberContext extends NgbPaginationLinkContext {
/**
* The page number, displayed by the current page link.
*/
$implicit: number;
}
/**
* A directive to match the 'ellipsis' link template
*
* @since 4.1.0
*/
@Directive({selector: 'ng-template[ngbPaginationEllipsis]'})
export class NgbPaginationEllipsis {
constructor(public templateRef: TemplateRef<NgbPaginationLinkContext>) {}
}
/**
* A directive to match the 'first' link template
*
* @since 4.1.0
*/
@Directive({selector: 'ng-template[ngbPaginationFirst]'})
export class NgbPaginationFirst {
constructor(public templateRef: TemplateRef<NgbPaginationLinkContext>) {}
}
/**
* A directive to match the 'last' link template
*
* @since 4.1.0
*/
@Directive({selector: 'ng-template[ngbPaginationLast]'})
export class NgbPaginationLast {
constructor(public templateRef: TemplateRef<NgbPaginationLinkContext>) {}
}
/**
* A directive to match the 'next' link template
*
* @since 4.1.0
*/
@Directive({selector: 'ng-template[ngbPaginationNext]'})
export class NgbPaginationNext {
constructor(public templateRef: TemplateRef<NgbPaginationLinkContext>) {}
}
/**
* A directive to match the page 'number' link template
*
* @since 4.1.0
*/
@Directive({selector: 'ng-template[ngbPaginationNumber]'})
export class NgbPaginationNumber {
constructor(public templateRef: TemplateRef<NgbPaginationNumberContext>) {}
}
/**
* A directive to match the 'previous' link template
*
* @since 4.1.0
*/
@Directive({selector: 'ng-template[ngbPaginationPrevious]'})
export class NgbPaginationPrevious {
constructor(public templateRef: TemplateRef<NgbPaginationLinkContext>) {}
}
/**
* A component that displays page numbers and allows to customize them in several ways.
*/
@Component({
selector: 'ngb-pagination',
changeDetection: ChangeDetectionStrategy.OnPush,
host: {'role': 'navigation'},
template: `
<ng-template #first><span aria-hidden="true" i18n="@@ngb.pagination.first">««</span></ng-template>
<ng-template #previous><span aria-hidden="true" i18n="@@ngb.pagination.previous">«</span></ng-template>
<ng-template #next><span aria-hidden="true" i18n="@@ngb.pagination.next">»</span></ng-template>
<ng-template #last><span aria-hidden="true" i18n="@@ngb.pagination.last">»»</span></ng-template>
<ng-template #ellipsis>...</ng-template>
<ng-template #defaultNumber let-page let-currentPage="currentPage">
{{ page }}
<span *ngIf="page === currentPage" class="sr-only">(current)</span>
</ng-template>
<ul [class]="'pagination' + (size ? ' pagination-' + size : '')">
<li *ngIf="boundaryLinks" class="page-item"
[class.disabled]="previousDisabled()">
<a aria-label="First" i18n-aria-label="@@ngb.pagination.first-aria" class="page-link" href
(click)="selectPage(1); $event.preventDefault()" [attr.tabindex]="(hasPrevious() && !disabled ? null : '-1')">
<ng-template [ngTemplateOutlet]="tplFirst?.templateRef || first"
[ngTemplateOutletContext]="{disabled: previousDisabled(), currentPage: page}"></ng-template>
</a>
</li>
<li *ngIf="directionLinks" class="page-item"
[class.disabled]="previousDisabled()">
<a aria-label="Previous" i18n-aria-label="@@ngb.pagination.previous-aria" class="page-link" href
(click)="selectPage(page-1); $event.preventDefault()" [attr.tabindex]="(hasPrevious() && !disabled ? null : '-1')">
<ng-template [ngTemplateOutlet]="tplPrevious?.templateRef || previous"
[ngTemplateOutletContext]="{disabled: previousDisabled()}"></ng-template>
</a>
</li>
<li *ngFor="let pageNumber of pages" class="page-item" [class.active]="pageNumber === page"
[class.disabled]="isEllipsis(pageNumber) || disabled">
<a *ngIf="isEllipsis(pageNumber)" class="page-link" [attr.tabindex]="(disabled ? '-1' : null)">
<ng-template [ngTemplateOutlet]="tplEllipsis?.templateRef || ellipsis"
[ngTemplateOutletContext]="{disabled: true, currentPage: page}"></ng-template>
</a>
<a *ngIf="!isEllipsis(pageNumber)" class="page-link" href (click)="selectPage(pageNumber); $event.preventDefault()" [attr.tabindex]="(disabled ? '-1' : null)">
<ng-template [ngTemplateOutlet]="tplNumber?.templateRef || defaultNumber"
[ngTemplateOutletContext]="{disabled: disabled, $implicit: pageNumber, currentPage: page}"></ng-template>
</a>
</li>
<li *ngIf="directionLinks" class="page-item" [class.disabled]="nextDisabled()">
<a aria-label="Next" i18n-aria-label="@@ngb.pagination.next-aria" class="page-link" href
(click)="selectPage(page+1); $event.preventDefault()" [attr.tabindex]="(hasNext() && !disabled ? null : '-1')">
<ng-template [ngTemplateOutlet]="tplNext?.templateRef || next"
[ngTemplateOutletContext]="{disabled: nextDisabled(), currentPage: page}"></ng-template>
</a>
</li>
<li *ngIf="boundaryLinks" class="page-item" [class.disabled]="nextDisabled()">
<a aria-label="Last" i18n-aria-label="@@ngb.pagination.last-aria" class="page-link" href
(click)="selectPage(pageCount); $event.preventDefault()" [attr.tabindex]="(hasNext() && !disabled ? null : '-1')">
<ng-template [ngTemplateOutlet]="tplLast?.templateRef || last"
[ngTemplateOutletContext]="{disabled: nextDisabled(), currentPage: page}"></ng-template>
</a>
</li>
</ul>
`
})
export class NgbPagination implements OnChanges {
pageCount = 0;
pages: number[] = [];
@ContentChild(NgbPaginationEllipsis, {static: false}) tplEllipsis: NgbPaginationEllipsis;
@ContentChild(NgbPaginationFirst, {static: false}) tplFirst: NgbPaginationFirst;
@ContentChild(NgbPaginationLast, {static: false}) tplLast: NgbPaginationLast;
@ContentChild(NgbPaginationNext, {static: false}) tplNext: NgbPaginationNext;
@ContentChild(NgbPaginationNumber, {static: false}) tplNumber: NgbPaginationNumber;
@ContentChild(NgbPaginationPrevious, {static: false}) tplPrevious: NgbPaginationPrevious;
/**
* If `true`, pagination links will be disabled.
*/
@Input() disabled: boolean;
/**
* If `true`, the "First" and "Last" page links are shown.
*/
@Input() boundaryLinks: boolean;
/**
* If `true`, the "Next" and "Previous" page links are shown.
*/
@Input() directionLinks: boolean;
/**
* If `true`, the ellipsis symbols and first/last page numbers will be shown when `maxSize` > number of pages.
*/
@Input() ellipses: boolean;
/**
* Whether to rotate pages when `maxSize` > number of pages.
*
* The current page always stays in the middle if `true`.
*/
@Input() rotate: boolean;
/**
* The number of items in your paginated collection.
*
* Note, that this is not the number of pages. Page numbers are calculated dynamically based on
* `collectionSize` and `pageSize`. Ex. if you have 100 items in your collection and displaying 20 items per page,
* you'll end up with 5 pages.
*/
@Input() collectionSize: number;
/**
* The maximum number of pages to display.
*/
@Input() maxSize: number;
/**
* The current page.
*
* Page numbers start with `1`.
*/
@Input() page = 1;
/**
* The number of items per page.
*/
@Input() pageSize: number;
/**
* An event fired when the page is changed. Will fire only if collection size is set and all values are valid.
*
* Event payload is the number of the newly selected page.
*
* Page numbers start with `1`.
*/
@Output() pageChange = new EventEmitter<number>(true);
/**
* The pagination display size.
*
* Bootstrap currently supports small and large sizes.
*/
@Input() size: 'sm' | 'lg';
constructor(config: NgbPaginationConfig) {
this.disabled = config.disabled;
this.boundaryLinks = config.boundaryLinks;
this.directionLinks = config.directionLinks;
this.ellipses = config.ellipses;
this.maxSize = config.maxSize;
this.pageSize = config.pageSize;
this.rotate = config.rotate;
this.size = config.size;
}
hasPrevious(): boolean { return this.page > 1; }
hasNext(): boolean { return this.page < this.pageCount; }
nextDisabled(): boolean { return !this.hasNext() || this.disabled; }
previousDisabled(): boolean { return !this.hasPrevious() || this.disabled; }
selectPage(pageNumber: number): void { this._updatePages(pageNumber); }
ngOnChanges(changes: SimpleChanges): void { this._updatePages(this.page); }
isEllipsis(pageNumber): boolean { return pageNumber === -1; }
/**
* Appends ellipses and first/last page number to the displayed pages
*/
private _applyEllipses(start: number, end: number) {
if (this.ellipses) {
if (start > 0) {
// The first page will always be included. If the displayed range
// starts after the third page, then add ellipsis. But if the range
// starts on the third page, then add the second page instead of
// an ellipsis, because the ellipsis would only hide a single page.
if (start > 2) {
this.pages.unshift(-1);
} else if (start === 2) {
this.pages.unshift(2);
}
this.pages.unshift(1);
}
if (end < this.pageCount) {
// The last page will always be included. If the displayed range
// ends before the third-last page, then add ellipsis. But if the range
// ends on third-last page, then add the second-last page instead of
// an ellipsis, because the ellipsis would only hide a single page.
if (end < (this.pageCount - 2)) {
this.pages.push(-1);
} else if (end === (this.pageCount - 2)) {
this.pages.push(this.pageCount - 1);
}
this.pages.push(this.pageCount);
}
}
}
/**
* Rotates page numbers based on maxSize items visible.
* Currently selected page stays in the middle:
*
* Ex. for selected page = 6:
* [5,*6*,7] for maxSize = 3
* [4,5,*6*,7] for maxSize = 4
*/
private _applyRotation(): [number, number] {
let start = 0;
let end = this.pageCount;
let leftOffset = Math.floor(this.maxSize / 2);
let rightOffset = this.maxSize % 2 === 0 ? leftOffset - 1 : leftOffset;
if (this.page <= leftOffset) {
// very beginning, no rotation -> [0..maxSize]
end = this.maxSize;
} else if (this.pageCount - this.page < leftOffset) {
// very end, no rotation -> [len-maxSize..len]
start = this.pageCount - this.maxSize;
} else {
// rotate
start = this.page - leftOffset - 1;
end = this.page + rightOffset;
}
return [start, end];
}
/**
* Paginates page numbers based on maxSize items per page.
*/
private _applyPagination(): [number, number] {
let page = Math.ceil(this.page / this.maxSize) - 1;
let start = page * this.maxSize;
let end = start + this.maxSize;
return [start, end];
}
private _setPageInRange(newPageNo) {
const prevPageNo = this.page;
this.page = getValueInRange(newPageNo, this.pageCount, 1);
if (this.page !== prevPageNo && isNumber(this.collectionSize)) {
this.pageChange.emit(this.page);
}
}
private _updatePages(newPage: number) {
this.pageCount = Math.ceil(this.collectionSize / this.pageSize);
if (!isNumber(this.pageCount)) {
this.pageCount = 0;
}
// fill-in model needed to render pages
this.pages.length = 0;
for (let i = 1; i <= this.pageCount; i++) {
this.pages.push(i);
}
// set page within 1..max range
this._setPageInRange(newPage);
// apply maxSize if necessary
if (this.maxSize > 0 && this.pageCount > this.maxSize) {
let start = 0;
let end = this.pageCount;
// either paginating or rotating page numbers
if (this.rotate) {
[start, end] = this._applyRotation();
} else {
[start, end] = this._applyPagination();
}
this.pages = this.pages.slice(start, end);
// adding ellipses
this._applyEllipses(start, end);
}
}
}