-
Notifications
You must be signed in to change notification settings - Fork 18
/
controller_loc.js
executable file
·78 lines (71 loc) · 2.46 KB
/
controller_loc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/** @class Controller */
import tree_state from '../tree_state';
export default function (Controller) {
/**
* Get locations to be used to construct evolutionary path toolbar.
* @method get_my_location
* @memberof Controller
* @return [Array, Midnode] returns location array and root of the tree.
* Where Array is [NameArray, IdArray, bool, RichnessArray], and RichnessArray is
* 1 unit longer than NameArray & IdArray
*/
Controller.prototype.get_my_location = function () {
return [get_location3(this.root), this.root];
}
}
/**
* Get metadatas of nodes on the branch from the root to the common ancestor of all nodes in current view.
* Only nodes with a common name are returned, (TO DO: apart from the last element of the richness_array).
* Recurses down because it calls get_child_loc() which in turn calls get_location3()
* Question: should we not use "get_largest_visible_node" from record.js?
* The returned results are in the following format:
* [
* [node1.cname, node2.cname, node3.cname, ....]
* [node1.metacode, node2.metacode, node3.metacode, ....]
* boolean of some kind
* [node1.richness_val, node2.richness_val, node3.richness_val, .... (TO DO: curr_view_ancestral_node.richness_val)]
* ]
*/
function get_location3(node) {
if (node.dvar) {
if (node.gvar && node.has_child) {
// stop
return concat_node_loc_with_loc(node, get_empty_loc(true));
} else if (node.gvar && !node.has_child) {
return get_empty_loc(false);
} else if (!node.gvar && node.has_child) {
return concat_node_loc_with_loc(node, get_child_loc(node));
} else if (!node.gvar && !node.has_child) {
return get_empty_loc(false);
}
} else {
// stop: node not on screen
return get_empty_loc(false);
}
}
function get_child_loc(node) {
let children_locs = [];
let length = node.children.length;
for (let i = 0; i < length; i++) {
let child = node.children[i];
let child_loc = get_location3(child);
if (child_loc[2] === true) {
children_locs.push(child_loc);
}
}
if (children_locs.length === 0 || children_locs.length > 1) {
return get_empty_loc(true);
} else {
return children_locs[0];
}
}
function get_empty_loc(bool) {
return [[], [], bool, []];
}
function concat_node_loc_with_loc(node, loc) {
if (node.cname) {
return [([node.cname]).concat(loc[0]), [node.metacode].concat(loc[1]), true, [node.richness_val].concat(loc[3])];
} else {
return [loc[0], loc[1], true, loc[3]];
}
}