-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs-traversal-relationship-display.js
196 lines (178 loc) · 5.4 KB
/
fs-traversal-relationship-display.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
(function(){
// Width of a person box, in px
var boxWidth = 180;
window.FSTraversalRelationshipDisplay = function(traversalPath, lang){
if(!lang){
lang = 'en';
}
// Modify path data structure
var path = [];
for(var i = 0; i < traversalPath.length; i ++){
path.push({
person: traversalPath[i].person,
relationship: traversalPath[i].rel,
corner: '',
lines: []
});
}
// Calculate height, corner, and lines
var prevHeight = path[0].height = 0;
var prevChange = 0;
var minHeight = 0;
for(var i = 1; i < path.length; i++){
// Calculate height
var change = 0;
switch(path[i].relationship){
case 'child':
change = -1;
break;
case 'mother':
case 'father':
change = 1;
break;
}
prevHeight = path[i].height = prevHeight + change;
minHeight = Math.min(prevHeight, minHeight);
// Calculate corner value for previous person
if(change === 1 && prevChange === -1){
path[i-1].corner = 'bottom';
} else if(prevChange === 1 && change === -1){
path[i-1].corner = 'top';
}
// Calculate lines
if(path[i].relationship === 'child'){
path[i].lines.push('top');
path[i-1].lines.push('bottom');
} else if(path[i].relationship === 'mother' || path[i].relationship === 'father'){
path[i].lines.push('bottom');
path[i-1].lines.push('top');
} else if(path[i].relationship === 'spouse'){
path[i].lines.push('left');
}
if(path[i-1].corner === 'top'){
path[i-2].lines.push('top-right');
path[i].lines.push('top-left');
} else if(path[i-1].corner === 'bottom'){
path[i-1].lines.push('top-full');
}
prevChange = change;
}
// Adjust heights so that the minimum is 0
if(minHeight < 0){
var change = Math.abs(minHeight);
for(var i = 0; i < path.length; i++){
path[i].height += change;
}
}
// Calculate left
var prevLeft = path[0].left = 0;
for(var i = 1; i < path.length; i++){
var change = 0;
if(path[i-1].corner || path[i].corner){
change = .5;
}
else if(path[i].relationship === 'spouse'){
change = 1;
}
prevLeft = path[i].left = prevLeft + change;
}
// Group people into rows based on height
var rowsMap = {};
for(var i = 0; i < path.length; i++){
var rowNumber = path[i].height;
if(!rowsMap[rowNumber]){
rowsMap[rowNumber] = [];
}
rowsMap[rowNumber].push(path[i]);
}
// Sort rows in reverse according height
var rows = [];
for(var n in rowsMap){
rows.push({
number: parseInt(n),
// Sort people in the row too according to left position
people: rowsMap[n].sort(function(a, b){
return a.left - b.left;
})
});
}
rows.sort(function(a, b){
return b.number - a.number;
});
// Output rows; use inline css positioning for now
var html = '',
maxRight = 180;
for(var i = 0; i < rows.length; i++){
html += '<div class="fst-row">';
var prevLeft = 0;
for(var j = 0; j < rows[i].people.length; j++){
var person = rows[i].people[j],
myLeft = person.left * boxWidth,
cornerClass = person.corner ? ' fst-corner-' + person.corner : '';
html += '<div class="fst-person'+cornerClass+'" style="margin-left:'+(myLeft - prevLeft)+'px">';
html += '<div class="fst-name">'+person.person.display.name+'</div>';
html += '<div class="fst-role">'+getRelationshipString(person, lang)+'</div>';
for(var k = 0; k < person.lines.length; k++){
html += '<div class="fst-line-'+person.lines[k]+'"></div>';
}
html += '</div>'; // end person
prevLeft = myLeft + 180;
}
maxRight = Math.max(maxRight, prevLeft);
html += '</div>';
}
return '<div class="fst-relationship-display" style="width:'+maxRight+'px">' + html + '</div>';
};
/**
* Get the relationship string in the specified language
*/
function getRelationshipString(person, lang){
var rel = person.relationship;
if(rel === 'child'){
if(person.person.gender.type === 'http://gedcomx.org/Male'){
rel = 'son';
} else if(person.person.gender.type === 'http://gedcomx.org/Female'){
rel = 'daughter';
} else {
rel = 'child';
}
}
else if(rel === 'spouse'){
if(person.person.gender.type === 'http://gedcomx.org/Male'){
rel = 'husband';
} else if(person.person.gender.type === 'http://gedcomx.org/Female'){
rel = 'wife';
} else {
rel = 'spouse';
}
}
return languages[lang][rel];
}
/**
* Map of available languages and relationship strings
*/
var languages = window.FSTraversalRelationshipDisplay.languages = {
en: {
child: 'child',
son: 'son',
daughter: 'daughter',
mother: 'mother',
father: 'father',
spouse: 'spouse',
wife: 'wife',
husband: 'husband',
start: 'start'
},
es: {
child: 'hijo',
son: 'hijo',
daughter: 'hija',
mother: 'madre',
father: 'padre',
spouse: 'cónyuge',
wife: 'esposa',
husband: 'marido',
start: 'principio'
}
};
}());