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
|
(function ($, Drupal, displace) {
"use strict";
/**
* Attaches sticky table headers.
*/
Drupal.behaviors.tableHeader = {
attach: function (context) {
$(window).one('scroll.TableHeaderInit', {context: context}, tableHeaderInitHandler);
}
};
function scrollValue(position) {
return document.documentElement[position] || document.body[position];
}
// Select and initialize sticky table headers.
function tableHeaderInitHandler(e) {
var $tables = $(e.data.context).find('table.sticky-enabled').once('tableheader');
var il = $tables.length;
for (var i = 0; i < il; i++) {
TableHeader.tables.push(new TableHeader($tables[i]));
}
forTables('onScroll');
}
// Helper method to loop through tables and execute a method.
function forTables(method, arg) {
var tables = TableHeader.tables;
var il = tables.length;
for (var i = 0; i < il; i++) {
tables[i][method](arg);
}
}
function tableHeaderResizeHandler(e) {
forTables('recalculateSticky');
}
function tableHeaderOnScrollHandler(e) {
forTables('onScroll');
}
function tableHeaderOffsetChangeHandler(e, offsets) {
forTables('stickyPosition', offsets.top);
}
// Bind event that need to change all tables.
$(window).on({
/**
* When resizing table width can change, recalculate everything.
*/
'resize.TableHeader': tableHeaderResizeHandler,
/**
* Bind only one event to take care of calling all scroll callbacks.
*/
'scroll.TableHeader': tableHeaderOnScrollHandler
});
// Bind to custom Drupal events.
$(document).on({
/**
* Recalculate columns width when window is resized and when show/hide
* weight is triggered.
*/
'columnschange.TableHeader': tableHeaderResizeHandler,
/**
* Recalculate TableHeader.topOffset when viewport is resized
*/
'drupalViewportOffsetChange.TableHeader': tableHeaderOffsetChangeHandler
});
/**
* Constructor for the tableHeader object. Provides sticky table headers.
*
* TableHeader will make the current table header stick to the top of the page
* if the table is very long.
*
* @param table
* DOM object for the table to add a sticky header to.
*
* @constructor
*/
function TableHeader(table) {
var $table = $(table);
this.$originalTable = $table;
this.$originalHeader = $table.children('thead');
this.$originalHeaderCells = this.$originalHeader.find('> tr > th');
this.displayWeight = null;
this.$originalTable.addClass('sticky-table');
this.tableHeight = $table[0].clientHeight;
this.tableOffset = this.$originalTable.offset();
// React to columns change to avoid making checks in the scroll callback.
this.$originalTable.on('columnschange', {tableHeader: this}, function (e, display) {
var tableHeader = e.data.tableHeader;
if (tableHeader.displayWeight === null || tableHeader.displayWeight !== display) {
tableHeader.recalculateSticky();
}
tableHeader.displayWeight = display;
});
// Create and display sticky header.
this.createSticky();
}
/**
* Store the state of TableHeader.
*/
$.extend(TableHeader, {
/**
* This will store the state of all processed tables.
*
* @type {Array}
*/
tables: []
});
/**
* Extend TableHeader prototype.
*/
$.extend(TableHeader.prototype, {
/**
* Minimum height in pixels for the table to have a sticky header.
*/
minHeight: 100,
/**
* Absolute position of the table on the page.
*/
tableOffset: null,
/**
* Absolute position of the table on the page.
*/
tableHeight: null,
/**
* Boolean storing the sticky header visibility state.
*/
stickyVisible: false,
/**
* Create the duplicate header.
*/
createSticky: function () {
// Clone the table header so it inherits original jQuery properties.
var $stickyHeader = this.$originalHeader.clone(true);
// Hide the table to avoid a flash of the header clone upon page load.
this.$stickyTable = $('<table class="sticky-header"/>')
.css({
visibility: 'hidden',
position: 'fixed',
top: '0px'
})
.append($stickyHeader)
.insertBefore(this.$originalTable);
this.$stickyHeaderCells = $stickyHeader.find('> tr > th');
// Initialize all computations.
this.recalculateSticky();
},
/**
* Set absolute position of sticky.
*
* @param offsetTop
* @param offsetLeft
*/
stickyPosition: function (offsetTop, offsetLeft) {
var css = {};
if (typeof offsetTop === 'number') {
css.top = offsetTop + 'px';
}
if (typeof offsetLeft === 'number') {
css.left = (this.tableOffset.left - offsetLeft) + 'px';
}
return this.$stickyTable.css(css);
},
/**
* Returns true if sticky is currently visible.
*/
checkStickyVisible: function () {
var scrollTop = scrollValue('scrollTop');
var tableTop = this.tableOffset.top - displace.offsets.top;
var tableBottom = tableTop + this.tableHeight;
var visible = false;
if (tableTop < scrollTop && scrollTop < (tableBottom - this.minHeight)) {
visible = true;
}
this.stickyVisible = visible;
return visible;
},
/**
* Check if sticky header should be displayed.
*
* This function is throttled to once every 250ms to avoid unnecessary calls.
*
* @param event
*/
onScroll: function (e) {
this.checkStickyVisible();
// Track horizontal positioning relative to the viewport.
this.stickyPosition(null, scrollValue('scrollLeft'));
this.$stickyTable.css('visibility', this.stickyVisible ? 'visible' : 'hidden');
},
/**
* Event handler: recalculates position of the sticky table header.
*
* @param event
* Event being triggered.
*/
recalculateSticky: function (event) {
// Update table size.
this.tableHeight = this.$originalTable[0].clientHeight;
// Update offset top.
displace.offsets.top = displace.calculateOffset('top');
this.tableOffset = this.$originalTable.offset();
this.stickyPosition(displace.offsets.top, scrollValue('scrollLeft'));
// Update columns width.
var $that = null;
var $stickyCell = null;
var display = null;
// Resize header and its cell widths.
// Only apply width to visible table cells. This prevents the header from
// displaying incorrectly when the sticky header is no longer visible.
var il = this.$originalHeaderCells.length;
for (var i = 0; i < il; i++) {
$that = $(this.$originalHeaderCells[i]);
$stickyCell = this.$stickyHeaderCells.eq($that.index());
display = $that.css('display');
if (display !== 'none') {
$stickyCell.css({'width': $that.css('width'), 'display': display});
}
else {
$stickyCell.css('display', 'none');
}
}
this.$stickyTable.css('width', this.$originalTable.outerWidth());
}
});
// Expose constructor in the public space.
Drupal.TableHeader = TableHeader;
}(jQuery, Drupal, window.parent.Drupal.displace));
|