UNPKG

10.9 kBJavaScriptView Raw
1import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ChangeDetectorRef, Input, Output, NgModule } from '@angular/core';
2import { CommonModule } from '@angular/common';
3import { FormsModule } from '@angular/forms';
4import { DropdownModule } from 'primeng/dropdown';
5import { RippleModule } from 'primeng/ripple';
6import { SharedModule } from 'primeng/api';
7
8class Paginator {
9 constructor(cd) {
10 this.cd = cd;
11 this.pageLinkSize = 5;
12 this.onPageChange = new EventEmitter();
13 this.alwaysShow = true;
14 this.dropdownScrollHeight = '200px';
15 this.currentPageReportTemplate = '{currentPage} of {totalPages}';
16 this.totalRecords = 0;
17 this.rows = 0;
18 this.showPageLinks = true;
19 this._first = 0;
20 this._page = 0;
21 }
22 ngOnInit() {
23 this.updatePaginatorState();
24 }
25 ngOnChanges(simpleChange) {
26 if (simpleChange.totalRecords) {
27 this.updatePageLinks();
28 this.updatePaginatorState();
29 this.updateFirst();
30 this.updateRowsPerPageOptions();
31 }
32 if (simpleChange.first) {
33 this._first = simpleChange.first.currentValue;
34 this.updatePageLinks();
35 this.updatePaginatorState();
36 }
37 if (simpleChange.rows) {
38 this.updatePageLinks();
39 this.updatePaginatorState();
40 }
41 if (simpleChange.rowsPerPageOptions) {
42 this.updateRowsPerPageOptions();
43 }
44 }
45 get first() {
46 return this._first;
47 }
48 set first(val) {
49 this._first = val;
50 }
51 updateRowsPerPageOptions() {
52 if (this.rowsPerPageOptions) {
53 this.rowsPerPageItems = [];
54 for (let opt of this.rowsPerPageOptions) {
55 if (typeof opt == 'object' && opt['showAll']) {
56 this.rowsPerPageItems.unshift({ label: opt['showAll'], value: this.totalRecords });
57 }
58 else {
59 this.rowsPerPageItems.push({ label: String(opt), value: opt });
60 }
61 }
62 }
63 }
64 isFirstPage() {
65 return this.getPage() === 0;
66 }
67 isLastPage() {
68 return this.getPage() === this.getPageCount() - 1;
69 }
70 getPageCount() {
71 return Math.ceil(this.totalRecords / this.rows) || 1;
72 }
73 calculatePageLinkBoundaries() {
74 let numberOfPages = this.getPageCount(), visiblePages = Math.min(this.pageLinkSize, numberOfPages);
75 //calculate range, keep current in middle if necessary
76 let start = Math.max(0, Math.ceil(this.getPage() - ((visiblePages) / 2))), end = Math.min(numberOfPages - 1, start + visiblePages - 1);
77 //check when approaching to last page
78 var delta = this.pageLinkSize - (end - start + 1);
79 start = Math.max(0, start - delta);
80 return [start, end];
81 }
82 updatePageLinks() {
83 this.pageLinks = [];
84 let boundaries = this.calculatePageLinkBoundaries(), start = boundaries[0], end = boundaries[1];
85 for (let i = start; i <= end; i++) {
86 this.pageLinks.push(i + 1);
87 }
88 if (this.showJumpToPageDropdown) {
89 this.pageItems = [];
90 for (let i = 0; i < this.getPageCount(); i++) {
91 this.pageItems.push({ label: String(i + 1), value: i });
92 }
93 }
94 }
95 changePage(p) {
96 var pc = this.getPageCount();
97 if (p >= 0 && p < pc) {
98 this._first = this.rows * p;
99 var state = {
100 page: p,
101 first: this.first,
102 rows: this.rows,
103 pageCount: pc
104 };
105 this.updatePageLinks();
106 this.onPageChange.emit(state);
107 this.updatePaginatorState();
108 }
109 }
110 updateFirst() {
111 const page = this.getPage();
112 if (page > 0 && this.totalRecords && (this.first >= this.totalRecords)) {
113 Promise.resolve(null).then(() => this.changePage(page - 1));
114 }
115 }
116 getPage() {
117 return Math.floor(this.first / this.rows);
118 }
119 changePageToFirst(event) {
120 if (!this.isFirstPage()) {
121 this.changePage(0);
122 }
123 event.preventDefault();
124 }
125 changePageToPrev(event) {
126 this.changePage(this.getPage() - 1);
127 event.preventDefault();
128 }
129 changePageToNext(event) {
130 this.changePage(this.getPage() + 1);
131 event.preventDefault();
132 }
133 changePageToLast(event) {
134 if (!this.isLastPage()) {
135 this.changePage(this.getPageCount() - 1);
136 }
137 event.preventDefault();
138 }
139 onPageLinkClick(event, page) {
140 this.changePage(page);
141 event.preventDefault();
142 }
143 onRppChange(event) {
144 this.changePage(this.getPage());
145 }
146 onPageDropdownChange(event) {
147 this.changePage(event.value);
148 }
149 updatePaginatorState() {
150 this.paginatorState = {
151 page: this.getPage(),
152 pageCount: this.getPageCount(),
153 rows: this.rows,
154 first: this.first,
155 totalRecords: this.totalRecords
156 };
157 }
158 get currentPageReport() {
159 return this.currentPageReportTemplate
160 .replace("{currentPage}", String(this.getPage() + 1))
161 .replace("{totalPages}", String(this.getPageCount()))
162 .replace("{first}", String(this._first + 1))
163 .replace("{last}", String(Math.min(this._first + this.rows, this.totalRecords)))
164 .replace("{rows}", String(this.rows))
165 .replace("{totalRecords}", String(this.totalRecords));
166 }
167}
168Paginator.decorators = [
169 { type: Component, args: [{
170 selector: 'p-paginator',
171 template: `
172 <div [class]="styleClass" [ngStyle]="style" [ngClass]="'p-paginator p-component'" *ngIf="alwaysShow ? true : (pageLinks && pageLinks.length > 1)">
173 <div class="p-paginator-left-content" *ngIf="templateLeft">
174 <ng-container *ngTemplateOutlet="templateLeft; context: {$implicit: paginatorState}"></ng-container>
175 </div>
176 <span class="p-paginator-current" *ngIf="showCurrentPageReport">{{currentPageReport}}</span>
177 <button type="button" [disabled]="isFirstPage()" (click)="changePageToFirst($event)" pRipple
178 class="p-paginator-first p-paginator-element p-link" [ngClass]="{'p-disabled':isFirstPage()}">
179 <span class="p-paginator-icon pi pi-angle-double-left"></span>
180 </button>
181 <button type="button" [disabled]="isFirstPage()" (click)="changePageToPrev($event)" pRipple
182 class="p-paginator-prev p-paginator-element p-link" [ngClass]="{'p-disabled':isFirstPage()}">
183 <span class="p-paginator-icon pi pi-angle-left"></span>
184 </button>
185 <span class="p-paginator-pages" *ngIf="showPageLinks">
186 <button type="button" *ngFor="let pageLink of pageLinks" class="p-paginator-page p-paginator-element p-link" [ngClass]="{'p-highlight': (pageLink-1 == getPage())}"
187 (click)="onPageLinkClick($event, pageLink - 1)" pRipple>{{pageLink}}</button>
188 </span>
189 <p-dropdown [options]="pageItems" [ngModel]="getPage()" *ngIf="showJumpToPageDropdown" styleClass="p-paginator-page-options"
190 (onChange)="onPageDropdownChange($event)" [appendTo]="dropdownAppendTo" [scrollHeight]="dropdownScrollHeight">
191 <ng-template pTemplate="selectedItem">{{currentPageReport}}</ng-template>
192 </p-dropdown>
193 <button type="button" [disabled]="isLastPage()" (click)="changePageToNext($event)" pRipple
194 class="p-paginator-next p-paginator-element p-link" [ngClass]="{'p-disabled':isLastPage()}">
195 <span class="p-paginator-icon pi pi-angle-right"></span>
196 </button>
197 <button type="button" [disabled]="isLastPage()" (click)="changePageToLast($event)" pRipple
198 class="p-paginator-last p-paginator-element p-link" [ngClass]="{'p-disabled':isLastPage()}">
199 <span class="p-paginator-icon pi pi-angle-double-right"></span>
200 </button>
201 <p-dropdown [options]="rowsPerPageItems" [(ngModel)]="rows" *ngIf="rowsPerPageOptions" styleClass="p-paginator-rpp-options"
202 (onChange)="onRppChange($event)" [appendTo]="dropdownAppendTo" [scrollHeight]="dropdownScrollHeight"></p-dropdown>
203 <div class="p-paginator-right-content" *ngIf="templateRight">
204 <ng-container *ngTemplateOutlet="templateRight; context: {$implicit: paginatorState}"></ng-container>
205 </div>
206 </div>
207 `,
208 changeDetection: ChangeDetectionStrategy.OnPush,
209 encapsulation: ViewEncapsulation.None,
210 styles: [".p-paginator{-ms-flex-align:center;-ms-flex-pack:center;-ms-flex-wrap:wrap;align-items:center;display:-ms-flexbox;display:flex;flex-wrap:wrap;justify-content:center}.p-paginator-left-content{margin-right:auto}.p-paginator-right-content{margin-left:auto}.p-paginator-current,.p-paginator-first,.p-paginator-last,.p-paginator-next,.p-paginator-page,.p-paginator-prev{-moz-user-select:none;-ms-flex-align:center;-ms-flex-pack:center;-ms-user-select:none;-webkit-user-select:none;align-items:center;cursor:pointer;display:-ms-inline-flexbox;display:inline-flex;justify-content:center;line-height:1;overflow:hidden;position:relative;user-select:none}.p-paginator-element:focus{position:relative;z-index:1}"]
211 },] }
212];
213Paginator.ctorParameters = () => [
214 { type: ChangeDetectorRef }
215];
216Paginator.propDecorators = {
217 pageLinkSize: [{ type: Input }],
218 onPageChange: [{ type: Output }],
219 style: [{ type: Input }],
220 styleClass: [{ type: Input }],
221 alwaysShow: [{ type: Input }],
222 templateLeft: [{ type: Input }],
223 templateRight: [{ type: Input }],
224 dropdownAppendTo: [{ type: Input }],
225 dropdownScrollHeight: [{ type: Input }],
226 currentPageReportTemplate: [{ type: Input }],
227 showCurrentPageReport: [{ type: Input }],
228 totalRecords: [{ type: Input }],
229 rows: [{ type: Input }],
230 rowsPerPageOptions: [{ type: Input }],
231 showJumpToPageDropdown: [{ type: Input }],
232 showPageLinks: [{ type: Input }],
233 first: [{ type: Input }]
234};
235class PaginatorModule {
236}
237PaginatorModule.decorators = [
238 { type: NgModule, args: [{
239 imports: [CommonModule, DropdownModule, FormsModule, SharedModule, RippleModule],
240 exports: [Paginator, DropdownModule, FormsModule, SharedModule],
241 declarations: [Paginator]
242 },] }
243];
244
245/**
246 * Generated bundle index. Do not edit.
247 */
248
249export { Paginator, PaginatorModule };
250//# sourceMappingURL=primeng-paginator.js.map