2014-10-27 00:11:12 +00:00
|
|
|
/*! tablesorter CSS Sticky Headers widget - updated 10/26/2014 (v2.18.0)
|
2013-12-08 20:02:02 +00:00
|
|
|
* Requires a modern browser, tablesorter v2.8+
|
2013-11-25 12:59:30 +00:00
|
|
|
*/
|
2014-04-21 00:39:59 +00:00
|
|
|
/*jshint jquery:true, unused:false */
|
2013-11-25 12:59:30 +00:00
|
|
|
;(function($){
|
2014-10-09 21:04:28 +00:00
|
|
|
'use strict';
|
2013-11-25 12:59:30 +00:00
|
|
|
|
2014-10-09 21:04:28 +00:00
|
|
|
var ts = $.tablesorter;
|
|
|
|
|
|
|
|
ts.addWidget({
|
|
|
|
id: 'cssStickyHeaders',
|
2013-11-25 12:59:30 +00:00
|
|
|
priority: 10,
|
|
|
|
options: {
|
2014-05-05 18:25:37 +00:00
|
|
|
cssStickyHeaders_offset : 0,
|
|
|
|
cssStickyHeaders_addCaption : false,
|
2014-10-09 21:04:28 +00:00
|
|
|
// jQuery selector or object to attach sticky header to
|
2014-05-05 18:25:37 +00:00
|
|
|
cssStickyHeaders_attachTo : null,
|
2014-10-09 21:04:28 +00:00
|
|
|
cssStickyHeaders_filteredToTop : true
|
2013-11-25 12:59:30 +00:00
|
|
|
},
|
|
|
|
init : function(table, thisWidget, c, wo) {
|
2014-10-09 21:04:28 +00:00
|
|
|
var isIE = 'ActiveXObject' in window, // target all versions of IE
|
2014-10-30 15:38:30 +00:00
|
|
|
isFF = navigator.userAgent.toLowerCase().indexOf('firefox') > -1,
|
2014-10-09 21:04:28 +00:00
|
|
|
$table = c.$table,
|
|
|
|
$attach = $(wo.cssStickyHeaders_attachTo),
|
|
|
|
namespace = c.namespace + 'cssstickyheader ',
|
|
|
|
$thead = $table.children('thead'),
|
|
|
|
$caption = $table.children('caption'),
|
|
|
|
$win = $attach.length ? $attach : $(window),
|
|
|
|
$parent = $table.parent().closest('table.' + ts.css.table),
|
2014-10-30 15:38:30 +00:00
|
|
|
$parentThead = $parent.length && ts.hasWidget($parent[0], 'cssStickyHeaders') ? $parent.children('thead') : [],
|
|
|
|
lastCaptionSetting = wo.cssStickyHeaders_addCaption;
|
2014-10-09 21:04:28 +00:00
|
|
|
|
|
|
|
$win
|
|
|
|
.unbind('scroll resize '.split(' ').join(namespace))
|
|
|
|
.bind('scroll resize '.split(' ').join(namespace), function() {
|
2014-10-30 15:38:30 +00:00
|
|
|
// make sure "wo" is current otherwise changes to widgetOptions
|
|
|
|
// are not dynamic (like the add caption button in the demo)
|
|
|
|
wo = c.widgetOptions;
|
2013-12-08 20:02:02 +00:00
|
|
|
var top = $attach.length ? $attach.offset().top : $win.scrollTop(),
|
|
|
|
// add caption height; include table padding top & border-spacing or text may be above the fold (jQuery UI themes)
|
|
|
|
// border-spacing needed in Firefox, but not webkit... not sure if I should account for that
|
2014-10-30 15:38:30 +00:00
|
|
|
captionHeight = ( $caption.outerHeight(true) || 0 ) +
|
|
|
|
( parseInt( $table.css('padding-top'), 10 ) || 0 ) +
|
|
|
|
( parseInt( $table.css('border-spacing'), 10 ) || 0 ),
|
|
|
|
|
|
|
|
bottom = $table.height() - $thead.height() - ( $table.children('tfoot').height() || 0 ) - ( wo.cssStickyHeaders_addCaption ? captionHeight : 0 ),
|
|
|
|
|
|
|
|
parentTheadHeight = $parentThead.length ? $parentThead.height() : 0,
|
2014-10-09 21:04:28 +00:00
|
|
|
|
|
|
|
// get bottom of nested sticky headers
|
2014-10-30 15:38:30 +00:00
|
|
|
nestedStickyBottom = $parentThead.length ? (
|
|
|
|
isIE ? $parent.data('cssStickyHeaderBottom') + parentTheadHeight :
|
|
|
|
$parentThead.offset().top + parentTheadHeight - $win.scrollTop()
|
|
|
|
) : 0,
|
2014-10-09 21:04:28 +00:00
|
|
|
|
|
|
|
// Detect nested tables - fixes #724
|
2014-10-30 15:38:30 +00:00
|
|
|
deltaY = top - $table.offset().top + nestedStickyBottom +
|
|
|
|
( parseInt( $table.css('border-top-width'), 10 ) || 0 ) +
|
|
|
|
( wo.cssStickyHeaders_offset || 0 ) +
|
2014-10-09 21:04:28 +00:00
|
|
|
// Again, I dislike browser sniffing... but I have no idea why I need to include a captionHeight
|
|
|
|
// for Firefox here and not for Chrome. Even IE behaves, sorta!
|
2014-10-30 15:38:30 +00:00
|
|
|
( wo.cssStickyHeaders_addCaption ? ( isFF ? captionHeight : 0 ) : -captionHeight ),
|
2014-10-09 21:04:28 +00:00
|
|
|
|
|
|
|
finalY = deltaY > 0 && deltaY <= bottom ? deltaY : 0,
|
|
|
|
|
|
|
|
// All IE (even IE11) can only transform header cells - fixes #447 thanks to @gakreol!
|
|
|
|
$cells = isIE ? $thead.children().children() : $thead;
|
|
|
|
|
2014-10-30 15:38:30 +00:00
|
|
|
// more crazy IE stuff...
|
2014-10-09 21:04:28 +00:00
|
|
|
if (isIE) {
|
2014-10-30 15:38:30 +00:00
|
|
|
// I didn't bother testing 3 nested tables deep in IE, because I hate it
|
|
|
|
c.$table.data( 'cssStickyHeaderBottom', ( $parentThead.length ? parentTheadHeight : 0 ) -
|
|
|
|
( wo.cssStickyHeaders_addCaption ? captionHeight : 0 ) );
|
2014-10-09 21:04:28 +00:00
|
|
|
}
|
|
|
|
|
2013-12-08 20:02:02 +00:00
|
|
|
if (wo.cssStickyHeaders_addCaption) {
|
|
|
|
$cells = $cells.add($caption);
|
|
|
|
}
|
2014-10-30 15:38:30 +00:00
|
|
|
if (lastCaptionSetting !== wo.cssStickyHeaders_addCaption) {
|
|
|
|
lastCaptionSetting = wo.cssStickyHeaders_addCaption;
|
|
|
|
// reset caption position if addCaption option is dynamically changed to false
|
|
|
|
if (!lastCaptionSetting) {
|
|
|
|
$caption.css({
|
|
|
|
'transform' : '',
|
|
|
|
'-ms-transform' : '',
|
|
|
|
'-webkit-transform' : ''
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2014-10-09 21:04:28 +00:00
|
|
|
|
2013-12-08 20:02:02 +00:00
|
|
|
$cells.css({
|
2014-10-09 21:04:28 +00:00
|
|
|
'transform' : finalY === 0 ? '' : 'translate(0px,' + finalY + 'px)',
|
|
|
|
'-ms-transform' : finalY === 0 ? '' : 'translate(0px,' + finalY + 'px)',
|
|
|
|
'-webkit-transform' : finalY === 0 ? '' : 'translate(0px,' + finalY + 'px)'
|
2013-11-25 12:59:30 +00:00
|
|
|
});
|
2013-12-08 20:02:02 +00:00
|
|
|
});
|
2014-10-09 21:04:28 +00:00
|
|
|
$table.unbind('filterEnd' + namespace).bind('filterEnd' + namespace, function() {
|
2014-05-05 18:25:37 +00:00
|
|
|
if (wo.cssStickyHeaders_filteredToTop) {
|
|
|
|
// scroll top of table into view
|
2014-10-09 21:04:28 +00:00
|
|
|
window.scrollTo(0, $table.position().top);
|
2014-05-05 18:25:37 +00:00
|
|
|
}
|
2014-02-02 07:28:03 +00:00
|
|
|
});
|
|
|
|
|
2013-11-25 12:59:30 +00:00
|
|
|
},
|
|
|
|
remove: function(table, c, wo){
|
2014-10-09 21:04:28 +00:00
|
|
|
var namespace = c.namespace + 'cssstickyheader ';
|
|
|
|
$(window).unbind('scroll resize '.split(' ').join(namespace));
|
2013-11-25 12:59:30 +00:00
|
|
|
c.$table
|
2014-10-09 21:04:28 +00:00
|
|
|
.unbind('filterEnd scroll resize '.split(' ').join(namespace))
|
|
|
|
.add( c.$table.children('thead').children().children() )
|
2013-12-08 20:02:02 +00:00
|
|
|
.children('thead, caption').css({
|
2014-10-09 21:04:28 +00:00
|
|
|
'transform' : '',
|
|
|
|
'-ms-transform' : '',
|
|
|
|
'-webkit-transform' : ''
|
2013-12-08 20:02:02 +00:00
|
|
|
});
|
2013-11-25 12:59:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
})(jQuery);
|