-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstractPerson.js
420 lines (381 loc) · 13.9 KB
/
abstractPerson.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* A general superclass for nodes on the Pedigree graph. Contains connections
* and basic information about gender, ID and a graphics element.
*
* @param x the x coordinate on the canvas
* @param x the y coordinate on the canvas
* @param gender should be "U", "F", or "M" depending on the gender
* @param id the unique ID number of this node
*/
var AbstractPerson = Class.create(AbstractNode, {
initialize: function($super, x, y, gender, id) {
this._parentPartnership = null;
this._partnerships = [];
this._gender = this.parseGender(gender);
$super(x, y, id);
},
/*
* Initializes the object responsible for creating graphics for this node
*
* @param x the x coordinate on the canvas at which the node is centered
* @param y the y coordinate on the canvas at which the node is centered
*/
generateGraphics: function(x, y) {
return new AbstractPersonVisuals(this, x, y);
},
/*
* Reads a string of input and converts it into the standard gender format of "M","F" or "U".
* Defaults to "U" if string is not recognized
*
* @param gender the string to be parsed
*/
parseGender: function(gender) {
return (gender == 'M' || gender == 'F')?gender:'U';
},
/*
* Returns "U", "F" or "M" depending on the gender of this node
*/
getGender: function() {
return this._gender;
},
/*
* Updates the gender of this node and (optionally) updates the
* graphics. Updates gender of all partners if it is unknown.
* Returns an array of nodes visited during the partner traversal.
*
* @param gender should be "U", "F", or "M" depending on the gender
* @param forceDraw set to true if you want to update the graphics
* @param visitedNodes an array of nodes that were visited during the traversal up until
* this node. OMIT this parameter. It is used for internal functionality.
*/
setGender: function(gender, forceDraw, visitedNodes) {
var visited = (visitedNodes) ? visitedNodes : [];
visited.push(this);
if(this.getPartners().length == 0) {
this._gender = this.parseGender(gender);
this.getGraphics().setGenderSymbol();
}
else if(this.getGender() == "U") {
var me = this;
this._gender = this.parseGender(gender);
this.getGraphics().setGenderSymbol();
this.getPartners().each(function(partner) {
if(visited.indexOf(partner) == -1) {
visited = partner.setGender(me.getOppositeGender(), forceDraw, visited);
}
});
}
return visited;
},
/*
* Returns an array of Partnership objects of this node
*/
getPartnerships: function() {
return this._partnerships;
},
/*
* Returns the Partnership affiliated with partner
*
* @partner can be a Person or a PlaceHolder
*/
getPartnership: function(partner) {
var partnerships = this.getPartnerships();
for(var i = 0; i < partnerships.length; i++) {
if(partnerships[i].getPartnerOf(this) == partner) {
return partnerships[i];
}
}
return null;
},
/*
* Returns an array nodes that share a Partnership with this node
*/
getPartners: function() {
var partners = [];
var me = this;
this.getPartnerships().each(function(partnership) {
var partner = partnership.getPartnerOf(me);
partner && partners.push(partner);
});
return partners;
},
/*
* Adds a new partnership to the list of partnerships of this node
*
* @param partnership is a Partnership object with this node as one of the partners
*/
addPartnership: function(partnership) {
if(this.getPartners().indexOf(partnership.getPartnerOf(this)) == -1) {
this._partnerships.push(partnership);
}
},
/*
* Removes a partnership from the list of partnerships
*
* @param partnership is a Partnership object with this node as one of the partners
*/
removePartnership: function(partnership) {
this._partnerships = this._partnerships.without(partnership);
},
/*
* Returns a Partnership containing this the parent nodes
*/
getParentPartnership: function() {
return this._parentPartnership;
},
/*
* Replaces the parents Partnership with the one passed in the parameter
*
* @param partnership is a Partnership object that should have this node listed as a child
*/
setParentPartnership: function(partnership) {
this._parentPartnership = partnership;
},
/*
* Returns an array containing the two parent AbstractPerson nodes of this node.
*/
getParents: function() {
if(this.getParentPartnership()){
return [this.getParentPartnership().getPartners()[0], this.getParentPartnership().getPartners()[1]]
}
return null;
},
/*
* Creates a Partnership of two new Person nodes of opposite gender, and sets parents to this partnership
*/
createParents: function() {
if(this.getParentPartnership() == null) {
var positions = editor.findPosition ({above : this.getID()}, ['mom', 'dad']);
var mother = editor.addNode(positions['mom'].x, positions['mom'].y, "F", false),
father = editor.addNode(positions['dad'].x, positions['dad'].y, "M", false);
var joinPosition = editor.findPosition({join : [mother.getID(), father.getID()]});
var partnership = editor.addPartnership(joinPosition.x, joinPosition.y, mother, father);
document.fire('pedigree:parents:added', {'node' : partnership, 'relatedNodes' : [mother, father], 'sourceNode' : this});
this.addParents(partnership);
}
},
/*
* Sets parents to the partnership passed in the parameter, and adds this node to partnership's list of children
*/
addParents: function(partnership) {
if(this.getParentPartnership() == null) {
partnership.addChild(this);
}
},
removeParents: function() {
if(this.getParentPartnership()) {
this.getParentPartnership().remove();
}
},
addParent: function(parent) {
if(parent.canBeParentOf(this)) {
var partnership = parent.createPartner(true, true);
partnership.addChild(this);
}
},
/*
* Returns a string representing the opposite gender of this node ("M" or "F"). Returns "U"
* if the gender of this node is unknown
*/
getOppositeGender : function() {
if (this.getGender() == "U") {
return "U";
}
else if(this.getGender() == "M") {
return "F";
}
else {
return "M";
}
},
/*
* Creates a new node and generates a Partnership with this node.
* Returns the Partnership.
*
* @param isPlaceHolder set to true if the new partner should be a PlaceHolder
*/
createPartner: function(isPlaceHolder, noChild) {
var position = editor.findPosition({side: this.getID()}),
partner = editor.addNode(position.x, position.y, this.getOppositeGender(), isPlaceHolder);
var result = this.addPartner(partner, noChild);
document.fire('pedigree:partner:added', {'node' : partner, 'relatedNodes' : [result], 'sourceNode' : this});
return result;
},
/*
* Creates a new Partnership with the partner passed in the parameter.
* Does not duplicate a partnership if one already exists.
* Returns the new Partnership or the preexisting partnership
*
* @param partner a Person or PlaceHolder.
*/
addPartner: function(partner, noChild) {
if(this.getPartners().indexOf(partner) != -1){
return this.getPartnership(partner);
}
else if(this.canPartnerWith(partner)) {
var joinPosition = editor.findPosition({join : [this.getID(), partner.getID()]});
var partnership = editor.addPartnership(joinPosition.x, joinPosition.y, this, partner);
if(this.getGender() == 'U' && partner.getGender() != 'U') {
this.setGender(partner.getOppositeGender(), true, null);
}
else if(this.getGender() != 'U' && partner.getGender() == 'U') {
partner.setGender(this.getOppositeGender(), true, null);
}
if(partnership.getChildren().length == 0 && !noChild) {
partnership.createChild(true);
}
document.fire('pedigree:partnership:added', {'node' : partnership, 'relatedNodes' : [partner], 'sourceNode' : this});
return partnership;
}
},
/*
* Returns an array of nodes that are children from all of this node's Partnerships.
* The array can include PlaceHolders.
*/
getChildren: function(type) {
var children = [];
this.getPartnerships().each(function(partnership) {
children = children.concat(partnership.getChildren(type))
});
return children;
},
createChild: function(isPlaceHolder) {
var partnership = this.createPartner(true, true);
partnership.createChild(isPlaceHolder);
},
addChild: function(child) {
if(this.canBeParentOf(child)) {
var partnership = this.createPartner(true, true);
partnership.addChild(child);
return child;
}
return null;
},
/*
* Returns true if this node is a parent of otherNode
*
* @param otherNode can be a Person or a PlaceHolder
*/
isParentOf: function(otherNode) {
return (this.getChildren().indexOf(otherNode) > -1);
},
/*
* Returns true if this node is a descendant of otherNode
*
* @param otherNode can be a Person or a PlaceHolder
*/
isDescendantOf: function(otherNode) {
if(otherNode.isParentOf(this)) {
return true;
}
else {
var found = false,
children = otherNode.getChildren(),
i = 0;
while((i < children.length) && !found) {
found = this.isDescendantOf(children[i]);
i++;
}
return found;
}
},
/*
* Returns true if this node is an ancestor of otherNode
*
* @param otherNode can be a Person or a PlaceHolder
*/
isAncestorOf: function(otherNode) {
return otherNode.isDescendantOf(this);
},
/*
* Returns true if this node is a partner of otherNode
*
* @param otherNode can be a Person or a PlaceHolder
*/
isPartnerOf: function(otherNode) {
return this.getPartners().indexOf(otherNode) > -1;
},
/*
* Returns true if this node can have a heterosexual Partnership with otherNode
*
* @param otherNode is a Person
*/
canPartnerWith: function(otherNode) {
var oppositeGender = (this.getOppositeGender() == otherNode.getGender() || this.getGender() == "U" || otherNode.getGender() == "U");
var numSteps = this.getStepsToNode(otherNode)[0];
var oddStepsAway = (numSteps == null || numSteps%2 == 1);
return oppositeGender && oddStepsAway;
},
/*
* Returns true if this node can be a parent of otherNode
*
* @param otherNode is a Person
*/
canBeParentOf: function(otherNode) {
var isDescendant = this.isDescendantOf(otherNode);
return (this != otherNode) && otherNode.getParentPartnership() == null && this.getChildren().indexOf(otherNode) == -1 && !isDescendant;
},
/*
* Breaks connections with all related nodes and removes this node from
* the record.
* (Optional) Removes all descendant nodes and their relatives that will become unrelated to the proband as a result
*
* @param isRecursive set to true if you want to remove all unrelated descendants as well
*/
remove: function($super, isRecursive) {
if(isRecursive) {
$super(true)
}
else {
var me = this,
toRemove = [],
parents = this.getParentPartnership();
parents && parents.removeChild(me);
this.getPartnerships().each(function(partnership) {
partnership.remove(false);
});
editor.removeNode(this);
this.getGraphics().remove();
}
},
/*
* Returns the parent's Partnership
*/
getUpperNeighbors: function() {
return this.getParentPartnership() ? [this.getParentPartnership()] : [];
},
/*
* Returns all of this node's Partnerships
*/
getSideNeighbors: function() {
return this.getPartnerships();
},
/*
* Returns an array with the number of partnerships between this node and otherNode, and the nodes visited
* in the process of the traversal
*
* @param otherNode an AbstractNode whose distance (in partnerships) from this node you're trying to calculate
* @param visitedNodes an array of nodes that were visited in the result of the traversal. This parameter is used
* internally so omit it when calling the function
*/
getStepsToNode: function(otherNode, visitedNodes) {
var visited = (visitedNodes) ? visitedNodes : [];
visited.push(this);
if(this === otherNode) {
return [0, visited];
}
else {
var numSteps = null;
this.getPartners().each(function(partner) {
if(visited.indexOf(partner) == -1) {
numSteps = partner.getStepsToNode(otherNode, visited)[0];
if(numSteps != null) {
numSteps = 1 + numSteps;
throw $break;
}
}
});
return [numSteps, visited];
}
}
});