-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathpx-data-grid-selection-column.html
More file actions
361 lines (308 loc) · 11.3 KB
/
Copy pathpx-data-grid-selection-column.html
File metadata and controls
361 lines (308 loc) · 11.3 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<!--
Copyright (c) 2018, General Electric
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../vaadin-grid/vaadin-grid-column.html">
<link rel="import" href="../vaadin-checkbox/vaadin-checkbox.html">
<link rel="import" href="../vaadin-radio-button/vaadin-radio-button.html">
<link rel="import" href="px-data-grid-select-all.html">
<link rel="import" href="css/px-data-grid-selection-column-styles.html">
<dom-module id="vaadin-checkbox-styles" theme-for="vaadin-checkbox">
<template>
<style include="px-data-grid-selection-column-styles"></style>
</template>
</dom-module>
<dom-module id="vaadin-radio-button-styles" theme-for="vaadin-radio-button">
<template>
<style include="px-data-grid-selection-column-styles"></style>
</template>
</dom-module>
<dom-module id="px-data-grid-selection-column">
<template>
<template class="header" id="defaultHeaderTemplate">
<template is="dom-if" if="[[multiSelect]]">
<px-data-grid-select-all
hidden$="[[_selectAllHidden]]"
on-select-all-checked="_onSelectAllCheckedChanged"
checked="[[_isChecked(selectAll, _indeterminate)]]"
indeterminate="[[_indeterminate]]"
count="[[_selectionCount]]"
allow-sort-by-selection="[[allowSortBySelection]]"
check-listener="[[_boundedSelectAllCheckListener]]"
></px-data-grid-select-all>
</template>
</template>
<template id="defaultBodyTemplate">
<template is="dom-if" if="[[!_isGroupItem(item)]]">
<template is="dom-if" if="[[multiSelect]]">
<vaadin-checkbox aria-label="Select Row" checked="{{selected}}">
</vaadin-checkbox>
</template>
<template is="dom-if" if="[[!multiSelect]]">
<vaadin-radio-button aria-label="Select Row" checked="{{selected}}">
</vaadin-radio-button>
</template>
</template>
</template>
</template>
<script>
{
/**
* `<px-data-grid-selection-column>` is a helper element for the `<px-data-grid>`
* to be used to offer selection column behavior with multi and single select.
* @memberof Predix
* @mixes Vaadin.GridColumnElement
*/
class DataGridSelectionColumnElement extends Vaadin.GridColumnElement {
static get is() {
return 'px-data-grid-selection-column';
}
static get properties() {
return {
/**
* Width of the cells for this column.
*/
width: {
type: String,
value: '56px'
},
/**
* Flex grow ratio for the cell widths. When set to 0, cell width is fixed.
*/
flexGrow: {
type: Number,
value: 0
},
/**
* When true, all the items are selected.
*/
selectAll: {
type: Boolean,
value: false,
notify: true
},
/**
* When true, the active gets automatically selected.
*/
autoSelect: {
type: Boolean,
value: false
},
/**
* When true, user is allowed to multiselect
*/
multiSelect: {
type: Boolean,
value: true
},
/**
* When true, the grid is in tree-grid mode (group by column/value)
*/
treeGrid: {
type: Boolean
},
/**
* If user is allowed to sort by selection
*/
allowSortBySelection: {
type: Boolean,
value: false
},
/**
* @private
*/
headerTemplate: Object,
/**
* @private
*/
template: Object,
_indeterminate: {
type: Boolean,
value: false
},
/*
* The previous state of activeItem. When activeItem turns to `null`,
* previousActiveItem will have an Object with just unselected activeItem
*/
_previousActiveItem: Object,
_selectAllHidden: {
type: Boolean,
value: true
},
/**
* Just to help to identify columns without data
*/
isDataColumn: {
type: Boolean,
value: false,
readOnly: true
},
/**
* Current selection count
*/
_selectionCount: {
type: Number,
value: 0
},
};
}
static get observers() {
return [
'_onSelectAllChanged(selectAll)',
'_onMultiSelectOrAllowSortBySelectionChanged(multiSelect,allowSortBySelection)'
];
}
ready() {
super.ready();
this._boundedSelectAllCheckListener = this._onSelectAllCheckedChanged.bind(this);
}
_prepareHeaderTemplate() {
const headerTemplate = this._prepareTemplatizer(this._findTemplate('template.header') || this.$.defaultHeaderTemplate);
// needed to override the dataHost correctly in case internal template is used.
headerTemplate.templatizer.dataHost = headerTemplate === this.$.defaultHeaderTemplate ? this : this.dataHost;
return headerTemplate;
}
_prepareBodyTemplate() {
const template = this._prepareTemplatizer(this._findTemplate('template:not(.header):not(.footer)') || this.$.defaultBodyTemplate);
// needed to override the dataHost correctly in case internal template is used.
template.templatizer.dataHost = template === this.$.defaultBodyTemplate ? this : this.dataHost;
return template;
}
constructor() {
super();
this._boundOnActiveItemChanged = this._onActiveItemChanged.bind(this);
this._boundOnDataProviderChanged = this._onDataProviderChanged.bind(this);
this._boundOnSelectedItemsChanged = this._onSelectedItemsChanged.bind(this);
}
/** @private */
disconnectedCallback() {
if (this._grid) {
this._grid.removeEventListener('active-item-changed', this._boundOnActiveItemChanged);
this._grid.removeEventListener('data-provider-changed', this._boundOnDataProviderChanged);
this._grid.removeEventListener('filter-changed', this._boundOnSelectedItemsChanged);
this._grid._pxDataGrid.removeEventListener('selected-items-changed', this._boundOnSelectedItemsChanged);
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
if (isSafari && window.ShadyDOM && this.parentElement) {
// Detach might have beem caused by order change.
// Shady on safari doesn't restore isAttached so we'll need to do it manually.
const parent = this.parentElement;
const nextSibling = this.nextElementSibling;
parent.removeChild(this);
if (nextSibling) {
parent.insertBefore(this, nextSibling);
} else {
parent.appendChild(this);
}
}
}
super.disconnectedCallback();
}
/** @private */
connectedCallback() {
super.connectedCallback();
if (this._grid) {
const _grid = this._grid;
_grid.addEventListener('active-item-changed', this._boundOnActiveItemChanged);
_grid.addEventListener('data-provider-changed', this._boundOnDataProviderChanged);
_grid.addEventListener('filter-changed', this._boundOnSelectedItemsChanged);
Polymer.RenderStatus.afterNextRender(_grid, () => {
_grid._pxDataGrid.addEventListener('selected-items-changed', this._boundOnSelectedItemsChanged);
this._selectAllHidden = !_grid._pxDataGrid._hasLocalDataProvider;
});
}
}
_isGroupItem(item) {
return this.treeGrid && item.hasChildren;
}
_onSelectAllChanged(selectAll) {
if (selectAll === undefined || !this._grid) {
return;
}
if (this._selectAllChangeLock) {
return;
}
if (selectAll && this._grid._pxDataGrid) {
this._grid.selectedItems = this._grid._pxDataGrid._getAllLocalItems();
} else {
this._grid.selectedItems = [];
}
}
// Return true if array `a` contains all the items in `b`
// We need this when sorting or to preserve selection after filtering.
_arrayContains(a, b) {
for (var i = 0; a && b && b[i] && a.indexOf(b[i]) >= 0; i++); // eslint-disable-line
return i == b.length;
}
_onSelectAllCheckedChanged(e) {
this.selectAll = this._indeterminate || e.target.checked;
// Events get out of sync when toggling from indeterminate to determinate
if (e.target.checked !== this.selectAll) {
e.target.checked = this.selectAll;
}
}
// iOS needs indeterminated + checked at the same time
_isChecked(selectAll, indeterminate) {
return indeterminate || selectAll;
}
_onActiveItemChanged(e) {
const activeItem = e.detail.value;
if (this.autoSelect) {
const item = activeItem || this._previousActiveItem;
if (item && !item.hasChildren) {
this._grid._toggleItem(item);
}
}
this._previousActiveItem = activeItem;
}
_onSelectedItemsChanged(e) {
this._selectAllChangeLock = true;
const allItems = this._grid._pxDataGrid._getAllLocalItems();
if (Array.isArray(allItems)) {
if (!this._grid.selectedItems.length) {
this.selectAll = false;
this._indeterminate = false;
} else if (this._arrayContains(this._grid.selectedItems, allItems)) {
this.selectAll = true;
this._indeterminate = false;
} else {
this.selectAll = false;
this._indeterminate = true;
}
}
this._selectAllChangeLock = false;
this._selectionCount = this._grid.selectedItems.length;
}
_onDataProviderChanged(e) {
this._selectAllHidden = !this._grid._pxDataGrid._hasLocalDataProvider;
}
/**
* Function that makes sure there is enough space for all content
*/
_onMultiSelectOrAllowSortBySelectionChanged(multiSelect, allowSortBySelection) {
if (!multiSelect) {
this.width = '40px';
} else if (!allowSortBySelection) {
this.width = '55px';
} else {
this.width = '90px';
}
}
}
customElements.define(DataGridSelectionColumnElement.is, DataGridSelectionColumnElement);
/**
* @namespace Predix
*/
window.Predix = window.Predix || {};
Predix.DataGridSelectionColumnElement = DataGridSelectionColumnElement;
}
</script>
</dom-module>