Skip to content

Commit

Permalink
fix: fix TreeGrid updating wrong expanded items state on setDataProvi…
Browse files Browse the repository at this point in the history
…der (#9336)

Fixes: #9328
Details: HierarchicalDataProvider's reset method was called before recreating the HierrarchyMapper in setDataProvider. This was creating wrong updates for the client side.
(cherry picked from commit 73ebb9e)
  • Loading branch information
taefi authored and mshabarov committed Nov 9, 2020
1 parent fc208da commit fa95a01
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,18 @@ public void reset() {
update.enqueue("$connector.ensureHierarchy");

Collection<T> expandedItems = getHierarchyMapper().getExpandedItems();
update.enqueue("$connector.expandItems",
expandedItems
.stream()
.map(getKeyMapper()::key)
.map(key -> {
JsonObject json = Json.createObject();
json.put("key", key);
return json;
}).collect(
JsonUtils.asArray()));
if (!expandedItems.isEmpty()) {
update.enqueue("$connector.expandItems",
expandedItems
.stream()
.map(getKeyMapper()::key)
.map(key -> {
JsonObject json = Json.createObject();
json.put("key", key);
return json;
}).collect(
JsonUtils.asArray()));
}

requestFlush(update);
}
Expand Down Expand Up @@ -240,15 +242,16 @@ parentKey, getKeyMapper(), mapper,
*/
public <F> SerializableConsumer<F> setDataProvider(
HierarchicalDataProvider<T, F> dataProvider, F initialFilter) {
SerializableConsumer<F> consumer = super.setDataProvider(dataProvider,
initialFilter);

// Remove old mapper
// Remove old mapper before super.setDataProvider(...) prevents calling
// reset() before clearing the already expanded items:
if (mapper != null) {
mapper.destroyAllData();
}
mapper = createHierarchyMapper(dataProvider);

SerializableConsumer<F> consumer = super.setDataProvider(dataProvider,
initialFilter);

// Set up mapper for requests
mapper.setBackEndSorting(getBackEndSorting());
mapper.setInMemorySorting(getInMemorySorting());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private void testItemRemove(String item, boolean refreshAll) {
dataProvider.refreshItem(item);
}

int number = refreshAll ? 6 : 5;
int number = refreshAll ? 7 : 6;

ArgumentCaptor<SerializableConsumer> attachCaptor = ArgumentCaptor
.forClass(SerializableConsumer.class);
Expand Down Expand Up @@ -213,11 +213,28 @@ public void reset_noDataControllers_hierarchicalUpdateIsCalled() {
// any data controllers
communicator.reset();

Assert.assertEquals(2, enqueueFunctions.size());
Assert.assertEquals(1, enqueueFunctions.size());
Assert.assertEquals("$connector.ensureHierarchy",
enqueueFunctions.get(0));
}

@Test
public void reset_expandSomeItems_hierarchicalUpdateContainsExpandItems() {
enqueueFunctions.clear();

communicator.expand(ROOT);

communicator.reset();

// One expandItems for calling expand(...)
// One expandItems and one ensureHierarchy for calling reset()
Assert.assertEquals(3, enqueueFunctions.size());
Assert.assertEquals("$connector.expandItems",
enqueueFunctions.get(0));
Assert.assertEquals("$connector.ensureHierarchy",
enqueueFunctions.get(1));
Assert.assertEquals("$connector.expandItems",
enqueueFunctions.get(2));
}

@Test
Expand Down Expand Up @@ -247,7 +264,6 @@ public void reset_expandSomeItems_updateContainsProperJsonObjectsToExpand() {

dataCommunicator.reset();


Assert.assertTrue(enqueueFunctionsWithParams
.containsKey("$connector.expandItems"));
JsonArray arguments = (JsonArray) enqueueFunctionsWithParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ public boolean hasChildren(TreeNode item) {
protected Stream<TreeNode> fetchChildrenFromBackEnd(
HierarchicalQuery<TreeNode, Void> query) {
if (query.getParent() == null) {
return Arrays.stream(new TreeNode[] {root});
}
if(query.getParent() == root) {
return Arrays.stream(secondLevelNodes);
}
return Arrays.stream(thirdLevelNodes)
Expand Down

0 comments on commit fa95a01

Please sign in to comment.