Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,28 @@ private List<Integer> convertJsonArrayToIntegerList(JsonArray jsonIds) {
return idList;
}

/**
* Converts a comma-separated string of numeric IDs to a JsonArray.
* <p>
* IDs are received from the client as a string because {@code String} is the only argument type
* that every supported Vaadin version is able to decode for a {@link ClientCallable} method: up
* to Vaadin 24 the RPC decoder only accepts {@code String}, {@code Boolean}, {@code Integer},
* {@code Double} and {@code elemental.json.JsonValue}, while Vaadin 25 decodes with Jackson and
* no longer knows about {@code elemental.json}.
*
* @param ids comma-separated numeric IDs, possibly empty or {@code null}
* @return the IDs as a JsonArray of numbers
*/
private JsonArray convertIdsToJsonArray(String ids) {
JsonArray jsonIds = Json.createArray();
if (ids != null && !ids.isEmpty()) {
for (String id : ids.split(",")) {
jsonIds.set(jsonIds.length(), Double.parseDouble(id.trim()));
}
}
return jsonIds;
}

/**
* Appends a list of items to a parent node's children list.
*
Expand Down Expand Up @@ -436,14 +458,27 @@ public void addSiblings(Integer nodeId, List<OrgChartItem> siblings) {
}


/**
* Bridge for {@link #onSiblingsAdded(String, JsonArray)}, invoked from the client side. The IDs
* are received as a comma-separated string, since {@code JsonArray} arguments cannot be decoded
* by all supported Vaadin versions.
*
* @param nodeId the ID of the node that received new siblings
* @param siblingIds comma-separated IDs for the newly added siblings
* @see #convertIdsToJsonArray(String)
*/
@ClientCallable
private void onSiblingsAdded(String nodeId, String siblingIds) {
onSiblingsAdded(nodeId, convertIdsToJsonArray(siblingIds));
}

/**
* Handles sibling addition events from the client side. Converts the received JsonArray of
* sibling IDs to a List and fires a {@link SiblingsAddedEvent}.
*
* @param nodeId the ID of the node that received new siblings
* @param siblingIds array of IDs for the newly added siblings
*/
@ClientCallable
private void onSiblingsAdded(String nodeId, JsonArray siblingIds) {
// Find the node where siblings were added
OrgChartItem targetItem = getById(Integer.valueOf(nodeId), orgChartItem);
Expand Down Expand Up @@ -518,7 +553,20 @@ public void addChildren(Integer nodeId, List<OrgChartItem> children) {
}
}

/**
* Bridge for {@link #onChildrenAdded(String, JsonArray)}, invoked from the client side. The IDs
* are received as a comma-separated string, since {@code JsonArray} arguments cannot be decoded
* by all supported Vaadin versions.
*
* @param nodeId the ID of the parent node that received new children
* @param childIds comma-separated IDs for the newly added children
* @see #convertIdsToJsonArray(String)
*/
@ClientCallable
private void onChildrenAdded(String nodeId, String childIds) {
onChildrenAdded(nodeId, convertIdsToJsonArray(childIds));
}

private void onChildrenAdded(String nodeId, JsonArray childIds) {
// Find the parent node where children were added
OrgChartItem parentItem = getById(Integer.valueOf(nodeId), orgChartItem);
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/META-INF/frontend/fc-orgchart.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class FCOrgChart extends PolymerElement {

// Notify server about siblings added with just the IDs
const siblingIds = siblingsData.map(sibling => sibling.id);
this.$server.onSiblingsAdded(nodeId, siblingIds);
this.$server.onSiblingsAdded(nodeId, siblingIds.join(','));
}
}
}
Expand All @@ -190,7 +190,7 @@ class FCOrgChart extends PolymerElement {
this._chartInstance.addChildren($node, childrenData);
// Notify server about children added with just the IDs
const childIds = childrenData.map(child => child.id);
this.$server.onChildrenAdded(nodeId, childIds);
this.$server.onChildrenAdded(nodeId, childIds.join(','));
}
}
}
Expand Down
Loading