1
|
|
|
var GedcomX = require('../'), |
2
|
|
|
utils = require('../utils'); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Someone or something that curates genealogical data, such as a genealogical |
6
|
|
|
* researcher, user of software, or organization. |
7
|
|
|
* |
8
|
|
|
* @see {@link https://github.com/FamilySearch/gedcomx/blob/master/specifications/json-format-specification.md#agent|GEDCOM X JSON Spec} |
9
|
|
|
* |
10
|
|
|
* @class |
11
|
|
|
* @extends ExtensibleData |
12
|
|
|
* @param {Object} [json] |
13
|
|
|
*/ |
14
|
|
|
var Agent = function(json){ |
15
|
|
|
|
16
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
17
|
|
|
if(!(this instanceof Agent)){ |
18
|
|
|
return new Agent(json); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
22
|
|
|
if(Agent.isInstance(json)){ |
23
|
|
|
return json; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
this.init(json); |
|
|
|
|
27
|
|
|
}; |
28
|
|
|
|
29
|
|
|
Agent.prototype = Object.create(GedcomX.ExtensibleData.prototype); |
30
|
|
|
|
31
|
|
|
Agent._gedxClass = Agent.prototype._gedxClass = 'GedcomX.Agent'; |
32
|
|
|
|
33
|
|
|
Agent.jsonProps = [ |
34
|
|
|
'identifiers', |
35
|
|
|
'names', |
36
|
|
|
'homepage', |
37
|
|
|
'openid', |
38
|
|
|
'accounts', |
39
|
|
|
'emails', |
40
|
|
|
'phones', |
41
|
|
|
'addresses', |
42
|
|
|
'person' |
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
|
|
|
Agent.isInstance = function(obj){ |
52
|
|
|
return utils.isInstance(obj, this._gedxClass); |
53
|
|
|
}; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Initialize from JSON |
57
|
|
|
* |
58
|
|
|
* @param {Object} |
|
|
|
|
59
|
|
|
* @return {Agent} this |
60
|
|
|
*/ |
61
|
|
|
Agent.prototype.init = function(json){ |
62
|
|
|
|
63
|
|
|
GedcomX.ExtensibleData.prototype.init.call(this, json); |
64
|
|
|
|
65
|
|
|
if(json){ |
66
|
|
|
this.setIdentifiers(json.identifiers); |
67
|
|
|
this.setNames(json.names); |
68
|
|
|
this.setHomepage(json.homepage); |
69
|
|
|
this.setOpenid(json.openid); |
70
|
|
|
this.setAccounts(json.accounts); |
71
|
|
|
this.setEmails(json.emails); |
72
|
|
|
this.setPhones(json.phones); |
73
|
|
|
this.setAddresses(json.addresses); |
74
|
|
|
this.setPerson(json.person); |
75
|
|
|
} |
76
|
|
|
return this; |
77
|
|
|
}; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the identifiers |
81
|
|
|
* |
82
|
|
|
* @returns {Identifiers} |
83
|
|
|
*/ |
84
|
|
|
Agent.prototype.getIdentifiers = function(){ |
85
|
|
|
return this.identifiers; |
86
|
|
|
}; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set the identifiers |
90
|
|
|
* |
91
|
|
|
* @param {Identifiers} identifiers |
92
|
|
|
* @returns {Agent} |
93
|
|
|
*/ |
94
|
|
|
Agent.prototype.setIdentifiers = function(identifiers){ |
95
|
|
|
if(identifiers){ |
96
|
|
|
this.identifiers = GedcomX.Identifiers(identifiers); |
97
|
|
|
} |
98
|
|
|
return this; |
99
|
|
|
}; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the names |
103
|
|
|
* |
104
|
|
|
* @returns {TextValue[]} |
105
|
|
|
*/ |
106
|
|
|
Agent.prototype.getNames = function(){ |
107
|
|
|
return this.names || []; |
108
|
|
|
}; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set the names |
112
|
|
|
* |
113
|
|
|
* @param {TextValue[]|Object[]} names |
114
|
|
|
* @returns {Agent} |
115
|
|
|
*/ |
116
|
|
|
Agent.prototype.setNames = function(names){ |
117
|
|
|
return this._setArray(names, 'names', 'addName'); |
118
|
|
|
}; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Add a name |
122
|
|
|
* |
123
|
|
|
* @param {TextValue|Object} name |
124
|
|
|
* @return {Agent} |
125
|
|
|
*/ |
126
|
|
|
Agent.prototype.addName = function(name){ |
127
|
|
|
return this._arrayPush(name, 'names', GedcomX.TextValue); |
128
|
|
|
}; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the home page |
132
|
|
|
* |
133
|
|
|
* @returns {ResourceReference} |
134
|
|
|
*/ |
135
|
|
|
Agent.prototype.getHomepage = function(){ |
136
|
|
|
return this.homepage; |
137
|
|
|
}; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set the home page |
141
|
|
|
* |
142
|
|
|
* @param {ResourceReference|Object} homepage |
143
|
|
|
* @returns {Agent} |
144
|
|
|
*/ |
145
|
|
|
Agent.prototype.setHomepage = function(homepage){ |
146
|
|
|
if(homepage){ |
147
|
|
|
this.homepage = GedcomX.ResourceReference(homepage); |
148
|
|
|
} |
149
|
|
|
return this; |
150
|
|
|
}; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get the openid |
154
|
|
|
* |
155
|
|
|
* @returns {ResourceReference} |
156
|
|
|
*/ |
157
|
|
|
Agent.prototype.getOpenid = function(){ |
158
|
|
|
return this.openid; |
159
|
|
|
}; |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set the openid |
163
|
|
|
* |
164
|
|
|
* @params {ResourceReference} openid |
165
|
|
|
* @returns {Agent} |
166
|
|
|
*/ |
167
|
|
|
Agent.prototype.setOpenid = function(openid){ |
168
|
|
|
if(openid){ |
169
|
|
|
this.openid = GedcomX.ResourceReference(openid); |
170
|
|
|
} |
171
|
|
|
return this; |
172
|
|
|
}; |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the accounts |
176
|
|
|
* |
177
|
|
|
* @returns {OnlineAccount[]} |
178
|
|
|
*/ |
179
|
|
|
Agent.prototype.getAccounts = function(){ |
180
|
|
|
return this.accounts || []; |
181
|
|
|
}; |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set the accounts |
185
|
|
|
* |
186
|
|
|
* @param {OnlineAccount[]|Object[]} accounts |
187
|
|
|
* @returns {Agent} |
188
|
|
|
*/ |
189
|
|
|
Agent.prototype.setAccounts = function(accounts){ |
190
|
|
|
return this._setArray(accounts, 'accounts', 'addAccount'); |
191
|
|
|
}; |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Add an account |
195
|
|
|
* |
196
|
|
|
* @param {OnlineAccount|Object} account |
197
|
|
|
* @returns {Agent} |
198
|
|
|
*/ |
199
|
|
|
Agent.prototype.addAccount = function(account){ |
200
|
|
|
return this._arrayPush(account, 'accounts', GedcomX.OnlineAccount); |
201
|
|
|
}; |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get the emails |
205
|
|
|
* |
206
|
|
|
* @returns {ResourceReference[]} |
207
|
|
|
*/ |
208
|
|
|
Agent.prototype.getEmails= function(){ |
209
|
|
|
return this.emails || []; |
210
|
|
|
}; |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Set the emails |
214
|
|
|
* |
215
|
|
|
* @param {ResourceReference[]|Object[]} emails |
216
|
|
|
* @returns {Agent} |
217
|
|
|
*/ |
218
|
|
|
Agent.prototype.setEmails = function(emails){ |
219
|
|
|
return this._setArray(emails, 'emails', 'addEmail'); |
220
|
|
|
}; |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Add an email |
224
|
|
|
* |
225
|
|
|
* @param {ResourceReference|Object} email |
226
|
|
|
* @returns {Agent} |
227
|
|
|
*/ |
228
|
|
|
Agent.prototype.addEmail = function(email){ |
229
|
|
|
return this._arrayPush(email, 'emails', GedcomX.ResourceReference); |
230
|
|
|
}; |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get the phones |
234
|
|
|
* |
235
|
|
|
* @returns {ResourceReference[]} |
236
|
|
|
*/ |
237
|
|
|
Agent.prototype.getPhones = function(){ |
238
|
|
|
return this.phones || []; |
239
|
|
|
}; |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Set the phones |
243
|
|
|
* |
244
|
|
|
* @param {ResourceReference[]|Object[]} phones |
245
|
|
|
* @returns {Agent} |
246
|
|
|
*/ |
247
|
|
|
Agent.prototype.setPhones = function(phones){ |
248
|
|
|
return this._setArray(phones, 'phones', 'addPhone'); |
249
|
|
|
}; |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Add a phone |
253
|
|
|
* |
254
|
|
|
* @param {ResourceReference|Object} phone |
255
|
|
|
* @returns {Agent} |
256
|
|
|
*/ |
257
|
|
|
Agent.prototype.addPhone = function(phone){ |
258
|
|
|
return this._arrayPush(phone, 'phones', GedcomX.ResourceReference); |
259
|
|
|
}; |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Get the addresses |
263
|
|
|
* |
264
|
|
|
* @returns {Address[]} |
265
|
|
|
*/ |
266
|
|
|
Agent.prototype.getAddresses = function(){ |
267
|
|
|
return this.addresses || []; |
268
|
|
|
}; |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Set the addresses |
272
|
|
|
* |
273
|
|
|
* @param {Address[]|Object[]} addresses |
274
|
|
|
* @returns {Agent} |
275
|
|
|
*/ |
276
|
|
|
Agent.prototype.setAddresses = function(addresses){ |
277
|
|
|
return this._setArray(addresses, 'addresses', 'addAddress'); |
278
|
|
|
}; |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Add an address |
282
|
|
|
* |
283
|
|
|
* @param {Address|Object} address |
284
|
|
|
* @returns {Agent} |
285
|
|
|
*/ |
286
|
|
|
Agent.prototype.addAddress = function(address){ |
287
|
|
|
return this._arrayPush(address, 'addresses', GedcomX.Address); |
288
|
|
|
}; |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Get the person reference |
292
|
|
|
* |
293
|
|
|
* @returns {ResourceReference} |
294
|
|
|
*/ |
295
|
|
|
Agent.prototype.getPerson = function(){ |
296
|
|
|
return this.person; |
297
|
|
|
}; |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Set the person reference |
301
|
|
|
* |
302
|
|
|
* @param {ResourceReference} person |
303
|
|
|
* @returns {Agent} |
304
|
|
|
*/ |
305
|
|
|
Agent.prototype.setPerson = function(person){ |
306
|
|
|
if(person){ |
307
|
|
|
this.person = GedcomX.ResourceReference(person); |
308
|
|
|
} |
309
|
|
|
return this; |
310
|
|
|
}; |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Export the object as JSON |
314
|
|
|
* |
315
|
|
|
* @return {Object} JSON object |
316
|
|
|
*/ |
317
|
|
|
Agent.prototype.toJSON = function(){ |
318
|
|
|
return this._toJSON(GedcomX.ExtensibleData, Agent.jsonProps); |
319
|
|
|
}; |
320
|
|
|
|
321
|
|
|
module.exports = Agent; |