forked from auxiliary/rpage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
responsive-paginate.js
executable file
·192 lines (178 loc) · 5.6 KB
/
responsive-paginate.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
/*
* A plugin for making Bootstrap's pagination more responsive
* https://github.com/auxiliary/rpage
*/
(function ($){
jQuery.fn.rPage = function () {
var $this = $(this);
for(var i = 0, max = $this.length; i < max; i++)
{
new rPage($($this[i]));
}
function rPage($container)
{
this.label = function()
{
var active_index = this.els.filter(".active").index();
var rp = this;
this.els.each(function(){
if (rp.isNextOrPrevLink($(this)) == false)
{
$(this).addClass("page-away-" + (Math.abs(active_index - $(this).index())).toString());
}
else
{
if ($(this).index() > active_index)
{
$(this).addClass("right-etc");
}
else
{
$(this).addClass("left-etc");
}
}
});
}
this.makeResponsive = function()
{
this.reset();
var width = this.calculateWidth();
while (width > this.els.parent().parent().width() - 10)
{
var did_remove = this.removeOne();
if (did_remove == false)
{
break;
}
width = this.calculateWidth();
}
}
this.isNextOrPrevLink = function(element)
{
return (
element.hasClass('pagination-prev')
|| element.hasClass('pagination-next')
|| element.text() == "»"
|| element.text() == "«"
);
}
this.isRemovable = function(element)
{
if (this.isNextOrPrevLink(element))
{
return false;
}
var index = this.els.filter(element).index();
if (index == 1 || this.isNextOrPrevLink($container.find("li").eq(index + 1)))
{
return false;
}
if (element.text() == "...")
{
return false;
}
return true;
}
this.removeOne = function()
{
var active_index = this.els.filter(".active").index();
var farthest_index = $container.find("li").length - 1;
var next = active_index + 1;
var prev = active_index - 1;
for (var i = farthest_index - 1; i > 0; i--)
{
var candidates = this.els.filter(".page-away-" + i.toString());
var candidate = candidates.filter(function(){
return this.style["display"] != "none";
});
if (candidate.length > 0)
{
for (var j = 0; j < candidate.length; j++)
{
var candid_candidate = candidate.eq(j);
if (this.isRemovable(candid_candidate))
{
candid_candidate.css("display", "none");
if (this.needsEtcSign(active_index, farthest_index - 1))
{
this.els.eq(farthest_index - 2).before("<li class='disabled removable'><span>...</span></li>");
}
if (this.needsEtcSign(1, active_index))
{
this.els.eq(1).after("<li class='disabled removable'><span>...</span></li>");
}
return true;
}
}
}
}
return false;
}
this.needsEtcSign = function(el1_index, el2_index)
{
if (el2_index - el1_index <= 1)
{
return false;
}
else
{
var hasEtcSign = false;
var hasHiddenElement = false;
for (var i = el1_index + 1; i < el2_index; i++)
{
var el = $container.find("li").eq(i);
if (el.css("display") == "none")
{
hasHiddenElement = true;
}
if (el.text() == "...")
{
hasEtcSign = true;
}
}
if (hasHiddenElement == true && hasEtcSign == false)
{
return true;
}
}
return false;
}
this.reset = function()
{
for (var i = 0; i < this.els.length; i++)
{
this.els.eq(i).css("display", "inline");
}
$container.find("li").filter(".removable").remove();
}
this.calculateWidth = function()
{
var width = 0;
for (var i = 0; i < $container.find("li").length; i++)
{
if(!($container.find("li").eq(i).css('display') == 'none'))
{
if($container.find("li").eq(i).children("a").eq(0).length > 0){
width += $container.find("li").eq(i).children("a").eq(0).outerWidth();
}
if($container.find("li").eq(i).children("span").eq(0).length > 0){
width += $container.find("li").eq(i).children("span").eq(0).outerWidth();
}
}
}
return width;
}
this.els = $container.find("li");
this.label();
this.makeResponsive();
var resize_timer;
$(window).resize(
$.proxy(function()
{
clearTimeout(resize_timer);
resize_timer = setTimeout($.proxy(function(){this.makeResponsive()}, this), 100);
}, this)
);
}
};
}(jQuery));