-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.simplePaginationAjax.js
554 lines (419 loc) · 17.5 KB
/
jquery.simplePaginationAjax.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/**
* simplePagination.js v1.6
* A simple jQuery pagination plugin.
* http://flaviusmatis.github.com/simplePagination.js/
*
* Copyright 2012, Flavius Matis
* Released under the MIT license.
* http://flaviusmatis.github.com/license.html
*/
(function($) {
var methods = {
init: function(options) {
var o = $.extend({
items: 1,
itemsOnPage: 1,
pages: 0,
displayedPages: 5,
edges: 2,
currentPage: 1,
hrefTextPrefix: '#page-',
hrefTextSuffix: '',
prevText: 'Prev',
nextText: 'Next',
ellipseText: '…',
cssStyle: 'light-theme',
labelMap: [],
selectOnClick: true,
nextAtFront: false,
//ajax
pagination_link: '.simple-pagination',
loading_div: '#loading_pagination_ajax',
loadingClass: 'loading_pagination',
targetUrl: '',
user_data: '',
page_class: '.paging',
page_list: '#pagination_list', //parent page
onPageClick: function(pageNumber, event) {
// Callback triggered when a page is clicked
// Page number is given as an optional parameter
methods.slide_page.call(self, pageNumber);
},
onInit: function() {
// Callback triggered immediately after initialization
}
}, options || {});
var self = this;
o.pages = o.pages ? o.pages : Math.ceil(o.items / o.itemsOnPage) ? Math.ceil(o.items / o.itemsOnPage) : 1;
o.currentPage = o.currentPage - 1;
o.halfDisplayed = o.displayedPages / 2;
this.each(function() {
self.addClass(o.cssStyle + ' simple-pagination').data('pagination', o);
methods._draw.call(self);
});
//ajax
//self.addClass('pagination-ajax');
methods._create_page_list_div.call(self);
methods._create_loading_div.call(self);
methods.slide_page.call(self);
o.onInit();
return this;
},
selectPage: function(page) {
methods._selectPage.call(this, page - 1);
return this;
},
prevPage: function() {
var o = this.data('pagination');
if (o.currentPage > 0) {
methods._selectPage.call(this, o.currentPage - 1);
}
return this;
},
nextPage: function() {
var o = this.data('pagination');
if (o.currentPage < o.pages - 1) {
methods._selectPage.call(this, o.currentPage + 1);
}
return this;
},
getPagesCount: function() {
return this.data('pagination').pages;
},
getCurrentPage: function() {
return this.data('pagination').currentPage + 1;
},
destroy: function() {
this.empty();
return this;
},
drawPage: function(page) {
var o = this.data('pagination');
o.currentPage = page - 1;
this.data('pagination', o);
methods._draw.call(this);
return this;
},
redraw: function() {
methods._draw.call(this);
return this;
},
disable: function() {
var o = this.data('pagination');
o.disabled = true;
this.data('pagination', o);
methods._draw.call(this);
return this;
},
enable: function() {
var o = this.data('pagination');
o.disabled = false;
this.data('pagination', o);
methods._draw.call(this);
return this;
},
updateItems: function(newItems) {
var o = this.data('pagination');
o.items = newItems;
o.pages = methods._getPages(o);
this.data('pagination', o);
methods._draw.call(this);
},
updateItemsOnPage: function(itemsOnPage) {
var o = this.data('pagination');
o.itemsOnPage = itemsOnPage;
o.pages = methods._getPages(o);
this.data('pagination', o);
methods._selectPage.call(this, 0);
return this;
},
_draw: function() {
var o = this.data('pagination'),
interval = methods._getInterval(o),
i,
tagName;
methods.destroy.call(this);
tagName = (typeof this.prop === 'function') ? this.prop('tagName') : this.attr('tagName');
var $panel = tagName === 'UL' ? this : $('<ul></ul>').appendTo(this);
// Generate Prev link
if (o.prevText) {
methods._appendItem.call(this, o.currentPage - 1, {
text: o.prevText,
classes: 'prev'
});
}
// Generate Next link (if option set for at front)
if (o.nextText && o.nextAtFront) {
methods._appendItem.call(this, o.currentPage + 1, {
text: o.nextText,
classes: 'next'
});
}
// Generate start edges
if (interval.start > 0 && o.edges > 0) {
var end = Math.min(o.edges, interval.start);
for (i = 0; i < end; i++) {
methods._appendItem.call(this, i);
}
if (o.edges < interval.start && (interval.start - o.edges != 1)) {
$panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
} else if (interval.start - o.edges == 1) {
methods._appendItem.call(this, o.edges);
}
}
// Generate interval links
for (i = interval.start; i < interval.end; i++) {
methods._appendItem.call(this, i);
}
// Generate end edges
if (interval.end < o.pages && o.edges > 0) {
if (o.pages - o.edges > interval.end && (o.pages - o.edges - interval.end != 1)) {
$panel.append('<li class="disabled"><span class="ellipse">' + o.ellipseText + '</span></li>');
} else if (o.pages - o.edges - interval.end == 1) {
methods._appendItem.call(this, interval.end++);
}
var begin = Math.max(o.pages - o.edges, interval.end);
for (i = begin; i < o.pages; i++) {
methods._appendItem.call(this, i);
}
}
// Generate Next link (unless option is set for at front)
if (o.nextText && !o.nextAtFront) {
methods._appendItem.call(this, o.currentPage + 1, {
text: o.nextText,
classes: 'next'
});
}
},
_getPages: function(o) {
var pages = Math.ceil(o.items / o.itemsOnPage);
return pages || 1;
},
_getInterval: function(o) {
return {
start: Math.ceil(o.currentPage > o.halfDisplayed ? Math.max(Math.min(o.currentPage - o.halfDisplayed, (o.pages - o.displayedPages)), 0) : 0),
end: Math.ceil(o.currentPage > o.halfDisplayed ? Math.min(o.currentPage + o.halfDisplayed, o.pages) : Math.min(o.displayedPages, o.pages))
};
},
_appendItem: function(pageIndex, opts) {
var self = this,
options, $link, o = self.data('pagination'),
$linkWrapper = $('<li></li>'),
$ul = self.find('ul');
pageIndex = pageIndex < 0 ? 0 : (pageIndex < o.pages ? pageIndex : o.pages - 1);
options = {
text: pageIndex + 1,
classes: ''
};
if (o.labelMap.length && o.labelMap[pageIndex]) {
options.text = o.labelMap[pageIndex];
}
options = $.extend(options, opts || {});
if (pageIndex == o.currentPage || o.disabled) {
if (o.disabled) {
$linkWrapper.addClass('disabled');
} else {
$linkWrapper.addClass('active');
}
$link = $('<span class="current">' + (options.text) + '</span>');
} else {
$link = $('<a href="' + o.hrefTextPrefix + (pageIndex + 1) + o.hrefTextSuffix + '" class="page-link">' + (options.text) + '</a>');
$link.click(function(event) {
return methods._selectPage.call(self, pageIndex, event);
});
}
if (options.classes) {
$link.addClass(options.classes);
}
$linkWrapper.append($link);
if ($ul.length) {
$ul.append($linkWrapper);
} else {
self.append($linkWrapper);
}
},
_selectPage: function(pageIndex, event) {
var o = this.data('pagination');
o.currentPage = pageIndex;
if (o.selectOnClick) {
methods._draw.call(this);
}
return o.onPageClick(pageIndex + 1, event);
},
//ajax
//pagination-ajax method
_create_page_list_div: function() {
var o = this.data('pagination');
page_list_div = '<div id="pagination_list"></div>';
$(o.pagination_link).before(page_list_div);
},
_create_loading_div: function() {
var o = this.data('pagination');
var loading = '<div class="rect1"></div> <div class="rect2"></div> <div class="rect3"></div> <div class="rect4"></div> <div class="rect5"></div>';
loading_div = '<div id="loading_pagination_ajax" class="spinner"> </div>';
$('#pagination_list').after(loading_div);
$('#loading_pagination_ajax').addClass(o.loadingClass);
$('#loading_pagination_ajax').html(loading);
//console.log(o.loadingClass);
},
//slide page
slide_page: function(page_number) {
var o = this.data('pagination');
var loading = $(o.loading_div);
var targetUrl = o.targetUrl;
var page_class = o.page_class;
var page_list = $(o.page_list);
var pagination_class = $(o.pagination_link);
//pagination_class.append('<div id="page_list"> </div>');
// $('.pagination').pagination('nextPage');
loading.show();
//pagination_class.hide();
var page_number = (page_number == undefined) ? 1 : page_number;
// var page_number = $(this).attr('href').replace("laporan_harian/beranda/","")
//alert(page_number);
var page_id = '#page' + page_number;
var current_page = page_list.find(page_class);
// cari dulu halaman dengan id = page2
var check_page = page_list.find(page_id).length;
// console.log(check_page);
if (check_page === 1) {
//select active page number (page1 --> take 1)
var active_page = page_list.find('.active').attr('id').replace("page", "");
var direction = 'left';
if (page_number > active_page) {
var direction = 'right';
}
//get_next_page so but hide it, so it would be lazy
var next_page = parseInt(page_number) + 1;
var check_next_page = page_list.find('#page' + next_page).length;
if (check_next_page !== 1) {
//get_page(next_page, 'none');
methods.get_next_page.call(this, next_page);
}
current_page.hide();
page_list.find('.active').removeClass('active');
page_list.find(page_id).addClass('active');
//scroll to top and show the page
$("body, html").animate({
scrollTop: $('body').offset().top
}, 2000);
setTimeout(function() {
//page_list.find(page_id).show('slide',{direction: direction},900);
page_list.find(page_id).fadeIn(900);
pagination_class.show();
loading.hide();
}, 1300);
} else {
methods.get_page.call(this, page_number, 'true');
//methods.get_page(page_number);
//console.log(page_number);
var next_page = parseInt(page_number) + 1;
var next_page_id = '#page' + next_page;
//get nextpage first if false then get next page
var check_next_page = page_list.find(next_page_id).length;
if (check_next_page === 0) {
//methods.get_page(next_page, 'none');
methods.get_next_page.call(this, next_page);
} //end if
}
},
//ajax to get page
get_page: function(page_number, display) {
var o = this.data('pagination');
var loading = $(o.loading_div);
var targetUrl = o.targetUrl;
var page_class = o.page_class;
var page_list = $(o.page_list);
var pagination_class = $(o.pagination_link);
page = page_number;
page_number2 = (parseInt(page) - 1) * parseInt(o.itemsOnPage);
//because it only send one digit, based on pagination and the 0,10 -> page 1, 10, 10 -> page 2 etc
//prepare data for posting
display = display || 'true';
var data_send = {
user_data: o.user_data,
per_page: o.itemsOnPage,
total_page: o.items,
current_page: page,
page_number: page_number2,
display: display
};
$.ajax({
url: o.targetUrl,
type: 'POST',
data: data_send,
beforeSend: function() {
//remove page_class active
loading.fadeIn();
if (display == 'true') {
//scroll to top
$("body, html").animate({
scrollTop: $('body').offset().top
}, 2000);
$(page_class).hide();
loading.fadeIn();
page_list.find('.active').removeClass('active');
}
},
success: function(html) {
loading.hide();
// $(page_class).fadeIn();
page_list.append(html);
page_list.find('#page' + page).addClass('paging');
page_list.find('#page' + page).addClass('swipe_page');
if (display == 'true') {
//page_list.find('#page'+page).show('slide',{direction: 'right'},1000).addClass('active');
page_list.find('#page' + page).fadeIn().addClass('active');
pagination_class.fadeIn();
// console.log(page);
}
if (display == 'none') {
page_list.find('#page' + page).hide();
pagination_class.fadeIn();
}
},
error: function() {
//call notification error
//noty_error();
page_list.append('<h3> Error koneksi terputus, coba refresh browser anda </h3>');
}
});
},
//get next page so it wil load fastly
get_next_page: function(page_number) {
var o = this.data('pagination');
var page_list = $(o.page_list);
var page = page_number;
var page_number2 = (parseInt(page) - 1) * parseInt(o.itemsOnPage);
var data_send = {
user_data: o.user_data,
per_page: o.itemsOnPage,
current_page: page,
page_number: page_number2
};
$.ajax({
url: o.targetUrl,
type: 'POST',
data: data_send,
success: function(html) {
page_list.append(html);
page_list.find('#page' + page).addClass('paging');
page_list.find('#page' + page).addClass('swipe_page');
},
error: function() {
console.log('<h3> Error koneksi terputus, coba refresh browser anda </h3>');
}
});
}
};
$.fn.pagination = function(method) {
// Method calling logic
if (methods[method] && method.charAt(0) != '_') {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.pagination');
}
};
})(jQuery);