annotate core/misc/tableheader.js @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
rev   line source
Chris@0 1 /**
Chris@0 2 * DO NOT EDIT THIS FILE.
Chris@0 3 * See the following change record for more information,
Chris@0 4 * https://www.drupal.org/node/2815083
Chris@0 5 * @preserve
Chris@0 6 **/
Chris@0 7
Chris@0 8 (function ($, Drupal, displace) {
Chris@0 9 Drupal.behaviors.tableHeader = {
Chris@0 10 attach: function attach(context) {
Chris@0 11 $(window).one('scroll.TableHeaderInit', { context: context }, tableHeaderInitHandler);
Chris@0 12 }
Chris@0 13 };
Chris@0 14
Chris@0 15 function scrollValue(position) {
Chris@0 16 return document.documentElement[position] || document.body[position];
Chris@0 17 }
Chris@0 18
Chris@0 19 function tableHeaderInitHandler(e) {
Chris@0 20 var $tables = $(e.data.context).find('table.sticky-enabled').once('tableheader');
Chris@0 21 var il = $tables.length;
Chris@0 22 for (var i = 0; i < il; i++) {
Chris@0 23 TableHeader.tables.push(new TableHeader($tables[i]));
Chris@0 24 }
Chris@0 25 forTables('onScroll');
Chris@0 26 }
Chris@0 27
Chris@0 28 function forTables(method, arg) {
Chris@0 29 var tables = TableHeader.tables;
Chris@0 30 var il = tables.length;
Chris@0 31 for (var i = 0; i < il; i++) {
Chris@0 32 tables[i][method](arg);
Chris@0 33 }
Chris@0 34 }
Chris@0 35
Chris@0 36 function tableHeaderResizeHandler(e) {
Chris@0 37 forTables('recalculateSticky');
Chris@0 38 }
Chris@0 39
Chris@0 40 function tableHeaderOnScrollHandler(e) {
Chris@0 41 forTables('onScroll');
Chris@0 42 }
Chris@0 43
Chris@0 44 function tableHeaderOffsetChangeHandler(e, offsets) {
Chris@0 45 forTables('stickyPosition', offsets.top);
Chris@0 46 }
Chris@0 47
Chris@0 48 $(window).on({
Chris@0 49 'resize.TableHeader': tableHeaderResizeHandler,
Chris@0 50
Chris@0 51 'scroll.TableHeader': tableHeaderOnScrollHandler
Chris@0 52 });
Chris@0 53
Chris@0 54 $(document).on({
Chris@0 55 'columnschange.TableHeader': tableHeaderResizeHandler,
Chris@0 56
Chris@0 57 'drupalViewportOffsetChange.TableHeader': tableHeaderOffsetChangeHandler
Chris@0 58 });
Chris@0 59
Chris@0 60 function TableHeader(table) {
Chris@0 61 var $table = $(table);
Chris@0 62
Chris@0 63 this.$originalTable = $table;
Chris@0 64
Chris@0 65 this.$originalHeader = $table.children('thead');
Chris@0 66
Chris@0 67 this.$originalHeaderCells = this.$originalHeader.find('> tr > th');
Chris@0 68
Chris@0 69 this.displayWeight = null;
Chris@0 70 this.$originalTable.addClass('sticky-table');
Chris@0 71 this.tableHeight = $table[0].clientHeight;
Chris@0 72 this.tableOffset = this.$originalTable.offset();
Chris@0 73
Chris@0 74 this.$originalTable.on('columnschange', { tableHeader: this }, function (e, display) {
Chris@0 75 var tableHeader = e.data.tableHeader;
Chris@0 76 if (tableHeader.displayWeight === null || tableHeader.displayWeight !== display) {
Chris@0 77 tableHeader.recalculateSticky();
Chris@0 78 }
Chris@0 79 tableHeader.displayWeight = display;
Chris@0 80 });
Chris@0 81
Chris@0 82 this.createSticky();
Chris@0 83 }
Chris@0 84
Chris@0 85 $.extend(TableHeader, {
Chris@0 86 tables: []
Chris@0 87 });
Chris@0 88
Chris@0 89 $.extend(TableHeader.prototype, {
Chris@0 90 minHeight: 100,
Chris@0 91
Chris@0 92 tableOffset: null,
Chris@0 93
Chris@0 94 tableHeight: null,
Chris@0 95
Chris@0 96 stickyVisible: false,
Chris@0 97
Chris@0 98 createSticky: function createSticky() {
Chris@0 99 var $stickyHeader = this.$originalHeader.clone(true);
Chris@0 100
Chris@0 101 this.$stickyTable = $('<table class="sticky-header"/>').css({
Chris@0 102 visibility: 'hidden',
Chris@0 103 position: 'fixed',
Chris@0 104 top: '0px'
Chris@0 105 }).append($stickyHeader).insertBefore(this.$originalTable);
Chris@0 106
Chris@0 107 this.$stickyHeaderCells = $stickyHeader.find('> tr > th');
Chris@0 108
Chris@0 109 this.recalculateSticky();
Chris@0 110 },
Chris@0 111 stickyPosition: function stickyPosition(offsetTop, offsetLeft) {
Chris@0 112 var css = {};
Chris@0 113 if (typeof offsetTop === 'number') {
Chris@0 114 css.top = offsetTop + 'px';
Chris@0 115 }
Chris@0 116 if (typeof offsetLeft === 'number') {
Chris@0 117 css.left = this.tableOffset.left - offsetLeft + 'px';
Chris@0 118 }
Chris@0 119 return this.$stickyTable.css(css);
Chris@0 120 },
Chris@0 121 checkStickyVisible: function checkStickyVisible() {
Chris@0 122 var scrollTop = scrollValue('scrollTop');
Chris@0 123 var tableTop = this.tableOffset.top - displace.offsets.top;
Chris@0 124 var tableBottom = tableTop + this.tableHeight;
Chris@0 125 var visible = false;
Chris@0 126
Chris@0 127 if (tableTop < scrollTop && scrollTop < tableBottom - this.minHeight) {
Chris@0 128 visible = true;
Chris@0 129 }
Chris@0 130
Chris@0 131 this.stickyVisible = visible;
Chris@0 132 return visible;
Chris@0 133 },
Chris@0 134 onScroll: function onScroll(e) {
Chris@0 135 this.checkStickyVisible();
Chris@0 136
Chris@0 137 this.stickyPosition(null, scrollValue('scrollLeft'));
Chris@0 138 this.$stickyTable.css('visibility', this.stickyVisible ? 'visible' : 'hidden');
Chris@0 139 },
Chris@0 140 recalculateSticky: function recalculateSticky(event) {
Chris@0 141 this.tableHeight = this.$originalTable[0].clientHeight;
Chris@0 142
Chris@0 143 displace.offsets.top = displace.calculateOffset('top');
Chris@0 144 this.tableOffset = this.$originalTable.offset();
Chris@0 145 this.stickyPosition(displace.offsets.top, scrollValue('scrollLeft'));
Chris@0 146
Chris@0 147 var $that = null;
Chris@0 148 var $stickyCell = null;
Chris@0 149 var display = null;
Chris@0 150
Chris@0 151 var il = this.$originalHeaderCells.length;
Chris@0 152 for (var i = 0; i < il; i++) {
Chris@0 153 $that = $(this.$originalHeaderCells[i]);
Chris@0 154 $stickyCell = this.$stickyHeaderCells.eq($that.index());
Chris@0 155 display = $that.css('display');
Chris@0 156 if (display !== 'none') {
Chris@0 157 $stickyCell.css({ width: $that.css('width'), display: display });
Chris@0 158 } else {
Chris@0 159 $stickyCell.css('display', 'none');
Chris@0 160 }
Chris@0 161 }
Chris@0 162 this.$stickyTable.css('width', this.$originalTable.outerWidth());
Chris@0 163 }
Chris@0 164 });
Chris@0 165
Chris@0 166 Drupal.TableHeader = TableHeader;
Chris@0 167 })(jQuery, Drupal, window.parent.Drupal.displace);