Debian Codegraph @

beta-testing/pager-custom-controls.js

raw symbols SCIP
1 /*!
2 * custom pager controls (beta) for Tablesorter - updated 9/28/2016 (v2.27.8)
3 initialize custom pager script BEFORE initializing tablesorter/tablesorter pager
4 custom pager looks like this:
5 1 | 2 … 5 | 6 | 7 … 99 | 100
6 _ _ _ _ adjacentSpacer
7 _ _ distanceSpacer
8 _____ ________ ends (2 default)
9 _________ aroundCurrent (1 default)
10
11 */
12 /*jshint browser:true, jquery:true, unused:false, loopfunc:true */
13 /*global jQuery: false */
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, // number of pages to show of either end
27 aroundCurrent : 1, // number of pages surrounding the current page
28 link : '<a href="#">{page}</a>', // page element; use {page} to include the page number
29 currentClass : 'current', // current page class name
30 adjacentSpacer : '<span> | </span>', // spacer for page numbers next to each other
31 distanceSpacer : '<span> &#133; <span>', // spacer for page numbers away from each other (ellipsis)
32 addKeyboard : true, // use left,right,up,down,pageUp,pageDown,home, or end to change current page
33 pageKeyStep : 10 // page step to use for pageUp and pageDown
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, // using widget
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 // include first and last pages (ends) in the pagination
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 // sort the list
68 pageArray = pageArray.sort(function(a, b) { return a - b; });
69 // only include unique pages
70 pageArray = $.grep(pageArray, function(value, key) {
71 return $.inArray(value, pageArray) === key;
72 });
73 // make links and spacers
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 // don't focus on pager when using filter - fixes #1296
93 $pager.find('.' + options.currentClass).focus();
94 }
95 });
96
97 // set up pager controls
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 // make right/left arrow keys work
121 if (options.addKeyboard) {
122 $(document).on('keydown', function(events) {
123 // ignore arrows inside form elements
124 if (/input|select|textarea/i.test(events.target.nodeName) ||
125 !(events.which > 32 && events.which < 41)) {
126 focusOnPager = false;
127 return;
128 }
129 // only allow keyboard use if element inside of pager is focused
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; } // pageUp
139 if (key === 34) { page += options.pageKeyStep; } // pageDown
140 if (key === 35) { page = max; } // end
141 if (key === 36) { page = 1; } // home
142 if (key === 37 || key === 38) { page -= 1; } // left/up
143 if (key === 39 || key === 40) { page += 1; } // right/down
144 $table.trigger('pageSet', page);
145 }
146 }
147 });
148 }
149 };
150
151 })(jQuery);