-
Notifications
You must be signed in to change notification settings - Fork 0
/
jTPS.js
345 lines (313 loc) · 16.4 KB
/
jTPS.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
/*
* jTPS - table sorting, pagination, and animated page scrolling
* version 0.5.1
* Author: Jim Palmer
* Released under MIT license.
*/
(function($) {
// apply table controls + setup initial jTPS namespace within jQuery
$.fn.jTPS = function ( opt ) {
$(this).data('tableSettings', $.extend({
perPages: [5, 6, 10, 20, 50, 'ALL'], // the "show per page" selection
perPageText: 'Show per page:', // text that appears before perPages links
perPageDelim: '<span style="color:#ccc;">|</span>', // text or dom node that deliminates each perPage link
perPageSeperator: '..', // text or dom node that deliminates split in select page links
scrollDelay: 30, // delay (in ms) between steps in anim. - IE has trouble showing animation with < 30ms delay
scrollStep: 2, // how many tr's are scrolled per step in the animated vertical pagination scrolling
fixedLayout: true, // autoset the width/height on each cell and set table-layout to fixed after auto layout
clickCallback: function () {} // callback function after clicks on sort, perpage and pagination
}, opt));
// generic pass-through object + other initial variables
var pT = $(this), page = page || 1, perPages = $(this).data('tableSettings').perPages, perPage = perPage || perPages[0],
rowCount = $('>tbody', this).find('tr').length;
// append jTPS class "stamp"
$(this).addClass('jTPS');
// setup the fixed table-layout so that the animation doesn't bounce around - faux grid for table
if ( $(this).data('tableSettings').fixedLayout ) {
// "fix" the table layout and individual cell width & height settings
if ( $(this).css('table-layout') != 'fixed' ) {
// find max tbody td cell height
var maxCellHeight = 0;
// set width style on the TH headers (rely on jQuery with computed styles support)
$('>thead', this).find('th,td').each(function () { $(this).css('width', $(this).width()); });
// ensure browser-formated widths for each column in the thead and tbody
var tbodyCh = $('>tbody',this)[0].childNodes, tmpp = 0;
// loop through tbody children and find the Nth <TR>
for ( var tbi=0, tbcl=tbodyCh.length; tbi < tbcl; tbi++ )
if ( tbodyCh[ tbi ].nodeName == 'TR' )
maxCellHeight = Math.max( maxCellHeight, tbodyCh[ tbi ].offsetHeight );
// now set the height attribute and/or style to the first TD cell (not the row)
for ( var tbi=0, tbcl=tbodyCh.length; tbi < tbcl; tbi++ )
if ( tbodyCh[ tbi ].nodeName == 'TR' )
for ( var tdi=0, trCh=tbodyCh[ tbi ].childNodes, tdcl=trCh.length; tdi < tdcl; tdi++ )
if ( trCh[ tdi ].nodeName == 'TD' ) {
trCh[ tdi ].style.height = maxCellHeight + 'px';
tdi = tdcl;
}
// now set the table layout to fixed
$(this).css('table-layout','fixed');
}
}
// remove all stub rows
$('.stubCell', this).remove();
// add the stub rows
var stubCount=0, cols = Math.max( $('>thead:first tr:last th,>thead:first tr:last td', this).length, parseInt( $('>thead:first tr:last th,>thead:first tr:last td').attr('colspan') || 0 ) ),
stubs = ( perPage - ( $('>tbody>tr', this).length % perPage ) ),
stubHeight = $('>tbody>tr:first>td:first', this).css('height');
for ( ; stubCount < stubs && stubs != perPage; stubCount++ )
$('>tbody>tr:last', this).after( '<tr class="stubCell"><td colspan="' + cols + '" style="height: ' + stubHeight + ';"> </td></tr>' );
// paginate the result
if ( rowCount > perPage && perPage != 0 )
$('>tbody>tr:gt(' + (perPage - 1) + ')', this).addClass('hideTR');
// bind sort functionality to theader
if (perPage != 0)
$('>thead [sort],>thead .sort', this).each(
function (tdInd) {
$(this).addClass('sortableHeader').unbind('click').bind('click',
function () {
var columnNo = $('>thead tr:last', pT).children().index( $(this) ),
desc = $('>thead [sort],>thead .sort', pT).eq(columnNo).hasClass('sortAsc') ? true : false;
// sort the rows
sort( pT, columnNo, desc );
// show first perPages rows
var page = parseInt( $('.hilightPageSelector:first', pT).html() ) || 1;
$('>tbody>tr', pT).removeClass('hideTR').filter(':gt(' + ( ( perPage - 1 ) * page ) + ')').addClass('hideTR');
$('>tbody>tr:lt(' + ( ( perPage - 1 ) * ( page - 1 ) ) + ')', pT).addClass('hideTR');
// scroll to first page if not already
if ($('.pageSelector', pT).index($('.hilightPageSelector', pT)) > 0)
$('.pageSelector:first', pT).click();
// hilight the sorted column header
$('>thead .sortDesc,>thead .sortAsc', pT).removeClass('sortDesc').removeClass('sortAsc');
$('>thead [sort],>thead .sort', pT).eq(columnNo).addClass( desc ? 'sortDesc' : 'sortAsc' );
// hilight the sorted column
$('>tbody>tr>td.sortedColumn', pT).removeClass('sortedColumn');
$('>tbody>tr:not(.stubCell)', pT).each( function () { $('>td:eq(' + columnNo + ')', this).addClass('sortedColumn'); } );
clearSelection();
// callback function after pagination renderd
$(pT).data('tableSettings').clickCallback();
}
);
}
);
// add perPage selection link + delim dom node
$('>.nav .selectPerPage', this).empty();
var pageSel = perPages.length;
while ( pageSel-- )
$('>.nav .selectPerPage', this).prepend( ( (pageSel > 0) ? $(this).data('tableSettings').perPageDelim : '' ) +
'<span class="perPageSelector">' + perPages[pageSel] + '</span>' );
// now draw the page selectors
drawPageSelectors( this, page || 1 );
// prepend the instructions and attach select hover and click events
$('>.nav .selectPerPage', this).prepend( $(this).data('tableSettings').perPageText ).find('.perPageSelector').each(
function () {
if ( ( parseInt($(this).html()) || rowCount ) == perPage )
$(this).addClass('perPageSelected');
$(this).bind('mouseover mouseout',
function (e) {
e.type == 'mouseover' ? $(this).addClass('perPageHilight') : $(this).removeClass('perPageHilight');
}
);
$(this).bind('click',
function () {
// set the new number of pages
perPage = parseInt( $(this).html() ) || rowCount;
if ( perPage > rowCount ) perPage = rowCount;
// remove all stub rows
$('.stubCell', this).remove();
// redraw stub rows
var stubCount=0, cols = $('>thead th,>thead td', pT).length,
stubs = ( perPage - ( $('>tbody>tr', pT).length % perPage ) ),
stubHeight = $('>tbody>tr:first>td:first', pT).css('height');
for ( ; stubCount < stubs && stubs != perPage; stubCount++ )
$('>tbody>tr:last', pT).after( '<tr class="stubCell"><td colspan="' + cols + '" style="height: ' + stubHeight + ';"> </td></tr>' );
// set new visible rows
$('>tbody>tr', pT).removeClass('hideTR').filter(':gt(' + ( ( perPage - 1 ) * page ) + ')').addClass('hideTR');
$('>tbody>tr:lt(' + ( ( perPage - 1 ) * ( page - 1 ) ) + ')', pT).addClass('hideTR');
// back to the first page
$('.pageSelector:first', pT).click();
$(this).siblings('.perPageSelected').removeClass('perPageSelected');
$(this).addClass('perPageSelected');
// redraw the pagination
drawPageSelectors( pT, 1 );
// update status bar
var cPos = $('>tbody>tr:not(.hideTR):first', pT).prevAll().length,
ePos = $('>tbody>tr:not(.hideTR):not(.stubCell)', pT).length;
$('>.nav .status', pT).html( 'Showing ' + ( cPos + 1 ) + ' - ' + ( cPos + ePos ) + ' of ' + rowCount + '' );
clearSelection();
// callback function after pagination renderd
$(pT).data('tableSettings').clickCallback();
}
);
}
);
// show the correct paging status
var cPos = $('>tbody>tr:not(.hideTR):first', this).prevAll().length,
ePos = $('>tbody>tr:not(.hideTR):not(.stubCell)', this).length;
$('>.nav .status', this).html( 'showing ' + ( cPos + 1 ) + ' - ' + ( cPos + ePos ) + ' of ' + rowCount );
// clear selected text function
function clearSelection () {
if ( document.selection && typeof(document.selection.empty) != 'undefined' )
document.selection.empty();
else if ( typeof(window.getSelection) === 'function' && typeof(window.getSelection().removeAllRanges) === 'function' )
window.getSelection().removeAllRanges();
}
// render the pagination functionality
function drawPageSelectors ( target, page ) {
// add pagination links
$('>.nav .pagination', target).empty();
var pages = ( perPage >= rowCount || perPage == 0 ) ? 0 : Math.ceil( rowCount / perPage ), totalPages = pages;
while ( pages-- )
$('>.nav .pagination', target).prepend( '<div class="pageSelector">' + ( pages + 1 ) + '</div>' );
var pageCount = $('>.nav:first .pageSelector', target).length;
$('>.nav', target).each(function () {
$('.hidePageSelector', this).removeClass('hidePageSelector');
$('.hilightPageSelector', this).removeClass('hilightPageSelector');
$('.pageSelectorSeperator', this).remove();
$('.pageSelector:lt(' + ( ( page > ( pageCount - 4 ) ) ? ( pageCount - 5 ) : ( page - 2 ) ) + '):not(:first)', this).addClass('hidePageSelector')
.eq(0).after( '<div class="pageSelectorSeperator">' + $(target).data('tableSettings').perPageSeperator + '</div>' );
$('.pageSelector:gt(' + ( ( page < 4 ) ? 4 : page ) + '):not(:last)', this).addClass('hidePageSelector')
.eq(0).after( '<div class="pageSelectorSeperator">' + $(target).data('tableSettings').perPageSeperator + '</div>' );
$('.pageSelector:eq(' + ( page - 1 ) + ')', this).addClass('hilightPageSelector');
});
// remove the pager title if no pages necessary
if ( perPage >= rowCount )
$('>.nav .paginationTitle', target).css('display','none');
else
$('>.nav .paginationTitle', target).css('display','');
// bind the pagination onclick
$('>.nav .pagination .pageSelector', target).each(
function () {
$(this).bind('click',
function () {
// if double clicked - stop animation and jump to selected page - this appears to be a tripple click in IE7
if ( $(this).hasClass('hilightPageSelector') ) {
if ( $(this).parent().queue().length > 0 ) {
// really stop all animations and create new queue
$(this).parent().stop().queue( "fx", [] ).stop();
// set the user directly on the correct page without animation
var beginPos = ( ( parseInt( $(this).html() ) - 1 ) * perPage ), endPos = beginPos + perPage;
$('>tbody> tr', pT).removeClass('hideTR').addClass('hideTR');
$('>tbody>tr:gt(' + (beginPos - 2) + '):lt(' + ( perPage ) + ')', pT).andSelf().removeClass('hideTR');
// update status bar
var cPos = $('>tbody>tr:not(.hideTR):first', pT).prevAll().length,
ePos = $('>tbody>tr:not(.hideTR):not(.stubCell)', pT).length;
$('>.nav .status', pT).html( 'Showing ' + ( cPos + 1 ) + ' - ' + ( cPos + ePos ) + ' of ' + rowCount + '' );
}
clearSelection();
return false;
}
// hilight the specific page button
$(this).addClass('hilightPageSelector');
// really stop all animations
$(this).parent().stop().queue( "fx", [] ).stop().dequeue();
// setup the pagination variables
var beginPos = $('>tbody>tr:not(.hideTR):first', pT).prevAll().length,
endPos = ( ( parseInt( $(this).html() ) - 1 ) * perPage );
if ( endPos > rowCount )
endPos = (rowCount - 1);
// set the steps to be exponential for all the page scroll difference - i.e. faster for more pages to scroll
var sStep = $(pT).data('tableSettings').scrollStep * Math.ceil( Math.abs( ( endPos - beginPos ) / perPage ) );
if ( sStep > perPage ) sStep = perPage;
var steps = Math.ceil( Math.abs( beginPos - endPos ) / sStep );
// start scrolling
while ( steps-- ) {
$(this).parent().animate({'opacity':1}, $(pT).data('tableSettings').scrollDelay,
function () {
// reset the scrollStep for the remaining items
if ( $(this).queue("fx").length == 0 )
sStep = ( Math.abs( beginPos - endPos ) % sStep ) || sStep;
/* scoll up */
if ( beginPos > endPos ) {
$('>tbody>tr:not(.hideTR):first', pT).prevAll(':lt(' + sStep + ')').removeClass('hideTR');
if ( $('>tbody>tr:not(.hideTR)', pT).length > perPage )
$('>tbody>tr:not(.hideTR):last', pT).prevAll(':lt(' + ( sStep - 1 ) + ')').andSelf().addClass('hideTR');
// if scrolling up from less rows than perPage - compensate if < perPage
var currRows = $('>tbody>tr:not(.hideTR)', pT).length;
if ( currRows < perPage )
$('>tbody>tr:not(.hideTR):last', pT).nextAll(':lt(' + ( perPage - currRows ) + ')').removeClass('hideTR');
/* scroll down */
} else {
var endPoint = $('>tbody>tr:not(.hideTR):last', pT);
$('>tbody>tr:not(.hideTR):lt(' + sStep + ')', pT).addClass('hideTR');
$(endPoint).nextAll(':lt(' + sStep + ')').removeClass('hideTR');
}
// update status bar
var cPos = $('>tbody>tr:not(.hideTR):first', pT).prevAll().length,
ePos = $('>tbody>tr:not(.hideTR):not(.stubCell)', pT).length;
$('>.nav .status', pT).html( 'Showing ' + ( cPos + 1 ) + ' - ' + ( cPos + ePos ) + ' of ' + rowCount + '' );
}
);
}
// redraw the pagination
drawPageSelectors( pT, parseInt( $(this).html() ) );
// callback function after pagination renderd
$(pT).data('tableSettings').clickCallback();
}
);
}
);
};
// sort wrapper function
function sort ( target, tdIndex, desc ) {
var fCol = $('>thead th,>thead th', target).get(tdIndex),
sorted = $(fCol).hasClass('sortAsc') || $(fCol).hasClass('sortDesc') || false,
nullChar = String.fromCharCode(0),
re = /([-]?[0-9\.]+)/g,
rows = $('>tbody>tr:not(.stubCell)', target).get(),
procRow = [];
$(rows).each(
function(key, val) {
procRow.push( $('>td:eq(' + tdIndex + ')', val).text() + nullChar + procRow.length );
}
);
if ( !sorted ) {
// natural sort
procRow.sort(
function naturalSort (a, b) {
// setup temp-scope variables for comparison evauluation
var re = /(-?[0-9\.]+)/g,
nC = String.fromCharCode(0),
x = a.toString().toLowerCase().split(nC)[0] || '',
y = b.toString().toLowerCase().split(nC)[0] || '',
xN = x.replace( re, nC + '$1' + nC ).split(nC),
yN = y.replace( re, nC + '$1' + nC ).split(nC),
xD = (new Date(x)).getTime(),
yD = xD ? (new Date(y)).getTime() : null;
// natural sorting of dates
if ( yD )
if ( xD < yD ) return -1;
else if ( xD > yD ) return 1;
// natural sorting through split numeric strings and default strings
for( var cLoc = 0, numS = Math.max(xN.length, yN.length); cLoc < numS; cLoc++ ) {
oFxNcL = parseFloat(xN[cLoc]) || xN[cLoc];
oFyNcL = parseFloat(yN[cLoc]) || yN[cLoc];
if (oFxNcL < oFyNcL) return -1;
else if (oFxNcL > oFyNcL) return 1;
}
return 0;
});
if ( !desc ) procRow.reverse(); // properly position order of sort
}
// now re-order the parent tbody based off the quick sorted tbody map
$('>tbody', target).addClass('jtpstemp').before('<tbody></tbody>');
var nr = procRow.length, tf = $('>tbody', target)[0];
// move the row from old tbody to new tbody in order of new tbody with replaceWith to retain original tbody row positioning
if ( sorted )
while ( nr-- )
tf.appendChild( rows[ nr ] );
else
while ( nr-- )
tf.appendChild( rows[ parseInt( procRow[ nr ].split(nullChar).pop() ) ] );
// remove the old table
$('>tbody.jtpstemp', target).remove();
// redraw stub rows
var stubCount=0, cols = $('>thead>tr:last th', target).length,
stubs = ( perPage - ( $('>tbody>tr', target).length % perPage ) ),
stubHeight = $('>tbody>tr:first>td:first', target).css('height');
for ( ; stubCount < stubs && stubs != perPage; stubCount++ )
$('>tbody>tr:last', target).after( '<tr class="stubCell"><td colspan="' + cols + '" style="height: ' + stubHeight + ';"> </td></tr>' );
}
// chainable
return this;
};
})(jQuery);