-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.paginate.js
208 lines (168 loc) · 7.96 KB
/
jquery.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
;(function($) {
/*
* Simple jQuery pagination plugin
* Version 1.2.5
*
* Copyright (c) 2011 Luminosity Group
*/
$.fn.paginate = function(options) {
return this.each(function() {
/* Default settings */
var defaults = {
/* Labels */
first_label: 'First',
prev_label: 'Previous',
next_label: 'Next',
last_label: 'Last',
ellipse_label: '...',
/* Push State */
pushstate: false,
/* Selectors */
content: '.page_content',
navigation: '.page_navigation',
contain_navigation: true,
items_per_page: 5,
abort_on_small_lists: false,
num_page_links: 5,
show_first: true,
show_last: true,
show_next: true,
show_prev: true,
show_ellipse: true,
/* Events */
events: {
afterPage: function() {}
}
};
/* Merge options with defaults */
var settings = $.extend(true, {}, defaults, options);
var container = $(this);
var content = $(settings.content, container);
var items = $(content).children();
var navigation_container = settings.contain_navigation ? $(settings.navigation, container) : $(settings.navigation);
var total = items.size();
var total_pages = Math.ceil(total / settings.items_per_page);
/* Is push state supported? */
var push_state_supported =
window.history && window.history.pushState && window.history.replaceState
&& !navigator.userAgent.match(/((iPod|iPhone|iPad).+\bOS\s+[1-4]|WebApps\/.+CFNetwork)/);
var replace_state = false;
/* Clear content in navigation container */
$(navigation_container).html('');
/* If abort_on_small_lists is set to true and the number of items
* is less than items_per_page, abort
*/
container.removeClass('no-pagination');
if (settings.abort_on_small_lists && (total <= settings.items_per_page)) {
$(items).css('display', '')
container.addClass('no-pagination')
return true;
}
/* Build out the page buttons */
build_nav();
/* If push state is supported, go to that page */
if (push_state_supported && settings.pushstate) {
var state = window.history.state;
if (state && state.page) {
goto_page(state.page, false);
} else {
replace_state = true;
goto_page(1);
}
} else {
goto_page(1, false);
}
/* Goes to page number page */
function goto_page(page, record_state) {
if (record_state == undefined) {
record_state = true;
}
var start = settings.items_per_page * (page - 1);
var end = start + settings.items_per_page;
var current = $(items).slice(start, end);
$(items).css('display', 'none');
$(current).css('display', '');
$('.page_link', navigation_container).removeClass('active');
$('.page_link[data-page="' + page + '"]', navigation_container).addClass('active');
var pages = $('.page_link[data-page]', navigation_container);
start = page - ((settings.num_page_links - 1) / 2) - 1;
end = page + ((settings.num_page_links - 1) / 2);
if (end > pages.size()) {
end = pages.size();
start = pages.size() - settings.num_page_links;
}
if (start < 0) {
start = 0;
end = settings.num_page_links;
}
if (end > pages.size()) {
end = pages.size();
}
$(pages).css('display', 'none')
$(pages).slice(start, end).css('display', '')
$('.ellipse', navigation_container).css('display', 'none')
if ($('.page_link[data-page]', navigation_container).first().css('display') == 'none') {
$('.ellipse.less', navigation_container).css('display', '')
}
if ($('.page_link[data-page]', navigation_container).last().css('display') == 'none') {
$('.ellipse.more', navigation_container).css('display', '')
}
if (push_state_supported && record_state && settings.pushstate) {
push(page);
}
settings.events.afterPage.call(this, page);
}
/* Builds out the page buttons */
function build_nav() {
if (settings.show_first)
navigation_container.append($('<span class="page_link first"><a href="#">' + settings.first_label + '</a></span>').data('page', 1));
if (settings.show_prev)
navigation_container.append($('<span class="page_link prev"><a href="#">' + settings.prev_label + '</a></span>'));
if (settings.show_ellipse)
navigation_container.append($('<span class="ellipse less">' + settings.ellipse_label + '</span>'));
for (i = 1; i <= total_pages; i++) {
navigation_container.append($('<span class="page_link" data-page="' + i + '"><a href="#">' + i + '</a></span>').data('page', i));
}
if (settings.show_ellipse)
navigation_container.append($('<span class="ellipse more">' + settings.ellipse_label + '</span>'));
if (settings.show_prev)
navigation_container.append($('<span class="page_link next"><a href="#">' + settings.next_label + '</a></span>'));
if (settings.show_last)
navigation_container.append($('<span class="page_link last"><a href="#">' + settings.last_label + '</a></span>').data('page', total_pages));
$('.page_link:not(.next, .prev)', navigation_container).click(function() {
var page = $(this).data('page');
goto_page(page);
return false;
});
$('.page_link.next', navigation_container).click(function() {
var current_page = $('.page_link.active', navigation_container).data('page');
if (current_page < total_pages)
goto_page(current_page + 1);
return false;
});
$('.page_link.prev', navigation_container).click(function() {
var current_page = $('.page_link.active', navigation_container).data('page');
if (current_page > 1)
goto_page(current_page - 1);
return false;
});
}
$(window).bind('popstate', function() {
var state = window.event.state;
if (state && state.page) {
goto_page(state.page, false);
}
});
/* Pushstate the page number */
function push(page) {
var state = {page: page}
if (replace_state) {
window.history.replaceState(state, null, null);
replace_state = false;
} else {
window.history.pushState(state, null, null);
}
}
});
}
})(jQuery);