-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paginator.js
254 lines (196 loc) · 8.1 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
254
/**
* Created by sasablagojevic on 3/7/17.
*/
function Paginator(config) {
var self = this;
this.paginationId = 'pagination';
var totalPages = 0;
this.rowsPerPage = 10;
var offset = 0;
var totalRows = 0;
var page = 1;
this.pagesLimit = 5;
this.parent = '';
this.clickEvent = null;
this.className = 'pagination';
/*Setters*/
var setTotalPages = function (rows) {
totalPages = Math.ceil(rows / self.rowsPerPage);
}
this.setTotalRows = function (rows) {
rows = parseInt(rows)
console.log('Total rows:'+rows)
totalRows = rows;
setTotalPages(rows);
}
var setOffset = function(page) {
offset = (page - 1) * self.rowsPerPage;
}
this.setPage = function(no) {
no = parseInt(no);
if (no < 1) {
no = 1;
}
if (totalPages !== 0 && no > totalPages) {
no = totalPages;
}
page = no;
setOffset(page);
}
/*Getters*/
this.getTotalRows = function () {
return totalRows;
}
this.getRowsPerPage = function() {
return this.rowsPerPage;
}
this.getPage = function () {
return page;
}
this.getTotalPages = function () {
return totalPages;
}
this.getOffset = function () {
return offset;
}
this.createLinks = function (clickEvent) {
var ul = document.createElement('ul');
ul.setAttribute('id', this.paginationId);
ul.className = this.className;
console.log('Total pages:'+totalPages);
console.log('Limit:'+this.pagesLimit);
if (totalPages > 1) {
var previous = document.createElement('li');
previous.innerHTML = '<a id="previous" href="#" aria-label="Previous"><i class="fa fa-angle-left"></i></a>';
ul.appendChild(previous);
var limit = this.pagesLimit;
if (limit >= totalPages) {
for (var i = 1; i <= totalPages; i++) {
var li = document.createElement('li');
li.innerHTML = '<a id="pg-'+i+'" href="#"'+(i == page ? 'class="active"' : '')+'>'+i+'</a>';
ul.appendChild(li);
}
} else {
if (page < limit) {
for (var i = 1; i <= limit; i++) {
var li = document.createElement('li');
li.innerHTML = '<a id="pg-'+i+'" href="#"'+(i == page ? 'class="active"' : '')+'>'+i+'</a>';
ul.appendChild(li);
}
if (limit < totalPages) {
var li = document.createElement('li');
li.innerHTML = '<a href="javascript:void(0)" data-disabled="1" disabled>...</a>';
var lastPage = document.createElement('li');
lastPage.innerHTML = '<a id="pg-'+totalPages+'" href="#"'+totalPages+'>'+totalPages+'</a>';
ul.appendChild(li);
ul.appendChild(lastPage);
}
}
if (page >= limit) {
var start = page - ((limit - 3) / 2);
start = start < 1 ? 1 : start;
var end = page + ((limit - 3) / 2);
end = end > totalPages ? totalPages : end;
if (page + limit - 1 > totalPages) {
start = totalPages - limit + 1;
end = totalPages;
}
var li = document.createElement('li');
li.innerHTML = '<a href="javascript:void(0)" data-disabled="1" disabled>...</a>';
var firstPage = document.createElement('li');
firstPage.innerHTML = '<a id="pg-1" href="#">1</a>';
ul.appendChild(firstPage);
ul.appendChild(li);
for (var i = start; i <= end; i++) {
var li = document.createElement('li');
li.innerHTML = '<a id="pg-'+i+'" href="#"'+(i == page ? 'class="active"' : '')+'>'+i+'</a>';
ul.appendChild(li);
}
if (page + limit - 1 <= totalPages) {
var li = document.createElement('li');
li.innerHTML = '<a href="javascript:void(0)" data-disabled="1" disabled>...</a>';
var lastPage = document.createElement('li');
lastPage.innerHTML = '<a id="pg-'+totalPages+'" href="#"'+totalPages+'>'+totalPages+'</a>';
ul.appendChild(li);
ul.appendChild(lastPage);
}
}
}
var next = document.createElement('li');
next.innerHTML = '<a id="next" href="#" aria-label="Next"><i class="fa fa-angle-right"></i></a>';
ul.appendChild(next);
} else {
var previous = document.createElement('li');
previous.innerHTML = '<a href="javascript:void(0)" aria-label="Previous" data-disabled="1" disabled=""><i class="fa fa-angle-left"></i></a>';
var disabled = document.createElement('li');
disabled.innerHTML = '<a href="javascript:void(0)" data-disabled="1" disabled>...</a>';
var next = document.createElement('li');
next.innerHTML = '<a href="javascript:void(0)" aria-label="Next" data-disabled="1" disabled><i class="fa fa-angle-right"></i></a>';
ul.appendChild(previous);
ul.appendChild(disabled);
ul.appendChild(next);
}
if (this.parent != '' && this.parent != null) {
var pagination = document.getElementById(this.paginationId);
if (pagination) {
pagination.parentNode.removeChild(pagination)
}
if (typeof this.parent !== 'string') {
this.parent.appendChild(ul);
} else {
document.querySelector(this.parent).appendChild(ul);
}
this.attachEvents(typeof clickEvent !== 'undefined' ? clickEvent : (this.clickEvent ? this.clickEvent : undefined));
}
return ul.outerHTML;
}
/**
* Attach pagination click events
* @param clickEventCallback function
*/
this.attachEvents = function (clickEventCallback) {
var ul = document.getElementById(this.paginationId);
if (ul) {
var links = Array.prototype.slice.call(ul.children);
links.forEach(function (link) {
link.firstChild.addEventListener('click', function (e) {
e.preventDefault();
if (this.dataset.disabled == 1) {
return false;
}
if (this.className.indexOf('active') > -1) {
return false;
}
var id = this.getAttribute('id');
if (id === 'next') {
if (self.getPage() + 1 > self.getTotalPages()) {
return false;
}
self.setPage(self.getPage() + 1);
} else if (id == 'previous') {
if ((self.getPage() - 1) < 1) {
return false;
}
self.setPage(self.getPage() - 1);
} else {
self.setPage(parseInt(this.innerHTML));
}
if (typeof clickEventCallback !== 'undefined') {
clickEventCallback(self);
}
});
})
}
}
var __construct = function(config) {
if (typeof config !== 'undefined') {
for (var key in config) {
if (key === 'totalRows') {
self.setTotalRows(config[key]);
} else if (self.hasOwnProperty(key)){
self[key] = config[key];
}
}
}
}(config);
}