Debian Codegraph @

js/widgets/widget-scroller.js

raw symbols SCIP
1 /*! Widget: scroller - updated 2018-05-07 (v2.30.4) *//*
2 Copyright (C) 2011 T. Connell & Associates, Inc.
3
4 Dual-licensed under the MIT and GPL licenses
5
6 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
7 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
8 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
9 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
10 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11
12 Resizable scroller widget for the jQuery tablesorter plugin
13
14 Version 2.0 - modified by Rob Garrison 4/12/2013;
15 updated 3/5/2015 (v2.22.2) with lots of help from TheSin-
16 Requires jQuery v1.7+
17 Requires the tablesorter plugin, v2.8+, available at http://mottie.github.com/tablesorter/docs/
18
19 Usage:
20 $(function() {
21 $('table.tablesorter').tablesorter({
22 widgets: ['zebra', 'scroller'],
23 widgetOptions : {
24 scroller_height : 300, // height of scroll window
25 scroller_jumpToHeader : true, // header snap to browser top when scrolling the tbody
26 scroller_upAfterSort : true, // scroll tbody to top after sorting
27 scroller_fixedColumns : 0 // set number of fixed columns
28 }
29 });
30 });
31
32 Website: www.tconnell.com
33 */
34 /*jshint browser:true, jquery:true, unused:false */
35 ;( function( $, window ) {
36 'use strict';
37
38 var ts = $.tablesorter,
39 tscss = ts.css;
40
41 $.extend( ts.css, {
42 scrollerWrap : 'tablesorter-scroller',
43 scrollerHeader : 'tablesorter-scroller-header',
44 scrollerTable : 'tablesorter-scroller-table',
45 scrollerFooter : 'tablesorter-scroller-footer',
46 scrollerFixed : 'tablesorter-scroller-fixed',
47 scrollerFixedPanel : 'tablesorter-scroller-fixed-panel',
48 scrollerHasFix : 'tablesorter-scroller-has-fixed-columns',
49 scrollerHideColumn : 'tablesorter-scroller-hidden-column',
50 scrollerHideElement : 'tablesorter-scroller-hidden',
51 scrollerSpacerRow : 'tablesorter-scroller-spacer',
52 scrollerBarSpacer : 'tablesorter-scroller-bar-spacer',
53 scrollerAddedHeight : 'tablesorter-scroller-added-height',
54 scrollerHack : 'tablesorter-scroller-scrollbar-hack',
55 // class name on table cannot start with 'tablesorter-' or the
56 // suffix 'scroller-rtl' will match as a theme name
57 scrollerRtl : 'ts-scroller-rtl'
58 });
59
60 ts.addWidget({
61 id : 'scroller',
62 priority : 60, // run after the filter widget
63 options : {
64 scroller_height : 300,
65 // pop table header into view while scrolling up the page
66 scroller_jumpToHeader : true,
67 // scroll tbody to top after sorting
68 scroller_upAfterSort : true,
69 // set number of fixed columns
70 scroller_fixedColumns : 0,
71 // add hover highlighting to the fixed column (disable if it causes slowing)
72 scroller_rowHighlight : 'hover',
73 // add a fixed column overlay for styling
74 scroller_addFixedOverlay : false,
75 // In tablesorter v2.19.0 the scroll bar width is auto-detected
76 // add a value here to override the auto-detected setting
77 scroller_barWidth : null
78 },
79 format : function( table, c, wo ) {
80 if ( c.isScrolling ) {
81 ts.scroller.resize( c, wo );
82 } else {
83 // initialize here instead of in widget init to give the
84 // filter widget time to finish building the filter row
85 ts.scroller.setup( c, wo );
86 }
87 },
88 remove : function( table, c, wo ) {
89 ts.scroller.remove( c, wo );
90 }
91 });
92
93 /* Add window resizeEnd event (also used by columnSelector widget) */
94 ts.window_resize = function() {
95 if ( ts.timer_resize ) {
96 clearTimeout( ts.timer_resize );
97 }
98 ts.timer_resize = setTimeout( function() {
99 $( window ).trigger( 'resizeEnd' );
100 }, 250 );
101 };
102
103 // Add extra scroller css
104 $( function() {
105 var style = '<style>' +
106 /* overall wrapper & table section wrappers */
107 '.' + tscss.scrollerWrap + ' { position: relative; overflow: hidden; }' +
108 /* add border-box sizing to all scroller widget tables; see #135 */
109 '.' + tscss.scrollerWrap + ' * { box-sizing: border-box; }' +
110 '.' + tscss.scrollerHeader + ', .' + tscss.scrollerFooter + ' { position: relative; overflow: hidden; }' +
111 '.' + tscss.scrollerHeader + ' table.' + tscss.table + ' { margin-bottom: 0; }' +
112 /* always leave the scroll bar visible for tbody, or table overflows into the scrollbar
113 when height < max height (filtering) */
114 '.' + tscss.scrollerTable + ' { position: relative; overflow: auto; }' +
115 '.' + tscss.scrollerTable + ' table.' + tscss.table +
116 ' { border-top: 0; margin-top: 0; margin-bottom: 0; overflow: hidden; max-width: initial; }' +
117 /* hide footer in original table */
118 '.' + tscss.scrollerTable + ' tfoot, .' + tscss.scrollerHideElement + ', .' + tscss.scrollerHideColumn +
119 ' { display: none; }' +
120
121 /*** fixed column ***/
122 /* disable pointer-events on fixed column wrapper or the user can't interact with the horizontal scrollbar */
123 '.' + tscss.scrollerFixed + ', .' + tscss.scrollerFixed + ' .' + tscss.scrollerFixedPanel +
124 ' { pointer-events: none; }' +
125 /* enable pointer-events for fixed column children; see #135 & #878 */
126 '.' + tscss.scrollerFixed + ' > div { pointer-events: all; }' +
127 '.' + tscss.scrollerWrap + ' .' + tscss.scrollerFixed + ' { position: absolute; top: 0; z-index: 1; left: 0 } ' +
128 '.' + tscss.scrollerWrap + ' .' + tscss.scrollerFixed + '.' + tscss.scrollerRtl + ' { left: auto; right: 0 } ' +
129 /* add horizontal scroll bar; set to 'auto', see #135 */
130 '.' + tscss.scrollerWrap + '.' + tscss.scrollerHasFix + ' > .' + tscss.scrollerTable + ' { overflow: auto; }' +
131 /* need to position the tbody & tfoot absolutely to hide the scrollbar & move the footer
132 below the horizontal scrollbar */
133 '.' + tscss.scrollerFixed + ' .' + tscss.scrollerFooter + ' { position: absolute; bottom: 0; }' +
134 /* hide fixed tbody scrollbar - see http://goo.gl/VsLe6n - set overflow to auto here for mousewheel scroll */
135 '.' + tscss.scrollerFixed + ' .' + tscss.scrollerTable +
136 ' { position: relative; left: 0; overflow: auto; -ms-overflow-style: none; }' +
137 '.' + tscss.scrollerFixed + ' .' + tscss.scrollerTable + '::-webkit-scrollbar { display: none; }' +
138 /*** fixed column panel ***/
139 '.' + tscss.scrollerWrap + ' .' + tscss.scrollerFixedPanel +
140 ' { position: absolute; top: 0; bottom: 0; z-index: 2; left: 0; right: 0; } ' +
141 '</style>';
142 $( 'head' ).append( style );
143 });
144
145 ts.scroller = {
146
147 // Ugh.. Firefox misbehaves, so it needs to be detected
148 isFirefox : navigator.userAgent.toLowerCase().indexOf( 'firefox' ) > -1,
149 // old IE needs a wrap to hide the fixed column scrollbar; http://stackoverflow.com/a/24408672/145346
150 isOldIE : document.all && !window.atob,
151 isIE : ( document.all && !window.atob ) || navigator.appVersion.indexOf( 'Trident/' ) > 0,
152 // http://stackoverflow.com/questions/7944460/detect-safari-browser - needed to position scrolling body
153 // when the table is set up in RTL direction
154 isSafari : navigator.userAgent.toLowerCase().indexOf( 'safari' ) > -1 &&
155 navigator.userAgent.toLowerCase().indexOf( 'chrome' ) === -1,
156
157 hasScrollBar : function( $target, checkWidth ) {
158 if ( checkWidth ) {
159 return $target.get(0).scrollWidth > $target.width();
160 } else {
161 return $target.get(0).scrollHeight > $target.height();
162 }
163 },
164
165 setWidth : function( $el, width ) {
166 $el.css({
167 'width' : width,
168 'min-width' : width,
169 'max-width' : width
170 });
171 },
172
173 // modified from http://davidwalsh.name/detect-scrollbar-width
174 getBarWidth : function() {
175 var $div = $( '<div>' ).css({
176 'position' : 'absolute',
177 'top' : '-9999px',
178 'left' : 0,
179 'width' : '100px',
180 'height' : '100px',
181 'overflow' : 'scroll',
182 'visibility' : 'hidden'
183 }).appendTo( 'body' ),
184 div = $div[0],
185 barWidth = div.offsetWidth - div.clientWidth;
186 $div.remove();
187 return barWidth;
188 },
189
190 setup : function( c, wo ) {
191 var tbHt, $hdr, $t, $hCells, $tableWrap, events, tmp, detectedWidth,
192 $win = $( window ),
193 tsScroller = ts.scroller,
194 namespace = c.namespace + 'tsscroller',
195 $foot = $(),
196 // c.namespace contains a unique tablesorter ID, per table
197 id = c.namespace.slice( 1 ) + 'tsscroller',
198 $table = c.$table;
199
200 // force config.widthFixed option - this helps maintain proper alignment across cloned tables
201 c.widthFixed = true;
202
203 wo.scroller_calcWidths = [];
204 wo.scroller_saved = [ 0, 0 ];
205 wo.scroller_isBusy = true;
206 wo.scroller_scrollTimer = null;
207
208 // set scrollbar width to one of the following (1) explicitly set scroller_barWidth option,
209 // (2) detected scrollbar width or (3) fallback of 15px
210 if ( wo.scroller_barWidth !== null ) {
211 wo.scroller_barSetWidth = wo.scroller_barWidth;
212 } else {
213 detectedWidth = tsScroller.getBarWidth();
214 wo.scroller_barSetWidth = detectedWidth !== null ? detectedWidth : 15;
215 }
216
217 tmp = $table.children( 'caption' );
218
219 $hdr = $( '<table class="' + $table.attr( 'class' ) + '" cellpadding=0 cellspacing=0>' +
220 ( tmp.length ? tmp[ 0 ].outerHTML : '' ) +
221 $table.children( 'thead' )[ 0 ].outerHTML + '</table>' );
222 wo.scroller_$header = $hdr.addClass( c.namespace.slice( 1 ) + '_extra_table' );
223
224 $t = $table.children( 'tfoot' );
225 if ( $t.length ) {
226 $foot = $( '<table class="' + $table.attr( 'class' ) +
227 '" cellpadding=0 cellspacing=0 style="margin-top:0"></table>' )
228 .addClass( c.namespace.slice( 1 ) + '_extra_table' )
229 // maintain any bindings on the tfoot cells
230 .append( $t.clone( true ) )
231 .wrap( '<div class="' + tscss.scrollerFooter + '"/>' );
232 }
233 wo.scroller_$footer = $foot;
234
235 $table
236 .wrap( '<div id="' + id + '" class="' + tscss.scrollerWrap + '" />' )
237 .before( $hdr )
238 // shrink filter row but don't completely hide it because the inputs/selectors may distort the columns
239 .find( '.' + tscss.filterRow )
240 .addClass( tscss.filterRowHide );
241
242 wo.scroller_$container = $table.parent();
243
244 if ( $foot.length ) {
245 // $foot.parent() to include <div> wrapper
246 $table.after( $foot.parent() );
247 }
248
249 $hCells = $hdr
250 .wrap( '<div class="' + tscss.scrollerHeader + '" />' )
251 .find( '.' + tscss.header );
252
253 // if max-height is greater than 0 use max-height, so the height resizes dynamically while filtering
254 // else let the table not have a vertical scroll
255 $table.wrap( '<div class="' + tscss.scrollerTable +
256 ( wo.scroller_height > 0 ? '" style="max-height:' + wo.scroller_height + 'px;">' : '">' ) );
257 $tableWrap = $table.parent();
258
259 // make scroller header sortable
260 ts.bindEvents( c.table, $hCells );
261
262 // look for filter widget
263 if ( $table.hasClass( 'hasFilters' ) ) {
264 ts.filter.bindSearch( $table, $hdr.find( '.' + tscss.filter ) );
265 }
266
267 $table
268 .children( 'thead, caption' )
269 .addClass( tscss.scrollerHideElement );
270 tbHt = $tableWrap.parent().height();
271
272 // The header will always jump into view if scrolling the table body
273 $tableWrap
274 .off( 'scroll' + namespace )
275 .on( 'scroll' + namespace, function() {
276 // Save position
277 clearTimeout(wo.scroller_scrollTimer);
278 wo.scroller_scrollTimer = setTimeout(function() {
279 wo.scroller_saved[0] = $tableWrap.scrollLeft();
280 wo.scroller_saved[1] = $tableWrap.scrollTop();
281 }, 300);
282 if ( wo.scroller_jumpToHeader ) {
283 var pos = $win.scrollTop() - $hdr.offset().top;
284 if ( $( this ).scrollTop() !== 0 && pos < tbHt && pos > 0 ) {
285 $win.scrollTop( $hdr.offset().top );
286 }
287 }
288 $hdr
289 .parent()
290 .add( $foot.parent() )
291 .scrollLeft( $( this ).scrollLeft() );
292 });
293
294 // resize/update events - filterEnd fires after "tablesorter-initialized" and "updateComplete"
295 tmp = ts.hasWidget( c.table, 'filter' ) ?
296 'filterEnd filterInit' :
297 'tablesorter-initialized updateComplete';
298 events = ( tmp + ' sortEnd pagerComplete columnUpdate ' ).split( ' ' ).join( namespace + ' ' );
299
300 $table
301 .off( namespace )
302 .on( 'sortStart' + namespace, function() {
303 clearTimeout(wo.scroller_scrollTimer);
304 wo.scroller_isBusy = true;
305 })
306 .on( 'sortEnd filterEnd'.split( ' ' ).join( namespace + ' ' ), function( event ) {
307 // Sorting, so scroll to top
308 if ( event.type === 'sortEnd' && wo.scroller_upAfterSort ) {
309 $tableWrap
310 .scrollLeft( wo.scroller_saved[0] )
311 .animate({ scrollTop : 0 }, 'fast', function() {
312 wo.scroller_isBusy = false;
313 });
314 } else if ( wo.scroller_fixedColumns ) {
315 setTimeout( function() {
316 // restore previous scroll position
317 $tableWrap
318 .scrollTop( wo.scroller_saved[1] )
319 .scrollLeft( wo.scroller_saved[0] );
320 tsScroller.updateFixed( c, wo );
321 }, 0 );
322 }
323 })
324 .on( 'setFixedColumnSize' + namespace, function( event, size ) {
325 var $wrap = wo.scroller_$container;
326 if ( typeof size !== 'undefined' && !isNaN( size ) ) {
327 wo.scroller_fixedColumns = parseInt( size, 10 );
328 }
329 // remove fixed columns
330 tsScroller.removeFixed( c, wo );
331 size = wo.scroller_fixedColumns;
332 if ( size > 0 && size < c.columns - 1 ) {
333 tsScroller.updateFixed( c, wo );
334 } else if ( $wrap.hasClass( tscss.scrollerHasFix ) ) {
335 $wrap.removeClass( tscss.scrollerHasFix );
336 // resize needed to make tables full width
337 tsScroller.resize( c, wo );
338 }
339 })
340 .on( events, function( event ) {
341 // Stop from running twice with pager
342 if ( ts.hasWidget( 'pager' ) && event.type === 'updateComplete' ) {
343 return;
344 }
345 if ( wo.scroller_fixedColumns > 0 ) {
346 tsScroller.updateFixed( c, wo );
347 }
348 // adjust column sizes after an update
349 tsScroller.resize( c, wo );
350 });
351
352 // Setup window.resizeEnd event
353 $win
354 .off( 'resize resizeEnd '.split( ' ' ).join( namespace + ' ' ) )
355 .on( 'resize' + namespace, ts.window_resize )
356 .on( 'resizeEnd' + namespace, function() {
357 // IE calls resize when you modify content, so we have to unbind the resize event
358 // so we don't end up with an infinite loop. we can rebind after we're done.
359 $win.off( 'resize' + namespace, ts.window_resize );
360 tsScroller.resize( c, wo );
361 $win.on( 'resize' + namespace, ts.window_resize );
362 $tableWrap.trigger( 'scroll' + namespace );
363 });
364
365 // initialization flag
366 c.isScrolling = true;
367
368 tsScroller.updateFixed( c, wo );
369
370 // updateAll called - need to give the browser time to adjust the layout
371 // before calculating fix column widths
372 if ( c.table.hasInitialized && c.isScrolling ) {
373 setTimeout(function() {
374 ts.scroller.resize( c, wo );
375 }, 50);
376 }
377
378 },
379
380 resize : function( c, wo ) {
381 if ( wo.scroller_isBusy ) { return; }
382 var index, borderWidth, setWidth, $headers, $this,
383 tsScroller = ts.scroller,
384 $container = wo.scroller_$container,
385 $table = c.$table,
386 $tableWrap = $table.parent(),
387 $hdr = wo.scroller_$header,
388 $foot = wo.scroller_$footer,
389 $win = $(window),
390 position = [ $win.scrollLeft(), $win.scrollTop() ],
391 id = c.namespace.slice( 1 ) + 'tsscroller',
392 // Hide other scrollers so we can resize
393 $div = $( 'div.' + tscss.scrollerWrap + '[id!="' + id + '"]' )
394 .addClass( tscss.scrollerHideElement ),
395 temp = 'padding:0;margin:0;border:0;height:0;max-height:0;min-height:0;',
396 row = '<tr class="' + tscss.scrollerSpacerRow + ' ' + c.selectorRemove.slice(1) +
397 '" style="' + temp + '">';
398
399 wo.scroller_calcWidths = [];
400
401 // Remove fixed so we get proper widths and heights
402 tsScroller.removeFixed( c, wo );
403 $container.find( '.' + tscss.scrollerSpacerRow ).remove();
404 // remove ts added colgroups
405 $container.find( '.' + ts.css.colgroup ).remove();
406
407 // show original table elements to get proper alignment
408 $table
409 .find( '.' + tscss.scrollerHideElement )
410 .removeClass( tscss.scrollerHideElement );
411
412 // include left & right border widths
413 borderWidth = parseInt( $table.css( 'border-left-width' ), 10 );
414
415 $headers = c.$headerIndexed;
416
417 for ( index = 0; index < c.columns; index++ ) {
418 $this = $headers[ index ];
419 // code from https://github.com/jmosbech/StickyTableHeaders
420 if ( $this.css( 'box-sizing' ) === 'border-box' ) {
421 setWidth = $this.outerWidth();
422 } else {
423 if ( $this.css( 'border-collapse' ) === 'collapse' ) {
424 if ( $this.length && window.getComputedStyle ) {
425 setWidth = parseFloat( window.getComputedStyle( $this[ 0 ], null ).width );
426 } else {
427 // ie8 only
428 setWidth = $this.outerWidth() - parseFloat( $this.css( 'padding-left' ) ) -
429 parseFloat( $this.css( 'padding-right' ) ) -
430 ( parseFloat( $this.css( 'border-width' ) ) || 0 );
431 }
432 } else {
433 setWidth = $this.width();
434 }
435 }
436 row += '<td data-column="' + index + '" style="' + temp + 'width:' + setWidth +
437 'px;min-width:' + setWidth + 'px;max-width:' + setWidth + 'px"></td>';
438
439 // save current widths
440 wo.scroller_calcWidths[ index ] = setWidth;
441 }
442 row += '</tr>';
443 c.$tbodies.eq(0).append( row ); // tbody
444 $hdr.children( 'thead' ).append( row );
445 $foot.children( 'tfoot' ).append( row );
446
447 // include colgroup or alignment is off
448 ts.fixColumnWidth( c.table );
449 row = c.$table.children( 'colgroup' )[0].outerHTML;
450 $hdr.append( row );
451 $foot.append( row );
452
453 temp = $tableWrap.parent().innerWidth() -
454 ( tsScroller.hasScrollBar( $tableWrap ) ? wo.scroller_barSetWidth : 0 );
455 $tableWrap.width( temp );
456
457 temp = ( tsScroller.hasScrollBar( $tableWrap ) ? wo.scroller_barSetWidth : 0 ) + borderWidth;
458 setWidth = $tableWrap.innerWidth() - temp;
459
460 $hdr
461 .parent()
462 .add( $foot.parent() )
463 .width( setWidth );
464
465 $tableWrap
466 .width( setWidth + temp );
467
468 // hide original table thead
469 $table.children( 'thead, caption' ).addClass( tscss.scrollerHideElement );
470
471 // update fixed column sizes
472 tsScroller.updateFixed( c, wo );
473
474 $div.removeClass( tscss.scrollerHideElement );
475
476 // restore scrollTop - fixes #926
477 $tableWrap.scrollTop( wo.scroller_saved[1] );
478 wo.scroller_$container
479 .find( '.' + tscss.scrollerFixed )
480 .find( '.' + tscss.scrollerTable )
481 .scrollTop( wo.scroller_saved[1] );
482 $win.scrollLeft( position[0] );
483 $win.scrollTop( position[1] );
484
485 // update resizable widget handles
486 setTimeout( function() {
487 c.$table.triggerHandler( 'resizableUpdate' );
488 c.$table.triggerHandler( 'scrollerComplete' );
489 }, 100 );
490
491 },
492
493 // Add fixed (frozen) columns (Do not call directly, use updateFixed)
494 setupFixed : function( c, wo ) {
495 var index, index2, $el, len, temp, $fixedColumn, $fixedTbody,
496 $table = c.$table,
497 $wrapper = wo.scroller_$container,
498 fixedColumns = wo.scroller_fixedColumns;
499
500 $fixedColumn = $wrapper
501 .addClass( tscss.scrollerHasFix )
502 .clone()
503 .addClass( tscss.scrollerFixed )
504 .removeClass( tscss.scrollerWrap )
505 .attr( 'id', '' );
506
507 $fixedColumn.find('caption').html('&nbsp;');
508
509 if ( wo.scroller_addFixedOverlay ) {
510 $fixedColumn.append( '<div class="' + tscss.scrollerFixedPanel + '">' );
511 }
512
513 $fixedTbody = $fixedColumn.find( '.' + tscss.scrollerTable );
514 $fixedTbody
515 .children( 'table' )
516 .addClass( c.namespace.slice( 1 ) + '_extra_table' )
517 .attr( 'id', '' )
518 .children( 'thead, tfoot' )
519 .remove();
520
521 wo.scroller_$fixedColumns = $fixedColumn;
522
523 // RTL support (fixes column on right)
524 if ( $table.hasClass( tscss.scrollerRtl ) ) {
525 $fixedColumn.addClass( tscss.scrollerRtl );
526 }
527
528 $el = $fixedColumn.find( 'tr' );
529 len = $el.length;
530 for ( index = 0; index < len; index++ ) {
531 $el.eq( index ).children( ':gt(' + ( fixedColumns - 1 ) + ')' ).remove();
532 }
533 $fixedColumn
534 .addClass( tscss.scrollerHideElement )
535 .prependTo( $wrapper );
536
537 // look for filter widget
538 if ( c.$table.hasClass( 'hasFilters' ) ) {
539 // make sure fixed column filters aren't disabled
540 $el = $fixedColumn
541 .find( '.' + tscss.filter )
542 .not( '.' + tscss.filterDisabled )
543 .prop( 'disabled', false );
544 ts.filter.bindSearch( $table, $fixedColumn.find( '.' + tscss.filter ) );
545 // disable/enable filters behind fixed column
546 $el = $wrapper
547 .children( '.' + tscss.scrollerHeader )
548 .find( '.' + tscss.filter );
549 len = $el.length;
550 for ( index = 0; index < len; index++ ) {
551 // previously disabled filter; don't mess with it! filterDisabled class added by filter widget
552 if ( !$el.eq( index ).hasClass( tscss.filterDisabled || 'disabled' ) ) {
553 // disable filters behind fixed column; don't disable visible filters
554 $el.eq( index ).prop( 'disabled', index < fixedColumns );
555 }
556 }
557 }
558
559 // disable/enable tab indexes behind fixed column
560 c.$table
561 .add( '.' + tscss.scrollerFooter + ' table' )
562 .children( 'thead' )
563 .children( 'tr.' + tscss.headerRow )
564 .children()
565 .attr( 'tabindex', -1 );
566
567 $el = wo.scroller_$header
568 .add( $fixedColumn.find( '.' + tscss.scrollerTable + ' table' ) )
569 .children( 'thead' )
570 .children( 'tr.' + tscss.headerRow );
571 len = $el.length;
572 for ( index = 0; index < len; index++ ) {
573 temp = $el.eq( index ).children();
574 for ( index2 = 0; index2 < temp.length; index2++ ) {
575 temp.eq( index2 ).attr( 'tabindex', index2 < fixedColumns ? -1 : 0 );
576 }
577 }
578
579 ts.bindEvents( c.table, $fixedColumn.find( '.' + tscss.header ) );
580 ts.scroller.bindFixedColumnEvents( c, wo );
581
582 /*** Scrollbar hack! Since we can't hide the scrollbar with css ***/
583 if ( ts.scroller.isFirefox || ts.scroller.isOldIE ) {
584 $fixedTbody.wrap( '<div class="' + tscss.scrollerHack + '" style="overflow:hidden;">' );
585 }
586
587 },
588
589 // https://remysharp.com/2010/07/21/throttling-function-calls
590 throttle : function(fn, threshhold, scope) {
591 threshhold = threshhold || 50;
592 var last, deferTimer;
593 return function() {
594 var context = scope || this,
595 now = +(new Date()),
596 args = arguments;
597 if (last && now < last + threshhold) {
598 // hold on to it
599 clearTimeout(deferTimer);
600 deferTimer = setTimeout(function() {
601 last = now;
602 fn.apply(context, args);
603 }, threshhold);
604 } else {
605 last = now;
606 fn.apply(context, args);
607 }
608 };
609 },
610
611 bindFixedColumnEvents : function( c, wo ) {
612 // update thead & tbody in fixed column
613 var tsScroller = ts.scroller,
614 namespace = c.namespace + 'tsscrollerFixed',
615 events = 'scroll' + namespace,
616 $fixedTbody = wo.scroller_$fixedColumns.find( '.' + tscss.scrollerTable ),
617 fixedScroll = true,
618 tableScroll = true;
619
620 c.$table
621 .parent()
622 // *** SCROLL *** scroll fixed column along with main
623 .off( events )
624 .on( events, tsScroller.throttle(function() {
625 // using flags to prevent firing the scroll event excessively leading to slow scrolling in Firefox
626 if ( !wo.scroller_isBusy && fixedScroll ) {
627 tableScroll = false;
628 var $this = $( this );
629 $fixedTbody[0].scrollTop = wo.scroller_saved[1] = $this.scrollTop();
630 wo.scroller_saved[0] = $this.scrollLeft();
631 setTimeout( function() {
632 tableScroll = true;
633 }, 20 );
634 }
635 }));
636 // scroll main along with fixed column
637 $fixedTbody
638 .off( events )
639 .on( events, tsScroller.throttle(function() {
640 // using flags to prevent firing the scroll event excessively leading to slow scrolling in Firefox
641 if ( !wo.scroller_isBusy && tableScroll ) {
642 fixedScroll = false;
643 c.$table.parent()[0].scrollTop = wo.scroller_saved[1] = $( this ).scrollTop();
644 setTimeout( function() {
645 fixedScroll = true;
646 }, 20 );
647 }
648 }))
649 .scroll();
650
651 // *** ROW HIGHLIGHT ***
652 if ( wo.scroller_rowHighlight !== '' ) {
653 events = 'mouseover mouseleave '.split( ' ' ).join( namespace + ' ' );
654 // can't use c.$tbodies because it doesn't include info-only tbodies
655 c.$table
656 .off( events, 'tbody > tr' )
657 .on( events, 'tbody > tr', function( event ) {
658 var indx = c.$table.children( 'tbody' ).children( 'tr' ).index( this );
659 $fixedTbody
660 .children( 'table' )
661 .children( 'tbody' )
662 .children( 'tr' )
663 .eq( indx )
664 .add( this )
665 .toggleClass( wo.scroller_rowHighlight, event.type === 'mouseover' );
666 });
667 $fixedTbody
668 .find( 'table' )
669 .off( events, 'tbody > tr' )
670 .on( events, 'tbody > tr', function( event ) {
671 var $fixed = $fixedTbody.children( 'table' ).children( 'tbody' ).children( 'tr' ),
672 indx = $fixed.index( this );
673 c.$table
674 .children( 'tbody' )
675 .children( 'tr' )
676 .eq( indx )
677 .add( this )
678 .toggleClass( wo.scroller_rowHighlight, event.type === 'mouseover' );
679 });
680 }
681 },
682
683 adjustWidth : function( c, wo, totalWidth, adj, dir ) {
684 var $wrapper = wo.scroller_$container;
685
686 // RTL support (fixes column on right)
687 $wrapper
688 .children( '.' + tscss.scrollerTable )
689 .css( dir ? 'right' : 'left', totalWidth );
690 $wrapper
691 .children( '.' + tscss.scrollerHeader + ', .' + tscss.scrollerFooter )
692 // Safari needs a scrollbar width of extra adjusment to align the fixed & scrolling columns
693 .css( dir ? 'right' : 'left', totalWidth + ( dir && ts.scroller.isSafari ? adj : 0 ) );
694 },
695
696 updateFixed : function( c, wo ) {
697 var temp, adj,
698 $wrapper = wo.scroller_$container,
699 $hdr = wo.scroller_$header,
700 $foot = wo.scroller_$footer,
701 $table = c.$table,
702 $tableWrap = $table.parent(),
703 scrollBarWidth = wo.scroller_barSetWidth,
704 dir = $table.hasClass( tscss.scrollerRtl );
705
706 if ( wo.scroller_fixedColumns === 0 ) {
707 wo.scroller_isBusy = false;
708 ts.scroller.removeFixed( c, wo );
709 temp = $wrapper.width();
710 $tableWrap.width( temp );
711 adj = ts.scroller.hasScrollBar( $tableWrap ) ? scrollBarWidth : 0;
712 $hdr
713 .parent()
714 .add( $foot.parent() )
715 .width( temp - adj );
716 return;
717 }
718
719 if ( !c.isScrolling ) {
720 return;
721 }
722
723 wo.scroller_isBusy = true;
724
725 // Make sure the wo.scroller_$fixedColumns container exists, if not build it
726 if ( !$wrapper.find( '.' + tscss.scrollerFixed ).length ) {
727 ts.scroller.setupFixed( c, wo );
728 }
729
730 // scroller_fixedColumns
731 var index, tbodyIndex, rowIndex, $tbody, $adjCol, $fb, totalRows,
732
733 // source cells for measurement
734 $mainTbodies = wo.scroller_$container
735 .children( '.' + tscss.scrollerTable )
736 .children( 'table' )
737 .children( 'tbody' ),
738 // variable gets redefined
739 $rows = wo.scroller_$header
740 .children( 'thead' )
741 .children( '.' + tscss.headerRow ),
742
743 // hide fixed column during resize, or we get a FOUC
744 $fixedColumn = wo.scroller_$fixedColumns
745 .addClass( tscss.scrollerHideElement ),
746
747 // target cells
748 $fixedTbodiesTable = $fixedColumn
749 .find( '.' + tscss.scrollerTable )
750 .children( 'table' ),
751 $fixedTbodies = $fixedTbodiesTable
752 .children( 'tbody' ),
753 // variables
754 tsScroller = ts.scroller,
755 fixedColumns = wo.scroller_fixedColumns,
756 // get dimensions
757 getDim = function ($el, name, deflt) {
758 return parseInt( $el.css(name) || '', 10 ) || deflt || 0;
759 },
760 $temp = $table.find( 'tbody td' ),
761 borderRightWidth = getDim( $temp, 'border-right-width', 1 ),
762 borderSpacing = getDim( $temp, 'border-spacing', 0 ),
763 totalWidth = getDim( $table, 'padding-left' ) +
764 getDim( $table, 'padding-right' ) +
765 // include table left & row left border
766 getDim( $table, 'border-left-width', 1 ) * 2 +
767 getDim( $table, 'border-right-width', 1 ) -
768 borderRightWidth + borderSpacing / 2,
769 widths = wo.scroller_calcWidths;
770
771 ts.scroller.removeFixed( c, wo, false );
772
773 // calculate fixed column width
774 for ( index = 0; index < fixedColumns; index++ ) {
775 totalWidth += widths[ index ] + borderSpacing;
776 }
777
778 // set fixed column width
779 totalWidth = totalWidth + borderRightWidth * 2;
780 tsScroller.setWidth( $fixedColumn.add( $fixedColumn.children() ), totalWidth );
781 tsScroller.setWidth( $fixedColumn.children().children( 'table' ), totalWidth );
782
783 // update fixed column tbody content, set cell widths on hidden row
784 for ( tbodyIndex = 0; tbodyIndex < c.$tbodies.length; tbodyIndex++ ) {
785 $tbody = $mainTbodies.eq( tbodyIndex );
786 if ( $tbody.length ) {
787 // get tbody
788 $rows = $tbody.children();
789 totalRows = $rows.length;
790 $fb = ts.processTbody( $fixedTbodiesTable, $fixedTbodies.eq( tbodyIndex ), true );
791 $fb.empty();
792 // update tbody cells after sort/filtering
793 for ( rowIndex = 0; rowIndex < totalRows; rowIndex++ ) {
794 $adjCol = $( $rows[ rowIndex ].outerHTML );
795 $adjCol
796 .children( 'td, th' )
797 .slice( fixedColumns )
798 .remove();
799 $fb.append( $adjCol );
800 }
801
802 // restore tbody
803 ts.processTbody( $fixedTbodiesTable, $fb, false );
804 }
805 }
806
807 adj = ts.scroller.hasScrollBar( $tableWrap ) ? scrollBarWidth : 0;
808
809 /*** scrollbar HACK! Since we can't hide the scrollbar with css ***/
810 if ( tsScroller.isFirefox || tsScroller.isOldIE ) {
811 $fixedTbodiesTable
812 .css( 'width', totalWidth )
813 .parent()
814 .css( 'width', totalWidth + adj );
815 }
816
817 for ( index = 0; index < fixedColumns; index++ ) {
818 temp = ':nth-child(' + ( index + 1 ) + ')';
819 $wrapper
820 .children( 'div' )
821 .children( 'table' )
822 .find( 'th' + temp + ', td' + temp + ', col' + temp )
823 .addClass( tscss.scrollerHideColumn );
824 }
825 $fixedColumn
826 .removeClass( tscss.scrollerHideElement )
827 .find('colgroup')
828 .each(function() {
829 $(this)
830 .find('col:gt(' + (fixedColumns - 1) + ')')
831 .addClass( tscss.scrollerHideElement );
832 });
833
834 totalWidth = totalWidth - borderRightWidth;
835 temp = $tableWrap.parent().innerWidth() - totalWidth;
836 $tableWrap.width( temp );
837 // RTL support (fixes column on right)
838 $wrapper
839 .children( '.' + tscss.scrollerTable )
840 .css( dir ? 'right' : 'left', totalWidth );
841 $wrapper
842 .children( '.' + tscss.scrollerHeader + ', .' + tscss.scrollerFooter )
843 // Safari needs a scrollbar width of extra adjusment to align the fixed & scrolling columns
844 .css( dir ? 'right' : 'left', totalWidth + ( dir && ts.scroller.isSafari ? adj : 0 ) );
845
846 $hdr
847 .parent()
848 .add( $foot.parent() )
849 .width( temp - adj );
850
851 // fix gap under the tbody for the horizontal scrollbar
852 temp = ts.scroller.hasScrollBar( $tableWrap, true );
853 adj = temp ? scrollBarWidth : 0;
854 if ( !$fixedColumn.find( '.' + tscss.scrollerBarSpacer ).length && temp ) {
855 $temp = $( '<div class="' + tscss.scrollerBarSpacer + '">' )
856 .css( 'height', adj + 'px' );
857 $fixedColumn.find( '.' + tscss.scrollerTable ).append( $temp );
858 } else if ( !temp ) {
859 $fixedColumn.find( '.' + tscss.scrollerBarSpacer ).remove();
860 }
861
862 ts.scroller.updateRowHeight( c, wo );
863 // set fixed column height (changes with filtering)
864 $fixedColumn.height( $wrapper.height() );
865
866 $fixedColumn.removeClass( tscss.scrollerHideElement );
867
868 // adjust caption height, see #1202
869 $fixedColumn.find('caption').height( wo.scroller_$header.find( 'caption' ).height() );
870
871 $tableWrap.scroll();
872 setTimeout(function() {
873 wo.scroller_isBusy = false;
874 }, 0);
875
876 },
877
878 fixHeight : function( $rows, $fixedRows ) {
879 var index, heightRow, heightFixed, $r, $f,
880 addedHt = tscss.scrollerAddedHeight,
881 len = $rows.length;
882 for ( index = 0; index < len; index++ ) {
883 $r = $rows.eq( index );
884 $f = $fixedRows.eq( index );
885 heightRow = $r.height();
886 heightFixed = $f.height();
887 if ( heightRow > heightFixed ) {
888 $f.addClass( addedHt ).height( heightRow );
889 } else if ( heightRow < heightFixed ) {
890 $r.addClass( addedHt ).height( heightFixed );
891 }
892 }
893 },
894
895 updateRowHeight : function( c, wo ) {
896 var $rows, $fixed,
897 $fixedColumns = wo.scroller_$fixedColumns;
898
899 wo.scroller_$container
900 .find( '.' + tscss.scrollerAddedHeight )
901 .removeClass( tscss.scrollerAddedHeight )
902 .height( '' );
903
904 $rows = wo.scroller_$header
905 .children( 'thead' )
906 .children( 'tr' );
907 $fixed = $fixedColumns
908 .children( '.' + tscss.scrollerHeader )
909 .children( 'table' )
910 .children( 'thead' )
911 .children( 'tr' );
912 ts.scroller.fixHeight( $rows, $fixed );
913
914 $rows = wo.scroller_$footer
915 .children( 'tfoot' )
916 .children( 'tr' );
917 $fixed = $fixedColumns
918 .children( '.' + tscss.scrollerFooter )
919 .children( 'table' )
920 .children( 'tfoot' )
921 .children( 'tr' );
922 ts.scroller.fixHeight( $rows, $fixed );
923
924 if ( ts.scroller.isFirefox || ts.scroller.isOldIE ) {
925 // Firefox/Old IE scrollbar hack (wraps table to hide the scrollbar)
926 $fixedColumns = $fixedColumns.find( '.' + tscss.scrollerHack );
927 }
928 $rows = c.$table
929 .children( 'tbody' )
930 .children( 'tr' );
931 $fixed = $fixedColumns
932 .children( '.' + tscss.scrollerTable )
933 .children( 'table' )
934 .children( 'tbody' )
935 .children( 'tr' );
936 ts.scroller.fixHeight( $rows, $fixed );
937
938 },
939
940 removeFixed : function( c, wo, removeIt ) {
941 var $table = c.$table,
942 $wrapper = wo.scroller_$container,
943 dir = $table.hasClass( tscss.scrollerRtl );
944
945 // remove fixed columns
946 if ( removeIt || typeof removeIt === 'undefined' ) {
947 $wrapper.find( '.' + tscss.scrollerFixed ).remove();
948 }
949
950 $wrapper
951 .find( '.' + tscss.scrollerHideColumn )
952 .removeClass( tscss.scrollerHideColumn );
953
954 // RTL support ( fixes column on right )
955 $wrapper
956 .children( ':not(.' + tscss.scrollerFixed + ')' )
957 .css( dir ? 'right' : 'left', 0 );
958 },
959
960 remove : function( c, wo ) {
961 var $wrap = wo.scroller_$container,
962 namespace = c.namespace + 'tsscroller';
963 c.$table.off( namespace );
964 $( window ).off( namespace );
965 if ( $wrap ) {
966 c.$table
967 .insertBefore( $wrap )
968 .find( 'thead' )
969 .removeClass( tscss.scrollerHideElement )
970 .children( 'tr.' + tscss.headerRow )
971 .children()
972 .attr( 'tabindex', 0 )
973 .end()
974 .find( '.' + tscss.filterRow )
975 .removeClass( tscss.scrollerHideElement + ' ' + tscss.filterRowHide );
976 c.$table
977 .find( '.' + tscss.filter )
978 .not( '.' + tscss.filterDisabled )
979 .prop( 'disabled', false );
980 $wrap.remove();
981 c.isScrolling = false;
982 }
983 }
984
985 };
986
987 })( jQuery, window );