-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs-person-families.html
332 lines (293 loc) · 10.7 KB
/
fs-person-families.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../polymer/lib/elements/dom-if.html">
<link rel="import" href="../fs-api-aware/fs-api-aware.html">
<link rel="import" href="../fs-request/fs-request.html">
<link rel="import" href="fs-family.html">
<!--
Display a FamilySearch person's families. A person's families include families
in which they are a child and families in which they are a spouse or parent.
Basic example:
<fs-person-families person-id="PPP-PPPP"></fs-person-families>
@group FamilySearch Elements
@customElement
@polymer
@demo demo/index.html
-->
<dom-module id="fs-person-families">
<template>
<style>
:host {
display: block;
width: 100%;
}
paper-card {
width: 100%;
}
#columns {
display: flex;
}
.column {
flex: 1;
}
.column:last-child {
margin-left: 32px;
}
fs-family {
margin: 16px 0;
}
</style>
<fs-request id="request"
auto
client-name="[[clientName]]"
require-authentication
url="{{_computeUrl(personId)}}"
on-response="_handleResponse"
on-loading-changed="_updateLoading"></fs-request>
<div class="card-content">
<div id="columns">
<div class="column">
<div class="col-title">Spouses and Children</div>
<template is="dom-if" if="addPersons">
<paper-button on-tap="_addSpouseEvent">+ Add Spouse</paper-button>
</template>
<template is="dom-repeat" items="{{_parentFamilies}}">
<fs-family family="[[item.persons]]"
add-persons="[[addPersons]]"
client-name="[[clientName]]"
on-add-child="_addChildEvent"
on-add-mother="_addCaprIds"
on-add-father="_addCaprIds"></fs-family>
</template>
<template is="dom-if" if="addPersons">
<paper-button on-tap="_addChildUnknownParentEvent">{{_addChildUnknownParentLabel(_gender)}}</paper-button>
</template>
</div>
<div class="column">
<div class="col-title">Parents and Siblings</div>
<template is="dom-if" if="addPersons">
<paper-button on-tap="_addParentEvent">+ Add Parent</paper-button>
</template>
<div>
<template is="dom-repeat" items="{{_childFamilies}}">
<fs-family family="[[item.persons]]"
add-persons="[[addPersons]]"
client-name="[[clientName]]"
on-add-child="_addChildEvent"
on-add-mother="_addCaprIds"
on-add-father="_addCaprIds"></fs-family>
</template>
</div>
</div>
</div>
</div>
</template>
<script>
class FSPersonFamilies extends FSApiAwareMixin(Polymer.Element) {
/**
* Fired when an add person button is clicked. The details may contain
* any of the documented parameters which are designed to be passed
* directly to an `fs-add-person` element.
*
* @event add-person
* @param {{fatherId:string}} ID of the father of the family that the person will be added to
* @param {{motherId:string}} ID of the mother of the family that the person will be added to
* @param {{childId:string}} ID of the child that a parent will be added to
* @param {{spouseId:string}} ID of a spouse that a person will be added to
* @param {{caprIds:array}} List of child and parents relationship IDs that the person should be added to as a parent
* @param {{gender:string}} Expected gender of the person that will be added
*/
static get is() { return 'fs-person-families'; }
static get properties() {
return {
personId: {
type: String,
observer: '_personIdChanged'
},
/** Whether the API request is still oustanding */
loading: {
type: Boolean,
value: false,
notify: true,
readOnly: true
},
/** Whether to show buttons for adding persons */
addPersons: {
type: Boolean,
value: false
},
/** Main person's gender */
_gender: String,
/** List of families where the person is a child */
_childFamilies: {
type: Array,
value: function(){
return [];
}
},
/** List of families where the person is a spouse */
_parentFamilies: {
type: Array,
value: function(){
return [];
}
}
};
}
reload() {
this._clearFamilies();
this._gender = undefined;
this.$.request.generateRequest();
}
_updateLoading(e) {
this._setLoading(e.detail.value);
}
_personIdChanged() {
this._clearFamilies();
}
_clearFamilies() {
this._childFamilies = [];
this._parentFamilies = [];
}
_addParentEvent() {
this._fireAddPerson({
childId: this.personId
});
}
_addSpouseEvent() {
this._fireAddPerson({
spouseId: this.personId,
gender: this._gender === 'http://gedcomx.org/Male' ? 'http://gedcomx.org/Female' : 'http://gedcomx.org/Male'
});
}
/**
* Capture the add-child event from `fs-family` and reemit as an
* add-person event from this element.
*/
_addChildEvent(e) {
this._fireAddPerson(e.detail);
}
_addChildUnknownParentEvent() {
const detail = {};
if(this._gender === 'http://gedcomx.org/Male') {
detail.fatherId = this.personId;
}
if(this._gender === 'http://gedcomx.org/Female') {
detail.motherId = this.personId;
}
this._fireAddPerson(detail);
}
/**
* Add a list of IDs of child-and-parents relationships to the add-mother
* and add-father events.
*/
_addCaprIds(e) {
e.detail.caprIds = e.model.item.caprIds;
this._fireAddPerson(e.detail);
}
/**
* Fire the `add-person` with the given details
*/
_fireAddPerson(detail) {
this.dispatchEvent(new CustomEvent('add-person', {
detail,
bubbles: true,
composed: true
}));
}
_addChildUnknownParentLabel(_gender) {
switch(_gender) {
case 'http://gedcomx.org/Male':
return '+ Add Child with an Unknown Mother';
case 'http://gedcomx.org/Female':
return '+ Add Child with an Unknown Father';
default:
return '+ Add Child of an Unknown Spouse';
}
}
_computeUrl() {
if(this.personId) {
return `/platform/tree/persons/${this.personId}/families`;
}
}
_handleResponse(e) {
if(e && e.detail && e.detail.response && e.detail.response.data && e.detail.response.data.persons) {
this._calculateFamilies(e.detail.response.data);
}
}
/**
* Given the response body of a call to the Person's Families resource,
* return a list of families in {father, mother, children[]} format.
*/
_calculateFamilies(data) {
// We put all persons into a map keyed by their ID for easy access
var persons = {};
for(i = 0; i < data.persons.length; i++){
persons[data.persons[i].id] = data.persons[i];
if(data.persons[i].id === this.personId) {
this._gender = data.persons[i].gender.type;
}
}
// We'll construct families by maintaining a map of family keys.
// The keys will be [husband]:[wife].
var families = {};
// First we process couple relationships where that the person is involved in.
var couples = Array.isArray(data.relationships) ? data.relationships.filter(r => r.type === 'http://gedcomx.org/Couple') : [];
couples.forEach((couple) => {
if(couple.person1.resourceId === this.personId || couple.person2.resourceId === this.personId) {
families[`${couple.person1.resourceId}:${couple.person2.resourceId}`] = {
persons: {
father: persons[couple.person1.resourceId],
mother: persons[couple.person2.resourceId],
children: []
},
caprIds: []
};
}
});
// Now we process child and parents relationships of the person.
var caprs = data.childAndParentsRelationships,
rel, familyId, father, fatherId, mother, motherId, childId;
for(var i = 0; i < caprs.length; i++){
rel = caprs[i];
father = rel.father;
fatherId = father ? father.resourceId : '';
mother = rel.mother;
motherId = mother ? mother.resourceId : '';
familyId = fatherId + ':' + motherId;
childId = rel.child.resourceId;
// Ignore relationships involving people that aren't returned in the
// persons list. They are from families of parents or children that this
// person isn't part of. I don't know why the API is returning their relationships.
if(persons[childId]
&& (!fatherId || (fatherId && persons[fatherId]))
&& (!motherId || (motherId && persons[motherId]))){
if(!families[familyId]){
families[familyId] = {
persons: {
father: persons[fatherId],
mother: persons[motherId],
children: []
},
caprIds: []
};
}
families[familyId].persons.children.push(persons[childId]);
families[familyId].caprIds.push(rel.id);
}
}
// Now we put the family objects in their proper category. We know that
// the persons is either a parent or a child in the families so we check
// if the personId is in the familyId [fatherId]:[motherId]. If yes then
// this is a parent family; if not then it's a child family.
for(familyId in families){
if(familyId.indexOf(this.personId) >= 0){
this.push('_parentFamilies', families[familyId]);
} else {
this.push('_childFamilies', families[familyId]);
}
}
}
}
customElements.define(FSPersonFamilies.is, FSPersonFamilies);
</script>
</dom-module>