-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimple-bootstrap-paginator.js
281 lines (248 loc) · 9 KB
/
simple-bootstrap-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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
;(function($, window, document, undefined) {
'use strict';
/**
* Simple boostrap pagination v 0.1
*/
var pluginName = 'simplePaginator';
var defaults = {
totalPages: 7,
maxButtonsVisible: 5,
currentPage: 1,
nextLabel: 'next',
prevLabel: 'prev',
firstLabel: 'first',
lastLabel: 'last',
clickCurrentPage: true,
pageChange: function(page) { console.log(page) }
};
/*
* Function that define the plugin
* element - the DOM element
* options - object with the plugin options
*/
function Plugin(element, options) {
options = verifyOptions(options)
this.element = element;
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
changeTotalButtons(this.options); // Change total buttons created by the Paginator
this.setTotalPages = function(total) {
if (total < 1) {
throw('Total Pages can\'t be less than 1');
}
this.options.totalPages = total;
changeTotalButtons(this.options);
createPaginator(this);
}
this.changePage = function(page) {
if (page < 1) {
throw('Page can\'t be less than 1');
} else if (page > this.options.totalPages) {
throw('Page is bigger than total pages');
}
this.options.currentPage = page;
this.options.pageChange(page);
createPaginator(this);
}
this.hide = function() {
$(this.element).empty();
}
this.setPageChangeFn = function(pageChange, currentPage) {
if (typeof pageChange !== 'function') {
throw ('pageChange is not a function');
}
this.options.pageChange = pageChange;
this.options.currentPage = (currentPage ? currentPage: 1);
this.options.pageChange(this.options.currentPage);
}
this.init();
}
Plugin.prototype.init = function() {
if (this.options.clickCurrentPage) {
this.changePage(this.options.currentPage);
} else {
createPaginator(this);
}
};
var verifyOptions = function(opts) {
// TotalPages - verifications
if (!opts.totalPages) {
throw('totalPages is not defined');
}
opts.totalPages = verifyOptionsNumber(opts.totalPages);
// pageChange() - verifications
if (!opts.pageChange) {
throw('function pageChange() its not defined');
} else if (typeof opts.pageChange !== 'function') {
throw('pageChange is not a function');
}
if (opts.maxButtonsVisible) {
opts.maxButtonsVisible = verifyOptionsNumber(opts.maxButtonsVisible);
}
if (opts.currentPage) {
opts.currentPage = verifyOptionsNumber(opts.currentPage);
}
// if has a label, convert to String.
if (opts.firstLabel) {
opts.firstLabel = opts.firstLabel.toString();
}
if (opts.nextLabel) {
opts.nextLabel = opts.nextLabel.toString();
}
if (opts.prevLabel) {
opts.prevLabel = opts.prevLabel.toString();
}
if (opts.lastLabel) {
opts.lastLabel = opts.lastLabel.toString();
}
// check clickCurrentPage
if (opts.clickCurrentPage) {
if (typeof opts.clickCurrentPage !== 'boolean') {
throw 'clickCurrentPage is must be a boolean';
}
}
return opts;
};
var verifyOptionsNumber = function(param) {
if (typeof param !== 'number') {
throw (param + ' is not a number');
} else if (param < 1) {
throw (param + ' must be bigger than 0');
}
return Math.floor(param);
};
var changeTotalButtons = function(options) {
// options.totalButtons - Its necessary to create the numeric buttons.
options.totalButtons = Math.min(options.totalPages, options.maxButtonsVisible);
};
var createPaginator = function(self) {
var opt = self.options;
//Clear the this.element
$(self.element).empty();
var str = '<ul class="pagination">';
// Create the prev and first button
if (opt.currentPage === 1) {
str = str.concat('<li class="page-item disabled"><a class="page-link">', opt.firstLabel, '</a></li>');
str = str.concat('<li class="page-item disabled"><a class="page-link">', opt.prevLabel, '</a></li>');
} else {
str = str.concat('<li class="page-item"><a class="page-link" style="cursor:pointer;">', opt.firstLabel, '</a></li>');
str = str.concat('<li class="page-item"><a class="page-link" style="cursor:pointer;">', opt.prevLabel, '</a></li>');
}
// Create the numeric buttons.
// Variable of number control in the buttons.
var begin = 1;
var end = begin + opt.totalButtons - 1;
/*
* Align the values in the begin and end variables if the user has the
* possibility that select a page that doens't appear in the paginador.
* e.g currentPage = 1, and user go to the 20 page.
*/
while ((opt.currentPage < begin) || (opt.currentPage > end)) {
if (opt.currentPage > end) {
begin += opt.totalButtons;
end += opt.totalButtons;
if (end > opt.totalPages) {
begin = begin - (end - opt.totalPages);
end = opt.totalPages;
}
} else {
begin -= opt.totalButtons;
end -= opt.totalButtons;
if (begin < 0) {
end = end + (begin + opt.totalButtons);
begin = 1;
}
}
}
/*
* Verify if the user clicks in the last page show by paginator.
* If yes, the paginator advances.
*/
if ((opt.currentPage === end) && (opt.totalPages != 1)) {
begin = opt.currentPage - 1;
end = begin + opt.totalButtons - 1;
if (end >= opt.totalPages) {
begin = begin - (end - (opt.totalPages));
end = opt.totalPages;
}
}
/*
* Verify it the user clicks in the first page show by paginator.
* If yes, the paginator retrogress
*/
if ((begin === opt.currentPage) && (opt.totalPages != 1)) {
if (opt.currentPage != 1) {
end = opt.currentPage + 1;
begin = end - (opt.totalButtons - 1);
}
}
// Create the numeric buttons.
for (var i = begin; i <= end; i++) {
if (i === opt.currentPage) {
str = str.concat('<li class="page-item active"><a class="page-link" style="cursor:pointer;">' + i + '</a></li>');
} else {
str = str.concat('<li class="page-item"><a class="page-link" style="cursor:pointer;">' + i + '</a></li>')
}
}
// Create the 'next' and 'last' button.
if (opt.currentPage == opt.totalPages) {
str = str.concat('<li class="page-item disabled"><a class="page-link">', opt.nextLabel, '</a></li>');
str = str.concat('<li class="page-item disabled"><a class="page-link">', opt.lastLabel, '</a></li>')
} else {
str = str.concat('<li class="page-item"><a class="page-link" style="cursor:pointer;">', opt.nextLabel, '</a></li>');
str = str.concat('<li class="page-item"><a class="page-link" style="cursor:pointer;">', opt.lastLabel, '</a></li>');
}
str = str.concat('</ul>');
$(self.element).append(str);
$(self.element).find('ul li').not('.disabled').not('.active').click(function() {
var btn = $(this).find('a').text();
var page;
switch (btn) {
case opt.firstLabel:
page = 1;
break;
case opt.prevLabel:
page = opt.currentPage - 1;
break;
case opt.nextLabel:
page = opt.currentPage + 1;
break;
case opt.lastLabel:
page = opt.totalPages;
break;
default:
page = parseInt(btn);
}
//var page = parseInt($(this).find('a').text());
//opt.currentPage = page;
self.changePage(page);
})
}
$.fn[pluginName] = function(options) {
/*
* Get the arguments
*/
var args = $.makeArray(arguments);
var after = args.slice(1);
return this.each(function() {
// Verify if already exists a plugin instance.
var instance = $.data(this, 'plugin_' + pluginName);
/*
* If exists, execute the method described in options param.
*/
if (instance) {
if (instance[options]) {
instance[options].apply(instance, after);
} else {
throw('Method ' + options + 'doesn\'t exists');
}
} else {
// Create and register the Plugin.
var plugin = new Plugin(this, options);
$.data(this, 'plugin_' + pluginName, plugin);
return plugin;
}
});
}
})(jQuery, window, document);