From 0e6b6ecdb8bfe835c67a361f1e49bfc5096e7bef Mon Sep 17 00:00:00 2001 From: wimrijnders Date: Tue, 30 May 2017 23:08:35 +0200 Subject: [PATCH] Added param 'direction' to Network.getConnectedNodes() (#3108) * Added param 'direction' to Network.getConnectedNodes() * Redo commit - Network.js does not need to change --- docs/network/index.html | 12 +++++++++--- lib/network/modules/NodesHandler.js | 8 +++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/docs/network/index.html b/docs/network/index.html index cc86669f..c98ced3a 100644 --- a/docs/network/index.html +++ b/docs/network/index.html @@ -866,13 +866,19 @@ function releaseFunction (clusterPosition, containedNodesPositions) { getConnectedNodes(String - nodeId or edgeId) + nodeId or edgeId, [String direction]) Returns: Array - Returns an array of nodeIds of the all the nodes that are directly connected to this node. If you supply an edgeId, - vis will first match the id to nodes. If no match is found, it will search in the edgelist and return an array: [fromId, toId]. + Returns an array of nodeIds of all the nodes that are directly connected to this node or edge.

+ For a node id, returns an array with the id's of the connected nodes.
+ If optional parameter direction is set to string 'from', only parent nodes are returned.
+ If direction is set to 'to', only child nodes are returned.
+ Any other value or undefined returns both parent and child nodes. +

+ For an edge id, returns an array: [fromId, toId]. + Parameter direction is ignored for edges. getConnectedEdges(String diff --git a/lib/network/modules/NodesHandler.js b/lib/network/modules/NodesHandler.js index f9999327..3e363ed5 100644 --- a/lib/network/modules/NodesHandler.js +++ b/lib/network/modules/NodesHandler.js @@ -408,22 +408,24 @@ class NodesHandler { /** * Get the Ids of nodes connected to this node. * @param nodeId + * @param direction {String|undefined} values 'from' and 'to' select respectively parent and child nodes only. + * Any other value returns both parent and child nodes. * @returns {Array} */ - getConnectedNodes(nodeId) { + getConnectedNodes(nodeId, direction) { let nodeList = []; if (this.body.nodes[nodeId] !== undefined) { let node = this.body.nodes[nodeId]; let nodeObj = {}; // used to quickly check if node already exists for (let i = 0; i < node.edges.length; i++) { let edge = node.edges[i]; - if (edge.toId == node.id) { // these are double equals since ids can be numeric or string + if (direction !== 'from' && edge.toId == node.id) { // these are double equals since ids can be numeric or string if (nodeObj[edge.fromId] === undefined) { nodeList.push(edge.fromId); nodeObj[edge.fromId] = true; } } - else if (edge.fromId == node.id) { // these are double equals since ids can be numeric or string + else if (direction !== 'to' && edge.fromId == node.id) { // these are double equals since ids can be numeric or string if (nodeObj[edge.toId] === undefined) { nodeList.push(edge.toId); nodeObj[edge.toId] = true;