@vaadin/grid 22.0.0-alpha7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +190 -0
- package/README.md +79 -0
- package/all-imports.js +1 -0
- package/package.json +58 -0
- package/src/all-imports.js +14 -0
- package/src/array-data-provider.js +145 -0
- package/src/interfaces.d.ts +75 -0
- package/src/vaadin-grid-a11y-mixin.js +158 -0
- package/src/vaadin-grid-active-item-mixin.d.ts +19 -0
- package/src/vaadin-grid-active-item-mixin.js +117 -0
- package/src/vaadin-grid-array-data-provider-mixin.d.ts +16 -0
- package/src/vaadin-grid-array-data-provider-mixin.js +75 -0
- package/src/vaadin-grid-column-group.d.ts +54 -0
- package/src/vaadin-grid-column-group.js +320 -0
- package/src/vaadin-grid-column-reordering-mixin.d.ts +19 -0
- package/src/vaadin-grid-column-reordering-mixin.js +387 -0
- package/src/vaadin-grid-column-resizing-mixin.js +111 -0
- package/src/vaadin-grid-column.d.ts +133 -0
- package/src/vaadin-grid-column.js +745 -0
- package/src/vaadin-grid-data-provider-mixin.d.ts +108 -0
- package/src/vaadin-grid-data-provider-mixin.js +520 -0
- package/src/vaadin-grid-drag-and-drop-mixin.d.ts +69 -0
- package/src/vaadin-grid-drag-and-drop-mixin.js +433 -0
- package/src/vaadin-grid-dynamic-columns-mixin.js +180 -0
- package/src/vaadin-grid-event-context-mixin.d.ts +33 -0
- package/src/vaadin-grid-event-context-mixin.js +57 -0
- package/src/vaadin-grid-filter-column.d.ts +35 -0
- package/src/vaadin-grid-filter-column.js +120 -0
- package/src/vaadin-grid-filter-mixin.js +76 -0
- package/src/vaadin-grid-filter.d.ts +67 -0
- package/src/vaadin-grid-filter.js +125 -0
- package/src/vaadin-grid-helpers.js +23 -0
- package/src/vaadin-grid-keyboard-navigation-mixin.js +891 -0
- package/src/vaadin-grid-row-details-mixin.d.ts +44 -0
- package/src/vaadin-grid-row-details-mixin.js +200 -0
- package/src/vaadin-grid-scroll-mixin.d.ts +18 -0
- package/src/vaadin-grid-scroll-mixin.js +202 -0
- package/src/vaadin-grid-selection-column.d.ts +71 -0
- package/src/vaadin-grid-selection-column.js +285 -0
- package/src/vaadin-grid-selection-mixin.d.ts +30 -0
- package/src/vaadin-grid-selection-mixin.js +93 -0
- package/src/vaadin-grid-sort-column.d.ts +63 -0
- package/src/vaadin-grid-sort-column.js +118 -0
- package/src/vaadin-grid-sort-mixin.d.ts +15 -0
- package/src/vaadin-grid-sort-mixin.js +139 -0
- package/src/vaadin-grid-sorter.d.ts +94 -0
- package/src/vaadin-grid-sorter.js +230 -0
- package/src/vaadin-grid-styles.js +297 -0
- package/src/vaadin-grid-styling-mixin.d.ts +37 -0
- package/src/vaadin-grid-styling-mixin.js +71 -0
- package/src/vaadin-grid-tree-column.d.ts +36 -0
- package/src/vaadin-grid-tree-column.js +119 -0
- package/src/vaadin-grid-tree-toggle.d.ts +104 -0
- package/src/vaadin-grid-tree-toggle.js +205 -0
- package/src/vaadin-grid.d.ts +397 -0
- package/src/vaadin-grid.js +1004 -0
- package/theme/lumo/all-imports.js +11 -0
- package/theme/lumo/vaadin-grid-column-group.js +1 -0
- package/theme/lumo/vaadin-grid-column.js +1 -0
- package/theme/lumo/vaadin-grid-filter-column.js +2 -0
- package/theme/lumo/vaadin-grid-filter.js +2 -0
- package/theme/lumo/vaadin-grid-selection-column.js +2 -0
- package/theme/lumo/vaadin-grid-sort-column.js +2 -0
- package/theme/lumo/vaadin-grid-sorter-styles.js +53 -0
- package/theme/lumo/vaadin-grid-sorter.js +2 -0
- package/theme/lumo/vaadin-grid-styles.js +378 -0
- package/theme/lumo/vaadin-grid-tree-column.js +2 -0
- package/theme/lumo/vaadin-grid-tree-toggle-styles.js +112 -0
- package/theme/lumo/vaadin-grid-tree-toggle.js +2 -0
- package/theme/lumo/vaadin-grid.js +9 -0
- package/theme/material/all-imports.js +11 -0
- package/theme/material/vaadin-grid-column-group.js +1 -0
- package/theme/material/vaadin-grid-column.js +1 -0
- package/theme/material/vaadin-grid-filter-column.js +2 -0
- package/theme/material/vaadin-grid-filter.js +2 -0
- package/theme/material/vaadin-grid-selection-column.js +2 -0
- package/theme/material/vaadin-grid-sort-column.js +2 -0
- package/theme/material/vaadin-grid-sorter-styles.js +72 -0
- package/theme/material/vaadin-grid-sorter.js +2 -0
- package/theme/material/vaadin-grid-styles.js +252 -0
- package/theme/material/vaadin-grid-tree-column.js +2 -0
- package/theme/material/vaadin-grid-tree-toggle-styles.js +42 -0
- package/theme/material/vaadin-grid-tree-toggle.js +2 -0
- package/theme/material/vaadin-grid.js +2 -0
- package/vaadin-grid-column-group.d.ts +1 -0
- package/vaadin-grid-column-group.js +3 -0
- package/vaadin-grid-column.d.ts +1 -0
- package/vaadin-grid-column.js +3 -0
- package/vaadin-grid-filter-column.d.ts +1 -0
- package/vaadin-grid-filter-column.js +3 -0
- package/vaadin-grid-filter.d.ts +1 -0
- package/vaadin-grid-filter.js +3 -0
- package/vaadin-grid-selection-column.d.ts +1 -0
- package/vaadin-grid-selection-column.js +3 -0
- package/vaadin-grid-sort-column.d.ts +1 -0
- package/vaadin-grid-sort-column.js +3 -0
- package/vaadin-grid-sorter.d.ts +1 -0
- package/vaadin-grid-sorter.js +3 -0
- package/vaadin-grid-tree-column.d.ts +1 -0
- package/vaadin-grid-tree-column.js +3 -0
- package/vaadin-grid-tree-toggle.d.ts +1 -0
- package/vaadin-grid-tree-toggle.js +3 -0
- package/vaadin-grid.d.ts +3 -0
- package/vaadin-grid.js +4 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2021 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
import { GridColumn } from './vaadin-grid-column.js';
|
|
7
|
+
import '@vaadin/checkbox/src/vaadin-checkbox.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* `<vaadin-grid-selection-column>` is a helper element for the `<vaadin-grid>`
|
|
11
|
+
* that provides default renderers and functionality for item selection.
|
|
12
|
+
*
|
|
13
|
+
* #### Example:
|
|
14
|
+
* ```html
|
|
15
|
+
* <vaadin-grid items="[[items]]">
|
|
16
|
+
* <vaadin-grid-selection-column frozen auto-select></vaadin-grid-selection-column>
|
|
17
|
+
*
|
|
18
|
+
* <vaadin-grid-column>
|
|
19
|
+
* ...
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* By default the selection column displays `<vaadin-checkbox>` elements in the
|
|
23
|
+
* column cells. The checkboxes in the body rows toggle selection of the corresponding row items.
|
|
24
|
+
*
|
|
25
|
+
* When the grid data is provided as an array of [`items`](#/elements/vaadin-grid#property-items),
|
|
26
|
+
* the column header gets an additional checkbox that can be used for toggling
|
|
27
|
+
* selection for all the items at once.
|
|
28
|
+
*
|
|
29
|
+
* __The default content can also be overridden__
|
|
30
|
+
*
|
|
31
|
+
* @fires {CustomEvent} select-all-changed - Fired when the `selectAll` property changes.
|
|
32
|
+
*/
|
|
33
|
+
class GridSelectionColumn extends GridColumn {
|
|
34
|
+
static get is() {
|
|
35
|
+
return 'vaadin-grid-selection-column';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static get properties() {
|
|
39
|
+
return {
|
|
40
|
+
/**
|
|
41
|
+
* Width of the cells for this column.
|
|
42
|
+
*/
|
|
43
|
+
width: {
|
|
44
|
+
type: String,
|
|
45
|
+
value: '58px'
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Flex grow ratio for the cell widths. When set to 0, cell width is fixed.
|
|
50
|
+
* @attr {number} flex-grow
|
|
51
|
+
* @type {number}
|
|
52
|
+
*/
|
|
53
|
+
flexGrow: {
|
|
54
|
+
type: Number,
|
|
55
|
+
value: 0
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* When true, all the items are selected.
|
|
60
|
+
* @attr {boolean} select-all
|
|
61
|
+
* @type {boolean}
|
|
62
|
+
*/
|
|
63
|
+
selectAll: {
|
|
64
|
+
type: Boolean,
|
|
65
|
+
value: false,
|
|
66
|
+
notify: true
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* When true, the active gets automatically selected.
|
|
71
|
+
* @attr {boolean} auto-select
|
|
72
|
+
* @type {boolean}
|
|
73
|
+
*/
|
|
74
|
+
autoSelect: {
|
|
75
|
+
type: Boolean,
|
|
76
|
+
value: false
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
/** @private */
|
|
80
|
+
__indeterminate: Boolean,
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The previous state of activeItem. When activeItem turns to `null`,
|
|
84
|
+
* previousActiveItem will have an Object with just unselected activeItem
|
|
85
|
+
* @private
|
|
86
|
+
*/
|
|
87
|
+
__previousActiveItem: Object,
|
|
88
|
+
|
|
89
|
+
/** @private */
|
|
90
|
+
__selectAllHidden: Boolean
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static get observers() {
|
|
95
|
+
return [
|
|
96
|
+
'__onSelectAllChanged(selectAll)',
|
|
97
|
+
'_onHeaderRendererOrBindingChanged(_headerRenderer, _headerCell, path, header, selectAll, __indeterminate, __selectAllHidden)'
|
|
98
|
+
];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
constructor() {
|
|
102
|
+
super();
|
|
103
|
+
|
|
104
|
+
this.__boundOnActiveItemChanged = this.__onActiveItemChanged.bind(this);
|
|
105
|
+
this.__boundOnDataProviderChanged = this.__onDataProviderChanged.bind(this);
|
|
106
|
+
this.__boundOnSelectedItemsChanged = this.__onSelectedItemsChanged.bind(this);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** @protected */
|
|
110
|
+
disconnectedCallback() {
|
|
111
|
+
this._grid.removeEventListener('active-item-changed', this.__boundOnActiveItemChanged);
|
|
112
|
+
this._grid.removeEventListener('data-provider-changed', this.__boundOnDataProviderChanged);
|
|
113
|
+
this._grid.removeEventListener('filter-changed', this.__boundOnSelectedItemsChanged);
|
|
114
|
+
this._grid.removeEventListener('selected-items-changed', this.__boundOnSelectedItemsChanged);
|
|
115
|
+
|
|
116
|
+
super.disconnectedCallback();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** @protected */
|
|
120
|
+
connectedCallback() {
|
|
121
|
+
super.connectedCallback();
|
|
122
|
+
if (this._grid) {
|
|
123
|
+
this._grid.addEventListener('active-item-changed', this.__boundOnActiveItemChanged);
|
|
124
|
+
this._grid.addEventListener('data-provider-changed', this.__boundOnDataProviderChanged);
|
|
125
|
+
this._grid.addEventListener('filter-changed', this.__boundOnSelectedItemsChanged);
|
|
126
|
+
this._grid.addEventListener('selected-items-changed', this.__boundOnSelectedItemsChanged);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Renders the Select All checkbox to the header cell.
|
|
132
|
+
*
|
|
133
|
+
* @override
|
|
134
|
+
*/
|
|
135
|
+
_defaultHeaderRenderer(root, _column) {
|
|
136
|
+
let checkbox = root.firstElementChild;
|
|
137
|
+
if (!checkbox) {
|
|
138
|
+
checkbox = document.createElement('vaadin-checkbox');
|
|
139
|
+
checkbox.setAttribute('aria-label', 'Select All');
|
|
140
|
+
checkbox.classList.add('vaadin-grid-select-all-checkbox');
|
|
141
|
+
checkbox.addEventListener('checked-changed', this.__onSelectAllCheckedChanged.bind(this));
|
|
142
|
+
root.appendChild(checkbox);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const checked = this.__isChecked(this.selectAll, this.__indeterminate);
|
|
146
|
+
checkbox.__rendererChecked = checked;
|
|
147
|
+
checkbox.checked = checked;
|
|
148
|
+
checkbox.hidden = this.__selectAllHidden;
|
|
149
|
+
checkbox.indeterminate = this.__indeterminate;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Renders the Select Row checkbox to the body cell.
|
|
154
|
+
*
|
|
155
|
+
* @override
|
|
156
|
+
*/
|
|
157
|
+
_defaultRenderer(root, _column, { item, selected }) {
|
|
158
|
+
let checkbox = root.firstElementChild;
|
|
159
|
+
if (!checkbox) {
|
|
160
|
+
checkbox = document.createElement('vaadin-checkbox');
|
|
161
|
+
checkbox.setAttribute('aria-label', 'Select Row');
|
|
162
|
+
checkbox.addEventListener('checked-changed', this.__onSelectRowCheckedChanged.bind(this));
|
|
163
|
+
root.appendChild(checkbox);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
checkbox.__item = item;
|
|
167
|
+
checkbox.__rendererChecked = selected;
|
|
168
|
+
checkbox.checked = selected;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** @private */
|
|
172
|
+
__onSelectAllChanged(selectAll) {
|
|
173
|
+
if (selectAll === undefined || !this._grid) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (!this.__selectAllInitialized) {
|
|
178
|
+
// The initial value for selectAll property was applied, avoid clearing pre-selected items
|
|
179
|
+
this.__selectAllInitialized = true;
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (this._selectAllChangeLock) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
this._grid.selectedItems = selectAll && Array.isArray(this._grid.items) ? this.__getRootLevelItems() : [];
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Return true if array `a` contains all the items in `b`
|
|
192
|
+
* We need this when sorting or to preserve selection after filtering.
|
|
193
|
+
* @private
|
|
194
|
+
*/
|
|
195
|
+
__arrayContains(a, b) {
|
|
196
|
+
return Array.isArray(a) && Array.isArray(b) && b.every((i) => a.includes(i));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Enables or disables the Select All mode once the Select All checkbox is switched.
|
|
201
|
+
* The listener handles only user-fired events.
|
|
202
|
+
*
|
|
203
|
+
* @private
|
|
204
|
+
*/
|
|
205
|
+
__onSelectAllCheckedChanged(e) {
|
|
206
|
+
// Skip if the state is changed by the renderer.
|
|
207
|
+
if (e.target.checked === e.target.__rendererChecked) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
this.selectAll = this.__indeterminate || e.target.checked;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Selects or deselects the row once the Select Row checkbox is switched.
|
|
216
|
+
* The listener handles only user-fired events.
|
|
217
|
+
*
|
|
218
|
+
* @private
|
|
219
|
+
*/
|
|
220
|
+
__onSelectRowCheckedChanged(e) {
|
|
221
|
+
// Skip if the state is changed by the renderer.
|
|
222
|
+
if (e.target.checked === e.target.__rendererChecked) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (e.target.checked) {
|
|
227
|
+
this._grid.selectItem(e.target.__item);
|
|
228
|
+
} else {
|
|
229
|
+
this._grid.deselectItem(e.target.__item);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* iOS needs indeterminated + checked at the same time
|
|
235
|
+
* @private
|
|
236
|
+
*/
|
|
237
|
+
__isChecked(selectAll, indeterminate) {
|
|
238
|
+
return indeterminate || selectAll;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** @private */
|
|
242
|
+
__onActiveItemChanged(e) {
|
|
243
|
+
const activeItem = e.detail.value;
|
|
244
|
+
if (this.autoSelect) {
|
|
245
|
+
const item = activeItem || this.__previousActiveItem;
|
|
246
|
+
if (item) {
|
|
247
|
+
this._grid._toggleItem(item);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
this.__previousActiveItem = activeItem;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** @private */
|
|
254
|
+
__getRootLevelItems() {
|
|
255
|
+
const rootCache = this._grid._cache;
|
|
256
|
+
return [...Array(rootCache.size)].map((_, idx) => rootCache.items[idx]);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** @private */
|
|
260
|
+
__onSelectedItemsChanged() {
|
|
261
|
+
this._selectAllChangeLock = true;
|
|
262
|
+
if (Array.isArray(this._grid.items)) {
|
|
263
|
+
if (!this._grid.selectedItems.length) {
|
|
264
|
+
this.selectAll = false;
|
|
265
|
+
this.__indeterminate = false;
|
|
266
|
+
} else if (this.__arrayContains(this._grid.selectedItems, this.__getRootLevelItems())) {
|
|
267
|
+
this.selectAll = true;
|
|
268
|
+
this.__indeterminate = false;
|
|
269
|
+
} else {
|
|
270
|
+
this.selectAll = false;
|
|
271
|
+
this.__indeterminate = true;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
this._selectAllChangeLock = false;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** @private */
|
|
278
|
+
__onDataProviderChanged() {
|
|
279
|
+
this.__selectAllHidden = !Array.isArray(this._grid.items);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
customElements.define(GridSelectionColumn.is, GridSelectionColumn);
|
|
284
|
+
|
|
285
|
+
export { GridSelectionColumn };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
declare function SelectionMixin<TItem, T extends new (...args: any[]) => {}>(
|
|
2
|
+
base: T
|
|
3
|
+
): T & SelectionMixinConstructor<TItem>;
|
|
4
|
+
|
|
5
|
+
interface SelectionMixinConstructor<TItem> {
|
|
6
|
+
new (...args: any[]): SelectionMixin<TItem>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface SelectionMixin<TItem> {
|
|
10
|
+
/**
|
|
11
|
+
* An array that contains the selected items.
|
|
12
|
+
*/
|
|
13
|
+
selectedItems: Array<TItem>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Selects the given item.
|
|
17
|
+
*
|
|
18
|
+
* @param item The item object
|
|
19
|
+
*/
|
|
20
|
+
selectItem(item: TItem): void;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Deselects the given item if it is already selected.
|
|
24
|
+
*
|
|
25
|
+
* @param item The item object
|
|
26
|
+
*/
|
|
27
|
+
deselectItem(item: TItem): void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { SelectionMixin, SelectionMixinConstructor };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2021 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @polymerMixin
|
|
9
|
+
*/
|
|
10
|
+
export const SelectionMixin = (superClass) =>
|
|
11
|
+
class SelectionMixin extends superClass {
|
|
12
|
+
static get properties() {
|
|
13
|
+
return {
|
|
14
|
+
/**
|
|
15
|
+
* An array that contains the selected items.
|
|
16
|
+
* @type {!Array<!GridItem>}
|
|
17
|
+
*/
|
|
18
|
+
selectedItems: {
|
|
19
|
+
type: Object,
|
|
20
|
+
notify: true,
|
|
21
|
+
value: () => []
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static get observers() {
|
|
27
|
+
return ['_selectedItemsChanged(selectedItems.*)'];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {!GridItem} item
|
|
32
|
+
* @return {boolean}
|
|
33
|
+
* @protected
|
|
34
|
+
*/
|
|
35
|
+
_isSelected(item) {
|
|
36
|
+
return this.selectedItems && this._getItemIndexInArray(item, this.selectedItems) > -1;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Selects the given item.
|
|
41
|
+
*
|
|
42
|
+
* @method selectItem
|
|
43
|
+
* @param {!GridItem} item The item object
|
|
44
|
+
*/
|
|
45
|
+
selectItem(item) {
|
|
46
|
+
if (!this._isSelected(item)) {
|
|
47
|
+
this.selectedItems = [...this.selectedItems, item];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Deselects the given item if it is already selected.
|
|
53
|
+
*
|
|
54
|
+
* @method deselect
|
|
55
|
+
* @param {!GridItem} item The item object
|
|
56
|
+
*/
|
|
57
|
+
deselectItem(item) {
|
|
58
|
+
if (this._isSelected(item)) {
|
|
59
|
+
this.selectedItems = this.selectedItems.filter((i) => !this._itemsEqual(i, item));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Toggles the selected state of the given item.
|
|
65
|
+
*
|
|
66
|
+
* @method toggle
|
|
67
|
+
* @param {!GridItem} item The item object
|
|
68
|
+
* @protected
|
|
69
|
+
*/
|
|
70
|
+
_toggleItem(item) {
|
|
71
|
+
const index = this._getItemIndexInArray(item, this.selectedItems);
|
|
72
|
+
if (index === -1) {
|
|
73
|
+
this.selectItem(item);
|
|
74
|
+
} else {
|
|
75
|
+
this.deselectItem(item);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** @private */
|
|
80
|
+
_selectedItemsChanged(e) {
|
|
81
|
+
if (this.$.items.children.length && (e.path === 'selectedItems' || e.path === 'selectedItems.splices')) {
|
|
82
|
+
Array.from(this.$.items.children).forEach((row) => {
|
|
83
|
+
this._updateItem(row, row._item);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Fired when the `selectedItems` property changes.
|
|
90
|
+
*
|
|
91
|
+
* @event selected-items-changed
|
|
92
|
+
*/
|
|
93
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { GridDefaultItem, GridSorterDirection } from './interfaces';
|
|
2
|
+
|
|
3
|
+
import { GridColumn } from './vaadin-grid-column.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Fired when the `direction` property changes.
|
|
7
|
+
*/
|
|
8
|
+
export type GridSortColumnDirectionChangedEvent = CustomEvent<{ value: GridSorterDirection }>;
|
|
9
|
+
|
|
10
|
+
export interface GridSortColumnCustomEventMap {
|
|
11
|
+
'direction-changed': GridSortColumnDirectionChangedEvent;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface GridSortColumnEventMap extends HTMLElementEventMap, GridSortColumnCustomEventMap {}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* `<vaadin-grid-sort-column>` is a helper element for the `<vaadin-grid>`
|
|
18
|
+
* that provides default header renderer and functionality for sorting.
|
|
19
|
+
*
|
|
20
|
+
* #### Example:
|
|
21
|
+
* ```html
|
|
22
|
+
* <vaadin-grid items="[[items]]">
|
|
23
|
+
* <vaadin-grid-sort-column path="name.first" direction="asc"></vaadin-grid-sort-column>
|
|
24
|
+
*
|
|
25
|
+
* <vaadin-grid-column>
|
|
26
|
+
* ...
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @fires {CustomEvent} direction-changed - Fired when the `direction` property changes.
|
|
30
|
+
*/
|
|
31
|
+
declare class GridSortColumn<TItem = GridDefaultItem> extends GridColumn<TItem> {
|
|
32
|
+
/**
|
|
33
|
+
* JS Path of the property in the item used for sorting the data.
|
|
34
|
+
*/
|
|
35
|
+
path: string | null | undefined;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* How to sort the data.
|
|
39
|
+
* Possible values are `asc` to use an ascending algorithm, `desc` to sort the data in
|
|
40
|
+
* descending direction, or `null` for not sorting the data.
|
|
41
|
+
*/
|
|
42
|
+
direction: GridSorterDirection | null | undefined;
|
|
43
|
+
|
|
44
|
+
addEventListener<K extends keyof GridSortColumnEventMap>(
|
|
45
|
+
type: K,
|
|
46
|
+
listener: (this: GridSortColumn<TItem>, ev: GridSortColumnEventMap[K]) => void,
|
|
47
|
+
options?: boolean | AddEventListenerOptions
|
|
48
|
+
): void;
|
|
49
|
+
|
|
50
|
+
removeEventListener<K extends keyof GridSortColumnEventMap>(
|
|
51
|
+
type: K,
|
|
52
|
+
listener: (this: GridSortColumn<TItem>, ev: GridSortColumnEventMap[K]) => void,
|
|
53
|
+
options?: boolean | EventListenerOptions
|
|
54
|
+
): void;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
declare global {
|
|
58
|
+
interface HTMLElementTagNameMap {
|
|
59
|
+
'vaadin-grid-sort-column': GridSortColumn<GridDefaultItem>;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export { GridSortColumn };
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2021 Vaadin Ltd.
|
|
4
|
+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
+
*/
|
|
6
|
+
import { GridColumn } from './vaadin-grid-column.js';
|
|
7
|
+
import './vaadin-grid-sorter.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* `<vaadin-grid-sort-column>` is a helper element for the `<vaadin-grid>`
|
|
11
|
+
* that provides default header renderer and functionality for sorting.
|
|
12
|
+
*
|
|
13
|
+
* #### Example:
|
|
14
|
+
* ```html
|
|
15
|
+
* <vaadin-grid items="[[items]]">
|
|
16
|
+
* <vaadin-grid-sort-column path="name.first" direction="asc"></vaadin-grid-sort-column>
|
|
17
|
+
*
|
|
18
|
+
* <vaadin-grid-column>
|
|
19
|
+
* ...
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @fires {CustomEvent} direction-changed - Fired when the `direction` property changes.
|
|
23
|
+
*/
|
|
24
|
+
class GridSortColumn extends GridColumn {
|
|
25
|
+
static get is() {
|
|
26
|
+
return 'vaadin-grid-sort-column';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static get properties() {
|
|
30
|
+
return {
|
|
31
|
+
/**
|
|
32
|
+
* JS Path of the property in the item used for sorting the data.
|
|
33
|
+
*/
|
|
34
|
+
path: String,
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* How to sort the data.
|
|
38
|
+
* Possible values are `asc` to use an ascending algorithm, `desc` to sort the data in
|
|
39
|
+
* descending direction, or `null` for not sorting the data.
|
|
40
|
+
* @type {GridSorterDirection | undefined}
|
|
41
|
+
*/
|
|
42
|
+
direction: {
|
|
43
|
+
type: String,
|
|
44
|
+
notify: true
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static get observers() {
|
|
50
|
+
return ['_onHeaderRendererOrBindingChanged(_headerRenderer, _headerCell, path, header, direction)'];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
constructor() {
|
|
54
|
+
super();
|
|
55
|
+
|
|
56
|
+
this.__boundOnDirectionChanged = this.__onDirectionChanged.bind(this);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Renders the grid sorter to the header cell.
|
|
61
|
+
*
|
|
62
|
+
* @override
|
|
63
|
+
*/
|
|
64
|
+
_defaultHeaderRenderer(root, _column) {
|
|
65
|
+
let sorter = root.firstElementChild;
|
|
66
|
+
if (!sorter) {
|
|
67
|
+
sorter = document.createElement('vaadin-grid-sorter');
|
|
68
|
+
sorter.addEventListener('direction-changed', this.__boundOnDirectionChanged);
|
|
69
|
+
root.appendChild(sorter);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
sorter.path = this.path;
|
|
73
|
+
sorter.__rendererDirection = this.direction;
|
|
74
|
+
sorter.direction = this.direction;
|
|
75
|
+
sorter.textContent = this.__getHeader(this.header, this.path);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The sort column doesn't allow to use a custom header renderer
|
|
80
|
+
* to override the header cell content.
|
|
81
|
+
* It always renders the grid sorter to the header cell.
|
|
82
|
+
*
|
|
83
|
+
* @override
|
|
84
|
+
*/
|
|
85
|
+
_computeHeaderRenderer() {
|
|
86
|
+
return this._defaultHeaderRenderer;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Updates the sorting direction once the grid sorter's direction is changed.
|
|
91
|
+
* The listener handles only user-fired events.
|
|
92
|
+
*
|
|
93
|
+
* @private
|
|
94
|
+
*/
|
|
95
|
+
__onDirectionChanged(e) {
|
|
96
|
+
// Skip if the direction is changed by the renderer.
|
|
97
|
+
if (e.detail.value === e.target.__rendererDirection) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
this.direction = e.detail.value;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** @private */
|
|
105
|
+
__getHeader(header, path) {
|
|
106
|
+
if (header) {
|
|
107
|
+
return header;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (path) {
|
|
111
|
+
return this._generateHeader(path);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
customElements.define(GridSortColumn.is, GridSortColumn);
|
|
117
|
+
|
|
118
|
+
export { GridSortColumn };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare function SortMixin<T extends new (...args: any[]) => {}>(base: T): T & SortMixinConstructor;
|
|
2
|
+
|
|
3
|
+
interface SortMixinConstructor {
|
|
4
|
+
new (...args: any[]): SortMixin;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface SortMixin {
|
|
8
|
+
/**
|
|
9
|
+
* When `true`, all `<vaadin-grid-sorter>` are applied for sorting.
|
|
10
|
+
* @attr {boolean} multi-sort
|
|
11
|
+
*/
|
|
12
|
+
multiSort: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { SortMixin, SortMixinConstructor };
|