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