@vaadin/grid 24.2.0-alpha1 → 24.2.0-alpha10

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.
@@ -0,0 +1,342 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2016 - 2023 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { addListener } from '@vaadin/component-base/src/gestures.js';
7
+
8
+ /**
9
+ * A mixin that provides basic functionality for the
10
+ * `<vaadin-grid-selection-column>`. This includes properties, cell rendering,
11
+ * and overridable methods for handling changes to the selection state.
12
+ *
13
+ * **NOTE**: This mixin is re-used by the Flow component, and as such must not
14
+ * implement any selection state updates for the column element or the grid.
15
+ * Web component-specific selection state updates must be implemented in the
16
+ * `<vaadin-grid-selection-column>` itself, by overriding the protected methods
17
+ * provided by this mixin.
18
+ *
19
+ * @polymerMixin
20
+ */
21
+ export const GridSelectionColumnBaseMixin = (superClass) =>
22
+ class GridSelectionColumnBaseMixin extends superClass {
23
+ static get properties() {
24
+ return {
25
+ /**
26
+ * Width of the cells for this column.
27
+ */
28
+ width: {
29
+ type: String,
30
+ value: '58px',
31
+ },
32
+
33
+ /**
34
+ * Flex grow ratio for the cell widths. When set to 0, cell width is fixed.
35
+ * @attr {number} flex-grow
36
+ * @type {number}
37
+ */
38
+ flexGrow: {
39
+ type: Number,
40
+ value: 0,
41
+ },
42
+
43
+ /**
44
+ * When true, all the items are selected.
45
+ * @attr {boolean} select-all
46
+ * @type {boolean}
47
+ */
48
+ selectAll: {
49
+ type: Boolean,
50
+ value: false,
51
+ notify: true,
52
+ },
53
+
54
+ /**
55
+ * When true, the active gets automatically selected.
56
+ * @attr {boolean} auto-select
57
+ * @type {boolean}
58
+ */
59
+ autoSelect: {
60
+ type: Boolean,
61
+ value: false,
62
+ },
63
+
64
+ /**
65
+ * When true, rows can be selected by dragging over the selection column.
66
+ * @attr {boolean} drag-select
67
+ * @type {boolean}
68
+ */
69
+ dragSelect: {
70
+ type: Boolean,
71
+ value: false,
72
+ },
73
+
74
+ /** @protected */
75
+ _indeterminate: Boolean,
76
+
77
+ /** @protected */
78
+ _selectAllHidden: Boolean,
79
+ };
80
+ }
81
+
82
+ static get observers() {
83
+ return [
84
+ '_onHeaderRendererOrBindingChanged(_headerRenderer, _headerCell, path, header, selectAll, _indeterminate, _selectAllHidden)',
85
+ ];
86
+ }
87
+
88
+ /**
89
+ * Renders the Select All checkbox to the header cell.
90
+ *
91
+ * @override
92
+ */
93
+ _defaultHeaderRenderer(root, _column) {
94
+ let checkbox = root.firstElementChild;
95
+ if (!checkbox) {
96
+ checkbox = document.createElement('vaadin-checkbox');
97
+ checkbox.setAttribute('aria-label', 'Select All');
98
+ checkbox.classList.add('vaadin-grid-select-all-checkbox');
99
+ root.appendChild(checkbox);
100
+ // Add listener after appending, so we can skip the initial change event
101
+ checkbox.addEventListener('checked-changed', this.__onSelectAllCheckedChanged.bind(this));
102
+ }
103
+
104
+ const checked = this.__isChecked(this.selectAll, this._indeterminate);
105
+ checkbox.__rendererChecked = checked;
106
+ checkbox.checked = checked;
107
+ checkbox.hidden = this._selectAllHidden;
108
+ checkbox.indeterminate = this._indeterminate;
109
+ }
110
+
111
+ /**
112
+ * Renders the Select Row checkbox to the body cell.
113
+ *
114
+ * @override
115
+ */
116
+ _defaultRenderer(root, _column, { item, selected }) {
117
+ let checkbox = root.firstElementChild;
118
+ if (!checkbox) {
119
+ checkbox = document.createElement('vaadin-checkbox');
120
+ checkbox.setAttribute('aria-label', 'Select Row');
121
+ root.appendChild(checkbox);
122
+ // Add listener after appending, so we can skip the initial change event
123
+ checkbox.addEventListener('checked-changed', this.__onSelectRowCheckedChanged.bind(this));
124
+ addListener(root, 'track', this.__onCellTrack.bind(this));
125
+ root.addEventListener('mousedown', this.__onCellMouseDown.bind(this));
126
+ root.addEventListener('click', this.__onCellClick.bind(this));
127
+ }
128
+
129
+ checkbox.__item = item;
130
+ checkbox.__rendererChecked = selected;
131
+ checkbox.checked = selected;
132
+ }
133
+
134
+ /**
135
+ * Updates the select all state when the Select All checkbox is switched.
136
+ * The listener handles only user-fired events.
137
+ *
138
+ * @private
139
+ */
140
+ __onSelectAllCheckedChanged(e) {
141
+ // Skip if the state is changed by the renderer.
142
+ if (e.target.checked === e.target.__rendererChecked) {
143
+ return;
144
+ }
145
+
146
+ if (this._indeterminate || e.target.checked) {
147
+ this._selectAll();
148
+ } else {
149
+ this._deselectAll();
150
+ }
151
+ }
152
+
153
+ /**
154
+ * Selects or deselects the row when the Select Row checkbox is switched.
155
+ * The listener handles only user-fired events.
156
+ *
157
+ * @private
158
+ */
159
+ __onSelectRowCheckedChanged(e) {
160
+ // Skip if the state is changed by the renderer.
161
+ if (e.target.checked === e.target.__rendererChecked) {
162
+ return;
163
+ }
164
+
165
+ if (e.target.checked) {
166
+ this._selectItem(e.target.__item);
167
+ } else {
168
+ this._deselectItem(e.target.__item);
169
+ }
170
+ }
171
+
172
+ /** @private */
173
+ __onCellTrack(event) {
174
+ if (!this.dragSelect) {
175
+ return;
176
+ }
177
+ this.__dragCurrentY = event.detail.y;
178
+ this.__dragDy = event.detail.dy;
179
+ if (event.detail.state === 'start') {
180
+ const renderedRows = this._grid._getRenderedRows();
181
+ // Get the row where the drag started
182
+ const dragStartRow = renderedRows.find((row) => row.contains(event.currentTarget.assignedSlot));
183
+ // Whether to select or deselect the items on drag
184
+ this.__dragSelect = !this._grid._isSelected(dragStartRow._item);
185
+ // Store the index of the row where the drag started
186
+ this.__dragStartIndex = dragStartRow.index;
187
+ // Store the item of the row where the drag started
188
+ this.__dragStartItem = dragStartRow._item;
189
+ // Start the auto scroller
190
+ this.__dragAutoScroller();
191
+ } else if (event.detail.state === 'end') {
192
+ // if drag start and end stays within the same item, then toggle its state
193
+ if (this.__dragStartItem) {
194
+ if (this.__dragSelect) {
195
+ this._selectItem(this.__dragStartItem);
196
+ } else {
197
+ this._deselectItem(this.__dragStartItem);
198
+ }
199
+ }
200
+ // clear drag state after timeout, which allows preventing the
201
+ // subsequent click event if drag started and ended on the same item
202
+ setTimeout(() => {
203
+ this.__dragStartIndex = undefined;
204
+ });
205
+ }
206
+ }
207
+
208
+ /** @private */
209
+ __onCellMouseDown(e) {
210
+ if (this.dragSelect) {
211
+ // Prevent text selection when starting to drag
212
+ e.preventDefault();
213
+ }
214
+ }
215
+
216
+ /** @private */
217
+ __onCellClick(e) {
218
+ if (this.__dragStartIndex !== undefined) {
219
+ // Stop the click event if drag was enabled. This click event should
220
+ // only occur if drag started and stopped on the same item. In that case
221
+ // the selection state has already been toggled on drag end, and we
222
+ // don't want to toggle it again from clicking the checkbox or changing
223
+ // the active item.
224
+ e.preventDefault();
225
+ }
226
+ }
227
+
228
+ /** @private */
229
+ __dragAutoScroller() {
230
+ if (this.__dragStartIndex === undefined) {
231
+ return;
232
+ }
233
+ // Get the row being hovered over
234
+ const renderedRows = this._grid._getRenderedRows();
235
+ const hoveredRow = renderedRows.find((row) => {
236
+ const rowRect = row.getBoundingClientRect();
237
+ return this.__dragCurrentY >= rowRect.top && this.__dragCurrentY <= rowRect.bottom;
238
+ });
239
+
240
+ // Get the index of the row being hovered over or the first/last
241
+ // visible row if hovering outside the grid
242
+ let hoveredIndex = hoveredRow ? hoveredRow.index : undefined;
243
+ const scrollableArea = this.__getScrollableArea();
244
+ if (this.__dragCurrentY < scrollableArea.top) {
245
+ hoveredIndex = this._grid._firstVisibleIndex;
246
+ } else if (this.__dragCurrentY > scrollableArea.bottom) {
247
+ hoveredIndex = this._grid._lastVisibleIndex;
248
+ }
249
+
250
+ if (hoveredIndex !== undefined) {
251
+ // Select all items between the start and the current row
252
+ renderedRows.forEach((row) => {
253
+ if (
254
+ (hoveredIndex > this.__dragStartIndex && row.index >= this.__dragStartIndex && row.index <= hoveredIndex) ||
255
+ (hoveredIndex < this.__dragStartIndex && row.index <= this.__dragStartIndex && row.index >= hoveredIndex)
256
+ ) {
257
+ if (this.__dragSelect) {
258
+ this._selectItem(row._item);
259
+ } else {
260
+ this._deselectItem(row._item);
261
+ }
262
+ this.__dragStartItem = undefined;
263
+ }
264
+ });
265
+ }
266
+
267
+ // Start scrolling in the top/bottom 15% of the scrollable area
268
+ const scrollTriggerArea = scrollableArea.height * 0.15;
269
+ // Maximum number of pixels to scroll per iteration
270
+ const maxScrollAmount = 10;
271
+
272
+ if (this.__dragDy < 0 && this.__dragCurrentY < scrollableArea.top + scrollTriggerArea) {
273
+ const dy = scrollableArea.top + scrollTriggerArea - this.__dragCurrentY;
274
+ const percentage = Math.min(1, dy / scrollTriggerArea);
275
+ this._grid.$.table.scrollTop -= percentage * maxScrollAmount;
276
+ }
277
+ if (this.__dragDy > 0 && this.__dragCurrentY > scrollableArea.bottom - scrollTriggerArea) {
278
+ const dy = this.__dragCurrentY - (scrollableArea.bottom - scrollTriggerArea);
279
+ const percentage = Math.min(1, dy / scrollTriggerArea);
280
+ this._grid.$.table.scrollTop += percentage * maxScrollAmount;
281
+ }
282
+
283
+ // Schedule the next auto scroll
284
+ setTimeout(() => this.__dragAutoScroller(), 10);
285
+ }
286
+
287
+ /**
288
+ * Gets the scrollable area of the grid as a bounding client rect. The
289
+ * scrollable area is the bounding rect of the grid minus the header and
290
+ * footer.
291
+ *
292
+ * @private
293
+ */
294
+ __getScrollableArea() {
295
+ const gridRect = this._grid.$.table.getBoundingClientRect();
296
+ const headerRect = this._grid.$.header.getBoundingClientRect();
297
+ const footerRect = this._grid.$.footer.getBoundingClientRect();
298
+
299
+ return {
300
+ top: gridRect.top + headerRect.height,
301
+ bottom: gridRect.bottom - footerRect.height,
302
+ left: gridRect.left,
303
+ right: gridRect.right,
304
+ height: gridRect.height - headerRect.height - footerRect.height,
305
+ width: gridRect.width,
306
+ };
307
+ }
308
+
309
+ /**
310
+ * Override to handle the user selecting all items.
311
+ * @protected
312
+ */
313
+ _selectAll() {}
314
+
315
+ /**
316
+ * Override to handle the user deselecting all items.
317
+ * @protected
318
+ */
319
+ _deselectAll() {}
320
+
321
+ /**
322
+ * Override to handle the user selecting an item.
323
+ * @param {Object} item the item to select
324
+ * @protected
325
+ */
326
+ _selectItem(item) {}
327
+
328
+ /**
329
+ * Override to handle the user deselecting an item.
330
+ * @param {Object} item the item to deselect
331
+ * @protected
332
+ */
333
+ _deselectItem(item) {}
334
+
335
+ /**
336
+ * IOS needs indeterminate + checked at the same time
337
+ * @private
338
+ */
339
+ __isChecked(selectAll, indeterminate) {
340
+ return indeterminate || selectAll;
341
+ }
342
+ };
@@ -5,6 +5,7 @@
5
5
  */
