Skip to content

Commit 362be0a

Browse files
authored
refactor!: revert scroll to index changes (#8119)
This reverts commit c22b953.
1 parent c22b953 commit 362be0a

File tree

4 files changed

+25
-177
lines changed

4 files changed

+25
-177
lines changed

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/treegrid/it/TreeGridScrollToIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void expandAll_scrollToItem30_1_correctFirstVisibleItem() {
114114
@Test
115115
public void scrollToIndex30_1_correctFirstVisibleItem() {
116116
scrollToIndexInput.sendKeys("30-1", Keys.TAB);
117-
assertFirstVisibleRowContent("Dad 30/1");
117+
assertFirstVisibleRowContent("Granddad 30");
118118
}
119119

120120
@Test

vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/treegrid/TreeGrid.java

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -956,23 +956,22 @@ public HierarchicalDataProvider<T, SerializablePredicate<T>> getDataProvider() {
956956
* {@link HierarchyFormat#FLATTENED}: the index refers to an item in the
957957
* entire flattened tree, not only the root level, allowing items at any
958958
* expanded level to be reached with this method.
959+
* <p>
960+
* If the index exceeds the valid range, scrolling stops at the last
961+
* available item.
959962
*
960963
* @param index
961964
* zero based index of the item to scroll to
962965
*/
963966
@Override
964967
public void scrollToIndex(int index) {
965-
var itemCount = getDataCommunicator().getItemCount();
966-
if (index >= itemCount || index < -itemCount) {
967-
// The index does not correspond to an item
968-
return;
969-
}
970-
doScrollToIndex(index);
968+
getUI().ifPresent(
969+
ui -> ui.beforeClientResponse(this, ctx -> getElement()
970+
.executeJs("this.scrollToIndex($0);", index)));
971971
}
972972

973973
/**
974-
* Scrolls to a nested item specified by its hierarchical path. Any
975-
* collapsed parent of the item is expanded before scrolling.
974+
* Scrolls to a nested item specified by its hierarchical path.
976975
* <p>
977976
* The hierarchical path is an array of zero-based indexes, where each index
978977
* refers to a child of the item at the previous index. Scrolling continues
@@ -1005,27 +1004,24 @@ public void scrollToIndex(int... path) {
10051004
For HierarchyFormat.FLATTENED, use scrollToIndex(int) with a flat index instead.
10061005
""");
10071006
}
1007+
10081008
if (path.length == 0) {
10091009
throw new IllegalArgumentException(
10101010
"At least one index should be provided.");
10111011
}
1012-
try {
1013-
var pathItems = ((TreeGridDataCommunicator<T>) getDataCommunicator())
1014-
.getPathItems(path);
1015-
var ancestors = pathItems.subList(0, pathItems.size() - 1);
1016-
expand(ancestors);
1017-
} catch (IllegalArgumentException e) {
1018-
// The path does not correspond to an item
1019-
return;
1020-
}
1021-
doScrollToIndex(path);
1012+
1013+
String joinedIndexes = Arrays.stream(path).mapToObj(String::valueOf)
1014+
.collect(Collectors.joining(","));
1015+
getUI().ifPresent(ui -> ui.beforeClientResponse(this,
1016+
ctx -> getElement().executeJs(
1017+
"this.scrollToIndex(" + joinedIndexes + ");")));
10221018
}
10231019

10241020
@Override
10251021
public void scrollToEnd() {
1026-
var indexes = new int[10];
1027-
Arrays.fill(indexes, -1);
1028-
doScrollToIndex(indexes);
1022+
getUI().ifPresent(ui -> ui.beforeClientResponse(this,
1023+
ctx -> getElement().executeJs(
1024+
"this.scrollToIndex(...Array(10).fill(-1))")));
10291025
}
10301026

10311027
/**
@@ -1161,12 +1157,4 @@ public void scrollToItem(T item) {
11611157
scrollToIndex(indexPath);
11621158
}
11631159
}
1164-
1165-
private void doScrollToIndex(int... path) {
1166-
var joinedIndexes = Arrays.stream(path).mapToObj(String::valueOf)
1167-
.collect(Collectors.joining(","));
1168-
getUI().ifPresent(ui -> ui.beforeClientResponse(this,
1169-
ctx -> getElement().executeJs(
1170-
"this.scrollToIndex(" + joinedIndexes + ");")));
1171-
}
11721160
}

vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/treegrid/TreeGridDataCommunicator.java

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ public int[] resolveItem(T item) {
9797
* @param ancestors
9898
* the ordered list of the ancestors of the item
9999
* @return index path for the given item
100-
* @throws IllegalArgumentException
101-
* if the item does not exist
102100
*/
103101
private int[] getIndexPath(T item, List<T> ancestors) {
104102
var path = new ArrayList<Integer>();
@@ -134,38 +132,6 @@ private List<T> getAncestors(T item) {
134132
return ancestors;
135133
}
136134

137-
/**
138-
* Gets the ordered list of items for the specified path. Returns empty if
139-
* any of the items are not found.
140-
*
141-
* @param path
142-
* the path to get the items for
143-
* @return ordered list of items for the specified path
144-
* @throws IllegalArgumentException
145-
* if the path does not correspond to an item
146-
*/
147-
public List<T> getPathItems(int... path) {
148-
var dataProvider = (HierarchicalDataProvider<T, Object>) getDataProvider();
149-
var pathItems = new ArrayList<T>();
150-
T parent = null;
151-
for (var index : path) {
152-
if (index < 0) {
153-
var childrenCount = dataProvider.getChildCount(
154-
buildQuery(parent, 0, Integer.MAX_VALUE));
155-
index = childrenCount + index;
156-
}
157-
var query = buildQuery(parent, index, 1);
158-
var childOptional = dataProvider.fetchChildren(query).findFirst();
159-
if (childOptional.isEmpty()) {
160-
throw new IllegalArgumentException(
161-
"There is no item with the specified path.");
162-
}
163-
parent = childOptional.get();
164-
pathItems.add(parent);
165-
}
166-
return pathItems;
167-
}
168-
169135
private List<Integer> getAncestorPath(List<T> ancestors) {
170136
var ancestorPath = new ArrayList<Integer>();
171137
for (var i = 0; i < ancestors.size(); i++) {

vaadin-grid-flow-parent/vaadin-grid-flow/src/test/java/com/vaadin/flow/component/treegrid/ScrollToIndexTest.java

Lines changed: 7 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -15,135 +15,29 @@
1515
*/
1616
package com.vaadin.flow.component.treegrid;
1717

18-
import java.util.stream.IntStream;
19-
20-
import org.junit.After;
2118
import org.junit.Assert;
22-
import org.junit.Before;
2319
import org.junit.Test;
24-
import org.mockito.Mockito;
2520

26-
import com.vaadin.flow.component.UI;
2721
import com.vaadin.flow.data.provider.hierarchy.HierarchicalDataProvider.HierarchyFormat;
2822
import com.vaadin.flow.data.provider.hierarchy.TreeData;
2923
import com.vaadin.flow.data.provider.hierarchy.TreeDataProvider;
30-
import com.vaadin.flow.function.DeploymentConfiguration;
31-
import com.vaadin.flow.server.VaadinContext;
32-
import com.vaadin.flow.server.VaadinService;
33-
import com.vaadin.flow.server.VaadinSession;
3424

3525
public class ScrollToIndexTest {
36-
private TreeGrid<String> treeGrid;
37-
private TreeData<String> treeData;
38-
private UI ui;
39-
40-
@Before
41-
public void init() {
42-
ui = new UI();
43-
UI.setCurrent(ui);
44-
var mockSession = Mockito.mock(VaadinSession.class);
45-
var mockService = Mockito.mock(VaadinService.class);
46-
var mockContext = Mockito.mock(VaadinContext.class);
47-
var mockConfiguration = Mockito.mock(DeploymentConfiguration.class);
48-
Mockito.when(mockSession.getService()).thenReturn(mockService);
49-
Mockito.when(mockSession.getConfiguration())
50-
.thenReturn(mockConfiguration);
51-
Mockito.when(mockService.getContext()).thenReturn(mockContext);
52-
ui.getInternals().setSession(mockSession);
53-
treeGrid = new TreeGrid<>();
54-
ui.add(treeGrid);
55-
treeData = getTreeData();
56-
}
57-
58-
@After
59-
public void tearDown() {
60-
UI.setCurrent(null);
61-
}
26+
private TreeGrid<String> treeGrid = new TreeGrid<>();
27+
private TreeData<String> treeData = new TreeData<>();
6228

6329
@Test
64-
public void flattenedHierarchyFormat_scrollToIndexPath_throwsUnsupportedOperationException() {
65-
treeGrid.setDataProvider(
66-
new TreeDataProvider<>(treeData, HierarchyFormat.FLATTENED));
67-
Assert.assertThrows(UnsupportedOperationException.class,
68-
() -> treeGrid.scrollToIndex(0, 0));
69-
}
70-
71-
@Test
72-
public void nestedHierarchyFormat_scrollToIndexPathWithCollapsedParent_expandsParent() {
30+
public void nestedHierarchyFormat_scrollToIndexPath_doesNotThrow() {
7331
treeGrid.setDataProvider(
7432
new TreeDataProvider<>(treeData, HierarchyFormat.NESTED));
75-
var rootItem = treeData.getRootItems().get(10);
76-
var firstChild = treeData.getChildren(rootItem).getFirst();
77-
Assert.assertFalse(treeGrid.isExpanded(rootItem));
78-
Assert.assertFalse(treeGrid.isExpanded(firstChild));
79-
treeGrid.scrollToIndex(10, 0);
80-
Assert.assertTrue(treeGrid.isExpanded(rootItem));
81-
Assert.assertFalse(treeGrid.isExpanded(firstChild));
33+
treeGrid.scrollToIndex(0, 0);
8234
}
8335

8436
@Test
85-
public void flattenedHierarchyFormat_scrollToIndexNotPresent_notScrolled() {
37+
public void flattenedHierarchyFormat_scrollToIndexPath_throws() {
8638
treeGrid.setDataProvider(
8739
new TreeDataProvider<>(treeData, HierarchyFormat.FLATTENED));
88-
assertScrollToIndexCalled(false, 1000);
89-
assertScrollToIndexCalled(false, -1000);
90-
}
91-
92-
@Test
93-
public void flattenedHierarchyFormat_scrollToIndex_scrolled() {
94-
treeGrid.setDataProvider(
95-
new TreeDataProvider<>(treeData, HierarchyFormat.FLATTENED));
96-
assertScrollToIndexCalled(true, 10);
97-
assertScrollToIndexCalled(true, -10);
98-
}
99-
100-
@Test
101-
public void nestedHierarchyFormat_scrollToPathNotPresent_notScrolled() {
102-
treeGrid.setDataProvider(
103-
new TreeDataProvider<>(treeData, HierarchyFormat.NESTED));
104-
assertScrollToPathCalled(false, 1000);
105-
assertScrollToPathCalled(false, -1000);
106-
assertScrollToPathCalled(false, 10, 5, 5);
107-
assertScrollToPathCalled(false, -10, -5, -5);
108-
}
109-
110-
@Test
111-
public void nestedHierarchyFormat_scrollToPath_scrolled() {
112-
treeGrid.setDataProvider(
113-
new TreeDataProvider<>(treeData, HierarchyFormat.NESTED));
114-
assertScrollToPathCalled(true, 10, 5);
115-
assertScrollToPathCalled(true, -10, -5);
116-
}
117-
118-
private void assertScrollToIndexCalled(boolean called, int index) {
119-
treeGrid.scrollToIndex(index);
120-
Assert.assertEquals(called, isScrollToIndexCalled());
121-
}
122-
123-
private void assertScrollToPathCalled(boolean called, int... path) {
124-
treeGrid.scrollToIndex(path);
125-
Assert.assertEquals(called, isScrollToIndexCalled());
126-
}
127-
128-
private boolean isScrollToIndexCalled() {
129-
ui.getInternals().getStateTree().runExecutionsBeforeClientResponse();
130-
ui.getInternals().getStateTree().collectChanges(ignore -> {
131-
});
132-
var invocations = ui.getInternals().dumpPendingJavaScriptInvocations();
133-
return invocations.stream().anyMatch(invocation -> invocation
134-
.getInvocation().getExpression().contains("scrollToIndex"));
135-
}
136-
137-
private static TreeData<String> getTreeData() {
138-
var rootItemCount = 50;
139-
var childItemCountPerRootItem = 10;
140-
var treeData = new TreeData<String>();
141-
var rootItems = IntStream.range(0, rootItemCount)
142-
.mapToObj(i -> "Item " + i).toList();
143-
treeData.addRootItems(rootItems);
144-
rootItems.forEach(rootItem -> treeData.addItems(rootItem,
145-
IntStream.range(0, childItemCountPerRootItem)
146-
.mapToObj(i -> rootItem + "-" + i)));
147-
return treeData;
40+
Assert.assertThrows(UnsupportedOperationException.class,
41+
() -> treeGrid.scrollToIndex(0, 0));
14842
}
14943
}

0 commit comments

Comments
 (0)