diff options
| author | Alon Levy <alon@pobox.com> | 2014-12-04 00:02:31 +0200 |
|---|---|---|
| committer | Alon Levy <alon@pobox.com> | 2014-12-04 00:06:03 +0200 |
| commit | dd51e5e964fbf3da32c4ccd8a8f71f9b1c30081f (patch) | |
| tree | 2aa5e449caab52c3685d57bea39465f2446b0e45 | |
| parent | 55ff4de5c041f7f1e34c1924d1f0df9dd863ac3f (diff) | |
graph: getConnectedNodesAndLinks: use forEach (still sucks)
| -rw-r--r-- | src/model/graph.js | 37 |
1 files changed, 15 insertions, 22 deletions
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; } |
