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