1 | import { Component, ElementRef, Renderer, Input, Output, EventEmitter, forwardRef } from '@angular/core';
|
2 | import { NG_VALUE_ACCESSOR } from '@angular/forms';
|
3 | import { PaginationConfig } from './pagination.config';
|
4 | export var PAGER_CONTROL_VALUE_ACCESSOR = {
|
5 | provide: NG_VALUE_ACCESSOR,
|
6 | useExisting: forwardRef(function () { return PagerComponent; }),
|
7 | multi: true
|
8 | };
|
9 | var PAGER_TEMPLATE = "\n <ul class=\"pager\">\n <li [class.disabled]=\"noPrevious()\" [class.previous]=\"align\" [ngClass]=\"{'pull-right': align}\" class=\"{{ pageBtnClass }}\">\n <a href (click)=\"selectPage(page - 1, $event)\">{{getText('previous')}}</a>\n </li>\n <li [class.disabled]=\"noNext()\" [class.next]=\"align\" [ngClass]=\"{'pull-right': align}\" class=\"{{ pageBtnClass }}\">\n <a href (click)=\"selectPage(page + 1, $event)\">{{getText('next')}}</a>\n </li>\n </ul>\n";
|
10 | export var PagerComponent = (function () {
|
11 | function PagerComponent(renderer, elementRef, paginationConfig) {
|
12 |
|
13 | this.numPages = new EventEmitter();
|
14 |
|
15 | this.pageChanged = new EventEmitter();
|
16 | this.onChange = Function.prototype;
|
17 | this.onTouched = Function.prototype;
|
18 | this.inited = false;
|
19 | this._page = 1;
|
20 | this.renderer = renderer;
|
21 | this.elementRef = elementRef;
|
22 | if (!this.config) {
|
23 | this.configureOptions(Object.assign({}, paginationConfig.main, paginationConfig.pager));
|
24 | }
|
25 | }
|
26 | Object.defineProperty(PagerComponent.prototype, "itemsPerPage", {
|
27 |
|
28 | get: function () {
|
29 | return this._itemsPerPage;
|
30 | },
|
31 | set: function (v) {
|
32 | this._itemsPerPage = v;
|
33 | this.totalPages = this.calculateTotalPages();
|
34 | },
|
35 | enumerable: true,
|
36 | configurable: true
|
37 | });
|
38 | Object.defineProperty(PagerComponent.prototype, "totalItems", {
|
39 |
|
40 | get: function () {
|
41 | return this._totalItems;
|
42 | },
|
43 | set: function (v) {
|
44 | this._totalItems = v;
|
45 | this.totalPages = this.calculateTotalPages();
|
46 | },
|
47 | enumerable: true,
|
48 | configurable: true
|
49 | });
|
50 | Object.defineProperty(PagerComponent.prototype, "totalPages", {
|
51 | get: function () {
|
52 | return this._totalPages;
|
53 | },
|
54 | set: function (v) {
|
55 | this._totalPages = v;
|
56 | this.numPages.emit(v);
|
57 | if (this.inited) {
|
58 | this.selectPage(this.page);
|
59 | }
|
60 | },
|
61 | enumerable: true,
|
62 | configurable: true
|
63 | });
|
64 | Object.defineProperty(PagerComponent.prototype, "page", {
|
65 | get: function () {
|
66 | return this._page;
|
67 | },
|
68 | set: function (value) {
|
69 | var _previous = this._page;
|
70 | this._page = (value > this.totalPages) ? this.totalPages : (value || 1);
|
71 | if (_previous === this._page || typeof _previous === 'undefined') {
|
72 | return;
|
73 | }
|
74 | this.pageChanged.emit({
|
75 | page: this._page,
|
76 | itemsPerPage: this.itemsPerPage
|
77 | });
|
78 | },
|
79 | enumerable: true,
|
80 | configurable: true
|
81 | });
|
82 | PagerComponent.prototype.configureOptions = function (config) {
|
83 | this.config = Object.assign({}, config);
|
84 | };
|
85 | PagerComponent.prototype.ngOnInit = function () {
|
86 | this.classMap = this.elementRef.nativeElement.getAttribute('class') || '';
|
87 |
|
88 | this.maxSize = typeof this.maxSize !== 'undefined'
|
89 | ? this.maxSize
|
90 | : this.config.maxSize;
|
91 | this.rotate = typeof this.rotate !== 'undefined'
|
92 | ? this.rotate
|
93 | : this.config.rotate;
|
94 | this.boundaryLinks = typeof this.boundaryLinks !== 'undefined'
|
95 | ? this.boundaryLinks
|
96 | : this.config.boundaryLinks;
|
97 | this.directionLinks = typeof this.directionLinks !== 'undefined'
|
98 | ? this.directionLinks
|
99 | : this.config.directionLinks;
|
100 | this.pageBtnClass = typeof this.pageBtnClass !== 'undefined'
|
101 | ? this.pageBtnClass
|
102 | : this.config.pageBtnClass;
|
103 |
|
104 | this.itemsPerPage = typeof this.itemsPerPage !== 'undefined'
|
105 | ? this.itemsPerPage
|
106 | : this.config.itemsPerPage;
|
107 | this.totalPages = this.calculateTotalPages();
|
108 |
|
109 | this.pages = this.getPages(this.page, this.totalPages);
|
110 | this.inited = true;
|
111 | };
|
112 | PagerComponent.prototype.writeValue = function (value) {
|
113 | this.page = value;
|
114 | this.pages = this.getPages(this.page, this.totalPages);
|
115 | };
|
116 | PagerComponent.prototype.getText = function (key) {
|
117 | return this[key + 'Text'] || this.config[key + 'Text'];
|
118 | };
|
119 | PagerComponent.prototype.noPrevious = function () {
|
120 | return this.page === 1;
|
121 | };
|
122 | PagerComponent.prototype.noNext = function () {
|
123 | return this.page === this.totalPages;
|
124 | };
|
125 | PagerComponent.prototype.registerOnChange = function (fn) {
|
126 | this.onChange = fn;
|
127 | };
|
128 | PagerComponent.prototype.registerOnTouched = function (fn) {
|
129 | this.onTouched = fn;
|
130 | };
|
131 | PagerComponent.prototype.selectPage = function (page, event) {
|
132 | if (event) {
|
133 | event.preventDefault();
|
134 | }
|
135 | if (!this.disabled) {
|
136 | if (event && event.target) {
|
137 | var target = event.target;
|
138 | target.blur();
|
139 | }
|
140 | this.writeValue(page);
|
141 | this.onChange(this.page);
|
142 | }
|
143 | };
|
144 |
|
145 | PagerComponent.prototype.makePage = function (num, text, active) {
|
146 | return { text: text, number: num, active: active };
|
147 | };
|
148 | PagerComponent.prototype.getPages = function (currentPage, totalPages) {
|
149 | var pages = [];
|
150 |
|
151 | var startPage = 1;
|
152 | var endPage = totalPages;
|
153 | var isMaxSized = typeof this.maxSize !== 'undefined' && this.maxSize < totalPages;
|
154 |
|
155 | if (isMaxSized) {
|
156 | if (this.rotate) {
|
157 |
|
158 | startPage = Math.max(currentPage - Math.floor(this.maxSize / 2), 1);
|
159 | endPage = startPage + this.maxSize - 1;
|
160 |
|
161 | if (endPage > totalPages) {
|
162 | endPage = totalPages;
|
163 | startPage = endPage - this.maxSize + 1;
|
164 | }
|
165 | }
|
166 | else {
|
167 |
|
168 | startPage = ((Math.ceil(currentPage / this.maxSize) - 1) * this.maxSize) + 1;
|
169 |
|
170 | endPage = Math.min(startPage + this.maxSize - 1, totalPages);
|
171 | }
|
172 | }
|
173 |
|
174 | for (var num = startPage; num <= endPage; num++) {
|
175 | var page = this.makePage(num, num.toString(), num === currentPage);
|
176 | pages.push(page);
|
177 | }
|
178 |
|
179 | if (isMaxSized && !this.rotate) {
|
180 | if (startPage > 1) {
|
181 | var previousPageSet = this.makePage(startPage - 1, '...', false);
|
182 | pages.unshift(previousPageSet);
|
183 | }
|
184 | if (endPage < totalPages) {
|
185 | var nextPageSet = this.makePage(endPage + 1, '...', false);
|
186 | pages.push(nextPageSet);
|
187 | }
|
188 | }
|
189 | return pages;
|
190 | };
|
191 |
|
192 | PagerComponent.prototype.calculateTotalPages = function () {
|
193 | var totalPages = this.itemsPerPage < 1
|
194 | ? 1
|
195 | : Math.ceil(this.totalItems / this.itemsPerPage);
|
196 | return Math.max(totalPages || 0, 1);
|
197 | };
|
198 | PagerComponent.decorators = [
|
199 | { type: Component, args: [{
|
200 | selector: 'pager',
|
201 | template: PAGER_TEMPLATE,
|
202 | providers: [PAGER_CONTROL_VALUE_ACCESSOR]
|
203 | },] },
|
204 | ];
|
205 |
|
206 | PagerComponent.ctorParameters = function () { return [
|
207 | { type: Renderer, },
|
208 | { type: ElementRef, },
|
209 | { type: PaginationConfig, },
|
210 | ]; };
|
211 | PagerComponent.propDecorators = {
|
212 | 'align': [{ type: Input },],
|
213 | 'maxSize': [{ type: Input },],
|
214 | 'boundaryLinks': [{ type: Input },],
|
215 | 'directionLinks': [{ type: Input },],
|
216 | 'firstText': [{ type: Input },],
|
217 | 'previousText': [{ type: Input },],
|
218 | 'nextText': [{ type: Input },],
|
219 | 'lastText': [{ type: Input },],
|
220 | 'rotate': [{ type: Input },],
|
221 | 'pageBtnClass': [{ type: Input },],
|
222 | 'disabled': [{ type: Input },],
|
223 | 'numPages': [{ type: Output },],
|
224 | 'pageChanged': [{ type: Output },],
|
225 | 'itemsPerPage': [{ type: Input },],
|
226 | 'totalItems': [{ type: Input },],
|
227 | };
|
228 | return PagerComponent;
|
229 | }());
|
230 |
|
\ | No newline at end of file |