| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
100 |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
;(function($) { |
| 16 |
'use strict'; |
| 17 |
|
| 18 |
$.tablesorter = $.tablesorter || {}; |
| 19 |
|
| 20 |
$.tablesorter.customPagerControls = function(settings) { |
| 21 |
var defaults = { |
| 22 |
table : 'table', |
| 23 |
pager : '.pager', |
| 24 |
pageSize : '.left a', |
| 25 |
currentPage : '.right a', |
| 26 |
ends : 2, |
| 27 |
aroundCurrent : 1, |
| 28 |
link : '<a href="#">{page}</a>', |
| 29 |
currentClass : 'current', |
| 30 |
adjacentSpacer : '<span> | </span>', |
| 31 |
distanceSpacer : '<span> … <span>', |
| 32 |
addKeyboard : true, |
| 33 |
pageKeyStep : 10 |
| 34 |
}, |
| 35 |
options = $.extend({}, defaults, settings), |
| 36 |
$table = $(options.table), |
| 37 |
$pager = $(options.pager), |
| 38 |
focusOnPager = false; |
| 39 |
|
| 40 |
$table |
| 41 |
.on('filterStart', function() { |
| 42 |
focusOnPager = false; |
| 43 |
}) |
| 44 |
.on('pagerInitialized pagerComplete', function (e, c) { |
| 45 |
var indx, |
| 46 |
p = c.pager ? c.pager : c, |
| 47 |
pages = $('<div/>'), |
| 48 |
cur = p.page + 1, |
| 49 |
pageArray = [], |
| 50 |
max = p.filteredPages, |
| 51 |
around = options.aroundCurrent; |
| 52 |
for (indx = -around; indx <= around; indx++) { |
| 53 |
if (cur + indx >= 1 && cur + indx <= max) { |
| 54 |
pageArray.push(cur + indx); |
| 55 |
} |
| 56 |
} |
| 57 |
if (pageArray.length) { |
| 58 |
|
| 59 |
for (indx = 0; indx < options.ends; indx++) { |
| 60 |
if ((indx + 1 <= max) && $.inArray(indx + 1, pageArray) === -1) { |
| 61 |
pageArray.push(indx + 1); |
| 62 |
} |
| 63 |
if ((max - indx > 0) && $.inArray(max - indx, pageArray) === -1) { |
| 64 |
pageArray.push(max - indx); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
pageArray = pageArray.sort(function(a, b) { return a - b; }); |
| 69 |
|
| 70 |
pageArray = $.grep(pageArray, function(value, key) { |
| 71 |
return $.inArray(value, pageArray) === key; |
| 72 |
}); |
| 73 |
|
| 74 |
if (pageArray.length) { |
| 75 |
max = pageArray.length - 1; |
| 76 |
$.each(pageArray, function(indx, value) { |
| 77 |
pages |
| 78 |
.append( |
| 79 |
$(options.link.replace(/\{page\}/g, value)) |
| 80 |
.toggleClass(options.currentClass, value === cur) |
| 81 |
.attr('data-page', value) |
| 82 |
) |
| 83 |
.append((indx < max && (pageArray[ indx + 1 ] - 1 !== value) ? |
| 84 |
options.distanceSpacer : |
| 85 |
(indx >= max ? '' : options.adjacentSpacer) |
| 86 |
)); |
| 87 |
}); |
| 88 |
} |
| 89 |
} |
| 90 |
$pager.find('.pagecount').html(pages.html()); |
| 91 |
if (focusOnPager) { |
| 92 |
|
| 93 |
$pager.find('.' + options.currentClass).focus(); |
| 94 |
} |
| 95 |
}); |
| 96 |
|
| 97 |
|
| 98 |
$pager |
| 99 |
.find(options.pageSize) |
| 100 |
.on('click', function () { |
| 101 |
$(this) |
| 102 |
.addClass(options.currentClass) |
| 103 |
.siblings() |
| 104 |
.removeClass(options.currentClass); |
| 105 |
$table.trigger('pageSize', $(this).html()); |
| 106 |
return false; |
| 107 |
}) |
| 108 |
.end() |
| 109 |
.on('click', options.currentPage, function() { |
| 110 |
focusOnPager = true; |
| 111 |
var $el = $(this); |
| 112 |
$el |
| 113 |
.addClass(options.currentClass) |
| 114 |
.siblings() |
| 115 |
.removeClass(options.currentClass); |
| 116 |
$table.trigger('pageSet', $el.attr('data-page')); |
| 117 |
return false; |
| 118 |
}); |
| 119 |
|
| 120 |
|
| 121 |
if (options.addKeyboard) { |
| 122 |
$(document).on('keydown', function(events) { |
| 123 |
|
| 124 |
if (/input|select|textarea/i.test(events.target.nodeName) || |
| 125 |
!(events.which > 32 && events.which < 41)) { |
| 126 |
focusOnPager = false; |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
if ($(document.activeElement).closest(options.pager).is($pager)) { |
| 131 |
events.preventDefault(); |
| 132 |
focusOnPager = true; |
| 133 |
var key = events.which, |
| 134 |
max = $table[0].config.totalRows, |
| 135 |
$el = $pager.find(options.currentPage).filter('.' + options.currentClass), |
| 136 |
page = $el.length ? parseInt($el.attr('data-page'), 10) : null; |
| 137 |
if (page) { |
| 138 |
if (key === 33) { page -= options.pageKeyStep; } |
| 139 |
if (key === 34) { page += options.pageKeyStep; } |
| 140 |
if (key === 35) { page = max; } |
| 141 |
if (key === 36) { page = 1; } |
| 142 |
if (key === 37 || key === 38) { page -= 1; } |
| 143 |
if (key === 39 || key === 40) { page += 1; } |
| 144 |
$table.trigger('pageSet', page); |
| 145 |
} |
| 146 |
} |
| 147 |
}); |
| 148 |
} |
| 149 |
}; |
| 150 |
|
| 151 |
})(jQuery); |