Debian Codegraph @

js/widgets/widget-cssStickyHeaders.js

raw symbols SCIP
1 /*! Widget: cssStickyHeaders - updated 6/16/2018 (v2.30.6) *//*
2 * Requires a modern browser, tablesorter v2.8+
3 */
4 /*jshint jquery:true, unused:false */
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 // target all versions of IE
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 // Fixes for Safari
24 tableH = $table.height(),
25 lastCaptionSetting = wo.cssStickyHeaders_addCaption,
26 // table offset top changes while scrolling in FF
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 // Firefox fixes
39 if ($caption.length) {
40 // Firefox does not include the caption height when getting the table height
41 // see https://bugzilla.mozilla.org/show_bug.cgi?id=820891, so lets detect it instead of browser sniff
42 $caption.hide();
43 addCaptionHeight = $table.height() === tableH;
44 $caption.show();
45
46 // Firefox changes the offset().top when translating the table caption
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 // make sure "wo" is current otherwise changes to widgetOptions
57 // are not dynamic (like the add caption button in the demo)
58 wo = c.widgetOptions;
59
60 if ( adjustOffsetTop ) {
61 // remove transform from caption to get the correct offset().top value
62 setTransform( $caption, 0 );
63 adjustY = $table.offset().top;
64 }
65
66 // Fix for safari, when caption present, table
67 // height changes while scrolling
68 if ($win.scrollTop() < $caption.outerHeight(true)) {
69 tableH = $table.height();
70 }
71
72 var top = $attach.length ? $attach.offset().top : $win.scrollTop(),
73 // add caption height; include table padding top & border-spacing or text may be above the fold (jQuery UI themes)
74 // border-spacing needed in Firefox, but not webkit... not sure if I should account for that
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 // get bottom of nested sticky headers
86 nestedStickyBottom = $parentThead.length ? (
87 isIE ? $parent.data('cssStickyHeaderBottom') + parentTheadHeight :
88 $parentThead.offset().top + parentTheadHeight - $win.scrollTop()
89 ) : 0,
90
91 // in this case FF's offsetTop changes while scrolling, so we get a saved offsetTop before scrolling occurs
92 // but when the table is inside a wrapper ($attach) we need to continually update the offset top
93 tableOffsetTop = adjustOffsetTop ? adjustY : $table.offset().top,
94
95 offsetTop = addCaptionHeight ? tableOffsetTop - ( wo.cssStickyHeaders_addCaption ? captionHeight : 0 ) : tableOffsetTop,
96
97 // Detect nested tables - fixes #724
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 // All IE (even IE11) can only transform header cells - fixes #447 thanks to @gakreol!
104 $cells = isIE ? $thead.children().children() : $thead;
105
106 // more crazy IE stuff...
107 if (isIE) {
108 // I didn't bother testing 3 nested tables deep in IE, because I hate it
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 // reset caption position if addCaption option is dynamically changed to false
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 // scroll top of table into view
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 // jQuery selector or object to attach sticky header to
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);