From 62a7d3e86b35b5613953766995c33ead32f42523 Mon Sep 17 00:00:00 2001 From: DeyaEddin Date: Tue, 9 Jan 2024 01:30:40 +0300 Subject: [PATCH] fix: #109 --- .../EnhancedSelectionGridHelper.java | 181 ++++++++++-------- 1 file changed, 106 insertions(+), 75 deletions(-) diff --git a/src/main/java/com/flowingcode/vaadin/addons/gridhelpers/EnhancedSelectionGridHelper.java b/src/main/java/com/flowingcode/vaadin/addons/gridhelpers/EnhancedSelectionGridHelper.java index 6fae391..9f5bf08 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/gridhelpers/EnhancedSelectionGridHelper.java +++ b/src/main/java/com/flowingcode/vaadin/addons/gridhelpers/EnhancedSelectionGridHelper.java @@ -4,18 +4,21 @@ 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.GridLazyDataView; import com.vaadin.flow.component.grid.dataview.GridListDataView; +import com.vaadin.flow.data.provider.BackEndDataProvider; +import com.vaadin.flow.data.provider.ListDataProvider; import com.vaadin.flow.shared.Registration; +import lombok.RequiredArgsConstructor; + 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 @@ -59,13 +62,27 @@ void enableEnhancedSelection() { if (ev.isShiftKey()) { grid.asMultiSelect().clear(); if (lastClickedItem != null) { - GridListDataView 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))); + GridListDataView listDataView = null; + GridLazyDataView lazyDataView = null; + if (grid.getDataProvider() instanceof ListDataProvider) { + listDataView = grid.getListDataView(); + } else if(grid.getDataProvider() instanceof BackEndDataProvider) { + lazyDataView = grid.getLazyDataView(); + } + if(listDataView != null) { + int index1 = listDataView.getItems().toList().indexOf(lastClickedItem); + int index2 = listDataView.getItems().toList().indexOf(clickedItem); + GridListDataView finalListDataView = listDataView; + IntStream.rangeClosed(Math.min(index1, index2), Math.max(index1, index2)) + .forEach(i -> grid.select(finalListDataView.getItem(i))); + } else if(lazyDataView != null) { + int index1 = lazyDataView.getItems().toList().indexOf(lastClickedItem); + int index2 = lazyDataView.getItems().toList().indexOf(clickedItem); + GridLazyDataView finalLazyDataView = lazyDataView; + IntStream.rangeClosed(Math.min(index1, index2), Math.max(index1, index2)) + .forEach(i -> grid.select(finalLazyDataView.getItem(i))); + } return; } } else if (isSpecialKey && !GridHelper.isSelectOnClick(grid)) { @@ -82,64 +99,78 @@ void enableEnhancedSelection() { }); keyUpRegistration = grid.getElement().addEventListener("keyup", ev -> { - String keyUp = ev.getEventData().getString(KEY_UP_EVENT_KEY); - boolean arrowsKey = "ArrowDown".equals(keyUp) || "ArrowUp".equals(keyUp); - - GridListDataView dataView = grid.getListDataView(); - - Optional newFocusedItemMaybe = Optional.empty(); - int newFocusedItemIndex = - (int) ev.getEventData().getNumber(KEY_UP_ELEMENT_FOCUSED_ITEM_INDEX); - if (newFocusedItemIndex >= 0) { - newFocusedItemMaybe = Optional.ofNullable(dataView.getItem(newFocusedItemIndex)); - } - - if (newFocusedItemMaybe.isPresent()) { - T newFocusedItem = newFocusedItemMaybe.get(); - boolean isSpecialKey = ev.getEventData().getBoolean(KEY_UP_EVENT_META_KEY) - || ev.getEventData().getBoolean(KEY_UP_EVENT_CTRL_KEY) - || ev.getEventData().getBoolean(KEY_UP_EVENT_ALT_KEY); - - Object lastFocusedItem = ComponentUtil.getData(grid, LAST_FOCUSED_ITEM); - boolean shiftKey = ev.getEventData().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); + String keyUp = ev.getEventData().getString(KEY_UP_EVENT_KEY); + boolean arrowsKey = "ArrowDown".equals(keyUp) || "ArrowUp".equals(keyUp); + GridListDataView listDataView = null; + GridLazyDataView lazyDataView = null; + if (grid.getDataProvider() instanceof ListDataProvider) { + listDataView = grid.getListDataView(); + } else if(grid.getDataProvider() instanceof BackEndDataProvider) { + lazyDataView = grid.getLazyDataView(); + } + + Optional newFocusedItemMaybe = Optional.empty(); + int newFocusedItemIndex = (int) ev.getEventData().getNumber(KEY_UP_ELEMENT_FOCUSED_ITEM_INDEX); + if (newFocusedItemIndex >= 0) { + if (listDataView != null) { + newFocusedItemMaybe = Optional.ofNullable(listDataView.getItems().toList().get(newFocusedItemIndex)); + } else if(lazyDataView != null) { + newFocusedItemMaybe = Optional.ofNullable(lazyDataView.getItems().toList().get(newFocusedItemIndex)); + } + } + + if (newFocusedItemMaybe.isPresent()) { + T newFocusedItem = newFocusedItemMaybe.get(); + boolean isSpecialKey = ev.getEventData().getBoolean(KEY_UP_EVENT_META_KEY) + || ev.getEventData().getBoolean(KEY_UP_EVENT_CTRL_KEY) + || ev.getEventData().getBoolean(KEY_UP_EVENT_ALT_KEY); + + Object lastFocusedItem = ComponentUtil.getData(grid, LAST_FOCUSED_ITEM); + boolean shiftKey = ev.getEventData().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; + if (listDataView != null) { + lastFocusedItemIndex = listDataView.getItems().toList().indexOf(lastFocusedItem); + GridListDataView finalListDataView = listDataView; + IntStream.rangeClosed(Math.min(lastFocusedItemIndex, newFocusedItemIndex), + Math.max(lastFocusedItemIndex, newFocusedItemIndex)) + .forEach(i -> grid.select(finalListDataView.getItem(i))); + } else { + + lastFocusedItemIndex = lazyDataView.getItems().toList().indexOf(lastFocusedItem); + GridLazyDataView finalLazyDataView = lazyDataView; + IntStream.rangeClosed(Math.min(lastFocusedItemIndex, newFocusedItemIndex), + Math.max(lastFocusedItemIndex, newFocusedItemIndex)) + .forEach(i -> grid.select(finalLazyDataView.getItem(i))); + } + } + } 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); } @@ -165,14 +196,14 @@ boolean isEnhancedSelectionEnabled() { */ 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();" + - " }" + - " }"); + + "const sel = window.getSelection ? window.getSelection() : document.selection;" + + " if (sel) {" + + " if (sel.removeAllRanges) {" + + " sel.removeAllRanges();" + + " } else if (sel.empty) {" + + " sel.empty();" + + " }" + + " }"); } }