-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.universalpaginate.js
336 lines (288 loc) · 11.5 KB
/
jquery.universalpaginate.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
/*
* jQuery Universal Paginate
* version: 2.1.0 (2010-02-15)
* @requires jQuery v1.4.2 or later
* @requires jQuery Templates
*
* Examples and documentation at: http://blog.pierrejeanparra.com/jquery-plugins/universal-paginate/
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
;(function ($) {
var methods = {
init: function (options) {
var params,
defaults = {
nbItemsByPage: 10,
nbPagesDisplayed: 10,
itemTemplate: '<li>${value}</li>',
dataUrl: null,
nbPreloadedPages: 2,
refreshInterval: null,
universalPaginateClass: 'universal_paginate',
controlsPosition: 'top',
paginationNavigationArrows: true,
allowItemsByPageChange: true,
displayItemsByPageSelector: true,
itemsByPageText: 'Items by page',
pageText: 'Page',
nbItemsByPageOptions: [5, 10, 15, 20, 30, 60, 100],
headerElement: null,
noDataText: '<div>No data to display</div>',
ajaxOptions: {},
onDataUpdate: function (data) {}
};
params = $.extend(defaults, options || {});
return this.each(function () {
var $list = $(this),
$pagesList = $('<div class="' + params.universalPaginateClass + '_pages"></div>'),
$header,
defaultAjaxOptions;
// Store parameters to make them available anywhere
$list.data('universal_paginate', {'params': params});
// Create the item template
$list.universalPaginate('changeItemTemplate', params.itemTemplate);
// Create the header
if (params.headerElement) {
$header = $(params.headerElement);
} else {
$header = $('<div class="' + params.universalPaginateClass + '_header"><div class="clear"></div></div>');
if (params.controlsPosition === 'top') {
$list.before($header);
} else {
$list.after($header);
}
}
$header.prepend($pagesList);
$list.data('universal_paginate').header = $header;
// Allow changing the number of items by page
if (params.allowItemsByPageChange === true) {
privateMethods.allowChangingItemsByPage($list, $header);
}
if (params.displayItemsByPageSelector === true) {
privateMethods.displayItemsByPageSelector($list, $header);
}
// Bind the pagination links
$pagesList.delegate('.' + params.universalPaginateClass + '_link', 'click', function () {
$list.universalPaginate('goToPage', $(this).data('universal_paginate').page_id);
return false;
});
// Init the refresh cycle, if needed
if (params.refreshInterval) {
privateMethods.initRefreshTimer($list, params.refreshInterval);
}
// Bind custom event to allow on-demand data refreshing
$list.bind('refresh.' + params.universalPaginateClass, function (ev, data) {
$list.data('universal_paginate').additionalData = data;
methods.refresh();
});
$list.bind('full_refresh.' + params.universalPaginateClass, function (ev, data) {
$list.data('universal_paginate').additionalData = data;
methods.refresh(true);
});
defaultAjaxOptions = {
url: params.dataUrl,
data: {
startIndex: 0,
nbItemsByPage: params.nbItemsByPage,
nbPreloadedPages: 0
},
dataType: 'json',
type: 'get',
cache: false,
success: function (serverData) {
$list.data('universal_paginate').serverData = serverData;
$list.data('universal_paginate').currentPage = 1;
// Generate the first page
$list.universalPaginate('goToPage', 1);
}
};
// Get the first set of data (we just fetch one page. Other pages will be pre-loaded later in background
$list.data('universal_paginate').xhr = $.ajax($.extend(defaultAjaxOptions, params.ajaxOptions || {}));
});
},
// Changes the template used to display the data
changeItemTemplate: function (newItemTemplate) {
return this.each(function () {
var $list = $(this),
itemTemplate;
// If the template is given as a string
if (typeof newItemTemplate === 'string') {
itemTemplate = $.template(newItemTemplate);
} else {
// Assume it is a jQuery object
itemTemplate = $(newItemTemplate).template();
}
$list.data('universal_paginate').itemTemplate = itemTemplate;
});
},
// Changes the current page to pageId
goToPage: function (pageId) {
return this.each(function () {
var $list = $(this),
data = $list.data('universal_paginate').additionalData || {},
params = $list.data('universal_paginate').params,
serverData = $list.data('universal_paginate').serverData,
// This index is the current page number amongst the pre-loaded pages
currentPage = $list.data('universal_paginate').currentPage,
locallyStoredData = serverData.data[pageId - currentPage]?true:false,
defaultAjaxOptions,
nbPages = Math.ceil(serverData.nbTotalItems / serverData.nbItemsByPage);
// If we already have the data, just populate the list right now.
// We will update our local data later
if (locallyStoredData) {
privateMethods.populateList($list, serverData, pageId);
}
// Whether we have the data locally or not, we still have to update the pagination
privateMethods.createPagination($list, $('.' + params.universalPaginateClass + '_pages', $list.data('universal_paginate').header), nbPages, pageId);
data.startIndex = (pageId - 1) * params.nbItemsByPage;
data.nbItemsByPage = params.nbItemsByPage;
data.nbPreloadedPages = params.nbPreloadedPages;
defaultAjaxOptions = {
url: params.dataUrl,
data: data,
dataType: 'json',
type: 'get',
cache: false,
success: function (serverData) {
var pageId = Math.ceil(serverData.startIndex / serverData.nbItemsByPage) + 1,
nbPages = Math.ceil(serverData.nbTotalItems / serverData.nbItemsByPage);
// As we are going to get fresh data, let's update current page number (this index is the current page number amongst the pre-loaded pages)
$list.data('universal_paginate').currentPage = pageId;
// If we didn't have the data when we were asked to change page, we had to wait for the data to be fetched from the server, which is now.
// So let's populate the list with fresh data.
if (!locallyStoredData) {
privateMethods.populateList($list, serverData, pageId);
}
// In all cases, we want to update the pagination links
// If we don't do this, we may risk to have unconsistent pagination in some cases
privateMethods.createPagination($list, $('.' + params.universalPaginateClass + '_pages', $list.data('universal_paginate').header), nbPages, pageId);
// Update local data
$list.data('universal_paginate').serverData = serverData;
params.onDataUpdate(serverData);
}
};
// Cancel any other Ajax request
$list.data('universal_paginate').xhr.abort();
// Let's update local data
$list.data('universal_paginate').xhr = $.ajax($.extend(defaultAjaxOptions, params.ajaxOptions || {}));
});
},
// Refreshes list data
refresh: function (opts) {
var options = opts || {};
return this.each(function () {
var $list = $(this),
newPageIndex = $list.data('universal_paginate').currentPage;
if (options.reInit) {
newPageIndex = 1;
}
if (options.data) {
$list.data('universal_paginate').additionalData = options.data;
}
// Empty cache
$list.data('universal_paginate').serverData.data = {};
$list.universalPaginate('goToPage', newPageIndex);
});
}
},
privateMethods = {
// Creates pagination links
createPagination: function ($list, $pagesList, nbPages, pageId) {
var prevPageId = pageId - 1,
nextPageId = pageId + 1,
i,
params = $list.data('universal_paginate').params,
$link;
$pagesList.empty();
for (i = 1; i <= nbPages; i += 1) {
if ((i === 1 || i === nbPages) || (nbPages > params.nbPagesDisplayed && i > pageId - 3 && i < pageId + 3) || nbPages <= params.nbPagesDisplayed) {
$link = $('<a href="#" class="' + params.universalPaginateClass + '_link ' + params.universalPaginateClass + '_page_' + i + (i === pageId ? ' active' : '') + '">' + i + '</a>');
$link.data('universal_paginate', {'page_id': i});
$pagesList
.append($link);
} else if (nbPages > params.nbPagesDisplayed && (i === pageId - 3 || i === pageId + 3)) {
$pagesList
.append('<span class="' + params.universalPaginateClass + '_dots">...</span>');
}
}
// Add the navigation arrows if needed
if (params.paginationNavigationArrows === true) {
if (prevPageId >= 1) {
$link = $('<a href="#" class="' + params.universalPaginateClass + '_link ' + params.universalPaginateClass + '_page_' + prevPageId + '"><</a>');
$link.data('universal_paginate', {'page_id': prevPageId});
$pagesList.prepend($link);
}
if (nextPageId <= nbPages) {
$link = $('<a href="#" class="' + params.universalPaginateClass + '_link ' + params.universalPaginateClass + '_page_' + nextPageId + '">></a>');
$link.data('universal_paginate', {'page_id': nextPageId});
$pagesList.append($link);
}
}
// Add the "page" text
$pagesList.prepend(params.pageText);
},
// Allows changing the number of items by page
allowChangingItemsByPage: function ($list, $header) {
var params = $list.data('universal_paginate').params;
$header.delegate('.' + params.universalPaginateClass + '_nb_items_by_page_selector', 'change', function () {
$list.data('universal_paginate').params.nbItemsByPage = $(this).val();
$list.universalPaginate('refresh', {reInit: true});
});
},
// Displays the number of items by page selector
displayItemsByPageSelector: function ($list, $header) {
var $select = $('<select>'),
$wrapper = $('<div>'),
params = $list.data('universal_paginate').params;
$.each(params.nbItemsByPageOptions, function (i, val) {
$select.append($('<option>', {
value: val,
text: val,
selected: val === params.nbItemsByPage
}));
});
$select.addClass(params.universalPaginateClass + '_nb_items_by_page_selector');
$wrapper.addClass(params.universalPaginateClass + '_nb_items_by_page').append(params.itemsByPageText).append($select);
// Create the pagination zone
$header.prepend($wrapper);
},
// Inits the refresh cycle
initRefreshTimer: function ($list, interval) {
var refreshTimer = $list.data('universal_paginate').refreshTimer;
if (refreshTimer) {
clearInterval(refreshTimer);
}
if (interval > 0) {
refreshTimer = setInterval(function () {
$list.universalPaginate('refresh');
}, interval);
}
},
// Populates the list with provided data
populateList: function ($list, serverData, pageId) {
var params = $list.data('universal_paginate').params,
currentPage = $list.data('universal_paginate').currentPage;
if (serverData.nbTotalItems === 0) {
$list.empty().append(params.noDataText);
} else {
// Create content
$.tmpl($list.data('universal_paginate').itemTemplate, serverData.data[pageId - currentPage]).appendTo($list.empty());
}
}
};
$.fn.universalPaginate = function (method) {
if (!this.length) {
return this;
}
// Method calling logic
if (methods[method]) {
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.universalPaginate');
}
};
})(jQuery);