-
Notifications
You must be signed in to change notification settings - Fork 0
/
paginator.js
118 lines (100 loc) · 2.44 KB
/
paginator.js
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
'use strict';
var url = require('url');
/**
* The Pager object.
*
* @param {object} params
* @param {number} [params.page] the current page. Default is 1
* @param {number} params.total total number of pages
* @param {number} [params.maxPages] the maximum of pages in the pager. Default is 10
* @param {string} params.url the url that gets the pager query parameter appended
* @param {string} [params.queryParam] the query parameter name, for example use: page
* @constructor
*/
function Pager(params) {
this.page = params.page || 1;
if (typeof params.total !== 'number') {
throw new Error('Parameter total is required!');
}
this.total = params.total;
this.maxPages = params.maxPages || 10;
if (typeof params.url !== 'string') {
throw new Error('Parameter url is required!');
}
this.url = params.url;
this.queryParam = params.queryParam || 'page';
}
/**
* Build a url for a page
*
* @param {number} page
* @returns {string} the constructed url
*/
Pager.prototype.buildUrl = function (page) {
var parsed = url.parse(this.url, true);
parsed.search = undefined;
parsed.query[this.queryParam] = page;
return url.format(parsed);
};
/**
* Get the first page
*
* @returns {{page: number, isActive: boolean, url: string}}
*/
Pager.prototype.getFirst = function () {
return {
page: 1,
isActive: this.page === 1,
url: this.buildUrl(1)
};
};
/**
* Get the last page
*
* @returns {{page: number, isActive: boolean, url: string}}
*/
Pager.prototype.getLast = function () {
return {
page: this.total,
isActive: this.page === this.total,
url: this.buildUrl(this.total)
};
};
/**
* Get all pages that you want to render in your template
*
* @returns [{page: number, isActive: boolean, url: string}]
*/
Pager.prototype.getPages = function () {
var half = Math.floor(this.maxPages / 2),
start,
end,
result = [];
if (this.maxPages >= this.total) {
start = 1;
end = this.total;
} else {
start = this.page - half;
end = this.page + half;
if (this.maxPages % 2 === 0) {
start += 1;
}
if (this.page <= half) {
start = 1;
end = this.maxPages;
}
if (end > this.total) {
start = this.total - this.maxPages + 1;
end = this.total;
}
}
for (var i = start; i <= end; i++) {
result.push({
page: i,
isActive: i === this.page,
url: this.buildUrl(i)
});
}
return result;
};
module.exports = Pager;