2013-11-13 22:15:04 +00:00
|
|
|
/*!
|
2013-06-01 00:32:50 +00:00
|
|
|
* custom pager controls - beta testing
|
2013-11-13 22:15:04 +00:00
|
|
|
initialize custom pager script BEFORE initializing tablesorter/tablesorter pager
|
|
|
|
custom pager looks like this:
|
|
|
|
1 | 2 … 5 | 6 | 7 … 99 | 100
|
|
|
|
_ _ _ _ adjacentSpacer
|
|
|
|
_ _ distanceSpacer
|
|
|
|
_____ ________ ends (2 default)
|
|
|
|
_________ aroundCurrent (1 default)
|
|
|
|
|
2013-06-01 00:32:50 +00:00
|
|
|
*/
|
|
|
|
/*jshint browser:true, jquery:true, unused:false, loopfunc:true */
|
2013-11-13 22:15:04 +00:00
|
|
|
/*global jQuery: false */
|
2013-06-01 00:32:50 +00:00
|
|
|
|
|
|
|
;(function($){
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
$.tablesorter = $.tablesorter || {};
|
|
|
|
|
2013-11-13 22:15:04 +00:00
|
|
|
$.tablesorter.customPagerControls = function(settings) {
|
2013-06-01 00:32:50 +00:00
|
|
|
var defaults = {
|
|
|
|
table : 'table',
|
|
|
|
pager : '.pager',
|
|
|
|
pageSize : '.left a',
|
|
|
|
currentPage : '.right a',
|
|
|
|
ends : 2, // number of pages to show of either end
|
|
|
|
aroundCurrent : 1, // number of pages surrounding the current page
|
|
|
|
link : '<a href="#">{page}</a>', // page element; use {page} to include the page number
|
|
|
|
currentClass : 'current', // current page class name
|
|
|
|
adjacentSpacer : ' | ', // spacer for page numbers next to each other
|
|
|
|
distanceSpacer : ' … ', // spacer for page numbers away from each other (ellipsis)
|
|
|
|
addKeyboard : true // add left/right keyboard arrows to change current page
|
|
|
|
},
|
2013-11-13 22:15:04 +00:00
|
|
|
options = $.extend({}, defaults, settings),
|
|
|
|
$table = $(options.table);
|
2013-06-01 00:32:50 +00:00
|
|
|
|
|
|
|
$table
|
|
|
|
.on('pagerInitialized pagerComplete', function (e, c) {
|
2013-11-13 22:15:04 +00:00
|
|
|
var indx, pages = $('<div/>'), pageArray = [],
|
2013-06-01 00:32:50 +00:00
|
|
|
cur = c.page + 1,
|
2013-11-13 22:15:04 +00:00
|
|
|
start = cur > 1 ? (c.totalPages - cur < options.aroundCurrent ? -(options.aroundCurrent + 1) + (c.totalPages - cur) : -options.aroundCurrent) : 0,
|
|
|
|
end = cur < options.aroundCurrent + 1 ? options.aroundCurrent + 3 - cur : options.aroundCurrent + 1;
|
|
|
|
for (indx = start; indx < end; indx++) {
|
|
|
|
if (cur + indx >= 1 && cur + indx < c.totalPages) { pageArray.push( cur + indx ); }
|
2013-06-01 00:32:50 +00:00
|
|
|
}
|
|
|
|
// include first and last pages (ends) in the pagination
|
2013-11-13 22:15:04 +00:00
|
|
|
for (indx = 0; indx < options.ends; indx++){
|
|
|
|
if ($.inArray(indx + 1, pageArray) === -1) { pageArray.push(indx + 1); }
|
|
|
|
if ($.inArray(c.totalPages - indx, pageArray) === -1) { pageArray.push(c.totalPages - indx); }
|
2013-06-01 00:32:50 +00:00
|
|
|
}
|
|
|
|
// sort the list
|
2013-11-13 22:15:04 +00:00
|
|
|
pageArray = pageArray.sort(function(a, b){ return a - b; });
|
2013-06-01 00:32:50 +00:00
|
|
|
// make links and spacers
|
2013-11-13 22:15:04 +00:00
|
|
|
$.each(pageArray, function(indx, value){
|
2013-06-01 00:32:50 +00:00
|
|
|
pages
|
2013-11-13 22:15:04 +00:00
|
|
|
.append( $(options.link.replace(/\{page\}/g, value)).toggleClass(options.currentClass, value === cur).attr('data-page', value) )
|
|
|
|
.append( '<span>' + (indx < pageArray.length - 1 && ( pageArray[ indx + 1 ] - 1 !== value ) ? options.distanceSpacer :
|
|
|
|
( indx >= pageArray.length - 1 ? '' : options.adjacentSpacer )) + '</span>' );
|
2013-06-01 00:32:50 +00:00
|
|
|
});
|
|
|
|
$('.pagecount').html(pages.html());
|
|
|
|
});
|
|
|
|
|
|
|
|
// set up pager controls
|
2013-11-13 22:15:04 +00:00
|
|
|
$(options.pager).find(options.pageSize).on('click', function () {
|
2013-06-01 00:32:50 +00:00
|
|
|
$(this)
|
2013-11-13 22:15:04 +00:00
|
|
|
.addClass(options.currentClass)
|
2013-06-01 00:32:50 +00:00
|
|
|
.siblings()
|
2013-11-13 22:15:04 +00:00
|
|
|
.removeClass(options.currentClass);
|
2013-06-01 00:32:50 +00:00
|
|
|
$table.trigger('pageSize', $(this).html());
|
|
|
|
return false;
|
|
|
|
}).end()
|
2013-11-13 22:15:04 +00:00
|
|
|
.on('click', options.currentPage, function(){
|
2013-06-01 00:32:50 +00:00
|
|
|
$(this)
|
2013-11-13 22:15:04 +00:00
|
|
|
.addClass(options.currentClass)
|
2013-06-01 00:32:50 +00:00
|
|
|
.siblings()
|
2013-11-13 22:15:04 +00:00
|
|
|
.removeClass(options.currentClass);
|
2013-06-01 00:32:50 +00:00
|
|
|
$table.trigger('pageSet', $(this).attr('data-page'));
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
// make right/left arrow keys work
|
2013-11-13 22:15:04 +00:00
|
|
|
if (options.addKeyboard) {
|
|
|
|
$(document).on('keydown', function(events){
|
2013-06-01 00:32:50 +00:00
|
|
|
// ignore arrows inside form elements
|
2013-11-13 22:15:04 +00:00
|
|
|
if (/input|select|textarea/i.test(events.target.tagName)) { return; }
|
|
|
|
if (events.which === 37) {
|
2013-06-01 00:32:50 +00:00
|
|
|
// left
|
2013-11-13 22:15:04 +00:00
|
|
|
$(options.pager).find(options.currentPage).filter('.' + options.currentClass).prevAll(':not(span):first').click();
|
|
|
|
} else if (events.which === 39) {
|
2013-06-01 00:32:50 +00:00
|
|
|
// right
|
2013-11-13 22:15:04 +00:00
|
|
|
$(options.pager).find(options.currentPage).filter('.' + options.currentClass).nextAll(':not(span):first').click();
|
2013-06-01 00:32:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})(jQuery);
|