paginator.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. var __metadata = (this && this.__metadata) || function (k, v) {
  9. if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. var core_1 = require("@angular/core");
  13. var common_1 = require("@angular/common");
  14. var forms_1 = require("@angular/forms");
  15. var dropdown_1 = require("../dropdown/dropdown");
  16. var shared_1 = require("../common/shared");
  17. var Paginator = /** @class */ (function () {
  18. function Paginator(cd) {
  19. this.cd = cd;
  20. this.pageLinkSize = 5;
  21. this.onPageChange = new core_1.EventEmitter();
  22. this.alwaysShow = true;
  23. this.dropdownScrollHeight = '200px';
  24. this.currentPageReportTemplate = '{currentPage} of {totalPages}';
  25. this._totalRecords = 0;
  26. this._first = 0;
  27. this._rows = 0;
  28. }
  29. Paginator.prototype.ngOnInit = function () {
  30. this.updatePaginatorState();
  31. };
  32. Object.defineProperty(Paginator.prototype, "totalRecords", {
  33. get: function () {
  34. return this._totalRecords;
  35. },
  36. set: function (val) {
  37. this._totalRecords = val;
  38. this.updatePageLinks();
  39. this.updatePaginatorState();
  40. this.updateFirst();
  41. this.updateRowsPerPageOptions();
  42. },
  43. enumerable: true,
  44. configurable: true
  45. });
  46. Object.defineProperty(Paginator.prototype, "first", {
  47. get: function () {
  48. return this._first;
  49. },
  50. set: function (val) {
  51. this._first = val;
  52. this.updatePageLinks();
  53. this.updatePaginatorState();
  54. },
  55. enumerable: true,
  56. configurable: true
  57. });
  58. Object.defineProperty(Paginator.prototype, "rows", {
  59. get: function () {
  60. return this._rows;
  61. },
  62. set: function (val) {
  63. this._rows = val;
  64. this.updatePageLinks();
  65. this.updatePaginatorState();
  66. },
  67. enumerable: true,
  68. configurable: true
  69. });
  70. Object.defineProperty(Paginator.prototype, "rowsPerPageOptions", {
  71. get: function () {
  72. return this._rowsPerPageOptions;
  73. },
  74. set: function (val) {
  75. this._rowsPerPageOptions = val;
  76. this.updateRowsPerPageOptions();
  77. },
  78. enumerable: true,
  79. configurable: true
  80. });
  81. Paginator.prototype.updateRowsPerPageOptions = function () {
  82. if (this.rowsPerPageOptions) {
  83. this.rowsPerPageItems = [];
  84. for (var _i = 0, _a = this.rowsPerPageOptions; _i < _a.length; _i++) {
  85. var opt = _a[_i];
  86. if (typeof opt == 'object' && opt['showAll']) {
  87. this.rowsPerPageItems.push({ label: opt['showAll'], value: this.totalRecords });
  88. }
  89. else {
  90. this.rowsPerPageItems.push({ label: String(opt), value: opt });
  91. }
  92. }
  93. }
  94. };
  95. Paginator.prototype.isFirstPage = function () {
  96. return this.getPage() === 0;
  97. };
  98. Paginator.prototype.isLastPage = function () {
  99. return this.getPage() === this.getPageCount() - 1;
  100. };
  101. Paginator.prototype.getPageCount = function () {
  102. return Math.ceil(this.totalRecords / this.rows) || 1;
  103. };
  104. Paginator.prototype.calculatePageLinkBoundaries = function () {
  105. var numberOfPages = this.getPageCount(), visiblePages = Math.min(this.pageLinkSize, numberOfPages);
  106. //calculate range, keep current in middle if necessary
  107. var start = Math.max(0, Math.ceil(this.getPage() - ((visiblePages) / 2))), end = Math.min(numberOfPages - 1, start + visiblePages - 1);
  108. //check when approaching to last page
  109. var delta = this.pageLinkSize - (end - start + 1);
  110. start = Math.max(0, start - delta);
  111. return [start, end];
  112. };
  113. Paginator.prototype.updatePageLinks = function () {
  114. this.pageLinks = [];
  115. var boundaries = this.calculatePageLinkBoundaries(), start = boundaries[0], end = boundaries[1];
  116. for (var i = start; i <= end; i++) {
  117. this.pageLinks.push(i + 1);
  118. }
  119. };
  120. Paginator.prototype.changePage = function (p) {
  121. var pc = this.getPageCount();
  122. if (p >= 0 && p < pc) {
  123. this.first = this.rows * p;
  124. var state = {
  125. page: p,
  126. first: this.first,
  127. rows: this.rows,
  128. pageCount: pc
  129. };
  130. this.updatePageLinks();
  131. this.onPageChange.emit(state);
  132. this.updatePaginatorState();
  133. }
  134. };
  135. Paginator.prototype.updateFirst = function () {
  136. var _this = this;
  137. var page = this.getPage();
  138. if (page > 0 && (this.first >= this.totalRecords)) {
  139. Promise.resolve(null).then(function () { return _this.changePage(page - 1); });
  140. }
  141. };
  142. Paginator.prototype.getPage = function () {
  143. return Math.floor(this.first / this.rows);
  144. };
  145. Paginator.prototype.changePageToFirst = function (event) {
  146. if (!this.isFirstPage()) {
  147. this.changePage(0);
  148. }
  149. event.preventDefault();
  150. };
  151. Paginator.prototype.changePageToPrev = function (event) {
  152. this.changePage(this.getPage() - 1);
  153. event.preventDefault();
  154. };
  155. Paginator.prototype.changePageToNext = function (event) {
  156. this.changePage(this.getPage() + 1);
  157. event.preventDefault();
  158. };
  159. Paginator.prototype.changePageToLast = function (event) {
  160. if (!this.isLastPage()) {
  161. this.changePage(this.getPageCount() - 1);
  162. }
  163. event.preventDefault();
  164. };
  165. Paginator.prototype.onPageLinkClick = function (event, page) {
  166. this.changePage(page);
  167. event.preventDefault();
  168. };
  169. Paginator.prototype.onRppChange = function (event) {
  170. this.changePage(this.getPage());
  171. };
  172. Paginator.prototype.updatePaginatorState = function () {
  173. this.paginatorState = {
  174. page: this.getPage(),
  175. pageCount: this.getPageCount(),
  176. rows: this.rows,
  177. first: this.first,
  178. totalRecords: this.totalRecords
  179. };
  180. };
  181. Object.defineProperty(Paginator.prototype, "currentPageReport", {
  182. get: function () {
  183. return this.currentPageReportTemplate
  184. .replace("{currentPage}", (this.getPage() + 1).toString())
  185. .replace("{totalPages}", this.getPageCount().toString());
  186. },
  187. enumerable: true,
  188. configurable: true
  189. });
  190. __decorate([
  191. core_1.Input(),
  192. __metadata("design:type", Number)
  193. ], Paginator.prototype, "pageLinkSize", void 0);
  194. __decorate([
  195. core_1.Output(),
  196. __metadata("design:type", core_1.EventEmitter)
  197. ], Paginator.prototype, "onPageChange", void 0);
  198. __decorate([
  199. core_1.Input(),
  200. __metadata("design:type", Object)
  201. ], Paginator.prototype, "style", void 0);
  202. __decorate([
  203. core_1.Input(),
  204. __metadata("design:type", String)
  205. ], Paginator.prototype, "styleClass", void 0);
  206. __decorate([
  207. core_1.Input(),
  208. __metadata("design:type", Boolean)
  209. ], Paginator.prototype, "alwaysShow", void 0);
  210. __decorate([
  211. core_1.Input(),
  212. __metadata("design:type", core_1.TemplateRef)
  213. ], Paginator.prototype, "templateLeft", void 0);
  214. __decorate([
  215. core_1.Input(),
  216. __metadata("design:type", core_1.TemplateRef)
  217. ], Paginator.prototype, "templateRight", void 0);
  218. __decorate([
  219. core_1.Input(),
  220. __metadata("design:type", Object)
  221. ], Paginator.prototype, "dropdownAppendTo", void 0);
  222. __decorate([
  223. core_1.Input(),
  224. __metadata("design:type", String)
  225. ], Paginator.prototype, "dropdownScrollHeight", void 0);
  226. __decorate([
  227. core_1.Input(),
  228. __metadata("design:type", String)
  229. ], Paginator.prototype, "currentPageReportTemplate", void 0);
  230. __decorate([
  231. core_1.Input(),
  232. __metadata("design:type", Boolean)
  233. ], Paginator.prototype, "showCurrentPageReport", void 0);
  234. __decorate([
  235. core_1.Input(),
  236. __metadata("design:type", Number),
  237. __metadata("design:paramtypes", [Number])
  238. ], Paginator.prototype, "totalRecords", null);
  239. __decorate([
  240. core_1.Input(),
  241. __metadata("design:type", Number),
  242. __metadata("design:paramtypes", [Number])
  243. ], Paginator.prototype, "first", null);
  244. __decorate([
  245. core_1.Input(),
  246. __metadata("design:type", Number),
  247. __metadata("design:paramtypes", [Number])
  248. ], Paginator.prototype, "rows", null);
  249. __decorate([
  250. core_1.Input(),
  251. __metadata("design:type", Array),
  252. __metadata("design:paramtypes", [Array])
  253. ], Paginator.prototype, "rowsPerPageOptions", null);
  254. Paginator = __decorate([
  255. core_1.Component({
  256. selector: 'p-paginator',
  257. template: "\n <div [class]=\"styleClass\" [ngStyle]=\"style\" [ngClass]=\"'ui-paginator ui-widget ui-widget-header ui-unselectable-text ui-helper-clearfix'\"\n *ngIf=\"alwaysShow ? true : (pageLinks && pageLinks.length > 1)\">\n <div class=\"ui-paginator-left-content\" *ngIf=\"templateLeft\">\n <ng-container *ngTemplateOutlet=\"templateLeft; context: {$implicit: paginatorState}\"></ng-container>\n </div>\n <span class=\"ui-paginator-current\" *ngIf=\"showCurrentPageReport\">{{currentPageReport}}</span>\n <a [attr.tabindex]=\"isFirstPage() ? null : '0'\" class=\"ui-paginator-first ui-paginator-element ui-state-default ui-corner-all\"\n (click)=\"changePageToFirst($event)\" (keydown.enter)=\"changePageToFirst($event)\" [ngClass]=\"{'ui-state-disabled':isFirstPage()}\" [tabindex]=\"isFirstPage() ? -1 : null\">\n <span class=\"ui-paginator-icon pi pi-step-backward\"></span>\n </a>\n <a tabindex=\"0\" [attr.tabindex]=\"isFirstPage() ? null : '0'\" class=\"ui-paginator-prev ui-paginator-element ui-state-default ui-corner-all\"\n (click)=\"changePageToPrev($event)\" (keydown.enter)=\"changePageToPrev($event)\" [ngClass]=\"{'ui-state-disabled':isFirstPage()}\" [tabindex]=\"isFirstPage() ? -1 : null\">\n <span class=\"ui-paginator-icon pi pi-caret-left\"></span>\n </a>\n <span class=\"ui-paginator-pages\">\n <a tabindex=\"0\" *ngFor=\"let pageLink of pageLinks\" class=\"ui-paginator-page ui-paginator-element ui-state-default ui-corner-all\"\n (click)=\"onPageLinkClick($event, pageLink - 1)\" (keydown.enter)=\"onPageLinkClick($event, pageLink - 1)\" [ngClass]=\"{'ui-state-active': (pageLink-1 == getPage())}\">{{pageLink}}</a>\n </span>\n <a [attr.tabindex]=\"isLastPage() ? null : '0'\" class=\"ui-paginator-next ui-paginator-element ui-state-default ui-corner-all\"\n (click)=\"changePageToNext($event)\" (keydown.enter)=\"changePageToNext($event)\" [ngClass]=\"{'ui-state-disabled':isLastPage()}\" [tabindex]=\"isLastPage() ? -1 : null\">\n <span class=\"ui-paginator-icon pi pi-caret-right\"></span>\n </a>\n <a [attr.tabindex]=\"isLastPage() ? null : '0'\" class=\"ui-paginator-last ui-paginator-element ui-state-default ui-corner-all\"\n (click)=\"changePageToLast($event)\" (keydown.enter)=\"changePageToLast($event)\" [ngClass]=\"{'ui-state-disabled':isLastPage()}\" [tabindex]=\"isLastPage() ? -1 : null\">\n <span class=\"ui-paginator-icon pi pi-step-forward\"></span>\n </a>\n <p-dropdown [options]=\"rowsPerPageItems\" [(ngModel)]=\"rows\" *ngIf=\"rowsPerPageOptions\" \n (onChange)=\"onRppChange($event)\" [appendTo]=\"dropdownAppendTo\" [scrollHeight]=\"dropdownScrollHeight\"></p-dropdown>\n <div class=\"ui-paginator-right-content\" *ngIf=\"templateRight\">\n <ng-container *ngTemplateOutlet=\"templateRight; context: {$implicit: paginatorState}\"></ng-container>\n </div>\n </div>\n "
  258. }),
  259. __metadata("design:paramtypes", [core_1.ChangeDetectorRef])
  260. ], Paginator);
  261. return Paginator;
  262. }());
  263. exports.Paginator = Paginator;
  264. var PaginatorModule = /** @class */ (function () {
  265. function PaginatorModule() {
  266. }
  267. PaginatorModule = __decorate([
  268. core_1.NgModule({
  269. imports: [common_1.CommonModule, dropdown_1.DropdownModule, forms_1.FormsModule, shared_1.SharedModule],
  270. exports: [Paginator, dropdown_1.DropdownModule, forms_1.FormsModule, shared_1.SharedModule],
  271. declarations: [Paginator]
  272. })
  273. ], PaginatorModule);
  274. return PaginatorModule;
  275. }());
  276. exports.PaginatorModule = PaginatorModule;
  277. //# sourceMappingURL=paginator.js.map