Skip to content

Commit

Permalink
Update hierarchy when node level changes (almende#3239)
Browse files Browse the repository at this point in the history
* Update hierarchy when node level changes

Fix for almende#3220

On change of data of an existing node, the level is checked for changes.
If changed, for a recalculation of the hierarchical layout.

The fix does not explicitly check for hierarchical layout; this should not be a problem.
The added code can be used to add further node fields which may trigger recalculation of layout.

* Changes due to review
  • Loading branch information
wimrijnders authored and Primoz Susa committed Jan 3, 2019
1 parent 8c3fbcc commit 4653218
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
23 changes: 19 additions & 4 deletions lib/network/modules/NodesHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class NodesHandler {

this.nodesListeners = {
add: (event, params) => { this.add(params.items); },
update: (event, params) => { this.update(params.items, params.data); },
update: (event, params) => { this.update(params.items, params.data, params.oldData); },
remove: (event, params) => { this.remove(params.items); }
};

Expand Down Expand Up @@ -273,10 +273,12 @@ class NodesHandler {

/**
* Update existing nodes, or create them when not yet existing
* @param {Number[] | String[]} ids
* @param {Number[] | String[]} ids id's of changed nodes
* @param {Array} changedData array with changed data
* @param {Array|undefined} oldData optional; array with previous data
* @private
*/
update(ids, changedData) {
update(ids, changedData, oldData) {
let nodes = this.body.nodes;
let dataChanged = false;
for (let i = 0; i < ids.length; i++) {
Expand All @@ -285,7 +287,9 @@ class NodesHandler {
let data = changedData[i];
if (node !== undefined) {
// update node
dataChanged = node.setOptions(data);
if (node.setOptions(data)) {
dataChanged = true;
}
}
else {
dataChanged = true;
Expand All @@ -294,6 +298,17 @@ class NodesHandler {
nodes[id] = node;
}
}

if (!dataChanged && oldData !== undefined) {
// Check for any changes which should trigger a layout recalculation
// For now, this is just 'level' for hierarchical layout
// Assumption: old and new data arranged in same order; at time of writing, this holds.
dataChanged = changedData.some(function(newValue, index) {
let oldValue = oldData[index];
return (oldValue && oldValue.level !== newValue.level);
});
}

if (dataChanged === true) {
this.body.emitter.emit("_dataChanged");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/network/modules/components/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Node {
setOptions(options) {
let currentShape = this.options.shape;
if (!options) {
return;
return; // Note that the return value will be 'undefined'! This is OK.
}

// basic options
Expand Down

0 comments on commit 4653218

Please sign in to comment.