From f805b1fd9bea9de1301ff7f0c0830de785295e16 Mon Sep 17 00:00:00 2001 From: wimrijnders Date: Fri, 21 Jul 2017 18:54:53 +0200 Subject: [PATCH] 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 --- lib/network/modules/NodesHandler.js | 23 +++++++++++++++++++---- lib/network/modules/components/Node.js | 2 +- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/network/modules/NodesHandler.js b/lib/network/modules/NodesHandler.js index 85a00c9c..25156728 100644 --- a/lib/network/modules/NodesHandler.js +++ b/lib/network/modules/NodesHandler.js @@ -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"); } diff --git a/lib/network/modules/components/Node.js b/lib/network/modules/components/Node.js index f42cdb3b..b231253e 100644 --- a/lib/network/modules/components/Node.js +++ b/lib/network/modules/components/Node.js @@ -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