annotate core/misc/tableheader.js @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
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@17 9 function TableHeader(table) {
Chris@17 10 var $table = $(table);
Chris@17 11
Chris@17 12 this.$originalTable = $table;
Chris@17 13
Chris@17 14 this.$originalHeader = $table.children('thead');
Chris@17 15
Chris@17 16 this.$originalHeaderCells = this.$originalHeader.find('> tr > th');
Chris@17 17
Chris@17 18 this.displayWeight = null;
Chris@17 19 this.$originalTable.addClass('sticky-table');
Chris@17 20 this.tableHeight = $table[0].clientHeight;
Chris@17 21 this.tableOffset = this.$originalTable.offset();
Chris@17 22
Chris@17 23 this.$originalTable.on('columnschange', { tableHeader: this }, function (e, display) {
Chris@17 24 var tableHeader = e.data.tableHeader;
Chris@17 25 if (tableHeader.displayWeight === null || tableHeader.displayWeight !== display) {
Chris@17 26 tableHeader.recalculateSticky();
Chris@17 27 }
Chris@17 28 tableHeader.displayWeight = display;
Chris@17 29 });
Chris@17 30
Chris@17 31 this.createSticky();
Chris@17 32 }
Chris@17 33
Chris@17 34 function forTables(method, arg) {
Chris@17 35 var tables = TableHeader.tables;
Chris@17 36 var il = tables.length;
Chris@17 37 for (var i = 0; i < il; i++) {
Chris@17 38 tables[i][method](arg);
Chris@0 39 }
Chris@0 40 }
Chris@0 41
Chris@0 42 function tableHeaderInitHandler(e) {
Chris@0 43 var $tables = $(e.data.context).find('table.sticky-enabled').once('tableheader');
Chris@0 44 var il = $tables.length;
Chris@0 45 for (var i = 0; i < il; i++) {
Chris@0 46 TableHeader.tables.push(new TableHeader($tables[i]));
Chris@0 47 }
Chris@0 48 forTables('onScroll');
Chris@0 49 }
Chris@0 50
Chris@17 51 Drupal.behaviors.tableHeader = {
Chris@17 52 attach: function attach(context) {
Chris@17 53 $(window).one('scroll.TableHeaderInit', { context: context }, tableHeaderInitHandler);
Chris@0 54 }
Chris@17 55 };
Chris@17 56
Chris@17 57 function scrollValue(position) {
Chris@17 58 return document.documentElement[position] || document.body[position];
Chris@0 59 }
Chris@0 60
Chris@0 61 function tableHeaderResizeHandler(e) {
Chris@0 62 forTables('recalculateSticky');
Chris@0 63 }
Chris@0 64
Chris@0 65 function tableHeaderOnScrollHandler(e) {
Chris@0 66 forTables('onScroll');
Chris@0 67 }
Chris@0 68
Chris@0 69 function tableHeaderOffsetChangeHandler(e, offsets) {
Chris@0 70 forTables('stickyPosition', offsets.top);
Chris@0 71 }
Chris@0 72
Chris@0 73 $(window).on({
Chris@0 74 'resize.TableHeader': tableHeaderResizeHandler,
Chris@0 75
Chris@0 76 'scroll.TableHeader': tableHeaderOnScrollHandler
Chris@0 77 });
Chris@0 78
Chris@0 79 $(document).on({
Chris@0 80 'columnschange.TableHeader': tableHeaderResizeHandler,
Chris@0 81
Chris@0 82 'drupalViewportOffsetChange.TableHeader': tableHeaderOffsetChangeHandler
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@17 167 })(jQuery, Drupal, window.Drupal.displace);