6
6
  import type { GridDefaultItem } from './vaadin-grid.js';
7
7
  import { GridColumn } from './vaadin-grid-column.js';
8
+ import type { GridSelectionColumnBaseMixinClass } from './vaadin-grid-selection-column-base-mixin.js';
8
9
 
9
10
  /**
10
11
  * Fired when the `selectAll` property changes.
@@ -42,18 +43,6 @@ export interface GridSelectionColumnEventMap extends HTMLElementEventMap, GridSe
42
43
  * @fires {CustomEvent} select-all-changed - Fired when the `selectAll` property changes.
43
44
  */
44
45
  declare class GridSelectionColumn<TItem = GridDefaultItem> extends GridColumn<TItem> {
45
- /**
46
- * When true, all the items are selected.
47
- * @attr {boolean} select-all
48
- */
49
- selectAll: boolean;
50
-
51
- /**
52
- * When true, the active gets automatically selected.
53
- * @attr {boolean} auto-select
54
- */
55
- autoSelect: boolean;
56
-
57
46
  addEventListener<K extends keyof GridSelectionColumnEventMap>(
58
47
  type: K,
59
48
  listener: (this: GridSelectionColumn<TItem>, ev: GridSelectionColumnEventMap[K]) => void,
@@ -67,6 +56,8 @@ declare class GridSelectionColumn<TItem = GridDefaultItem> extends GridColumn<TI
67
56
  ): void;
68
57
  }
