| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
;(function($, window) { |
| 6 |
'use strict'; |
| 7 |
|
| 8 |
var ts = $.tablesorter; |
| 9 |
|
| 10 |
function cssStickyHeadersInit(c, wo) { |
| 11 |
var offst, adjustY, |
| 12 |
$table = c.$table, |
| 13 |
$attach = $(wo.cssStickyHeaders_attachTo), |
| 14 |
|
| 15 |
isIE = 'ActiveXObject' in window || window.navigator.userAgent.indexOf('Edge') > -1, |
| 16 |
namespace = c.namespace + 'cssstickyheader ', |
| 17 |
$thead = $table.children('thead'), |
| 18 |
$caption = $table.children('caption'), |
| 19 |
$win = $attach.length ? $attach : $(window), |
| 20 |
$parent = $table.parent().closest('table.' + ts.css.table), |
| 21 |
$parentThead = $parent.length && ts.hasWidget($parent[0], 'cssStickyHeaders') ? $parent.children('thead') : [], |
| 22 |
borderTopWidth = ( parseInt( $table.css('border-top-width'), 10 ) || 0 ), |
| 23 |
|
| 24 |
tableH = $table.height(), |
| 25 |
lastCaptionSetting = wo.cssStickyHeaders_addCaption, |
| 26 |
|
| 27 |
adjustOffsetTop = false, |
| 28 |
addCaptionHeight = false, |
| 29 |
setTransform = function( $elms, y ) { |
| 30 |
var translate = y === 0 ? '' : 'translate(0px,' + y + 'px)'; |
| 31 |
$elms.css({ |
| 32 |
'transform' : translate, |
| 33 |
'-ms-transform' : translate, |
| 34 |
'-webkit-transform' : translate |
| 35 |
}); |
| 36 |
}; |
| 37 |
|
| 38 |
|
| 39 |
if ($caption.length) { |
| 40 |
|
| 41 |
|
| 42 |
$caption.hide(); |
| 43 |
addCaptionHeight = $table.height() === tableH; |
| 44 |
$caption.show(); |
| 45 |
|
| 46 |
|
| 47 |
offst = $table.offset().top; |
| 48 |
setTransform( $caption, 20 ); |
| 49 |
adjustOffsetTop = $table.offset().top !== offst; |
| 50 |
setTransform( $caption, 0 ); |
| 51 |
} |
| 52 |
|
| 53 |
$win |
| 54 |
.unbind( ('scroll resize '.split(' ').join(namespace)).replace(/\s+/g, ' ') ) |
| 55 |
.bind('scroll resize '.split(' ').join(namespace), function() { |
| 56 |
|
| 57 |
|
| 58 |
wo = c.widgetOptions; |
| 59 |
|
| 60 |
if ( adjustOffsetTop ) { |
| 61 |
|
| 62 |
setTransform( $caption, 0 ); |
| 63 |
adjustY = $table.offset().top; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
if ($win.scrollTop() < $caption.outerHeight(true)) { |
| 69 |
tableH = $table.height(); |
| 70 |
} |
| 71 |
|
| 72 |
var top = $attach.length ? $attach.offset().top : $win.scrollTop(), |
| 73 |
|
| 74 |
|
| 75 |
captionHeight = ( $caption.outerHeight(true) || 0 ) + |
| 76 |
( parseInt( $table.css('padding-top'), 10 ) || 0 ) + |
| 77 |
( parseInt( $table.css('border-spacing'), 10 ) || 0 ), |
| 78 |
|
| 79 |
bottom = tableH + ( addCaptionHeight && wo.cssStickyHeaders_addCaption ? captionHeight : 0 ) - |
| 80 |
$thead.height() - ( $table.children('tfoot').height() || 0 ) - |
| 81 |
( wo.cssStickyHeaders_addCaption ? captionHeight : ( addCaptionHeight ? 0 : captionHeight ) ), |
| 82 |
|
| 83 |
parentTheadHeight = $parentThead.length ? $parentThead.height() : 0, |
| 84 |
|
| 85 |
|
| 86 |
nestedStickyBottom = $parentThead.length ? ( |
| 87 |
isIE ? $parent.data('cssStickyHeaderBottom') + parentTheadHeight : |
| 88 |
$parentThead.offset().top + parentTheadHeight - $win.scrollTop() |
| 89 |
) : 0, |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
tableOffsetTop = adjustOffsetTop ? adjustY : $table.offset().top, |
| 94 |
|
| 95 |
offsetTop = addCaptionHeight ? tableOffsetTop - ( wo.cssStickyHeaders_addCaption ? captionHeight : 0 ) : tableOffsetTop, |
| 96 |
|
| 97 |
|
| 98 |
deltaY = top - offsetTop + nestedStickyBottom + borderTopWidth + ( wo.cssStickyHeaders_offset || 0 ) - |
| 99 |
( wo.cssStickyHeaders_addCaption ? ( addCaptionHeight ? captionHeight : 0 ) : captionHeight ), |
| 100 |
|
| 101 |
finalY = deltaY > 0 && deltaY <= bottom ? deltaY : 0, |
| 102 |
|
| 103 |
|
| 104 |
$cells = isIE ? $thead.children().children() : $thead; |
| 105 |
|
| 106 |
|
| 107 |
if (isIE) { |
| 108 |
|
| 109 |
c.$table.data( 'cssStickyHeaderBottom', ( $parentThead.length ? parentTheadHeight : 0 ) - |
| 110 |
( wo.cssStickyHeaders_addCaption ? captionHeight : 0 ) ); |
| 111 |
} |
| 112 |
|
| 113 |
if (wo.cssStickyHeaders_addCaption) { |
| 114 |
$cells = $cells.add($caption); |
| 115 |
} |
| 116 |
if (lastCaptionSetting !== wo.cssStickyHeaders_addCaption) { |
| 117 |
lastCaptionSetting = wo.cssStickyHeaders_addCaption; |
| 118 |
|
| 119 |
if (!lastCaptionSetting) { |
| 120 |
setTransform( $caption, 0 ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
setTransform( $cells, finalY ); |
| 125 |
|
| 126 |
}); |
| 127 |
$table |
| 128 |
.unbind( ('filterEnd updateComplete '.split(' ').join(namespace)).replace(/\s+/g, ' ') ) |
| 129 |
.bind('filterEnd' + namespace, function() { |
| 130 |
if (wo.cssStickyHeaders_filteredToTop) { |
| 131 |
|
| 132 |
window.scrollTo(0, $table.position().top); |
| 133 |
} |
| 134 |
}) |
| 135 |
.bind('updateComplete' + namespace, function() { |
| 136 |
cssStickyHeadersInit(c, c.widgetOptions); |
| 137 |
}); |
| 138 |
} |
| 139 |
|
| 140 |
ts.addWidget({ |
| 141 |
id: 'cssStickyHeaders', |
| 142 |
priority: 10, |
| 143 |
options: { |
| 144 |
cssStickyHeaders_offset : 0, |
| 145 |
cssStickyHeaders_addCaption : false, |
| 146 |
|
| 147 |
cssStickyHeaders_attachTo : null, |
| 148 |
cssStickyHeaders_filteredToTop : true |
| 149 |
}, |
| 150 |
init : function(table, thisWidget, c, wo) { |
| 151 |
cssStickyHeadersInit(c, wo); |
| 152 |
}, |
| 153 |
remove: function(table, c, wo, refreshing) { |
| 154 |
if (refreshing) { return; } |
| 155 |
var namespace = c.namespace + 'cssstickyheader '; |
| 156 |
$(window).unbind( ('scroll resize '.split(' ').join(namespace)).replace(/\s+/g, ' ') ); |
| 157 |
c.$table |
| 158 |
.unbind( ('filterEnd scroll resize updateComplete '.split(' ').join(namespace)).replace(/\s+/g, ' ') ) |
| 159 |
.add( c.$table.children('thead').children().children() ) |
| 160 |
.children('thead, caption').css({ |
| 161 |
'transform' : '', |
| 162 |
'-ms-transform' : '', |
| 163 |
'-webkit-transform' : '' |
| 164 |
}); |
| 165 |
} |
| 166 |
}); |
| 167 |
|
| 168 |
})(jQuery, window); |