2015-06-08 14:04:39 +00:00
|
|
|
/**
|
2017-05-19 22:12:53 +00:00
|
|
|
* DO NOT EDIT THIS FILE.
|
|
|
|
* See the following change record for more information,
|
2017-05-23 14:30:14 +00:00
|
|
|
* https://www.drupal.org/node/2815083
|
2017-05-19 22:12:53 +00:00
|
|
|
* @preserve
|
|
|
|
**/
|
2015-06-08 14:04:39 +00:00
|
|
|
|
2013-03-29 15:23:50 +00:00
|
|
|
(function ($, Drupal, displace) {
|
2014-01-27 21:41:32 +00:00
|
|
|
Drupal.behaviors.tableHeader = {
|
2017-05-19 22:12:53 +00:00
|
|
|
attach: function attach(context) {
|
|
|
|
$(window).one('scroll.TableHeaderInit', { context: context }, tableHeaderInitHandler);
|
2014-01-27 21:41:32 +00:00
|
|
|
}
|
|
|
|
};
|
2012-08-24 12:19:11 +00:00
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
function scrollValue(position) {
|
|
|
|
return document.documentElement[position] || document.body[position];
|
2012-08-24 12:19:11 +00:00
|
|
|
}
|
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
function tableHeaderInitHandler(e) {
|
|
|
|
var $tables = $(e.data.context).find('table.sticky-enabled').once('tableheader');
|
2015-04-19 15:30:43 +00:00
|
|
|
var il = $tables.length;
|
|
|
|
for (var i = 0; i < il; i++) {
|
2014-01-27 21:41:32 +00:00
|
|
|
TableHeader.tables.push(new TableHeader($tables[i]));
|
|
|
|
}
|
2014-06-03 18:35:48 +00:00
|
|
|
forTables('onScroll');
|
2012-08-24 12:19:11 +00:00
|
|
|
}
|
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
function forTables(method, arg) {
|
|
|
|
var tables = TableHeader.tables;
|
2015-04-19 15:30:43 +00:00
|
|
|
var il = tables.length;
|
|
|
|
for (var i = 0; i < il; i++) {
|
2014-01-27 21:41:32 +00:00
|
|
|
tables[i][method](arg);
|
|
|
|
}
|
|
|
|
}
|
2012-08-24 12:19:11 +00:00
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
function tableHeaderResizeHandler(e) {
|
|
|
|
forTables('recalculateSticky');
|
|
|
|
}
|
2012-08-24 12:19:11 +00:00
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
function tableHeaderOnScrollHandler(e) {
|
|
|
|
forTables('onScroll');
|
|
|
|
}
|
2012-08-24 12:19:11 +00:00
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
function tableHeaderOffsetChangeHandler(e, offsets) {
|
|
|
|
forTables('stickyPosition', offsets.top);
|
|
|
|
}
|
2012-08-24 12:19:11 +00:00
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
$(window).on({
|
|
|
|
'resize.TableHeader': tableHeaderResizeHandler,
|
|
|
|
|
|
|
|
'scroll.TableHeader': tableHeaderOnScrollHandler
|
|
|
|
});
|
2015-06-08 14:04:39 +00:00
|
|
|
|
2017-05-19 22:12:53 +00:00
|
|
|
$(document).on({
|
2014-01-27 21:41:32 +00:00
|
|
|
'columnschange.TableHeader': tableHeaderResizeHandler,
|
|
|
|
|
|
|
|
'drupalViewportOffsetChange.TableHeader': tableHeaderOffsetChangeHandler
|
2012-02-29 07:27:01 +00:00
|
|
|
});
|
2007-02-06 08:35:13 +00:00
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
function TableHeader(table) {
|
|
|
|
var $table = $(table);
|
2012-08-24 12:19:11 +00:00
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
this.$originalTable = $table;
|
2015-06-08 14:04:39 +00:00
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
this.$originalHeader = $table.children('thead');
|
2015-06-08 14:04:39 +00:00
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
this.$originalHeaderCells = this.$originalHeader.find('> tr > th');
|
2010-07-28 01:38:28 +00:00
|
|
|
|
2015-06-08 14:04:39 +00:00
|
|
|
this.displayWeight = null;
|
2014-01-27 21:41:32 +00:00
|
|
|
this.$originalTable.addClass('sticky-table');
|
|
|
|
this.tableHeight = $table[0].clientHeight;
|
|
|
|
this.tableOffset = this.$originalTable.offset();
|
2012-08-24 12:19:11 +00:00
|
|
|
|
2017-05-19 22:12:53 +00:00
|
|
|
this.$originalTable.on('columnschange', { tableHeader: this }, function (e, display) {
|
2014-01-27 21:41:32 +00:00
|
|
|
var tableHeader = e.data.tableHeader;
|
|
|
|
if (tableHeader.displayWeight === null || tableHeader.displayWeight !== display) {
|
|
|
|
tableHeader.recalculateSticky();
|
|
|
|
}
|
|
|
|
tableHeader.displayWeight = display;
|
|
|
|
});
|
2012-08-24 12:19:11 +00:00
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
this.createSticky();
|
|
|
|
}
|
2008-10-29 10:01:28 +00:00
|
|
|
|
2017-05-19 22:12:53 +00:00
|
|
|
$.extend(TableHeader, {
|
2014-01-27 21:41:32 +00:00
|
|
|
tables: []
|
|
|
|
});
|
2012-08-24 12:19:11 +00:00
|
|
|
|
2017-05-19 22:12:53 +00:00
|
|
|
$.extend(TableHeader.prototype, {
|
2014-01-27 21:41:32 +00:00
|
|
|
minHeight: 100,
|
|
|
|
|
|
|
|
tableOffset: null,
|
|
|
|
|
|
|
|
tableHeight: null,
|
|
|
|
|
|
|
|
stickyVisible: false,
|
|
|
|
|
2017-05-19 22:12:53 +00:00
|
|
|
createSticky: function createSticky() {
|
2014-01-27 21:41:32 +00:00
|
|
|
var $stickyHeader = this.$originalHeader.clone(true);
|
2017-05-19 22:12:53 +00:00
|
|
|
|
|
|
|
this.$stickyTable = $('<table class="sticky-header"/>').css({
|
|
|
|
visibility: 'hidden',
|
|
|
|
position: 'fixed',
|
|
|
|
top: '0px'
|
|
|
|
}).append($stickyHeader).insertBefore(this.$originalTable);
|
2014-01-27 21:41:32 +00:00
|
|
|
|
|
|
|
this.$stickyHeaderCells = $stickyHeader.find('> tr > th');
|
|
|
|
|
|
|
|
this.recalculateSticky();
|
|
|
|
},
|
2017-05-19 22:12:53 +00:00
|
|
|
stickyPosition: function stickyPosition(offsetTop, offsetLeft) {
|
2014-01-27 21:41:32 +00:00
|
|
|
var css = {};
|
2014-06-03 18:35:48 +00:00
|
|
|
if (typeof offsetTop === 'number') {
|
2014-01-27 21:41:32 +00:00
|
|
|
css.top = offsetTop + 'px';
|
2012-02-29 07:27:01 +00:00
|
|
|
}
|
2014-06-03 18:35:48 +00:00
|
|
|
if (typeof offsetLeft === 'number') {
|
2017-05-19 22:12:53 +00:00
|
|
|
css.left = this.tableOffset.left - offsetLeft + 'px';
|
2012-02-29 07:27:01 +00:00
|
|
|
}
|
2014-01-27 21:41:32 +00:00
|
|
|
return this.$stickyTable.css(css);
|
|
|
|
},
|
2017-05-19 22:12:53 +00:00
|
|
|
checkStickyVisible: function checkStickyVisible() {
|
2014-01-27 21:41:32 +00:00
|
|
|
var scrollTop = scrollValue('scrollTop');
|
|
|
|
var tableTop = this.tableOffset.top - displace.offsets.top;
|
|
|
|
var tableBottom = tableTop + this.tableHeight;
|
|
|
|
var visible = false;
|
|
|
|
|
2017-05-19 22:12:53 +00:00
|
|
|
if (tableTop < scrollTop && scrollTop < tableBottom - this.minHeight) {
|
2014-01-27 21:41:32 +00:00
|
|
|
visible = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stickyVisible = visible;
|
|
|
|
return visible;
|
|
|
|
},
|
2017-05-19 22:12:53 +00:00
|
|
|
onScroll: function onScroll(e) {
|
2014-01-27 21:41:32 +00:00
|
|
|
this.checkStickyVisible();
|
2017-05-19 22:12:53 +00:00
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
this.stickyPosition(null, scrollValue('scrollLeft'));
|
|
|
|
this.$stickyTable.css('visibility', this.stickyVisible ? 'visible' : 'hidden');
|
|
|
|
},
|
2017-05-19 22:12:53 +00:00
|
|
|
recalculateSticky: function recalculateSticky(event) {
|
2014-01-27 21:41:32 +00:00
|
|
|
this.tableHeight = this.$originalTable[0].clientHeight;
|
|
|
|
|
|
|
|
displace.offsets.top = displace.calculateOffset('top');
|
|
|
|
this.tableOffset = this.$originalTable.offset();
|
|
|
|
this.stickyPosition(displace.offsets.top, scrollValue('scrollLeft'));
|
|
|
|
|
|
|
|
var $that = null;
|
|
|
|
var $stickyCell = null;
|
|
|
|
var display = null;
|
2017-05-19 22:12:53 +00:00
|
|
|
|
2015-04-19 15:30:43 +00:00
|
|
|
var il = this.$originalHeaderCells.length;
|
|
|
|
for (var i = 0; i < il; i++) {
|
2014-01-27 21:41:32 +00:00
|
|
|
$that = $(this.$originalHeaderCells[i]);
|
|
|
|
$stickyCell = this.$stickyHeaderCells.eq($that.index());
|
|
|
|
display = $that.css('display');
|
|
|
|
if (display !== 'none') {
|
2017-05-19 22:12:53 +00:00
|
|
|
$stickyCell.css({ width: $that.css('width'), display: display });
|
|
|
|
} else {
|
2014-01-27 21:41:32 +00:00
|
|
|
$stickyCell.css('display', 'none');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.$stickyTable.css('width', this.$originalTable.outerWidth());
|
2012-02-29 07:27:01 +00:00
|
|
|
}
|
2014-01-27 21:41:32 +00:00
|
|
|
});
|
2012-08-24 12:19:11 +00:00
|
|
|
|
2014-01-27 21:41:32 +00:00
|
|
|
Drupal.TableHeader = TableHeader;
|
2017-05-19 22:12:53 +00:00
|
|
|
})(jQuery, Drupal, window.parent.Drupal.displace);
|