From dd51e5e964fbf3da32c4ccd8a8f71f9b1c30081f Mon Sep 17 00:00:00 2001 From: Alon Levy Date: Thu, 4 Dec 2014 00:02:31 +0200 Subject: graph: getConnectedNodesAndLinks: use forEach (still sucks) --- src/model/graph.js | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/model/graph.js b/src/model/graph.js index ea715655..5efab353 100644 --- a/src/model/graph.js +++ b/src/model/graph.js @@ -125,12 +125,7 @@ function Graph() { * */ this.getConnectedNodesAndLinks = function(chosen_nodes, d) { - var i = 0, - j = 0, - adjacentnode, - link, - link2, - ret = {'nodes':[], 'links':[]}; + var ret = {'nodes':[], 'links':[]}; function addNode(node) { if (chosen_nodes.filter(function (n) { return n.id == node.id; }).length == 1) { @@ -138,6 +133,10 @@ function Graph() { } ret.nodes.push(node); } + function same(n1, n2) { + // XXX: using name comparison because one of the nodes might be stale + return compareNames(n1.name, n2.name); + } if (chosen_nodes === undefined) { console.log('getConnectedNodesAndLinks: bug: called with undefined node'); @@ -152,20 +151,17 @@ function Graph() { console.log('getConnectedNodesAndLinks: expected array'); } - while (i < links.length) { - link = links[i]; + links.forEach(function(link) { chosen_nodes.forEach(function (n) { - // XXX: using name comparison because n might be stale - if (compareNames(link.__src.name, n.name)) { + var adjacentnode; + if (same(link.__src, n)) { adjacentnode = findNode(link.__dst.id, null); if (adjacentnode.state !== "temp") { addNode({type: 'exit', node: adjacentnode}); } ret.links.push({type: 'exit', link: link}); - if (link.__dst.type === "chainlink") { - while (j < links.length) { - link2 = links[j]; + links.foreach(function(link2) { if (link.__dst.id === link2.__dst.id && link2.__dst.type === "chainlink" && link2.__dst.state !== "temp") { @@ -175,21 +171,18 @@ function Graph() { } ret.links.push({type: 'enter', link: link2}); } - j++; - } + }); } - j=0; } - if (compareNames(links[i].__dst.name, n.name)) { - adjacentnode = findNode(links[i].__src.id, null); + if (same(link.__dst, n)) { + adjacentnode = findNode(link.__src.id, null); if (adjacentnode.state !== "temp") { - adjacentnode.state = "enter"; + addNode({type: 'enter', node: adjacentnode}); } - links[i].state = "enter"; + ret.links.push({type: 'enter', link: link}); } }); - i++; - } + }); return ret; } -- cgit v1.3.1