-
Notifications
You must be signed in to change notification settings - Fork 12
/
jquery.bootpag.js
127 lines (109 loc) · 4.42 KB
/
jquery.bootpag.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
/**
* @preserve
* bootpag - jQuery plugin for dynamic pagination
*
* Copyright (c) 2013 botmonster@7items.com
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* http://botmonster.com/jquery-bootpag/
*
* Version: 1.0.4
*
*/
(function($, window) {
$.fn.bootpag = function(options){
var $owner = this,
settings = $.extend({
total: 0,
page: 1,
maxVisible: null,
leaps: true,
href: 'javascript:void(0);',
hrefVariable: '{{number}}',
next: '»',
prev: '«'
},
$owner.data('settings') || {},
options || {});
if(settings.total <= 0)
return this;
if(!$.isNumeric(settings.maxVisible) && !settings.maxVisible){
settings.maxVisible = settings.total;
}
$owner.data('settings', settings);
function renderPage($bootpag, page){
var lp,
maxV = settings.maxVisible == 0 ? 1 : settings.maxVisible,
step = settings.maxVisible == 1 ? 0 : 1,
vis = Math.floor((page - 1) / maxV) * maxV,
$page = $bootpag.find('li');
settings.page = page = page < 0 ? 0 : page > settings.total ? settings.total : page;
$page.removeClass('disabled');
lp = page - 1 < 1 ? 1 :
settings.leaps && page - 1 >= settings.maxVisible ?
Math.floor((page - 1) / maxV) * maxV : page - 1;
$page
.first()
.toggleClass('disabled', page === 1)
.attr('data-lp', lp)
.find('a').attr('href', href(lp));
var step = settings.maxVisible == 1 ? 0 : 1;
lp = page + 1 > settings.total ? settings.total :
settings.leaps && page + 1 < settings.total - settings.maxVisible ?
vis + settings.maxVisible + step: page + 1;
$page
.last()
.toggleClass('disabled', page === settings.total)
.attr('data-lp', lp)
.find('a').attr('href', href(lp));;
var $currPage = $page.filter('[data-lp='+page+']');
if(!$currPage.not('.next,.prev').length){
var d = page <= vis ? -settings.maxVisible : 0;
$page.not('.next,.prev').each(function(index){
lp = index + 1 + vis + d;
$(this)
.attr('data-lp', lp)
.toggle(lp <= settings.total)
.find('a').html(lp).attr('href', href(lp));
});
$currPage = $page.filter('[data-lp='+page+']');
}
$currPage.addClass('disabled');
$owner.trigger('page', page);
$owner.data('settings', settings);
}
function href(c){
return settings.href.replace(settings.hrefVariable, c);
}
return this.each(function(){
var $bootpag, lp, me = $(this),
p = ['<ul class="pagination pagination-mg">'];
if(settings.prev){
p.push('<li data-lp="1" class="prev"><a href="'+href(1)+'">'+settings.prev+'</a></li>');
}
for(var c = 1; c <= Math.min(settings.total, settings.maxVisible); c++){
p.push('<li data-lp="'+c+'"><a href="'+href(c)+'">'+c+'</a></li>');
}
if(settings.next){
lp = settings.leaps && settings.total > settings.maxVisible
? Math.min(settings.maxVisible + 1, settings.total) : 2;
p.push('<li data-lp="'+lp+'" class="next"><a href="'+href(lp)+'">'+settings.next+'</a></li>');
}
p.push('</ul>');
me.find('ul.pagination').remove();
me.append(p.join('')).addClass('pagination');
$bootpag = me.find('ul.pagination');
me.find('li').click(function paginationClick(){
var me = $(this);
if(me.hasClass('disabled')){
return;
}
renderPage($bootpag, parseInt(me.attr('data-lp'), 10));
});
renderPage($bootpag, settings.page);
});
}
})(jQuery, window);