-
Notifications
You must be signed in to change notification settings - Fork 0
/
person.js
470 lines (432 loc) · 16.3 KB
/
person.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
* Person is a class representing any AbstractPerson that has sufficient information to be
* displayed on the final pedigree graph (printed or exported). Person objects
* contain information about disorders, age and other relevant properties, as well
* as graphical data to visualize this information.
*
* @param x the x coordinate on the Raphael canvas at which the node drawing will be centered
* @param the y coordinate on the Raphael canvas at which the node drawing will be centered
* @param gender either 'M', 'F' or 'U' depending on the gender
* @param id a unique ID number
* @param isProband set to true if this person is the proband
*/
var Person = Class.create(AbstractPerson, {
initialize: function($super, x, y, gender, id, isProband) {
this._firstName = null;
this._lastName = null;
this._birthDate = null;
this._deathDate = null;
this._conceptionDate = null;
this._isAdopted = false;
this._lifeStatus = 'alive';
this._isProband = isProband;
this._disorders = [];
this._evaluations = [];
$super(x, y, gender, id);
},
/*
* Initializes the object responsible for creating graphics for this Person
*
* @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 PersonVisuals(this, x, y);
},
/*
* Returns "pn" ("Person Node")
*/
getType: function() {
return "pn";
},
/*
* Returns true if this node is the proband (i.e. the main patient)
*/
isProband: function() {
return this._isProband;
},
/*
* 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($super, partnership) {
this.getGraphics().getHoverBox().hideChildHandle();
$super(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($super, partnership) {
this.getGraphics().getHoverBox().unhideChildHandle();
$super(partnership);
},
/*
* Replaces the parents Partnership with partnership
*
* @param partnership is a Partnership object
*/
setParentPartnership: function($super, partnership) {
$super(partnership);
if(partnership) {
this.getGraphics().getHoverBox().hideParentHandle();
}
else {
this.getGraphics().getHoverBox().unHideParentHandle();
}
},
/*
* Returns the first name of this Person
*/
getFirstName: function() {
return this._firstName;
},
/*
* Replaces the first name of this Person with firstName, and displays the label
*
* @param firstName any string that represents the first name of this Person
*/
setFirstName: function(firstName) {
firstName && (firstName = firstName.charAt(0).toUpperCase() + firstName.slice(1));
this._firstName = firstName;
this.getGraphics().updateNameLabel();
},
/*
* Returns the last name of this Person
*/
getLastName: function() {
return this._lastName;
},
/*
* Replaces the last name of this Person with lastName, and displays the label
*
* @param lastName any string that represents the last name of this Person
*/
setLastName: function(lastName) {
lastName && (lastName = lastName.charAt(0).toUpperCase() + lastName.slice(1));
this._lastName = lastName;
this.getGraphics().updateNameLabel();
},
/*
* Returns the status of this Person, which can be "alive", "deceased", "stillborn", "unborn" or "aborted"
*/
getLifeStatus: function() {
return this._lifeStatus;
},
/*
* Returns true if this node's status is not 'alive' or 'deceased'.
*/
isFetus: function() {
return (this.getLifeStatus() != 'alive' && this.getLifeStatus() != 'deceased');
},
/*
* Changes the life status of this Person to newStatus
*
* @param newStatus can be "alive", "deceased", "stillborn", "unborn" or "aborted"
*/
setLifeStatus: function(newStatus) {
if(newStatus == 'unborn' || newStatus == 'stillborn' || newStatus == 'aborted' || newStatus == 'alive' || newStatus == 'deceased'){
this._lifeStatus = newStatus;
(newStatus != 'deceased') && this.setDeathDate(null);
this.getGraphics().updateSBLabel();
if(this.isFetus()) {
this.setBirthDate(null);
this.setAdopted(false);
}
this.getGraphics().updateLifeStatusShapes();
editor.nodeMenu.update(this,
{
'gestation_age': {value : this.getGestationAge(), inactive : !this.isFetus()},
'date_of_birth': {value : this.getBirthDate(), inactive : this.isFetus()},
'adopted': {value : this.isAdopted(), inactive: this.isFetus()},
'date_of_death': {value : this.getDeathDate(), inactive: newStatus != 'deceased'}
});
}
},
/*
* Returns the date object for the conception date of this Person
*/
getConceptionDate: function() {
return this._conceptionDate;
},
/*
* Replaces the conception date with newDate
*
* @param newDate a javascript Date object
*/
setConceptionDate: function(newDate) {
this._conceptionDate = newDate;
this.getGraphics().updateAgeLabel();
},
/*
* Returns the number of weeks since conception
*/
getGestationAge: function() {
if(this.getLifeStatus() == 'unborn' && this.getConceptionDate()) {
var oneWeek = 1000 * 60 * 60 * 24 * 7,
lastDay = new Date();
return Math.round((lastDay.getTime()- this.getConceptionDate().getTime())/oneWeek)
}
else if(this.isFetus()){
return this._gestationAge;
}
else {
return null;
}
},
/*
* Updates the conception age of the Person given the number of weeks passed since conception,
*
* @param numWeeks a number greater than or equal to 0
*/
setGestationAge: function(numWeeks) {
if(numWeeks){
this._gestationAge = numWeeks;
var daysAgo = numWeeks * 7,
d = new Date();
d.setDate(d.getDate() - daysAgo);
this.setConceptionDate(d);
}
else {
this.setConceptionDate(null);
}
},
/*
* Returns the date object for the birth date of this Person
*/
getBirthDate: function() {
return this._birthDate;
},
/*
* Replaces the birth date with newDate
*
* @param newDate a javascript Date object, that must be an earlier date than deathDate and
* a later date than conception date
*/
setBirthDate: function(newDate) {
if (!newDate || newDate && !this.getDeathDate() || newDate.getDate() < this.getDeathDate()) {
this._birthDate = newDate;
this.getGraphics().updateAgeLabel();
}
},
/*
* Returns the date object for the death date of this Person
*/
getDeathDate: function() {
return this._deathDate;
},
/*
* Replaces the death date with newDate
*
* @param newDate a javascript Date object, that must be a later date than deathDate and
* a later date than conception date
*/
setDeathDate: function(deathDate) {
if(!deathDate || deathDate && !this.getBirthDate() || deathDate.getDate()>this.getBirthDate().getDate()) {
this._deathDate = deathDate;
this._deathDate && (this.getLifeStatus() == 'alive') && this.setLifeStatus('deceased');
}
this.getGraphics().updateAgeLabel();
},
/*
* Returns an array of objects with fields 'id' and 'value', where id is the id number
* for the disorder, taken from the OMIM database, and 'value' is the name of the disorder.
* eg. [{id: 33244, value: 'Down Syndrome'}, {id: 13241, value: 'Huntington's Disease'}, ...]
*/
getDisorders: function() {
return this._disorders;
},
/*
* Replaces the list of disorder IDs of this person with disorderArray
*
* @param disorderArray should be an array of objects with fields 'id' and 'value', where id is the id number
* for the disorder, taken from the OMIM database, and 'value' is the name of the disorder.
* eg. [{id: 33244, value: 'Down Syndrome'}, {id: 13241, value: 'Huntington's Disease'}, ...]
*/
setDisorders: function(disorderArray) {
this._disorders = disorderArray;
},
/*
* Adds disorder to the list of this node's disorders and updates the Legend.
*
* @param disorder an object with fields 'id' and 'value', where id is the id number
* for the disorder, taken from the OMIM database, and 'value' is the name of the disorder.
* eg. {id: 33244, value: 'Down Syndrome'}
* @param forceDisplay set to true if you want to display the change on the canvas
*/
addDisorder: function(disorder, forceDisplay) {
if(!this.hasDisorder(disorder['id'])) {
editor.getLegend().addCase(disorder, this);
this.getDisorders().push(disorder);
}
forceDisplay && this.getGraphics().updateDisorderShapes();
},
/*
* Removes disorder to the list of this node's disorders and updates the Legend.
*
* @param disorder an object with fields 'id' and 'value', where id is the id number
* for the disorder, taken from the OMIM database, and 'value' is the name of the disorder.
* eg. {id: 33244, value: 'Down Syndrome'}
* @param forceDisplay set to true if you want to display the change on the canvas
*/
removeDisorder: function(disorder, forceDisplay) {
if(this.getDisorders().indexOf(disorder) >= 0) {
editor.getLegend().removeCase(disorder, this);
this.setDisorders(this.getDisorders().without(disorder));
}
else {
alert("This person doesn't have the specified disorder");
}
forceDisplay && this.getGraphics().updateDisorderShapes();
},
/*
* Given a list of disorders, adds and removes the disorders of this node to match
* the new list
*
* @param disorderArray should be an array of objects with fields 'id' and 'value', where id is the id number
* for the disorder, taken from the OMIM database, and 'value' is the name of the disorder.
* eg. [{id: 33244, value: 'Down Syndrome'}, {id: 13241, value: 'Huntington's Disease'}, ...]
*/
updateDisorders: function(disorders) {
var me = this;
this.getDisorders().each(function(disorder) {
var found = false;
disorders.each(function(newDisorder) {
disorder['id'] == newDisorder['id'] && (found = true);
});
!found && me.removeDisorder(disorder);
});
disorders.each(function(newDisorder) {
if (!me.hasDisorder(newDisorder.id)) {
me.addDisorder(newDisorder);
}
});
this.getGraphics().updateDisorderShapes();
},
/*
* Returns true if this person has the disorder with id
*
* @param id a string id for the disorder, taken from the OMIM database
*/
hasDisorder: function(id) {
for(var i = 0; i < this.getDisorders().length; i++) {
if(this.getDisorders()[i].id == id) {
return true;
}
}
return false;
},
/**
* Changes the adoption status of this Person to isAdopted
*
* @param isAdopted set to true if you want to mark the Person adopted
*/
setAdopted: function(isAdopted) {
//TODO: implement adopted and social parents
if(isAdopted) {
this.getGraphics().drawAdoptedShape();
}
else {
this.getGraphics().removeAdoptedShape();
}
},
/**
* Returns true if this Person is marked adopted
*/
isAdopted: function() {
return this._isAdopted;
},
/*
* Returns true if this node can be a parent of otherNode
*
* @param otherNode is a Person
*/
canBeParentOf: function($super, otherNode) {
var preliminary = $super(otherNode);
var incompatibleBirthDate = this.getBirthDate() && otherNode.getBirthDate() && this.getBirthDate() < otherNode.getBirthDate();
var incompatibleDeathDate = this.getDeathDate() && otherNode.getBirthDate() && this.getDeathDate() < otherNode.getBirthDate().clone().setDate(otherNode.getBirthDate().getDate()-700);
return preliminary && !incompatibleBirthDate && !incompatibleDeathDate && !this.isFetus();
},
/*
* Replaces this Person with a placeholder without breaking all the connections.
*
* @param otherNode is a Person
*/
convertToPlaceholder: function() {
var me = this;
var placeholder = editor.addNode(this.getX(), this.getY(), this.getGender(), true);
var parents = this.getParentPartnership();
if(parents) {
parents.removeChild(me);
parents.addChild(placeholder);
}
this.getPartnerships().each(function(partnership) {
var newPartnership = editor.addPartnership(partnership.getX(), partnership.getY(), partnership.getPartnerOf(me), placeholder);
partnership.getChildren().each(function(child) {
partnership.removeChild(child);
newPartnership.addChild(child);
});
});
me.remove(false);
return placeholder;
},
/*
* Deletes this node, it's placeholder partners and children and optionally
* removes all the other nodes that are unrelated to the proband node.
*
* @param isRecursive set to true if you want to remove related nodes that are
* not connected to the proband
*/
remove: function($super, isRecursive) {
if(!isRecursive) {
var me = this;
var hasPersonPartners = function() {
var found = false;
me.getPartners().each(function(partner) {
if(partner.getType() == 'pn') {
found = true;
throw $break;
}
});
return found;
};
this.getPartners().each(function(partner) {
if(partner.getType() == 'ph') {
partner.remove(false);
}
});
if((this.getParentPartnership() && this.getParentPartnership().getChildren().length == 1) || (this.getChildren('pn').length > 0 && hasPersonPartners())) {
this.convertToPlaceholder();
}
else {
this.getDisorders().each(function(disorder) {
editor.getLegend().removeCase(disorder, this);
});
this.getGraphics().getHoverBox().remove();
$super(isRecursive);
}
}
else {
$super(isRecursive);
}
},
/*
* Returns an object (to be accepted by the menu) with information about this Person
*/
getSummary: function() {
return {
identifier: {value : this.getID()},
first_name: {value : this.getFirstName()},
last_name: {value : this.getLastName()},
gender: {value : this.getGender(), inactive: (this.getGender() != 'U' && this.getPartners().length > 0)},
date_of_birth: {value : this.getBirthDate(), inactive: this.isFetus()},
disorders: {value : this.getDisorders()},
adopted: {value : this.isAdopted(), inactive: this.isFetus()},
state: {value : this.getLifeStatus(), inactive: [(this.getPartnerships().length > 0) ? ['unborn','aborted','stillborn'] : ''].flatten()},
date_of_death: {value : this.getDeathDate(), inactive: this.getLifeStatus() != 'deceased'},
gestation_age: {value : this.getGestationAge(), inactive : !this.isFetus()}
};
}
});