-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEnhancedSelectionGridHelper.java
More file actions
200 lines (166 loc) · 6.95 KB
/
EnhancedSelectionGridHelper.java
File metadata and controls
200 lines (166 loc) · 6.95 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
/*-
* #%L
* Grid Helpers Add-on
* %%
* Copyright (C) 2022 - 2025 Flowing Code
* %%
* 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.
* #L%
*/
package com.flowingcode.vaadin.addons.gridhelpers;
import com.flowingcode.vaadin.jsonmigration.JsonMigration;
import com.vaadin.flow.component.ComponentUtil;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.dataview.GridListDataView;
import com.vaadin.flow.shared.Registration;
import elemental.json.JsonObject;
import java.io.Serializable;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.RequiredArgsConstructor;
/**
* Add support for multiple items selection using click, arrow up/down, shift+click, shift+arrow
* up/down, ctrl+click and ctrl+space.
*
*/
@SuppressWarnings("serial")
@RequiredArgsConstructor
class EnhancedSelectionGridHelper<T> implements Serializable {
private static final String KEY_UP_EVENT_SHIFT_KEY = "event.shiftKey";
private static final String KEY_UP_EVENT_ALT_KEY = "event.altKey";
private static final String KEY_UP_EVENT_CTRL_KEY = "event.ctrlKey";
private static final String KEY_UP_EVENT_META_KEY = "event.metaKey";
private static final String KEY_UP_ELEMENT_FOCUSED_ITEM_INDEX = "element._focusedItemIndex";
private static final String KEY_UP_EVENT_KEY = "event.key";
private static final String LAST_FOCUSED_ITEM = "lastFocusedItem";
private static final String SPACEBAR_KEY = " ";
private final GridHelper<T> helper;
private Registration itemClickRegistration;
private Registration keyUpRegistration;
void enableEnhancedSelection() {
disableEnhancedSelection();
clearTextSelection();
Grid<T> grid = helper.getGrid();
// disable text selection
grid.getStyle().set("user-select", "none");
itemClickRegistration = grid.addItemClickListener(ev -> {
boolean isSpecialKey = ev.isCtrlKey() || ev.isMetaKey() || ev.isAltKey();
T clickedItem = ev.getItem();
Object lastClickedItem = ComponentUtil.getData(grid, LAST_FOCUSED_ITEM);
if (ev.isShiftKey()) {
grid.asMultiSelect().clear();
if (lastClickedItem != null) {
GridListDataView<T> dataView = grid.getListDataView();
int index1 = dataView.getItems().collect(Collectors.toList()).indexOf(lastClickedItem);
int index2 = dataView.getItems().collect(Collectors.toList()).indexOf(clickedItem);
IntStream.rangeClosed(Math.min(index1, index2), Math.max(index1, index2))
.forEach(i -> grid.select(dataView.getItem(i)));
return;
}
} else if (isSpecialKey && !GridHelper.isSelectOnClick(grid)) {
if (grid.asMultiSelect().isSelected(clickedItem)) {
grid.deselect(clickedItem);
} else {
grid.select(clickedItem);
}
} else if (!GridHelper.isSelectOnClick(grid)) {
grid.asMultiSelect().clear();
grid.select(clickedItem);
}
ComponentUtil.setData(grid, LAST_FOCUSED_ITEM, clickedItem);
});
keyUpRegistration = grid.getElement().addEventListener("keyup", ev -> {
JsonObject eventData = JsonMigration.getEventData(ev);
String keyUp = eventData.getString(KEY_UP_EVENT_KEY);
boolean arrowsKey = "ArrowDown".equals(keyUp) || "ArrowUp".equals(keyUp);
GridListDataView<T> dataView = grid.getListDataView();
Optional<T> newFocusedItemMaybe = Optional.empty();
int newFocusedItemIndex = (int) eventData.getNumber(KEY_UP_ELEMENT_FOCUSED_ITEM_INDEX);
if (newFocusedItemIndex >= 0) {
newFocusedItemMaybe = dataView.getItems().skip(newFocusedItemIndex).findFirst();
}
if (newFocusedItemMaybe.isPresent()) {
T newFocusedItem = newFocusedItemMaybe.get();
boolean isSpecialKey = eventData.getBoolean(KEY_UP_EVENT_META_KEY)
|| eventData.getBoolean(KEY_UP_EVENT_CTRL_KEY)
|| eventData.getBoolean(KEY_UP_EVENT_ALT_KEY);
Object lastFocusedItem = ComponentUtil.getData(grid, LAST_FOCUSED_ITEM);
boolean shiftKey = eventData.getBoolean(KEY_UP_EVENT_SHIFT_KEY);
if (shiftKey) {
if (lastFocusedItem == null) {
ComponentUtil.setData(grid, LAST_FOCUSED_ITEM, newFocusedItem);
}
if (arrowsKey) {
grid.asMultiSelect().clear();
int lastFocusedItemIndex =
dataView.getItems().collect(Collectors.toList()).indexOf(lastFocusedItem);
IntStream
.rangeClosed(Math.min(lastFocusedItemIndex, newFocusedItemIndex),
Math.max(lastFocusedItemIndex, newFocusedItemIndex))
.forEach(i -> grid.select(dataView.getItem(i)));
return;
}
} else if (arrowsKey && !isSpecialKey) {
grid.asMultiSelect().clear();
grid.select(newFocusedItem);
ComponentUtil.setData(grid, LAST_FOCUSED_ITEM, newFocusedItem);
} else if (isSpecialKey && SPACEBAR_KEY.equals(keyUp)) {
boolean isItemSelected = grid.asMultiSelect().isSelected(newFocusedItem);
if (isItemSelected) {
grid.deselect(newFocusedItem);
} else {
grid.select(newFocusedItem);
}
ComponentUtil.setData(grid, LAST_FOCUSED_ITEM, newFocusedItem);
}
}
})
.addEventData(KEY_UP_EVENT_META_KEY)
.addEventData(KEY_UP_EVENT_CTRL_KEY)
.addEventData(KEY_UP_EVENT_SHIFT_KEY)
.addEventData(KEY_UP_EVENT_ALT_KEY)
.addEventData(KEY_UP_EVENT_KEY)
.addEventData(KEY_UP_ELEMENT_FOCUSED_ITEM_INDEX);
}
void disableEnhancedSelection() {
if (itemClickRegistration != null) {
itemClickRegistration.remove();
itemClickRegistration = null;
}
if (keyUpRegistration != null) {
keyUpRegistration.remove();
keyUpRegistration = null;
}
// restore text selection
helper.getGrid().getStyle().set("user-select", "auto");
}
boolean isEnhancedSelectionEnabled() {
return itemClickRegistration != null && keyUpRegistration != null;
}
/**
* Clear text selection.
*/
private void clearTextSelection() {
UI.getCurrent().getPage().executeJs(""
+ "const sel = window.getSelection ? window.getSelection() : document.selection;" +
" if (sel) {" +
" if (sel.removeAllRanges) {" +
" sel.removeAllRanges();" +
" } else if (sel.empty) {" +
" sel.empty();" +
" }" +
" }");
}
}