-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstractNode.js
More file actions
192 lines (173 loc) · 5.57 KB
/
abstractNode.js
File metadata and controls
192 lines (173 loc) · 5.57 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* A general abstract class for nodes on the Pedigree graph. Contains information about
* position on canvas and other neighboring nodes
*
* @param x the x coordinate on the canvas
* @param x the y coordinate on the canvas
*/
var AbstractNode = Class.create( {
initialize: function(x, y, id) {
this._id = id ? id : editor.generateID();
this._graphics = this.generateGraphics(x, y);
},
/*
* Returns the unique ID of this node
*/
getID: function() {
return this._id;
},
/*
* Generates and returns an instance of AbstractNodeVisuals
*/
generateGraphics: function(x, y) {
return new AbstractNodeVisuals(this, x, y);
},
/*
* Returns the object responsible for managing graphics
*/
getGraphics: function() {
return this._graphics;
},
/*
* Returns the X coordinate of the node on the canvas
*/
getX: function() {
return this.getGraphics().getX();
},
/*
* Changes the X coordinate of the node
*
* @param x the x coordinate on the canvas
* @param animate set to true if you want to animate, callback the transition
* @param callback the function called at the end of the animation
*/
setX: function(x, animate, callback) {
this.getGraphics().setX(x, animate, callback)
},
/*
* Returns the Y coordinate of the node on the canvas
*/
getY: function() {
return this.getGraphics().getY();
},
/*
* Changes the Y coordinate of the node
*
* @param y the y coordinate on the canvas
* @param animate set to true if you want to animate the transition
* @param callback the function called at the end of the animation
*/
setY: function(y, animate, callback) {
this.getGraphics().setX(y, animate, callback)
},
/*
* Returns an array containing the x and y coordinates of the node on canvas.
*/
getPos: function() {
return [this.getX(), this.getY()];
},
/*
* Changes the position of the node to (X,Y)
*
* @param x the x coordinate on the canvas
* @param y the y coordinate on the canvas
* @param animate set to true if you want to animate the transition
* @param callback the function called at the end of the animation
*/
setPos: function(x,y, animate, callback) {
this.getGraphics().setPos(x, y, animate, callback);
},
/**
* Provides access to the type of the node
*
* @return a string expressing the type
*/
getType: function() {
return "";
},
/*
* Returns an array of all adjacent nodes (neighbors).
*/
getNeighbors: function() {
return (this.getLowerNeighbors().concat(this.getSideNeighbors())).concat(this.getUpperNeighbors());
},
/*
* Returns an array of all adjacent nodes (neighbors) located below this node.
*/
getLowerNeighbors: function() {
return [];
},
/*
* Returns an array of all adjacent nodes (neighbors) located on the sides of this node.
*/
getSideNeighbors: function() {
return [];
},
/*
* Returns an array of all adjacent nodes (neighbors) located above this node.
*/
getUpperNeighbors: function() {
return [];
},
/*
* Removes all connections with neighboring nodes and (optionally) removes
* all the graphics of this node.
*
* @param removeVisuals set to true if you want to remove the graphics of this
* node as well.
*/
remove: function(isRecursive) {
var me = this;
var toRemove = [];
if(isRecursive) {
toRemove.push(me);
this.getNeighbors().each(function(neighbor) {
var result = neighbor.isRelatedTo(editor.getProband(), toRemove.clone());
if(!result[0]) {
toRemove = result[1];
}
});
toRemove.each(function(node) {
node.getGraphics().getHighlightBox && node.getGraphics().highlight();
});
var confirmation;
if(toRemove.length > 1) {
confirmation = confirm("Removing this person will also remove all the highlighted individuals. Are you sure you want to proceed?");
}
else {
confirmation = confirm("Removing this person will also remove all the related connections. Are you sure you want to proceed?");
}
if(confirmation) {
toRemove.reverse().each(function(node) {
node && node.remove(false);
});
}
else {
toRemove.each(function(node) {
node && node.getGraphics().getHighlightBox && node.getGraphics().unHighlight();
});
}
}
},
isRelatedTo: function(node, visited) {
var visitedNodes = (visited) ? visited : [];
if(visitedNodes.indexOf(this) >= 0) {
return [false, visitedNodes];
}
visitedNodes.push(this);
if(node == this) {
return [true, visitedNodes];
}
else {
var found = false;
var neighbors = this.getNeighbors();
neighbors = neighbors.without.apply(neighbors, visitedNodes);
neighbors.each(function(neighbor) {
var result = neighbor.isRelatedTo(node, visitedNodes);
visitedNodes = result[1];
result[0] && (found = true);
});
return [found, visitedNodes];
}
}
});