forked from rexxars/paginator.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paginator.js
253 lines (211 loc) · 7.12 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
Paginator = (function() {
var p = function() {};
/**
* Sets the total number of items in the dataset
*/
p.prototype.setTotalItems = function(num) {
this.totalItems = num;
return this;
};
/**
* Returns the total number of items (or 0 if not defined)
*/
p.prototype.getTotalItems = function() {
return this.totalItems || 0;
};
/**
* Sets the number of items per page
*/
p.prototype.setItemsPerPage = function(num) {
this.itemsPerPage = num;
return this;
};
/**
* Returns the number of items per page
* Defaults to 10.
*/
p.prototype.getItemsPerPage = function() {
return this.itemsPerPage || 10;
};
/**
* Sets an array to use as the dataset, for use with getItemsByPage() etc
*/
p.prototype.setData = function(data) {
this.data = data;
this.setTotalItems(data.length);
return this;
};
/**
* Sets the current page number
*/
p.prototype.setCurrentPage = function(num) {
this.currentPage = this.normalize(num);
return this;
};
/**
* Returns the total number of pages
*/
p.prototype.getNumberOfPages = function() {
return Math.ceil(this.getTotalItems() / this.getItemsPerPage());
};
/**
* Normalizes page number by making sure it is within the range of the paginator
*/
p.prototype.normalize = function(num) {
num = Math.max(parseInt(num, 10), 1);
var pages = this.getNumberOfPages();
if (pages > 0 && num > pages) {
num = pages;
}
return num;
};
/**
* Returns the given page of the dataset, or false if no data has been set
*/
p.prototype.getItemsByPage = function(page) {
page = this.normalize(page);
var offset = (page - 1) * this.getItemsPerPage();
return this.data ? this.data.slice(offset, this.getItemsPerPage()) : false;
};
/**
* Returns the current page of the dataset, or false if no data has been set
*/
p.prototype.getCurrentItems = function() {
return this.getItemsByPage(this.currentPage || 1);
};
/**
* Sets page range (number of pages to show in pagination control)
*/
p.prototype.setPageRange = function(num) {
this.pageRange = num;
return this;
};
/**
* Returns the set page range (number of pages to show in pagination control)
* Defaults to 10.
*/
p.prototype.getPageRange = function() {
return this.pageRange || 10;
};
/**
* Creates an array containing the page numbers within the current range
*/
p.prototype.getPagesInRange = function() {
var pageRange = this.getPageRange();
var pageNumber = this.currentPage || 1;
var pageCount = this.getNumberOfPages();
if (pageRange > pageCount) {
pageRange = pageCount;
}
var delta = Math.ceil(pageRange / 2), lowerBound, upperBound;
if (pageNumber - delta > pageCount - pageRange) {
lowerBound = pageCount - pageRange + 1;
upperBound = pageCount;
} else {
if (pageNumber - delta < 0) {
delta = pageNumber;
}
offset = pageNumber - delta;
lowerBound = offset + 1;
upperBound = offset + pageRange;
}
lowerBound = this.normalize(lowerBound);
upperBound = this.normalize(upperBound);
var pages = [];
for (pageNumber = lowerBound; pageNumber <= upperBound; pageNumber++) {
pages.push(pageNumber);
}
return pages;
};
/**
* Creates an object containing all the information needed to build a pagination control
*/
p.prototype.getInfo = function() {
var pageCount = this.getNumberOfPages();
var current = this.currentPage || 1;
var info = {
pageCount : pageCount,
itemsPerPage : this.getItemsPerPage(),
totalItems : this.getTotalItems(),
current : current,
first : 1,
last : pageCount,
};
// Previous and next
if (current - 1 > 0) {
info.previous = current - 1;
}
if (current + 1 <= pageCount) {
info.next = current + 1;
}
// Pages in range
info.pagesInRange = this.getPagesInRange();
info.firstPageInRange = Math.min.apply(Math, info.pagesInRange);
info.lastPageInRange = Math.max.apply(Math, info.pagesInRange);
return info;
};
/**
* Build a URL based on the passed page and URL template
*
* Allowed template variables:
* {page} - Replaced with the page number passed
* {limit} - Replaced with the number of items per page
* {offset} - Replaced with the calculated offset for passed page number
*/
p.prototype.getUrl = function(pageNum, url) {
var page = this.normalize(pageNum);
var offset = (page - 1) * this.getItemsPerPage();
var limit = this.getItemsPerPage();
return (url || '?page={page}').replace(/\{page\}/g, page).replace(/\{offset\}/g, offset).replace(/\{limit\}/g, limit);
}
/**
* Creates HTML markup for a pagination control
*/
p.prototype.render = function(options) {
var pages = this.getInfo(), o = options || {};
if (pages.pageCount <= 1) {
return '';
}
var html = ['<ul class="' + (o.paginatorClass || 'paginator') + '">'];
if (pages.previous) {
// "First"-link
html.push(
'<li class="', (o.firstClass || 'first'), '">',
'<a href="', this.getUrl(pages.first, o.url), '">', (o.firstText || '«'), '</a>',
'</li>'
);
// "Previous"-link
html.push(
'<li class="', (o.prevClass || 'previous'), '">',
'<a href="', this.getUrl(pages.previous, o.url), '">', (o.prevText || '←'), '</a>',
'</li>'
);
}
var page, active, i = 0;
for (i = 0; i < pages.pagesInRange.length; i++) {
page = pages.pagesInRange[i];
active = (pages.current == page) ? ' class="' + (o.activeClass || 'active') + '"' : '';
html.push('<li', active, '><a href="', this.getUrl(page, o.url), '">', page, '</a></li>');
}
if (pages.next) {
// "Next"-link
html.push(
'<li class="', (o.nextClass || 'next'), '">',
'<a href="', this.getUrl(pages.next, o.url), '">', (o.prevText || '→'), '</a>',
'</li>'
);
// "Last"-link
html.push(
'<li class="', (o.lastClass || 'last'), '">',
'<a href="', this.getUrl(pages.last, o.url), '">', (o.lastText || '»'), '</a>',
'</li>'
);
}
html.push('</ul>');
return html.join('');
};
return p;
})();
if (typeof module !== 'undefined') {
module.exports = Paginator;
}