-
Notifications
You must be signed in to change notification settings - Fork 1
/
flexible.pagination.js
503 lines (442 loc) · 19.4 KB
/
flexible.pagination.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/**
* @Created By: Ademola Aina
* @Email: [email protected]
* @Year : 2018 Production
*
* DOCUMENTATION:
* ==============
* www.noahsarkeducation.com/code-lab/js/flexible-pagination
*
* LICENSE:
* ========
*
* TERMS OF USE - Flexible Pagination
*
* Open source under the BSD License.
*
* Copyright © 2018 Ademola Aina
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* Neither the name of the author nor the names of contributors may be used to endorse
* or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var Flexible = (typeof Flexible === 'undefined') ? {} : Flexible;
Flexible.Pagination = function(options){
var pager = this, defaultOption = {};
defaultOption.pagingControlsContainer = "#pagingControls";
defaultOption.pagingContainer = "#content";
defaultOption.itemSelector = ".item:visible"; //A Filtered Visible Paragraphs
defaultOption.itemsPerPageSelector = ".itemPerPageDropDown"; //Paragraphs Per Page
defaultOption.itemsPerPage = 1;
defaultOption.currentPage = 1;
defaultOption.searchBoxSelector = '.searchBox';
defaultOption.searchPhrase = '';
defaultOption.showingInfoSelector = '.showingInfo';
/** How many Page Number should be visible while navigating. Minimum allowed is 3 (previous, current & next) */
defaultOption.displayedPages = 10;
/**Show / Hide Control Buttons : Default to TRUE */
defaultOption.showGotoFirst = true;
defaultOption.showGotoLast = true;
defaultOption.showPrevious = true;
defaultOption.showNext = true;
/** all Text can accept an icon using a <span> OR <i> tag. */
defaultOption.btnFirstText = "<<";
defaultOption.btnLastText = ">>";
defaultOption.btnNextText = ">";
defaultOption.btnPreviousText = "<";
/**CSS properties */
defaultOption.css = {};
defaultOption.css.paginationLayout = "<style> " +
defaultOption.pagingControlsContainer+" ul{display:inline; padding-left: 0.2em} " +
defaultOption.pagingControlsContainer+" li{display:inline; padding-left: 0.2em}" +
"</style>";
defaultOption.css.btnNumberingClass = "btn btn-sm btn-primary";
defaultOption.css.btnActiveClass = "btn btn-sm btn-default";
defaultOption.css.btnFirstClass = defaultOption.css.btnNumberingClass;
defaultOption.css.btnLastClass = defaultOption.css.btnNumberingClass;
defaultOption.css.btnNextClass = defaultOption.css.btnNumberingClass;
defaultOption.css.btnPreviousClass = defaultOption.css.btnNumberingClass;
/**@Advanced Implementation default using a custom Json Data Source */
defaultOption.dataSource = {};
/**@Using_Ajax as DataSource */
defaultOption.ajax = {};
defaultOption.ajax.params = {};
defaultOption.ajax.url = '';
defaultOption.ajax.onSuccessCallBack = function(response){
console.log(response);
return response;
};
defaultOption.ajax.onFailureCallBack = function(error){
alert("Ajax Error - See Console for details!");
console.log(error);
};
defaultOption.searchBox = {};
defaultOption.searchBox.onClick = false;
defaultOption.searchBox.onClickSelector = '';
if (typeof options === 'undefined') {
options = defaultOption;
}
var getOption = function(field){
return (typeof options[field] === 'undefined') ? defaultOption[field] : options[field];
};
var getAjaxOption = function(ajaxFieldName){
var value = getOption('ajax'), defaultValue = defaultOption['ajax'];
return (typeof value[ajaxFieldName] === 'undefined') ? defaultValue[ajaxFieldName] : value[ajaxFieldName];
};
var getCssOption = function(cssFieldName){
var value = getOption('css'), defaultValue = defaultOption['css'];
return (typeof value[cssFieldName] === 'undefined') ? defaultValue[cssFieldName] : value[cssFieldName];
};
var getSearchBoxOption = function(searchFieldName){
var value = getOption('searchBox'), defaultValue = defaultOption['searchBox'];
return (typeof value[searchFieldName] === 'undefined') ? defaultValue[searchFieldName] : value[searchFieldName];
};
this.itemsPerPageSelector = getOption('itemsPerPageSelector');
this.itemsPerPage = getOption('itemsPerPage');
this.currentPage = getOption('currentPage');
this.pagingControlsContainer = getOption('pagingControlsContainer');
this.pagingContainer = getOption('pagingContainer');
this.itemSelector = getOption('itemSelector');
this.showingInfoSelector = getOption('showingInfoSelector');
this.displayedPages = getOption('displayedPages');
this.numOfPages = 0;
this.searchBoxSelector = getOption('searchBoxSelector');
this.searchPhrase = getOption('searchPhrase');
this.showGotoFirst = getOption('showGotoFirst');
this.showGotoLast = getOption('showGotoLast');
this.showPrevious = getOption('showPrevious');
this.showNext = getOption('showNext');
this.btnFirstText = getOption('btnFirstText');
this.btnLastText = getOption('btnLastText');
this.btnPreviousText = getOption('btnPreviousText');
this.btnNextText = getOption('btnNextText');
this.dataSource = getOption('dataSource');
this.ajax = getOption('ajax');
this.ajax.params = getAjaxOption('params');
this.ajax.url = getAjaxOption('url');
this.ajax.onSuccessCallBack = getAjaxOption("onSuccessCallBack");
this.ajax.onFailureCallBack = getAjaxOption("onFailureCallBack");
this.css = getOption('css');
this.css.paginationLayout = getCssOption('paginationLayout');
this.css.btnNumberingClass = getCssOption('btnNumberingClass');
this.css.btnActiveClass = getCssOption('btnActiveClass');
this.css.btnFirstClass = getCssOption('btnFirstClass');
this.css.btnLastClass = getCssOption('btnLastClass');
this.css.btnNextClass = getCssOption('btnNextClass');
this.css.btnPreviousClass = getCssOption('btnPreviousClass');
this.css.paginationLayout = "<style> " +
this.pagingControlsContainer+" ul{display:inline; padding-left: 0.2em} " +
this.pagingControlsContainer+" li{display:inline; padding-left: 0.2em}" +
"</style>";
this.searchBox = getOption('searchBox');
this.searchBox.onClick = getSearchBoxOption('onClick');
this.searchBox.onClickSelector = getSearchBoxOption('onClickSelector');
/**Private Property Below */
this.instanceId = Math.floor(Math.random() * 20);
this.controller = null;
this.init = function () {
this.pagingContainer = $(this.pagingContainer);
this.itemSelector = $(this.itemSelector, this.pagingContainer);
if (this.displayedPages < 3){
console.log("Minimum Displayed Page Numbers Allowed While Navigating is 3. " +
"\nTo avoid pagination control malfunction, please set this to >= 3, even if you have lesser items to display.");
this.displayedPages = 3;
}
};
this.getController = function(){
if (this.controller == null) {
this.init();
this.controller = new Flexible.PaginationController(this);
}
return this.controller;
};
/**
* @search
* @param trString
* @param words
* @returns {boolean}
*/
this.search = function(trString, words){
for (var i = 0; i < words.length; i++) {
if (trString.indexOf(words[i]) > -1) {
return true;
}
}
return false;
};
/**
* @getRowData
* @returns {*}
*/
this.getData = function(){
var words = pager.searchPhrase.toLowerCase().split(" "), dataSource = pager.itemSelector;
if (pager.dataSource.length > 0){
/**@Advanced Implementation Using a Custom Json Data Source */
dataSource = pager.dataSource;
}
if (pager.searchPhrase.length > 0){
/**@Filter By Search Phrase */
return dataSource.filter(function(key, value){
return pager.search($(value).html().replace(/<[^>]+>/g,"").toLowerCase(), words);
});
}
/**@NoFilter */
return dataSource;
};
this.showingInfoHandler = function(start, end, totalItems){
if ($(pager.showingInfoSelector)[0]){
$(pager.showingInfoSelector).html("Showing "+(totalItems > 0 ? start+1 : 0)+' to '+
(end > totalItems ? totalItems : end)+' of '+totalItems+' Entries'
);
}
};
/**
* @showCurrentPage
*/
this.showCurrentPage = function(){
if (pager.controller == null) {
pager.init();
}
var html = '', start = 0, end = 'all', totalItems = 0;
if (pager.itemsPerPage != 'all'){
start = (pager.currentPage-1) * this.itemsPerPage;
end = start+pager.itemsPerPage;
}
if (this.ajax.url != '' && this.ajax.url!='undefined'){
totalItems = end-start;
/**@Ajax Implementation */
var request_params = {status:true, search: pager.searchPhrase, currentPage:pager.currentPage, start:start, end:end, totalItems:totalItems};
pager.ajax.params = $.extend(this.ajax.params, request_params);
$.post(pager.ajax.url, pager.ajax.params, function(response){
html = pager.ajax.onSuccessCallBack(response);
if (pager.itemsPerPage == 'all'){
pager.itemsPerPage = response.totalItems;
}
totalItems = response.totalItems;
start = (pager.currentPage-1) * pager.itemsPerPage;
end = start+pager.itemsPerPage;
pager.pagingContainer.html(html);
pager.showingInfoHandler(start, end, totalItems);
}, 'json').fail(pager.ajax.onFailureCallBack);
}
else{
/**@Basic_OR_Advanced Implementation */
var data = pager.getData(); totalItems = data.length;
if (pager.itemsPerPage=='all'){
pager.itemsPerPage = totalItems;
start = (pager.currentPage-1) * this.itemsPerPage;
end = start+pager.itemsPerPage;
}
data.slice(start, end).each(function(){ html += this.outerHTML; });
pager.pagingContainer.html(html);
pager.showingInfoHandler(start, end, totalItems);
}
$(pager.pagingControlsContainer).html('');
pager.numOfPages = 0;
if (totalItems > pager.itemsPerPage){
/** Auto-Calculate the number of Pages. */
pager.numOfPages = Math.ceil(totalItems / pager.itemsPerPage);
/**@render Pagination Controls */
pager.renderControls();
}
};
var createButton = function (Label, btnClasses, isDisabled) {
return '<li><a href="javascript:void();" class="'+btnClasses+' '+(isDisabled ? "disabled" : "")+'">'+Label+'</a></li>';
};
/**
* @renderControls
*/
this.renderControls = function () {
var container = this.pagingControlsContainer,
currentPage = this.currentPage,
numPages = this.numOfPages,
displayedPages = this.displayedPages; //How many Page Number should be visible while navigating
var pagingControls = this.css.paginationLayout + "<ul>";
if (this.showGotoFirst){
pagingControls += createButton(this.btnFirstText, 'se-first'+this.instanceId+' '+this.css.btnFirstClass, currentPage==1);
}
if (this.showPrevious){
pagingControls += createButton(this.btnPreviousText, 'se-prev'+this.instanceId+' '+this.css.btnPreviousClass, currentPage==1);
}
var starting = 1, upTo = numPages-2, secondToLastPage = numPages-1, lastPage = numPages,
maximumStartingPoint = (numPages - displayedPages)+1;
if (displayedPages < numPages) {
upTo = displayedPages - 2;
if (currentPage > upTo) {
//Shift one down to allow the visibility of currently active page.
starting += (currentPage-upTo);
upTo += (currentPage-upTo);
if (starting > maximumStartingPoint){
//Normalize any escalating starting points...and subsequently upTo
starting = maximumStartingPoint;
upTo = secondToLastPage-1;
}
}
}
while(starting <= upTo){
if (starting != currentPage){
pagingControls += createButton(starting, 'se-page-num'+this.instanceId+' '+this.css.btnNumberingClass, false);
}
else{
pagingControls += createButton(starting, 'se-active-page'+this.instanceId+' '+this.css.btnActiveClass, false);
}
starting++;
}
if ( starting != (numPages-1)){
pagingControls += createButton('...', 'se-page-num'+this.instanceId+' '+this.css.btnActiveClass, true);
}
if (secondToLastPage==currentPage){
pagingControls += createButton(secondToLastPage, 'se-active-page'+this.instanceId+' '+this.css.btnActiveClass, false);
}
else{
pagingControls += createButton(secondToLastPage, 'se-page-num'+this.instanceId+' '+this.css.btnNumberingClass, false);
}
if (lastPage==currentPage){
pagingControls += createButton(lastPage, 'se-active-page'+this.instanceId+' '+this.css.btnActiveClass, false);
}
else{
pagingControls += createButton(lastPage, 'se-page-num'+this.instanceId+' '+this.css.btnNumberingClass, false);
}
if (this.showNext){
pagingControls += createButton(this.btnNextText, 'se-next'+this.instanceId+' '+this.css.btnNextClass, currentPage==numPages);
}
if (this.showGotoLast){
pagingControls += createButton(this.btnLastText, 'se-last'+this.instanceId+' '+this.css.btnLastClass, currentPage==numPages);
}
pagingControls += '</ul>';
$(container).html(pagingControls);
if (this.controller == null){
this.controller = new Flexible.PaginationController(this);
}
};
};
/**
* @Flexible.PaginationController
* @param pager
* @constructor
*/
Flexible.PaginationController = function(pager){
var body = $('body'), controller = this, instanceId = pager.instanceId;
/**
* @showPage
* @param page
*/
controller.showPage = function(page){
pager.currentPage = page;
pager.showCurrentPage();
return pager.currentPage;
};
/**
* @returns {number}
*/
controller.showFirstPage = function() {
return controller.showPage(1);
};
/**
* @returns {number|*}
*/
controller.showLastPage = function() {
return controller.showPage(pager.numOfPages);
};
/**
* @returns {number|*}
*/
controller.showPreviousPage = function(){
return controller.showPage(parseInt(body.find('.se-active-page'+instanceId).html())-1);
};
/**
* @returns {number|*}
*/
controller.showNextPage = function(){
return controller.showPage(parseInt(body.find('.se-active-page'+instanceId).html())+1);
};
/**
* @onPageClick
* @param pageNumber Optional Parameter
* @param event Optional Parameter
* @returns {boolean}
*/
controller.onPageClick = function (pageNumber, event) {
return true;
};
if (pager.searchBox.onClick){
body.on("click", pager.searchBox.onClickSelector, function(e){
pager.searchPhrase = $(pager.searchBoxSelector).val();
controller.showFirstPage();
});
}
else {
/**@search Text field control */
body.on("keyup", pager.searchBoxSelector, function(e){
pager.searchPhrase = $(this).val();
controller.showFirstPage();
});
}
/**@dropDown for selecting Items Per Page */
body.on("change", pager.itemsPerPageSelector, function(e){
e.preventDefault();
pager.itemsPerPage = ($(this).val()=='all') ? $(this).val() : parseInt($(this).val());
controller.showFirstPage();
});
/**@onClick Page Number */
body.on('click', '.se-page-num'+instanceId, function (e) {
e.preventDefault();
var pageNum = parseInt($(this).html());
controller.showPage(pageNum);
controller.onPageClick(pageNum, e);
});
/**@onClick Previous Button */
body.on('click', '.se-prev'+instanceId, function (e) {
e.preventDefault();
var pageNum = controller.showPreviousPage();
controller.onPageClick(pageNum, e);
});
/**@onClick Next Button */
body.on('click', '.se-next'+instanceId, function (e) {
e.preventDefault();
var pageNum = controller.showNextPage();
controller.onPageClick(pageNum, e);
});
/**@onClick First Button */
body.on('click', '.se-first'+instanceId, function (e) {
e.preventDefault();
var pageNum = controller.showFirstPage();
controller.onPageClick(pageNum, e);
});
/**@onClick Last Button */
body.on('click', '.se-last'+instanceId, function (e) {
e.preventDefault();
var pageNum = controller.showLastPage();
controller.onPageClick(pageNum, e);
});
};
(function($) {
$.fn.flexiblePagination = function (newOptions) {
return $.flexiblePagination(this, newOptions);
};
$.flexiblePagination = function (pagingContainer, newOptions) {
newOptions['pagingContainer'] = pagingContainer;
var pager = new Flexible.Pagination(newOptions);
pager.showCurrentPage();
return pager;
};
})(jQuery);