Pager-custom-controls: Allow setting ends and/or aroundCurrent to zero. See #1276

This commit is contained in:
Rob Garrison 2016-08-23 22:51:36 -05:00
parent a42e762eda
commit a4ad8fd7b3
No known key found for this signature in database
GPG Key ID: 0A42D160D71978E1

View File

@ -12,7 +12,7 @@
/*jshint browser:true, jquery:true, unused:false, loopfunc:true */ /*jshint browser:true, jquery:true, unused:false, loopfunc:true */
/*global jQuery: false */ /*global jQuery: false */
;(function($){ ;(function($) {
"use strict"; "use strict";
$.tablesorter = $.tablesorter || {}; $.tablesorter = $.tablesorter || {};
@ -35,32 +35,24 @@ $.tablesorter.customPagerControls = function(settings) {
$table = $(options.table), $table = $(options.table),
$pager = $(options.pager); $pager = $(options.pager);
function validate(val, min, max) {
if (val < min) { return min; }
if (val > max) { return max; }
return val;
}
$table $table
.on('pagerInitialized pagerComplete', function (e, c) { .on('pagerInitialized pagerComplete', function (e, c) {
var indx, var indx,
p = c.pager ? c.pager : c, // using widget p = c.pager ? c.pager : c, // using widget
pages = $('<div/>'), pages = $('<div/>'),
cur = p.page + 1, cur = p.page + 1,
pageArray = [cur], pageArray = [],
max = p.filteredPages, max = p.filteredPages,
around = options.aroundCurrent, around = options.aroundCurrent;
start = validate(cur > 1 ? (max - cur < around ? -(around + 1) + (max - cur) : -around) : 0, 0, max), for (indx = -around; indx <= around; indx++) {
end = validate(cur < around + 1 ? around + 3 - cur : around + 1, 0, max); if (cur + indx >= 1 && cur + indx <= max) {
for (indx = start; indx < end; indx++) {
if (cur + indx >= 1 && cur + indx < max) {
pageArray.push(cur + indx); pageArray.push(cur + indx);
} }
} }
if (pageArray.length) { if (pageArray.length) {
// include first and last pages (ends) in the pagination // include first and last pages (ends) in the pagination
for (indx = 0; indx < options.ends; indx++){ for (indx = 0; indx < options.ends; indx++) {
if ((indx + 1 < max) && $.inArray(indx + 1, pageArray) === -1) { if ((indx + 1 <= max) && $.inArray(indx + 1, pageArray) === -1) {
pageArray.push(indx + 1); pageArray.push(indx + 1);
} }
if ((max - indx > 0) && $.inArray(max - indx, pageArray) === -1) { if ((max - indx > 0) && $.inArray(max - indx, pageArray) === -1) {
@ -68,13 +60,14 @@ $.tablesorter.customPagerControls = function(settings) {
} }
} }
// sort the list // sort the list
pageArray = pageArray.sort(function(a, b){ return a - b; }); pageArray = pageArray.sort(function(a, b) { return a - b; });
// only include unique pages // only include unique pages
pageArray = $.grep(pageArray, function(value, key){ pageArray = $.grep(pageArray, function(value, key) {
return $.inArray(value, pageArray) === key; return $.inArray(value, pageArray) === key;
}); });
// make links and spacers // make links and spacers
if (pageArray.length > 1) { if (pageArray.length) {
max = pageArray.length - 1;
$.each(pageArray, function(indx, value) { $.each(pageArray, function(indx, value) {
pages pages
.append( $(options.link.replace(/\{page\}/g, value)).toggleClass(options.currentClass, value === cur).attr('data-page', value) ) .append( $(options.link.replace(/\{page\}/g, value)).toggleClass(options.currentClass, value === cur).attr('data-page', value) )
@ -100,8 +93,9 @@ $.tablesorter.customPagerControls = function(settings) {
return false; return false;
}) })
.end() .end()
.on('click', options.currentPage, function(){ .on('click', options.currentPage, function() {
var $el =$(this) var $el = $(this);
$el
.addClass(options.currentClass) .addClass(options.currentClass)
.siblings() .siblings()
.removeClass(options.currentClass); .removeClass(options.currentClass);
@ -111,7 +105,7 @@ $.tablesorter.customPagerControls = function(settings) {
// make right/left arrow keys work // make right/left arrow keys work
if (options.addKeyboard) { if (options.addKeyboard) {
$(document).on('keydown', function(events){ $(document).on('keydown', function(events) {
// ignore arrows inside form elements // ignore arrows inside form elements
if (/input|select|textarea/i.test(events.target.nodeName)) { if (/input|select|textarea/i.test(events.target.nodeName)) {
return; return;