summaryrefslogtreecommitdiff
path: root/scripts/model
diff options
context:
space:
mode:
authorAlon Levy <alon@pobox.com>2014-11-03 18:38:06 +0200
committerAlon Levy <alon@pobox.com>2014-11-09 10:30:37 +0200
commiteef818ad99c117df2b6abb46ab8144c297887d5b (patch)
tree596f126c3b88abf48a22f159e469c8ab2a397538 /scripts/model
parent6f1ed70fa0583417cb630d8e733e856e6bfc666d (diff)
split highlight (search neighborhood of node) between graph & rhizicore
multiple TODOs added in code. in general, datastructure = graph, control/view state in rhizicore.
Diffstat (limited to 'scripts/model')
-rw-r--r--scripts/model/graph.js103
1 files changed, 59 insertions, 44 deletions
diff --git a/scripts/model/graph.js b/scripts/model/graph.js
index 657640bb..5f2d51eb 100644
--- a/scripts/model/graph.js
+++ b/scripts/model/graph.js
@@ -96,62 +96,77 @@ function Graph(el) {
}
}
-
- this.highlightNode = function(id, state) {
+ /**
+ *
+ * getConnectedNodesAndLinks
+ *
+ * @id
+ * @state - defines the starting node (must have id and state)
+ * @d - depth defining connected component. If -1 returns the entire connected component. (can be the whole graph)
+ *
+ * NOTE: chainlinks are treated specially, they don't count for distance. So all their decendants will be added.
+ *
+ * NOTE: temp state nodes (n.state === 'temp') are ignored.
+ *
+ * @return - {
+ * 'node': [node]
+ * 'link': [link]
+ * }
+ *
+ * TODO: rewrite using efficient data structure. Right now iterates over everything
+ * TODO: implement for d !== 1
+ *
+ */
+ this.getConnectedNodesAndLinks = function(n, d) {
var i = 0,
- j = 0;
- var n = findNode(id, state);
- var adjacentnode;
+ j = 0,
+ adjacentnode,
+ ret = {'nodes':[], 'links':{}};
+
$(".debug").html(n.state);
- //highlight node
- if (n !== undefined && n.state !== "chosen" && n.state !== "temp") {
- this.removeHighlight();
- n.state = "chosen";
+ if (n === undefined) {
+ console.log('getConnectedNodesAndLinks: bug: called with undefined node');
+ return;
+ }
+ if (d !== 1) {
+ console.log('getConnectedNodesAndLinks: bug: not implemented for d == ' + d);
+ }
+ d = d || 1;
- while (i < links.length) {
- if (links[i]['source'] === n) {
- adjacentnode = findNode(links[i]['target'].id, null);
- if (adjacentnode.state !== "temp") adjacentnode.state = "exit";
- links[i]['state'] = "exit";
+ while (i < links.length) {
+ if (links[i].source === n) {
+ adjacentnode = findNode(links[i].target.id, null);
+ if (adjacentnode.state !== "temp") {
+ adjacentnode.state = "exit";
+ }
+ links[i].state = "exit";
- if(links[i]['target'].type==="chainlink"){
- console.log("chain");
- while (j < links.length) {
- if(links[i]['target'].id===links[j]['target'].id && links[j]['target'].type==="chainlink" && links[j]['target'].state!=="temp"){
- adjacentnode = findNode(links[j]['source'].id, null);
- if (adjacentnode.state !== "temp") adjacentnode.state = "enter";
- links[j]['state'] = "enter";
+ if (links[i].target.type === "chainlink") {
+ console.log("chain");
+ while (j < links.length) {
+ if (links[i].target.id === links[j].target.id &&
+ links[j].target.type === "chainlink" &&
+ links[j].target.state !== "temp") {
+ adjacentnode = findNode(links[j].source.id, null);
+ if (adjacentnode.state !== "temp") {
+ adjacentnode.state = "enter";
}
- j++;
+ links[j].state = "enter";
}
+ j++;
}
- j=0;
- }
- if (links[i]['target'] === n) {
- adjacentnode = findNode(links[i]['source'].id, null);
- if (adjacentnode.state !== "temp") adjacentnode.state = "enter";
- links[i]['state'] = "enter";
}
- i++;
+ j=0;
}
- }
- }
-
- this.removeHighlight = function() {
- var k = 0;
- while (k < nodes.length) {
- if (nodes[k]['state'] === "enter" || nodes[k]['state'] === "exit" || nodes[k]['state'] === "chosen") {
- nodes[k]['state'] = "perm";
+ if (links[i].target === n) {
+ adjacentnode = findNode(links[i].source.id, null);
+ if (adjacentnode.state !== "temp") adjacentnode.state = "enter";
+ links[i].state = "enter";
}
- k++;
- }
- var j = 0;
- //highlight all connections
- while (j < links.length) {
- links[j]['state'] = "perm";
- j++;
+ i++;
}
+ return ret;
}
/* compareSubset: