forked from bombledmonk/advancedsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.jqpagination.js
417 lines (290 loc) · 10.5 KB
/
jquery.jqpagination.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
/*!
* jqPagination, a jQuery pagination plugin (obviously)
* Version: 1.4 (26th July 2013)
*
* Copyright (C) 2013 Ben Everard
*
* http://beneverard.github.com/jqPagination
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
(function ($) {
"use strict";
$.jqPagination = function (el, options) {
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// get input jQuery object
base.$input = base.$el.find('input');
// Add a reverse reference to the DOM object
base.$el.data("jqPagination", base);
base.init = function () {
base.options = $.extend({}, $.jqPagination.defaultOptions, options);
// if the user hasn't provided a max page number in the options try and find
// the data attribute for it, if that cannot be found, use one as a max page number
if (base.options.max_page === null) {
if (base.$input.data('max-page') !== undefined) {
base.options.max_page = base.$input.data('max-page');
} else {
base.options.max_page = 1;
}
}
// if the current-page data attribute is specified this takes priority
// over the options passed in, so long as it's a number
if (base.$input.data('current-page') !== undefined && base.isNumber(base.$input.data('current-page'))) {
base.options.current_page = base.$input.data('current-page');
}
// remove the readonly attribute as JavaScript must be working by now ;-)
base.$input.removeAttr('readonly');
// set the initial input value
// pass true to prevent paged callback form being fired
base.updateInput(true);
//***************
// BIND EVENTS
base.$input.on('focus.jqPagination mouseup.jqPagination', function (event) {
// if event === focus, select all text...
if (event.type === 'focus') {
var current_page = parseInt(base.options.current_page, 10);
$(this).val(current_page).select();
}
// if event === mouse up, return false. Fixes Chrome bug
if (event.type === 'mouseup') {
return false;
}
});
base.$input.on('blur.jqPagination keydown.jqPagination', function (event) {
var $self = $(this),
current_page = parseInt(base.options.current_page, 10);
// if the user hits escape revert the input back to the original value
if (event.keyCode === 27) {
$self.val(current_page);
$self.blur();
}
// if the user hits enter, trigger blur event but DO NOT set the page value
if (event.keyCode === 13) {
$self.blur();
}
// only set the page is the event is focusout.. aka blur
if (event.type === 'blur') {
base.setPage($self.val());
}
});
base.$el.on('click.jqPagination', 'a', function (event) {
var $self = $(this);
// we don't want to do anything if we've clicked a disabled link
// return false so we stop normal link action btu also drop out of this event
if ($self.hasClass('disabled')) {
return false;
}
// for mac + windows (read: other), maintain the cmd + ctrl click for new tab
if (!event.metaKey && !event.ctrlKey) {
event.preventDefault();
base.setPage($self.data('action'));
}
});
};
base.setPage = function (page, prevent_paged) {
// return current_page value if getting instead of setting
if (page === undefined) {
return base.options.current_page;
}
var current_page = parseInt(base.options.current_page, 10),
max_page = parseInt(base.options.max_page, 10);
if (isNaN(parseInt(page, 10))) {
switch (page) {
case 'first':
page = 1;
break;
case 'prev':
case 'previous':
page = current_page - 1;
break;
case 'next':
page = current_page + 1;
break;
case 'last':
page = max_page;
break;
}
}
page = parseInt(page, 10);
// reject any invalid page requests
if (isNaN(page) || page < 1 || page > max_page) {
// update the input element
base.setInputValue(current_page);
return false;
}
// update current page options
base.options.current_page = page;
base.$input.data('current-page', page);
// update the input element
base.updateInput( prevent_paged );
};
base.setMaxPage = function (max_page, prevent_paged) {
// return the max_page value if getting instead of setting
if (max_page === undefined) {
return base.options.max_page;
}
// ignore if max_page is not a number
if (!base.isNumber(max_page)) {
console.error('jqPagination: max_page is not a number');
return false;
}
// ignore if max_page is less than the current_page
if (max_page < base.options.current_page) {
console.error('jqPagination: max_page lower than current_page');
return false;
}
// set max_page options
base.options.max_page = max_page;
base.$input.data('max-page', max_page);
// update the input element
base.updateInput( prevent_paged );
};
// ATTN this isn't really the correct name is it?
base.updateInput = function (prevent_paged) {
var current_page = parseInt(base.options.current_page, 10);
// set the input value
base.setInputValue(current_page);
// set the link href attributes
base.setLinks(current_page);
// we may want to prevent the paged callback from being fired
if (prevent_paged !== true) {
// fire the callback function with the current page
base.options.paged(current_page);
}
};
base.setInputValue = function (page) {
var page_string = base.options.page_string,
max_page = base.options.max_page;
// this looks horrible :-(
page_string = page_string
.replace("{current_page}", page)
.replace("{max_page}", max_page);
base.$input.val(page_string);
};
base.isNumber = function(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
};
base.setLinks = function (page) {
var link_string = base.options.link_string,
current_page = parseInt(base.options.current_page, 10),
max_page = parseInt(base.options.max_page, 10);
if (link_string !== '') {
// set initial page numbers + make sure the page numbers aren't out of range
var previous = current_page - 1;
if (previous < 1) {
previous = 1;
}
var next = current_page + 1;
if (next > max_page) {
next = max_page;
}
// apply each page number to the link string, set it back to the element href attribute
base.$el.find('a.first').attr('href', link_string.replace('{page_number}', '1'));
base.$el.find('a.prev, a.previous').attr('href', link_string.replace('{page_number}', previous));
base.$el.find('a.next').attr('href', link_string.replace('{page_number}', next));
base.$el.find('a.last').attr('href', link_string.replace('{page_number}', max_page));
}
// set disable class on appropriate links
base.$el.find('a').removeClass('disabled');
if (current_page === max_page) {
base.$el.find('.next, .last').addClass('disabled');
}
if (current_page === 1) {
base.$el.find('.previous, .first').addClass('disabled');
}
};
base.callMethod = function (method, key, value) {
switch (method.toLowerCase()) {
case 'option':
// if we're getting, immediately return the value
if ( value === undefined && typeof key !== "object" ) {
return base.options[key];
}
// set default object to trigger the paged event (legacy opperation)
var options = {'trigger': true},
result = false;
// if the key passed in is an object
if($.isPlainObject(key) && !value){
$.extend(options, key)
}
else{ // make the key value pair part of the default object
options[key] = value;
}
var prevent_paged = (options.trigger === false);
// if current_page property is set call setPage
if(options.current_page !== undefined){
result = base.setPage(options.current_page, prevent_paged);
}
// if max_page property is set call setMaxPage
if(options.max_page !== undefined){
result = base.setMaxPage(options.max_page, prevent_paged);
}
// if we've not got a result fire an error and return false
if( result === false ) console.error('jqPagination: cannot get / set option ' + key);
return result;
break;
case 'destroy':
base.$el
.off('.jqPagination')
.find('*')
.off('.jqPagination');
break;
default:
// the function name must not exist
console.error('jqPagination: method "' + method + '" does not exist');
return false;
}
};
// Run initializer
base.init();
};
$.jqPagination.defaultOptions = {
current_page : 1,
link_string : '',
max_page : null,
page_string : 'Page {current_page} of {max_page}',
paged : function () {}
};
$.fn.jqPagination = function () {
// get any function parameters
var self = this,
$self = $(self),
args = Array.prototype.slice.call(arguments),
result = false;
// if the first argument is a string call the desired function
// note: we can only do this to a single element, and not a collection of elements
if (typeof args[0] === 'string') {
// if we're getting, we can only get value for the first pagination element
if (args[2] === undefined) {
result = $self.first().data('jqPagination').callMethod(args[0], args[1]);
} else {
// if we're setting, set values for all pagination elements
$self.each(function(){
result = $(this).data('jqPagination').callMethod(args[0], args[1], args[2]);
});
}
return result;
}
// if we're not dealing with a method, initialise plugin
self.each(function () {
(new $.jqPagination(this, args[0]));
});
};
})(jQuery);