Browse Source

Update hierarchy when node level changes (#3239)

* Update hierarchy when node level changes

Fix for #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
revert-3409-performance
wimrijnders 7 years ago
committed by yotamberk
parent
commit
f805b1fd9b
2 changed files with 20 additions and 5 deletions
  1. +19
    -4
      lib/network/modules/NodesHandler.js
  2. +1
    -1
      lib/network/modules/components/Node.js

+ 19
- 4
lib/network/modules/NodesHandler.js View File

@ -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); }
};
@ -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++) {
@ -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;
@ -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");
}

+ 1
- 1
lib/network/modules/components/Node.js View File

@ -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

Loading…
Cancel
Save