forked from vaadin/vaadin-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vaadin-grid-column-group.html
281 lines (237 loc) · 9.46 KB
/
vaadin-grid-column-group.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!--
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../polymer/lib/utils/async.html">
<link rel="import" href="vaadin-grid-column.html">
<dom-module id="vaadin-grid-column-group">
<script>
{
/**
* A `<vaadin-grid-column-group>` is used to make groups of colums in `<vaadin-grid>` and
* to configure additional headers and footers.
*
* Groups can be nested to create complex header and footer configurations.
*
* The `class` attribute is used to differentiate header and footer templates.
*
* #### Example:
* ```html
* <vaadin-grid-column-group resizable>
* <template class="header">Name</template>
*
* <vaadin-grid-column>
* <template class="header">First</template>
* <template>[[item.name.first]]</template>
* </vaadin-grid-column>
* <vaadin-grid-column>
* <template class="header">Last</template>
* <template>[[item.name.last]]</template>
* </vaadin-grid-column>
* </vaadin-grid-column-group>
* ```
*
* @memberof Vaadin
* @mixes Vaadin.Grid.ColumnBaseMixin
*/
class GridColumnGroupElement extends Vaadin.Grid.ColumnBaseMixin(Polymer.Element) {
static get is() {
return 'vaadin-grid-column-group';
}
static get properties() {
return {
_childColumns: {
value: function() {
return this._getChildColumns(this);
}
},
/**
* Flex grow ratio for the column group as the sum of the ratios of its child columns.
*/
flexGrow: {
type: Number,
readOnly: true
},
/**
* Width of the column group as the sum of the widths of its child columns.
*/
width: {
type: String,
readOnly: true
},
_visibleChildColumns: Array,
_colSpan: Number,
_rootColumns: Array
};
}
static get observers() {
return [
'_updateVisibleChildColumns(_childColumns)',
'_childColumnsChanged(_childColumns)',
'_groupFrozenChanged(frozen, _rootColumns)',
'_groupHiddenChanged(hidden, _rootColumns)',
'_visibleChildColumnsChanged(_visibleChildColumns)',
'_colSpanChanged(_colSpan, _headerCell, _footerCell)',
'_groupOrderChanged(_order, _rootColumns)',
'_groupReorderStatusChanged(_reorderStatus, _rootColumns)',
'_groupResizableChanged(resizable, _rootColumns)'
];
}
/** @private */
connectedCallback() {
super.connectedCallback();
this._addNodeObserver();
this._updateFlexAndWidth();
}
detached() {
if (this._observer) {
Polymer.dom(this).unobserveNodes(this._observer);
}
}
_columnPropChanged(path, value) {
if (path === 'hidden') {
this._preventHiddenCascade = true;
this._updateVisibleChildColumns(this._childColumns);
this._preventHiddenCascade = false;
}
if (/flexGrow|width|hidden|_childColumns/.test(path)) {
this._updateFlexAndWidth();
}
if (path === 'frozen') {
// Don’t unfreeze the frozen group because of a non-frozen child
this.frozen = this.frozen || value;
}
if (path === 'lastFrozen') {
// Don’t unfreeze the frozen group because of a non-frozen child
this._lastFrozen = this._lastFrozen || value;
}
}
_groupOrderChanged(order, rootColumns) {
if (order && rootColumns) {
// The parent column order number cascades downwards to it's children
// so that the resulting order numbering constructs as follows:
// [ 1000 ]
// [ 1100 ] | [ 1200 ]
// [1110] | [1120] | [1210] | [1220]
// Trailing zeros are counted so we know the level on which we're working on.
const trailingZeros = /(0+)$/.exec(order).pop().length;
// In an unlikely situation where a group has more than 9 child columns,
// the child scope must have 1 digit less...
const childCountDigits = ~~(Math.log(rootColumns.length) / Math.log(Math.LN10)) + 1;
// Final scope for the child columns needs to mind both factors.
const scope = Math.pow(10, trailingZeros - childCountDigits);
const _rootColumns = rootColumns.slice(0);
if (_rootColumns[0] && _rootColumns[0]._order) {
_rootColumns.sort((a, b) => a._order - b._order);
}
_rootColumns.forEach((column, index) => column._order = order + ((index + 1) * scope));
}
}
_groupReorderStatusChanged(reorderStatus, rootColumns) {
if (reorderStatus === undefined || rootColumns === undefined) {
return;
}
rootColumns.forEach(column => column._reorderStatus = reorderStatus);
}
_groupResizableChanged(resizable, rootColumns) {
if (rootColumns === undefined) {
return;
}
rootColumns.forEach(column => column.resizable = resizable);
}
_updateVisibleChildColumns(childColumns) {
this._visibleChildColumns = Array.prototype.filter.call(childColumns, col => !col.hidden);
}
_childColumnsChanged(childColumns) {
if (!this._autoHidden && this.hidden) {
Array.prototype.forEach.call(childColumns, column => column.hidden = true);
this._updateVisibleChildColumns(childColumns);
}
}
_updateFlexAndWidth() {
if (!this._visibleChildColumns) {
return;
}
if (this._visibleChildColumns.length) {
this._setWidth('calc(' + Array.prototype
.reduce.call(this._visibleChildColumns, (prev, curr) => prev += ' + ' + (curr.width || '0px')
.replace('calc', ''), '')
.substring(3) + ')');
} else {
this._setWidth('0px');
}
this._setFlexGrow(Array.prototype.reduce.call(this._visibleChildColumns, (prev, curr) => prev + curr.flexGrow, 0));
}
_groupFrozenChanged(frozen, rootColumns) {
if (rootColumns === undefined || frozen === undefined) {
return;
}
// Don’t propagate the default `false` value.
if (frozen !== false) {
Array.from(rootColumns).forEach(col => col.frozen = frozen);
}
}
_groupHiddenChanged(hidden, rootColumns) {
if (rootColumns && !this._preventHiddenCascade) {
this._ignoreVisibleChildColumns = true;
rootColumns.forEach(column => column.hidden = hidden);
this._ignoreVisibleChildColumns = false;
}
this._columnPropChanged('hidden');
}
_visibleChildColumnsChanged(visibleChildColumns) {
this._colSpan = visibleChildColumns.length;
if (!this._ignoreVisibleChildColumns) {
if (visibleChildColumns.length === 0) {
this._autoHidden = this.hidden = true;
} else if (this.hidden && this._autoHidden) {
this._autoHidden = this.hidden = false;
}
}
}
_colSpanChanged(colSpan, headerCell, footerCell) {
if (headerCell) {
headerCell.setAttribute('colspan', colSpan);
this._grid && this._grid._a11yUpdateCellColspan(headerCell, colSpan);
}
if (footerCell) {
footerCell.setAttribute('colspan', colSpan);
this._grid && this._grid._a11yUpdateCellColspan(footerCell, colSpan);
}
}
_getChildColumns(el) {
// TODO: Drop legacy api
return Polymer.dom(el).queryDistributedElements('*').filter(this._isColumnElement);
}
_addNodeObserver() {
// TODO: Drop legacy api
this._observer = Polymer.dom(this).observeNodes(info => {
if (info.addedNodes.filter(this._isColumnElement).length > 0 ||
info.removedNodes.filter(this._isColumnElement).length > 0) {
this._preventHiddenCascade = true;
this._rootColumns = this._getChildColumns(this);
this._childColumns = this._rootColumns;
this._preventHiddenCascade = false;
// Update the column tree with microtask timing to avoid shady style scope issues
Polymer.Async.microTask.run(() => {
this._grid && this._grid._updateColumnTree && this._grid._updateColumnTree();
});
}
});
this._observer.flush();
}
_isColumnElement(node) {
return node.nodeType === Node.ELEMENT_NODE && /\bcolumn\b/.test(node.localName);
}
}
customElements.define(GridColumnGroupElement.is, GridColumnGroupElement);
/**
* @namespace Vaadin
*/
window.Vaadin = window.Vaadin || {};
Vaadin.GridColumnGroupElement = GridColumnGroupElement;
}
</script>
</dom-module>