1
|
(function ($) {
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
Drupal.behaviors.tableHeader = {
|
7
|
attach: function (context, settings) {
|
8
|
if (!$.support.positionFixed) {
|
9
|
return;
|
10
|
}
|
11
|
|
12
|
$('table.sticky-enabled', context).once('tableheader', function () {
|
13
|
$(this).data("drupal-tableheader", new Drupal.tableHeader(this));
|
14
|
});
|
15
|
}
|
16
|
};
|
17
|
|
18
|
|
19
|
|
20
|
|
21
|
|
22
|
|
23
|
|
24
|
Drupal.tableHeader = function (table) {
|
25
|
var self = this;
|
26
|
|
27
|
this.originalTable = $(table);
|
28
|
this.originalHeader = $(table).children('thead');
|
29
|
this.originalHeaderCells = this.originalHeader.find('> tr > th');
|
30
|
this.displayWeight = null;
|
31
|
|
32
|
|
33
|
this.originalTable.bind('columnschange', function (e, display) {
|
34
|
|
35
|
self.widthCalculated = (self.displayWeight !== null && self.displayWeight === display);
|
36
|
self.displayWeight = display;
|
37
|
});
|
38
|
|
39
|
|
40
|
|
41
|
this.stickyTable = $('<table class="sticky-header"/>')
|
42
|
.insertBefore(this.originalTable)
|
43
|
.css({ position: 'fixed', top: '0px' });
|
44
|
this.stickyHeader = this.originalHeader.clone(true)
|
45
|
.hide()
|
46
|
.appendTo(this.stickyTable);
|
47
|
this.stickyHeaderCells = this.stickyHeader.find('> tr > th');
|
48
|
|
49
|
this.originalTable.addClass('sticky-table');
|
50
|
$(window)
|
51
|
.bind('scroll.drupal-tableheader', $.proxy(this, 'eventhandlerRecalculateStickyHeader'))
|
52
|
.bind('resize.drupal-tableheader', { calculateWidth: true }, $.proxy(this, 'eventhandlerRecalculateStickyHeader'))
|
53
|
|
54
|
|
55
|
.bind('drupalDisplaceAnchor.drupal-tableheader', function () {
|
56
|
window.scrollBy(0, -self.stickyTable.outerHeight());
|
57
|
})
|
58
|
|
59
|
|
60
|
.bind('drupalDisplaceFocus.drupal-tableheader', function (event) {
|
61
|
if (self.stickyVisible && event.clientY < (self.stickyOffsetTop + self.stickyTable.outerHeight()) && event.$target.closest('sticky-header').length === 0) {
|
62
|
window.scrollBy(0, -self.stickyTable.outerHeight());
|
63
|
}
|
64
|
})
|
65
|
.triggerHandler('resize.drupal-tableheader');
|
66
|
|
67
|
|
68
|
|
69
|
this.stickyHeader.show();
|
70
|
};
|
71
|
|
72
|
|
73
|
|
74
|
|
75
|
|
76
|
|
77
|
|
78
|
Drupal.tableHeader.prototype.eventhandlerRecalculateStickyHeader = function (event) {
|
79
|
var self = this;
|
80
|
var calculateWidth = event.data && event.data.calculateWidth;
|
81
|
|
82
|
|
83
|
this.stickyOffsetTop = Drupal.settings.tableHeaderOffset ? eval(Drupal.settings.tableHeaderOffset + '()') : 0;
|
84
|
this.stickyTable.css('top', this.stickyOffsetTop + 'px');
|
85
|
|
86
|
|
87
|
var viewHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
|
88
|
if (calculateWidth || this.viewHeight !== viewHeight) {
|
89
|
this.viewHeight = viewHeight;
|
90
|
this.vPosition = this.originalTable.offset().top - 4 - this.stickyOffsetTop;
|
91
|
this.hPosition = this.originalTable.offset().left;
|
92
|
this.vLength = this.originalTable[0].clientHeight - 100;
|
93
|
calculateWidth = true;
|
94
|
}
|
95
|
|
96
|
|
97
|
var hScroll = document.documentElement.scrollLeft || document.body.scrollLeft;
|
98
|
var vOffset = (document.documentElement.scrollTop || document.body.scrollTop) - this.vPosition;
|
99
|
this.stickyVisible = vOffset > 0 && vOffset < this.vLength;
|
100
|
this.stickyTable.css({ left: (-hScroll + this.hPosition) + 'px', visibility: this.stickyVisible ? 'visible' : 'hidden' });
|
101
|
|
102
|
|
103
|
|
104
|
if (this.stickyVisible && (calculateWidth || !this.widthCalculated)) {
|
105
|
this.widthCalculated = true;
|
106
|
var $that = null;
|
107
|
var $stickyCell = null;
|
108
|
var display = null;
|
109
|
var cellWidth = null;
|
110
|
|
111
|
|
112
|
|
113
|
for (var i = 0, il = this.originalHeaderCells.length; i < il; i += 1) {
|
114
|
$that = $(this.originalHeaderCells[i]);
|
115
|
$stickyCell = this.stickyHeaderCells.eq($that.index());
|
116
|
display = $that.css('display');
|
117
|
if (display !== 'none') {
|
118
|
cellWidth = $that.css('width');
|
119
|
|
120
|
if (cellWidth === 'auto') {
|
121
|
cellWidth = $that[0].clientWidth + 'px';
|
122
|
}
|
123
|
$stickyCell.css({'width': cellWidth, 'display': display});
|
124
|
}
|
125
|
else {
|
126
|
$stickyCell.css('display', 'none');
|
127
|
}
|
128
|
}
|
129
|
this.stickyTable.css('width', this.originalTable.outerWidth());
|
130
|
}
|
131
|
};
|
132
|
|
133
|
})(jQuery);
|