-
Notifications
You must be signed in to change notification settings - Fork 0
/
partnership.js
207 lines (190 loc) · 6.65 KB
/
partnership.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
/*
* Partnership is a class that represents the relationship between two AbstractNodes
* and their children.
*
* @param x the x coordinate at which the partnership junction will be placed
* @param y the y coordinate at which the partnership junction will be placed
* @param partner1 an AbstractPerson who's one of the partners in the relationship.
* @param partner2 an AbstractPerson who's the other partner in the relationship. The order of partners is irrelevant.
*/
var Partnership = Class.create(AbstractNode, {
initialize: function($super, x, y, partner1, partner2, id) {
if(partner1.getType() != 'ph' || partner2.getType() != 'ph') {
this._type = 'partnership';
this._partners = [partner1, partner2];
this._children = [[/*Person*/],[/*PlaceHolder*/]];
this._partners[0].addPartnership(this);
this._partners[1].addPartnership(this);
$super(x, y, id);
}
},
getType: function() {
return this._type;
},
/*
* Generates and returns an instance of PartnershipVisuals
*/
generateGraphics: function(x, y) {
return new PartnershipVisuals(this, x, y);
},
/*
* Returns an array containing the two partners. Partners are AbstractPerson objects
*/
getPartners: function() {
return this._partners;
},
/*
* Returns the female partner in the partnership. Returns null if none of the parents are female
*/
getMother: function() {
if(this.getPartners()[0].getGender() == "F") {
return this.getPartners()[0];
}
else if(this.getPartners()[1].getGender() == "F") {
return this.getPartners()[1];
}
else {
return null;
}
},
/*
* Returns the male partner in the partnership. Returns null if none of the parents are male
*/
getFather: function() {
if(this.getPartners()[0].getGender() == "M") {
return this.getPartners()[0];
}
else if(this.getPartners()[1].getGender() == "M") {
return this.getPartners()[1];
}
else {
return null;
}
},
/*
* Returns the partner of someNode if someNode is a partner in this relationship. Otherwise, returns null.
*
* @param someNode is an AbstractPerson
*/
getPartnerOf: function(someNode) {
if(someNode == this.getPartners()[0]) {
return this.getPartners()[1];
}
else if(someNode == this.getPartners()[1]) {
return this.getPartners()[0];
}
else {
return null;
}
},
/*
* Returns true if someNode is a partner in this relationship.
*
* @param someNode is an AbstractPerson
*/
contains: function(someNode) {
return (this.getPartners()[0] == someNode || this.getPartners()[1] == someNode);
},
/*
* Returns an array of AbstractNodes that are children of this partnership
*/
getChildren: function(type) {
if(type == 'pn') {
return this._children[0];
}
else if(type == 'ph') {
return this._children[1];
}
return this._children.flatten();
},
/*
* Returns true if someNode is a child of this partnership.
*
* @param someNode is an AbstractPerson
*/
hasChild: function(someNode) {
return this.getChildren().indexOf(someNode) > -1;
},
/*
* Creates a new AbstractNode and sets it as a child of this partnership. Returns the child.
*
* @param isPlaceHolder set to true if the child is a placeholder
*/
createChild: function(isPlaceHolder) {
if(this.getChildren() && this.getChildren()[0] && this.getChildren()[0].getType() == 'ph') {
return this.getChildren()[0].convertToPerson();
}
else {
var position = editor.findPosition({below: this.getID()}, ['child']);
var child = editor.addNode(position['child'].x, position['child'].y, "U", isPlaceHolder);
var result = this.addChild(child);
document.fire("pedigree:child:added", {node: child, 'relatedNodes' : [], 'sourceNode' : this});
return result;
}
},
/*
* Adds someNode to the list of children of this partnership, and stores this partnership
* as it's parent partnership. Returns someNode.
*
* @param someNode is an AbstractPerson
*/
addChild: function(someNode) {
//TODO: elaborate on restrictions for adding parents to existing node
if(someNode && !this.hasChild(someNode) && (someNode.getParentPartnership() == null)) {
this.getChildren('ph').each(function(child){
child.remove();
});
this._children[+(someNode.getType() == 'ph')].push(someNode);
someNode.parentConnection = this.getGraphics().updateChildConnection(someNode, someNode.getX(), someNode.getY(), this.getX(), this.getY());
someNode.setParentPartnership(this);
}
return someNode;
},
/*
* Removes someNode from the list of children of this partnership, and removes this partnership as its parents
* reference. Returns someNode.
*
* @param someNode is an AbstractPerson
*/
removeChild: function(someNode) {
someNode.setParentPartnership(null);
var index = +(someNode.getType() == 'ph');
this._children[index] = this._children[index].without(someNode);
someNode.parentConnection.remove();
someNode.parentConnection = null;
},
remove: function($super, isRecursive) {
if(isRecursive) {
$super(isRecursive);
}
else {
var me = this;
editor.removePartnership(this);
this.getChildren().each(function(child) {
child.setParentPartnership(null);
me.removeChild(child);
if(child.getType() == 'ph') {
child.remove(false, true);
}
});
this.getPartners()[0].removePartnership(this);
this.getPartners()[1].removePartnership(this);
this.getGraphics().remove();
}
},
/*
* Returns an array of children nodes of this partnership
*/
getLowerNeighbors: function() {
return this.getChildren();
},
/*
* Returns an array containing the two partners of this relationship
*/
getSideNeighbors: function() {
return this.getPartners();
},
canBeParentOf: function(someNode) {
return (this.getPartners()[0].canBeParentOf(someNode) && this.getPartners()[1].canBeParentOf(someNode));
}
});