1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | const Base = require('../base/Base');
|
7 |
|
8 | module.exports = class Pagination extends Base {
|
9 |
|
10 | static getConstants () {
|
11 | return {
|
12 | LINK_SELF: 'self',
|
13 | LINK_NEXT: 'next',
|
14 | LINK_PREV: 'prev',
|
15 | LINK_FIRST: 'first',
|
16 | LINK_LAST: 'last'
|
17 | };
|
18 | }
|
19 |
|
20 | constructor (config) {
|
21 | super({
|
22 | pageParam: 'page',
|
23 | pageSizeParam: 'pageSize',
|
24 | defaultPageSize: 10,
|
25 | pageSizeLimit: [1, 50],
|
26 | forcePageParam: true,
|
27 | validatePage: true,
|
28 | pageSize: null,
|
29 | page: null,
|
30 |
|
31 |
|
32 |
|
33 | ...config
|
34 | });
|
35 | if (!this.route) {
|
36 | this.route = this.controller.getCurrentRoute();
|
37 | }
|
38 | if (this.pageSize === null) {
|
39 | this.pageSize = this.getQueryParam(this.pageSizeParam, this.defaultPageSize);
|
40 | }
|
41 | this.setPageSize(this.pageSize);
|
42 | if (this.page === null) {
|
43 | this.page = parseInt(this.getQueryParam(this.pageParam, 1)) - 1;
|
44 | }
|
45 | this.setPage(this.page);
|
46 | }
|
47 |
|
48 | setPageSize (value) {
|
49 | if (value === null) {
|
50 | return this.pageSize = null;
|
51 | }
|
52 | value = parseInt(value);
|
53 | if (Array.isArray(this.pageSizeLimit) && this.pageSizeLimit.length === 2) {
|
54 | if (value < this.pageSizeLimit[0]) {
|
55 | value = this.pageSizeLimit[0];
|
56 | } else if (value > this.pageSizeLimit[1]) {
|
57 | value = this.pageSizeLimit[1];
|
58 | }
|
59 | }
|
60 | this.pageSize = value;
|
61 | }
|
62 |
|
63 | setPage (value) {
|
64 | if (value === null) {
|
65 | return this.page = null;
|
66 | }
|
67 | value = parseInt(value);
|
68 | if (this.validatePage) {
|
69 | const pageCount = this.getPageCount();
|
70 | if (value >= pageCount) {
|
71 | value = pageCount - 1;
|
72 | }
|
73 | }
|
74 | this.page = value < 0 ? 0 : value;
|
75 | }
|
76 |
|
77 | getPageCount () {
|
78 | if (this.pageSize < 1) {
|
79 | return this.totalCount > 0 ? 1 : 0;
|
80 | }
|
81 | const total = this.totalCount < 0 ? 0 : this.totalCount;
|
82 | return Math.floor((total + this.pageSize - 1) / this.pageSize);
|
83 | }
|
84 |
|
85 | getOffset () {
|
86 | return this.pageSize < 1 ? 0 : this.page * this.pageSize;
|
87 | }
|
88 |
|
89 | getLimit () {
|
90 | return this.pageSize < 1 ? -1 : this.pageSize;
|
91 | }
|
92 |
|
93 | getLinks () {
|
94 | const currentPage = this.page;
|
95 | const pageCount = this.getPageCount();
|
96 | const links = {
|
97 | [this.LINK_SELF]: this.createUrl(currentPage)
|
98 | };
|
99 | if (currentPage > 0) {
|
100 | links[this.LINK_FIRST] = this.createUrl(0);
|
101 | links[this.LINK_PREV] = this.createUrl(currentPage - 1);
|
102 | }
|
103 | if (currentPage < pageCount - 1) {
|
104 | links[this.LINK_NEXT] = this.createUrl(currentPage + 1);
|
105 | links[this.LINK_LAST] = this.createUrl(pageCount - 1);
|
106 | }
|
107 | return links;
|
108 | }
|
109 |
|
110 | getQueryParam (name, defaultValue) {
|
111 | const params = this.params || this.controller.getQueryParams();
|
112 | const value = params[name] ? parseInt(params[name]) : NaN;
|
113 | return Number.isNaN(value) ? defaultValue : value;
|
114 | }
|
115 |
|
116 | createUrl (page, pageSize) {
|
117 | const params = this.params || {...this.controller.getQueryParams()};
|
118 | if (page > 0 || page >= 0 && this.forcePageParam) {
|
119 | params[this.pageParam] = page + 1;
|
120 | } else {
|
121 | delete params[this.pageParam];
|
122 | }
|
123 | pageSize = pageSize || this.pageSize;
|
124 | if (pageSize !== this.defaultPageSize) {
|
125 | params[this.pageSizeParam] = pageSize;
|
126 | } else {
|
127 | delete params[this.pageSizeParam];
|
128 | }
|
129 | return this.controller.createUrl([this.route, params]);
|
130 | }
|
131 | };
|
132 | module.exports.init(); |
\ | No newline at end of file |