69
58
 
59
+ interface GridSelectionColumn<TItem = GridDefaultItem> extends GridSelectionColumnBaseMixinClass<TItem> {}
60
+
70
61
  declare global {
71
62
  interface HTMLElementTagNameMap {
72
63
  'vaadin-grid-selection-column': GridSelectionColumn<GridDefaultItem>;
@@ -5,6 +5,7 @@
5
5
  */
6
6
  import '@vaadin/checkbox/src/vaadin-checkbox.js';
7
7
  import { GridColumn } from './vaadin-grid-column.js';
8
+ import { GridSelectionColumnBaseMixin } from './vaadin-grid-selection-column-base-mixin.js';
8
9
 
9
10
  /**
10
11
  * `<vaadin-grid-selection-column>` is a helper element for the `<vaadin-grid>`
@@ -28,74 +29,27 @@ import { GridColumn } from './vaadin-grid-column.js';
28
29
  *
29
30
  * __The default content can also be overridden__
30
31
  *
32
+ * @mixes GridSelectionColumnBaseMixin
31
33
  * @fires {CustomEvent} select-all-changed - Fired when the `selectAll` property changes.
32
34
  */
33
- class GridSelectionColumn extends GridColumn {
35
+ class GridSelectionColumn extends GridSelectionColumnBaseMixin(GridColumn) {
34
36
  static get is() {
35
37
  return 'vaadin-grid-selection-column';
36
38
  }
37
39
 
38
40
  static get properties() {
39
41
  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
42
  /**
83
43
  * The previous state of activeItem. When activeItem turns to `null`,
84
44
  * previousActiveItem will have an Object with just unselected activeItem
85
45
  * @private
86
46
  */
87
47
  __previousActiveItem: Object,
88
-
89
- /** @private */
90
- __selectAllHidden: Boolean,
91
48
  };
92
49
  }
93
50
 
94
51
  static get observers() {
95
- return [
96
- '__onSelectAllChanged(selectAll)',
97
- '_onHeaderRendererOrBindingChanged(_headerRenderer, _headerCell, path, header, selectAll, __indeterminate, __selectAllHidden)',
98
- ];
52
+ return ['__onSelectAllChanged(selectAll)'];
99
53
  }
100
54
 
101
55
  constructor() {
@@ -127,47 +81,6 @@ class GridSelectionColumn extends GridColumn {
127
81
  }
128
82
  }
129
83
 
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
84
  /** @private */
172
85
  __onSelectAllChanged(selectAll) {
173
86
  if (selectAll === undefined || !this._grid) {
@@ -203,45 +116,49 @@ class GridSelectionColumn extends GridColumn {
203
116
  }
204
117
 
205
118
  /**
206
- * Enables or disables the Select All mode once the Select All checkbox is switched.
207
- * The listener handles only user-fired events.
119
+ * Override a method from `GridSelectionColumnBaseMixin` to handle the user
120
+ * selecting all items.
208
121
  *
209
- * @private
122
+ * @protected
123
+ * @override
210
124
  */
211
- __onSelectAllCheckedChanged(e) {
212
- // Skip if the state is changed by the renderer.
213
- if (e.target.checked === e.target.__rendererChecked) {
214
- return;
215
- }
216
-
217
- this.selectAll = this.__indeterminate || e.target.checked;
125
+ _selectAll() {
126
+ this.selectAll = true;
218
127
  }
219
128
 
220
129
  /**
221
- * Selects or deselects the row once the Select Row checkbox is switched.
222
- * The listener handles only user-fired events.
130
+ * Override a method from `GridSelectionColumnBaseMixin` to handle the user
131
+ * deselecting all items.
223
132
  *
224
- * @private
133
+ * @protected
134
+ * @override
225
135
  */
226
- __onSelectRowCheckedChanged(e) {
227
- // Skip if the state is changed by the renderer.
228
- if (e.target.checked === e.target.__rendererChecked) {
229
- return;
230
- }
136
+ _deselectAll() {
137
+ this.selectAll = false;
138
+ }
231
139
 
232
- if (e.target.checked) {
233
- this._grid.selectItem(e.target.__item);
234
- } else {
235
- this._grid.deselectItem(e.target.__item);
236
- }
140
+ /**
141
+ * Override a method from `GridSelectionColumnBaseMixin` to handle the user
142
+ * selecting an item.
143
+ *
144
+ * @param {Object} item the item to select
145
+ * @protected
146
+ * @override
147
+ */
148
+ _selectItem(item) {
149
+ this._grid.selectItem(item);
237
150
  }
238
151
 
239
152
  /**
240
- * IOS needs indeterminate + checked at the same time
241
- * @private
153
+ * Override a method from `GridSelectionColumnBaseMixin` to handle the user
154
+ * deselecting an item.
155
+ *
156
+ * @param {Object} item the item to deselect
157
+ * @protected
158
+ * @override
242
159
  */
243
- __isChecked(selectAll, indeterminate) {
244
- return indeterminate || selectAll;
160
+ _deselectItem(item) {
161
+ this._grid.deselectItem(item);
245
162
  }
246
163
 
247
164
  /** @private */
@@ -268,13 +185,13 @@ class GridSelectionColumn extends GridColumn {
268
185
  this.__withFilteredItemsArray((items) => {
269
186
  if (!this._grid.selectedItems.length) {
270
187
  this.selectAll = false;
271
- this.__indeterminate = false;
188
+ this._indeterminate = false;
272
189
  } else if (this.__arrayContains(this._grid.selectedItems, items)) {
273
190
  this.selectAll = true;
274
- this.__indeterminate = false;
191
+ this._indeterminate = false;
275
192
  } else {
276
193
  this.selectAll = false;
277
- this.__indeterminate = true;
194
+ this._indeterminate = true;
278
195
  }
279
196
  });
280
197
  }
@@ -283,7 +200,7 @@ class GridSelectionColumn extends GridColumn {
283
200
 
284
201
  /** @private */
285
202
  __onDataProviderChanged() {
286
- this.__selectAllHidden = !Array.isArray(this._grid.items);
203
+ this._selectAllHidden = !Array.isArray(this._grid.items);
287
204
  }
288
205
 
289
206
  /**
@@ -40,10 +40,7 @@ registerStyles(
40
40
  width: auto;
41
41
  height: auto;
42
42
  position: absolute;
43
- top: 0;
44
- right: 0;
45
- bottom: 0;
46
- left: 0;
43
+ inset: 0;
47
44
  }
48
45
 
49
46
  :host([all-rows-visible]) {
@@ -154,10 +151,7 @@ registerStyles(
154
151
  align-items: inherit;
155
152
  outline: none;
156
153
  position: absolute;
157
- top: 0;
158
- bottom: 0;
159
- left: 0;
160
- right: 0;
154
+ inset: 0;
161
155
  }
162
156
 
163
157
  [part~='details-cell'] {
@@ -4,6 +4,7 @@
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import './vaadin-grid-tree-toggle.js';
7
+ import { get } from '@vaadin/component-base/src/path-utils.js';
7
8
  import { GridColumn } from './vaadin-grid-column.js';
8
9
 
9
10
  /**
@@ -101,7 +102,7 @@ class GridTreeColumn extends GridColumn {
101
102
 
102
103
  /** @private */
103
104
  __getToggleContent(path, item) {
104
- return path && this.get(path, item);
105
+ return path && get(path, item);
105
106
  }
106
107
  }
107
108