1
|
|
|
var utils = require('../utils'), |
2
|
|
|
GedcomX = require('../'); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* A GEDCOM X document. |
6
|
|
|
* |
7
|
|
|
* @see {@link https://github.com/FamilySearch/gedcomx/blob/master/specifications/json-format-specification.md#gedcomx-type|GEDCOM X JSON Spec} |
8
|
|
|
* |
9
|
|
|
* @class |
10
|
|
|
* @extends ExtensibleData |
11
|
|
|
* @param {Object} [json] |
12
|
|
|
*/ |
13
|
|
|
var Root = function(json){ |
14
|
|
|
|
15
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
16
|
|
|
if(!(this instanceof Root)){ |
17
|
|
|
return new Root(json); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
21
|
|
|
if(Root.isInstance(json)){ |
22
|
|
|
return json; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
this.init(json); |
|
|
|
|
26
|
|
|
}; |
27
|
|
|
|
28
|
|
|
Root.prototype = Object.create(GedcomX.ExtensibleData.prototype); |
29
|
|
|
|
30
|
|
|
Root._gedxClass = Root.prototype._gedxClass = 'Root'; |
31
|
|
|
|
32
|
|
|
Root.jsonProps = [ |
33
|
|
|
'lang', |
34
|
|
|
'description', |
35
|
|
|
'persons', |
36
|
|
|
'relationships', |
37
|
|
|
'sourceDescriptions', |
38
|
|
|
'agents', |
39
|
|
|
'events', |
40
|
|
|
'documents', |
41
|
|
|
'places', |
42
|
|
|
'attribution' |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Check whether the given object is an instance of this class. |
47
|
|
|
* |
48
|
|
|
* @param {Object} obj |
49
|
|
|
* @returns {Boolean} |
50
|
|
|
*/ |
51
|
|
|
Root.isInstance = function(obj){ |
52
|
|
|
return utils.isInstance(obj, this._gedxClass); |
53
|
|
|
}; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Initialize from JSON |
57
|
|
|
* |
58
|
|
|
* @param {Object} |
|
|
|
|
59
|
|
|
* @return {Root} this |
60
|
|
|
*/ |
61
|
|
|
Root.prototype.init = function(json){ |
62
|
|
|
|
63
|
|
|
GedcomX.ExtensibleData.prototype.init.call(this, json); |
64
|
|
|
|
65
|
|
|
if(json){ |
66
|
|
|
this.setLang(json.lang); |
67
|
|
|
this.setPersons(json.persons); |
68
|
|
|
this.setRelationships(json.relationships); |
69
|
|
|
this.setSourceDescriptions(json.sourceDescriptions); |
70
|
|
|
this.setAgents(json.agents); |
71
|
|
|
this.setEvents(json.events); |
72
|
|
|
this.setDocuments(json.documents); |
73
|
|
|
this.setPlaces(json.places); |
74
|
|
|
this.setAttribution(json.attribution); |
75
|
|
|
this.setDescription(json.description); |
76
|
|
|
} |
77
|
|
|
return this; |
78
|
|
|
}; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the lang |
82
|
|
|
* |
83
|
|
|
* @return {String} |
84
|
|
|
*/ |
85
|
|
|
Root.prototype.getLang = function(){ |
86
|
|
|
return this.lang; |
87
|
|
|
}; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set the lang |
91
|
|
|
* |
92
|
|
|
* @param {String} lang |
93
|
|
|
* @return {Root} This instance |
94
|
|
|
*/ |
95
|
|
|
Root.prototype.setLang = function(lang){ |
96
|
|
|
if(lang){ |
97
|
|
|
this.lang = lang; |
98
|
|
|
} |
99
|
|
|
return this; |
100
|
|
|
}; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the description |
104
|
|
|
* |
105
|
|
|
* @return {String} |
106
|
|
|
*/ |
107
|
|
|
Root.prototype.getDescription = function(){ |
108
|
|
|
return this.description; |
109
|
|
|
}; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set the description |
113
|
|
|
* |
114
|
|
|
* @param {String} description URI that must resolve to a SourceDescription |
115
|
|
|
* @return {Root} This instance |
116
|
|
|
*/ |
117
|
|
|
Root.prototype.setDescription = function(description){ |
118
|
|
|
if(description){ |
119
|
|
|
this.description = description; |
120
|
|
|
} |
121
|
|
|
return this; |
122
|
|
|
}; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the persons |
126
|
|
|
* |
127
|
|
|
* @returns {Person[]} |
128
|
|
|
*/ |
129
|
|
|
Root.prototype.getPersons = function(){ |
130
|
|
|
return this.persons || []; |
131
|
|
|
}; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set the persons |
135
|
|
|
* |
136
|
|
|
* @param {Person[]|Object[]} persons |
137
|
|
|
* @returns {Root} This instance |
138
|
|
|
*/ |
139
|
|
|
Root.prototype.setPersons = function(persons){ |
140
|
|
|
return this._setArray(persons, 'persons', 'addPerson'); |
141
|
|
|
}; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Add a person |
145
|
|
|
* |
146
|
|
|
* @param {Person|Object} |
|
|
|
|
147
|
|
|
* @returns {Root} This instance |
148
|
|
|
*/ |
149
|
|
|
Root.prototype.addPerson = function(person){ |
150
|
|
|
return this._arrayPush(person, 'persons', GedcomX.Person); |
151
|
|
|
}; |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the person matching a given ID. |
155
|
|
|
* |
156
|
|
|
* @param {String|Integer} id Person ID |
157
|
|
|
* @return {Person} Person matching the given ID. |
158
|
|
|
*/ |
159
|
|
|
Root.prototype.getPersonById = function(id){ |
160
|
|
|
return this.getPersons().find(function(p){ |
161
|
|
|
return p.getId() === id; |
162
|
|
|
}); |
163
|
|
|
}; |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get all relationships |
167
|
|
|
* |
168
|
|
|
* @returns {Relationship[]} |
169
|
|
|
*/ |
170
|
|
|
Root.prototype.getRelationships = function(){ |
171
|
|
|
return this.relationships || []; |
172
|
|
|
}; |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the relationships involving a specific person |
176
|
|
|
* |
177
|
|
|
* @param {Person|String} person Person or person ID |
178
|
|
|
* @return {Relationship[]} Person's relationships. |
179
|
|
|
*/ |
180
|
|
|
Root.prototype.getPersonsRelationships = function(person){ |
181
|
|
|
return this.getRelationships().filter(function(rel){ |
182
|
|
|
return rel.involvesPerson(person); |
183
|
|
|
}); |
184
|
|
|
}; |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get a person's parent relationships, meaning relationships where the person |
188
|
|
|
* is a child (relationships with their parents). |
189
|
|
|
* |
190
|
|
|
* @param {Person|String} person Person or person ID |
191
|
|
|
* @return {Relationship[]} |
192
|
|
|
*/ |
193
|
|
|
Root.prototype.getPersonsParentRelationships = function(person){ |
194
|
|
|
return this.getPersonsRelationships(person).filter(function(rel){ |
195
|
|
|
return rel.getType() === 'http://gedcomx.org/ParentChild' && rel.getPerson2().matches(person); |
196
|
|
|
}); |
197
|
|
|
}; |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get a person's parents. |
201
|
|
|
* |
202
|
|
|
* @param {Person|String} person Person or person ID |
203
|
|
|
* @return {Person[]} |
204
|
|
|
*/ |
205
|
|
|
Root.prototype.getPersonsParents = function(person){ |
206
|
|
|
var root = this; |
207
|
|
|
return this.getPersonsParentRelationships(person).map(function(rel){ |
208
|
|
|
return root.getPersonById(rel.getPerson1().getResource().substring(1)); |
209
|
|
|
}) |
210
|
|
|
|
211
|
|
|
// Remove any falsy values from the array. That can happen if the rel -> person |
212
|
|
|
// mapping fails to find a matching person. |
213
|
|
|
.filter(Boolean); |
214
|
|
|
}; |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get a person's couple relationships. |
218
|
|
|
* |
219
|
|
|
* @param {Person|String} person Person or person ID |
220
|
|
|
* @return {Relationship[]} |
221
|
|
|
*/ |
222
|
|
|
Root.prototype.getPersonsCoupleRelationships = function(person){ |
223
|
|
|
return this.getPersonsRelationships(person).filter(function(rel){ |
224
|
|
|
return rel.getType() === 'http://gedcomx.org/Couple'; |
225
|
|
|
}); |
226
|
|
|
}; |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Get a person's spouses. |
230
|
|
|
* |
231
|
|
|
* @param {Person|String} person Person or person ID |
232
|
|
|
* @return {Person[]} |
233
|
|
|
*/ |
234
|
|
|
Root.prototype.getPersonsSpouses = function(person){ |
235
|
|
|
var root = this; |
236
|
|
|
return this.getPersonsCoupleRelationships(person).map(function(rel){ |
237
|
|
|
return root.getPersonById(rel.getOtherPerson(person).getResource().substring(1)); |
238
|
|
|
}) |
239
|
|
|
|
240
|
|
|
// Remove any falsy values from the array. That can happen if the rel -> person |
241
|
|
|
// mapping fails to find a matching person. |
242
|
|
|
.filter(Boolean); |
243
|
|
|
}; |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Get a person's child relationships, meaning relationships where the person |
247
|
|
|
* is a parent (relationships with their children). |
248
|
|
|
* |
249
|
|
|
* @param {Person|String} person Person or person ID |
250
|
|
|
* @return {Relationship[]} |
251
|
|
|
*/ |
252
|
|
|
Root.prototype.getPersonsChildRelationships = function(person){ |
253
|
|
|
return this.getPersonsRelationships(person).filter(function(rel){ |
254
|
|
|
return rel.getType() === 'http://gedcomx.org/ParentChild' && rel.getPerson1().matches(person); |
255
|
|
|
}); |
256
|
|
|
}; |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Get a person's children. |
260
|
|
|
* |
261
|
|
|
* @param {Person|String} person Person or person ID |
262
|
|
|
* @return {Person[]} |
263
|
|
|
*/ |
264
|
|
|
Root.prototype.getPersonsChildren = function(person){ |
265
|
|
|
var root = this; |
266
|
|
|
return this.getPersonsChildRelationships(person).map(function(rel){ |
267
|
|
|
return root.getPersonById(rel.getPerson2().getResource().substring(1)); |
268
|
|
|
}) |
269
|
|
|
|
270
|
|
|
// Remove any falsy values from the array. That can happen if the rel -> person |
271
|
|
|
// mapping fails to find a matching person. |
272
|
|
|
.filter(Boolean); |
273
|
|
|
}; |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Set the relationships |
277
|
|
|
* |
278
|
|
|
* @param {Relationship[]|Object[]} relationships |
279
|
|
|
* @returns {Root} |
280
|
|
|
*/ |
281
|
|
|
Root.prototype.setRelationships = function(relationships){ |
282
|
|
|
return this._setArray(relationships, 'relationships', 'addRelationship'); |
283
|
|
|
}; |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Add a relationship |
287
|
|
|
* |
288
|
|
|
* @param {Relationship|Object} relationship |
289
|
|
|
* @returns {Root} |
290
|
|
|
*/ |
291
|
|
|
Root.prototype.addRelationship = function(relationship){ |
292
|
|
|
return this._arrayPush(relationship, 'relationships', GedcomX.Relationship); |
293
|
|
|
}; |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Get the source descriptions |
297
|
|
|
* |
298
|
|
|
* @returns {SourceDescription[]} |
299
|
|
|
*/ |
300
|
|
|
Root.prototype.getSourceDescriptions = function(){ |
301
|
|
|
return this.sourceDescriptions || []; |
302
|
|
|
}; |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Set the source descriptions |
306
|
|
|
* |
307
|
|
|
* @param {SourceDescription[]|Object[]} sourceDescriptions |
308
|
|
|
* @returns {Root} |
309
|
|
|
*/ |
310
|
|
|
Root.prototype.setSourceDescriptions = function(sourceDescriptions){ |
311
|
|
|
return this._setArray(sourceDescriptions, 'sourceDescriptions', 'addSourceDescription'); |
312
|
|
|
}; |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Add a ource description |
316
|
|
|
* |
317
|
|
|
* @param {SourceDescription|Object} sourceDescription |
318
|
|
|
* @returns {Root} |
319
|
|
|
*/ |
320
|
|
|
Root.prototype.addSourceDescription = function(sourceDescription){ |
321
|
|
|
return this._arrayPush(sourceDescription, 'sourceDescriptions', GedcomX.SourceDescription); |
322
|
|
|
}; |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Get the agents |
326
|
|
|
* |
327
|
|
|
* @returns {Agent[]} |
328
|
|
|
*/ |
329
|
|
|
Root.prototype.getAgents = function(){ |
330
|
|
|
return this.agents || []; |
331
|
|
|
}; |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Set the agents |
335
|
|
|
* |
336
|
|
|
* @param {Agent[]|Object[]} agents |
337
|
|
|
* @returns {Root} |
338
|
|
|
*/ |
339
|
|
|
Root.prototype.setAgents = function(agents){ |
340
|
|
|
return this._setArray(agents, 'agents', 'addAgent'); |
341
|
|
|
}; |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Add an agent |
345
|
|
|
* |
346
|
|
|
* @param {Agent|Object} |
|
|
|
|
347
|
|
|
* @returns {Root} |
348
|
|
|
*/ |
349
|
|
|
Root.prototype.addAgent = function(agent){ |
350
|
|
|
return this._arrayPush(agent, 'agents', GedcomX.Agent); |
351
|
|
|
}; |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Get events |
355
|
|
|
* |
356
|
|
|
* @returns {Event[]} |
357
|
|
|
*/ |
358
|
|
|
Root.prototype.getEvents = function(){ |
359
|
|
|
return this.events || []; |
360
|
|
|
}; |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Set events |
364
|
|
|
* |
365
|
|
|
* @param {Event[]|Object[]} events |
366
|
|
|
* @returns {Root} |
367
|
|
|
*/ |
368
|
|
|
Root.prototype.setEvents = function(events){ |
369
|
|
|
return this._setArray(events, 'events', 'addEvent'); |
370
|
|
|
}; |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Add an event |
374
|
|
|
* |
375
|
|
|
* @param {Event|Object} event |
376
|
|
|
* @returns {Root} |
377
|
|
|
*/ |
378
|
|
|
Root.prototype.addEvent = function(event){ |
379
|
|
|
return this._arrayPush(event, 'events', GedcomX.Event); |
380
|
|
|
}; |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Get the documents |
384
|
|
|
* |
385
|
|
|
* @returns {Document[]} |
386
|
|
|
*/ |
387
|
|
|
Root.prototype.getDocuments = function(){ |
388
|
|
|
return this.documents || []; |
389
|
|
|
}; |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Set the documents |
393
|
|
|
* |
394
|
|
|
* @param {Documents[]|Object[]} documents |
395
|
|
|
* @returns {Root} |
396
|
|
|
*/ |
397
|
|
|
Root.prototype.setDocuments = function(documents){ |
398
|
|
|
return this._setArray(documents, 'documents', 'addDocument'); |
399
|
|
|
}; |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Add a document |
403
|
|
|
* |
404
|
|
|
* @param {Document|Object} doc |
405
|
|
|
* @returns {Root} |
406
|
|
|
*/ |
407
|
|
|
Root.prototype.addDocument = function(doc){ |
408
|
|
|
return this._arrayPush(doc, 'documents', GedcomX.Document); |
409
|
|
|
}; |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Get places |
413
|
|
|
* |
414
|
|
|
* @returns {PlaceDescription[]} |
415
|
|
|
*/ |
416
|
|
|
Root.prototype.getPlaces = function(){ |
417
|
|
|
return this.places || []; |
418
|
|
|
}; |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Set the places |
422
|
|
|
* |
423
|
|
|
* @param {PlaceDescription[]|Object} places |
424
|
|
|
* @returns {Root} |
425
|
|
|
*/ |
426
|
|
|
Root.prototype.setPlaces = function(places){ |
427
|
|
|
return this._setArray(places, 'places', 'addPlace'); |
428
|
|
|
}; |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Add a place |
432
|
|
|
* |
433
|
|
|
* @param {PlaceDescription} place |
434
|
|
|
* @returns {Root} |
435
|
|
|
*/ |
436
|
|
|
Root.prototype.addPlace = function(place){ |
437
|
|
|
return this._arrayPush(place, 'places', GedcomX.PlaceDescription); |
438
|
|
|
}; |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* Get attritbution |
442
|
|
|
* |
443
|
|
|
* @returns {Attribution} |
444
|
|
|
*/ |
445
|
|
|
Root.prototype.getAttribution = function(){ |
446
|
|
|
return this.attribution; |
447
|
|
|
}; |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* Set attribution |
451
|
|
|
* |
452
|
|
|
* @param {Attribution} attribution |
453
|
|
|
* @returns {Root} |
454
|
|
|
*/ |
455
|
|
|
Root.prototype.setAttribution = function(attribution){ |
456
|
|
|
if(attribution){ |
457
|
|
|
this.attribution = GedcomX.Attribution(attribution); |
458
|
|
|
} |
459
|
|
|
return this; |
460
|
|
|
}; |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* Export the object as JSON |
464
|
|
|
* |
465
|
|
|
* @return {Object} JSON object |
466
|
|
|
*/ |
467
|
|
|
Root.prototype.toJSON = function(){ |
468
|
|
|
return this._toJSON(GedcomX.ExtensibleData, Root.jsonProps); |
469
|
|
|
}; |
470
|
|
|
|
471
|
|
|
module.exports = Root; |