1
|
|
|
module.exports = function(GedcomX){ |
2
|
|
|
|
3
|
|
|
var utils = require('../utils'), |
4
|
|
|
AtomCommon = require('./AtomCommon'); |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* An individual atom feed entry. |
8
|
|
|
* |
9
|
|
|
* @see {@link https://github.com/FamilySearch/gedcomx-rs/blob/master/specifications/atom-model-specification.md#atom-json-media-type|GEDCOM X Atom JSON Spec} |
10
|
|
|
* @see {@link https://tools.ietf.org/html/rfc4287#section-4.1.2|RFC 4287} |
11
|
|
|
* |
12
|
|
|
* @class AtomEntry |
13
|
|
|
* @extends AtomCommon |
14
|
|
|
* @param {Object} [json] |
15
|
|
|
*/ |
16
|
|
|
var AtomEntry = function(json){ |
17
|
|
|
|
18
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
19
|
|
|
if(!(this instanceof AtomEntry)){ |
20
|
|
|
return new AtomEntry(json); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
24
|
|
|
if(AtomEntry.isInstance(json)){ |
25
|
|
|
return json; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
this.init(json); |
|
|
|
|
29
|
|
|
}; |
30
|
|
|
|
31
|
|
|
AtomEntry.prototype = Object.create(AtomCommon.prototype); |
32
|
|
|
|
33
|
|
|
AtomEntry._gedxClass = AtomEntry.prototype._gedxClass = 'GedcomX.AtomEntry'; |
34
|
|
|
|
35
|
|
|
AtomEntry.jsonProps = [ |
36
|
|
|
'authors', |
37
|
|
|
'contributors', |
38
|
|
|
'categories', |
39
|
|
|
'generator', |
40
|
|
|
'icon', |
41
|
|
|
'id', |
42
|
|
|
'links', |
43
|
|
|
'logo', |
44
|
|
|
'rights', |
45
|
|
|
'subtitle', |
46
|
|
|
'title', |
47
|
|
|
'updated', |
48
|
|
|
'content', |
49
|
|
|
'confidence', |
50
|
|
|
'published', |
51
|
|
|
'source', |
52
|
|
|
'summary', |
53
|
|
|
'score' |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Check whether the given object is an instance of this class. |
58
|
|
|
* |
59
|
|
|
* @param {Object} obj |
60
|
|
|
* @returns {Boolean} |
61
|
|
|
*/ |
62
|
|
|
AtomEntry.isInstance = function(obj){ |
63
|
|
|
return utils.isInstance(obj, this._gedxClass); |
64
|
|
|
}; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Initialize from JSON |
68
|
|
|
* |
69
|
|
|
* @param {Object} |
|
|
|
|
70
|
|
|
* @return {AtomEntry} this |
71
|
|
|
*/ |
72
|
|
|
AtomEntry.prototype.init = function(json){ |
73
|
|
|
|
74
|
|
|
AtomCommon.prototype.init.call(this, json); |
75
|
|
|
|
76
|
|
|
if(json){ |
77
|
|
|
this.setAuthors(json.authors); |
78
|
|
|
this.setContributors(json.contributors); |
79
|
|
|
this.setCategories(json.categories); |
80
|
|
|
this.setGenerator(json.generator); |
81
|
|
|
this.setIcon(json.icon); |
82
|
|
|
this.setId(json.id); |
83
|
|
|
this.setLinks(json.links); |
84
|
|
|
this.setLogo(json.logo); |
85
|
|
|
this.setRights(json.rights); |
86
|
|
|
this.setSubtitle(json.subtitle); |
87
|
|
|
this.setTitle(json.title); |
88
|
|
|
this.setUpdated(json.updated); |
89
|
|
|
this.setContent(json.content); |
90
|
|
|
this.setConfidence(json.confidence); |
91
|
|
|
this.setPublished(json.published); |
92
|
|
|
this.setSource(json.source); |
93
|
|
|
this.setSummary(json.summary); |
94
|
|
|
this.setScore(json.score); |
95
|
|
|
} |
96
|
|
|
return this; |
97
|
|
|
}; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set the authors |
101
|
|
|
* |
102
|
|
|
* @param {AtomPerson[]} authors |
103
|
|
|
* @return {AtomEntry} this |
104
|
|
|
*/ |
105
|
|
|
AtomEntry.prototype.setAuthors = function(authors){ |
106
|
|
|
return this._setArray(authors, 'authors', 'addAuthor'); |
107
|
|
|
}; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Add an author |
111
|
|
|
* |
112
|
|
|
* @param {AtomPerson} author |
113
|
|
|
* @return {AtomEntry} this |
114
|
|
|
*/ |
115
|
|
|
AtomEntry.prototype.addAuthor = function(author){ |
116
|
|
|
return this._arrayPush(author, 'authors', GedcomX.AtomPerson); |
117
|
|
|
}; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the authors |
121
|
|
|
* |
122
|
|
|
* @return {AtomPerson[]} authors |
123
|
|
|
*/ |
124
|
|
|
AtomEntry.prototype.getAuthors = function(){ |
125
|
|
|
return this.authors || []; |
126
|
|
|
}; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set the contributors |
130
|
|
|
* |
131
|
|
|
* @param {AtomPerson[]} contributors |
132
|
|
|
* @return {AtomEntry} this |
133
|
|
|
*/ |
134
|
|
|
AtomEntry.prototype.setContributors = function(contributors){ |
135
|
|
|
return this._setArray(contributors, 'contributors', 'addContributor'); |
136
|
|
|
}; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Add a contributor |
140
|
|
|
* |
141
|
|
|
* @param {AtomPerson} contributor |
142
|
|
|
* @return {AtomEntry} this |
143
|
|
|
*/ |
144
|
|
|
AtomEntry.prototype.addContributor = function(contributor){ |
145
|
|
|
return this._arrayPush(contributor, 'contributors', GedcomX.AtomPerson); |
146
|
|
|
}; |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the contributors |
150
|
|
|
* |
151
|
|
|
* @return {AtomPerson[]} contributors |
152
|
|
|
*/ |
153
|
|
|
AtomEntry.prototype.getContributors = function(){ |
154
|
|
|
return this.contributors || []; |
155
|
|
|
}; |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Set the categories |
159
|
|
|
* |
160
|
|
|
* @param {AtomCategory[]} categories |
161
|
|
|
* @return {AtomEntry} this |
162
|
|
|
*/ |
163
|
|
|
AtomEntry.prototype.setCategories = function(categories){ |
164
|
|
|
return this._setArray(categories, 'categories', 'addCategory'); |
165
|
|
|
}; |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Add a category |
169
|
|
|
* |
170
|
|
|
* @param {AtomCategory} category |
171
|
|
|
* @return {AtomEntry} this |
172
|
|
|
*/ |
173
|
|
|
AtomEntry.prototype.addCategory = function(category){ |
174
|
|
|
return this._arrayPush(category, 'categories', GedcomX.AtomCategory); |
175
|
|
|
}; |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get the categories |
179
|
|
|
* |
180
|
|
|
* @return {AtomCategory[]} categories |
181
|
|
|
*/ |
182
|
|
|
AtomEntry.prototype.getCategories = function(){ |
183
|
|
|
return this.categories || []; |
184
|
|
|
}; |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Set the generator |
188
|
|
|
* |
189
|
|
|
* @param {AtomGenerator} generator |
190
|
|
|
* @return {AtomEntry} |
191
|
|
|
*/ |
192
|
|
|
AtomEntry.prototype.setGenerator = function(generator){ |
193
|
|
|
if(generator){ |
194
|
|
|
this.generator = GedcomX.AtomGenerator(generator); |
195
|
|
|
} |
196
|
|
|
return this; |
197
|
|
|
}; |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get the generator |
201
|
|
|
* |
202
|
|
|
* @return {AtomGenerator} generator |
203
|
|
|
*/ |
204
|
|
|
AtomEntry.prototype.getGenerator = function(){ |
205
|
|
|
return this.generator; |
206
|
|
|
}; |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Set the icon |
210
|
|
|
* |
211
|
|
|
* @param {String} icon |
212
|
|
|
* @return {AtomEntry} |
213
|
|
|
*/ |
214
|
|
|
AtomEntry.prototype.setIcon = function(icon){ |
215
|
|
|
this.icon = icon; |
216
|
|
|
return this; |
217
|
|
|
}; |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get the icon |
221
|
|
|
* |
222
|
|
|
* @return {String} icon |
223
|
|
|
*/ |
224
|
|
|
AtomEntry.prototype.getIcon = function(){ |
225
|
|
|
return this.icon; |
226
|
|
|
}; |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Set the id |
230
|
|
|
* |
231
|
|
|
* @param {String} id |
232
|
|
|
* @return {AtomEntry} |
233
|
|
|
*/ |
234
|
|
|
AtomEntry.prototype.setId = function(id){ |
235
|
|
|
this.id = id; |
236
|
|
|
return this; |
237
|
|
|
}; |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Get the id |
241
|
|
|
* |
242
|
|
|
* @return {String} id |
243
|
|
|
*/ |
244
|
|
|
AtomEntry.prototype.getId = function(){ |
245
|
|
|
return this.id; |
246
|
|
|
}; |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Set the links |
250
|
|
|
* |
251
|
|
|
* @param {Links} links |
252
|
|
|
* @return {AtomEntry} this |
253
|
|
|
*/ |
254
|
|
|
AtomEntry.prototype.setLinks = function(links){ |
255
|
|
|
if(links){ |
256
|
|
|
this.links = GedcomX.Links(links); |
257
|
|
|
} |
258
|
|
|
return this; |
259
|
|
|
}; |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Add a link |
263
|
|
|
* |
264
|
|
|
* @param {Link} link |
265
|
|
|
* @return {AtomEntry} this |
266
|
|
|
*/ |
267
|
|
|
AtomEntry.prototype.addLink = function(link){ |
268
|
|
|
if(link){ |
269
|
|
|
if(!this.links){ |
270
|
|
|
this.links = GedcomX.Links(); |
271
|
|
|
} |
272
|
|
|
this.links.addLink(link); |
273
|
|
|
} |
274
|
|
|
return this; |
275
|
|
|
}; |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Get the links |
279
|
|
|
* |
280
|
|
|
* @return {Link[]} |
281
|
|
|
*/ |
282
|
|
|
AtomEntry.prototype.getLinks = function(){ |
283
|
|
|
return this.links ? this.links.getLinks() : []; |
284
|
|
|
}; |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Get a link |
288
|
|
|
* |
289
|
|
|
* @param {String} rel |
290
|
|
|
* @return {Links} |
291
|
|
|
*/ |
292
|
|
|
AtomEntry.prototype.getLink = function(rel){ |
293
|
|
|
if(this.links){ |
|
|
|
|
294
|
|
|
return this.links.getLink(rel); |
295
|
|
|
} |
296
|
|
|
}; |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Set the logo |
300
|
|
|
* |
301
|
|
|
* @param {String} logo |
302
|
|
|
* @return {AtomEntry} |
303
|
|
|
*/ |
304
|
|
|
AtomEntry.prototype.setLogo = function(logo){ |
305
|
|
|
this.logo = logo; |
306
|
|
|
return this; |
307
|
|
|
}; |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Get the logo |
311
|
|
|
* |
312
|
|
|
* @return {String} logo |
313
|
|
|
*/ |
314
|
|
|
AtomEntry.prototype.getLogo = function(){ |
315
|
|
|
return this.logo; |
316
|
|
|
}; |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Set the rights |
320
|
|
|
* |
321
|
|
|
* @param {String} rights |
322
|
|
|
* @return {AtomEntry} |
323
|
|
|
*/ |
324
|
|
|
AtomEntry.prototype.setRights = function(rights){ |
325
|
|
|
this.rights = rights; |
326
|
|
|
return this; |
327
|
|
|
}; |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Get the rights |
331
|
|
|
* |
332
|
|
|
* @return {String} rights |
333
|
|
|
*/ |
334
|
|
|
AtomEntry.prototype.getRights = function(){ |
335
|
|
|
return this.rights; |
336
|
|
|
}; |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Set the subtitle |
340
|
|
|
* |
341
|
|
|
* @param {String} subtitle |
342
|
|
|
* @return {AtomEntry} |
343
|
|
|
*/ |
344
|
|
|
AtomEntry.prototype.setSubtitle = function(subtitle){ |
345
|
|
|
this.subtitle = subtitle; |
346
|
|
|
return this; |
347
|
|
|
}; |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Get the subtitle |
351
|
|
|
* |
352
|
|
|
* @return {String} subtitle |
353
|
|
|
*/ |
354
|
|
|
AtomEntry.prototype.getSubtitle = function(){ |
355
|
|
|
return this.subtitle; |
356
|
|
|
}; |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Set the title |
360
|
|
|
* |
361
|
|
|
* @param {String} title |
362
|
|
|
* @return {AtomEntry} |
363
|
|
|
*/ |
364
|
|
|
AtomEntry.prototype.setTitle = function(title){ |
365
|
|
|
this.title = title; |
366
|
|
|
return this; |
367
|
|
|
}; |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Get the title |
371
|
|
|
* |
372
|
|
|
* @return {String} title |
373
|
|
|
*/ |
374
|
|
|
AtomEntry.prototype.getTitle = function(){ |
375
|
|
|
return this.title; |
376
|
|
|
}; |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Get the updated timestamp |
380
|
|
|
* |
381
|
|
|
* @returns {Date} updated |
382
|
|
|
*/ |
383
|
|
|
AtomEntry.prototype.getUpdated = function(){ |
384
|
|
|
return this.updated; |
385
|
|
|
}; |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Set the updated timestamp |
389
|
|
|
* |
390
|
|
|
* @param {Date|Number} date Integer timestamp (milliseconds since epoch) or JavaScript Date instance |
391
|
|
|
* @returns {AtomEntry} this |
392
|
|
|
*/ |
393
|
|
|
AtomEntry.prototype.setUpdated = function(date){ |
394
|
|
|
if(date){ |
395
|
|
|
this.updated = new Date(date); |
396
|
|
|
} |
397
|
|
|
return this; |
398
|
|
|
}; |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Set the content |
402
|
|
|
* |
403
|
|
|
* @param {AtomContent} content |
404
|
|
|
* @return {AtomEntry} this |
405
|
|
|
*/ |
406
|
|
|
AtomEntry.prototype.setContent = function(content){ |
407
|
|
|
if(content){ |
408
|
|
|
this.content = GedcomX.AtomContent(content); |
409
|
|
|
} |
410
|
|
|
return this; |
411
|
|
|
}; |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Get the content |
415
|
|
|
* |
416
|
|
|
* @return {AtomContent} content |
417
|
|
|
*/ |
418
|
|
|
AtomEntry.prototype.getContent = function(){ |
419
|
|
|
return this.content; |
420
|
|
|
}; |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Set the confidence |
424
|
|
|
* |
425
|
|
|
* @param {Integer} confidence |
426
|
|
|
* @return {AtomEntry} this |
427
|
|
|
*/ |
428
|
|
|
AtomEntry.prototype.setConfidence = function(confidence){ |
429
|
|
|
this.confidence = confidence; |
430
|
|
|
return this; |
431
|
|
|
}; |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Get the confidence |
435
|
|
|
* |
436
|
|
|
* @return {Integer} confidence |
437
|
|
|
*/ |
438
|
|
|
AtomEntry.prototype.getConfidence = function(){ |
439
|
|
|
return this.confidence; |
440
|
|
|
}; |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Get the published timestamp |
444
|
|
|
* |
445
|
|
|
* @returns {Date} published |
446
|
|
|
*/ |
447
|
|
|
AtomEntry.prototype.getPublished = function(){ |
448
|
|
|
return this.published; |
449
|
|
|
}; |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Set the published timestamp |
453
|
|
|
* |
454
|
|
|
* @param {Date|Number} date Integer timestamp (milliseconds since epoch) or JavaScript Date instance |
455
|
|
|
* @returns {AtomEntry} this |
456
|
|
|
*/ |
457
|
|
|
AtomEntry.prototype.setPublished = function(date){ |
458
|
|
|
if(date){ |
459
|
|
|
this.published = new Date(date); |
460
|
|
|
} |
461
|
|
|
return this; |
462
|
|
|
}; |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Set the source |
466
|
|
|
* |
467
|
|
|
* @param {AtomSource} source |
468
|
|
|
* @return {AtomEntry} this |
469
|
|
|
*/ |
470
|
|
|
AtomEntry.prototype.setSource = function(source){ |
471
|
|
|
if(source){ |
472
|
|
|
this.source = GedcomX.AtomSource(source); |
473
|
|
|
} |
474
|
|
|
return this; |
475
|
|
|
}; |
476
|
|
|
|
477
|
|
|
/** |
478
|
|
|
* Get the source |
479
|
|
|
* |
480
|
|
|
* @return {AtomSource} source |
481
|
|
|
*/ |
482
|
|
|
AtomEntry.prototype.getSource = function(){ |
483
|
|
|
return this.source; |
484
|
|
|
}; |
485
|
|
|
|
486
|
|
|
/** |
487
|
|
|
* Set the summary |
488
|
|
|
* |
489
|
|
|
* @param {String} summary |
490
|
|
|
* @return {AtomEntry} this |
491
|
|
|
*/ |
492
|
|
|
AtomEntry.prototype.setSummary = function(summary){ |
493
|
|
|
this.summary = summary; |
494
|
|
|
return this; |
495
|
|
|
}; |
496
|
|
|
|
497
|
|
|
/** |
498
|
|
|
* Get the summary |
499
|
|
|
* |
500
|
|
|
* @return {String} summary |
501
|
|
|
*/ |
502
|
|
|
AtomEntry.prototype.getSummary = function(){ |
503
|
|
|
return this.summary; |
504
|
|
|
}; |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Set the score |
508
|
|
|
* |
509
|
|
|
* @param {Number} score |
510
|
|
|
* @return {AtomEntry} this |
511
|
|
|
*/ |
512
|
|
|
AtomEntry.prototype.setScore = function(score){ |
513
|
|
|
this.score = score; |
514
|
|
|
return this; |
515
|
|
|
}; |
516
|
|
|
|
517
|
|
|
/** |
518
|
|
|
* Get the score |
519
|
|
|
* |
520
|
|
|
* @return {Number} score |
521
|
|
|
*/ |
522
|
|
|
AtomEntry.prototype.getScore = function(){ |
523
|
|
|
return this.score; |
524
|
|
|
}; |
525
|
|
|
|
526
|
|
|
/** |
527
|
|
|
* Export the object as JSON |
528
|
|
|
* |
529
|
|
|
* @return {Object} JSON object |
530
|
|
|
*/ |
531
|
|
|
AtomEntry.prototype.toJSON = function(){ |
532
|
|
|
return this._toJSON(AtomCommon, AtomEntry.jsonProps); |
533
|
|
|
}; |
534
|
|
|
|
535
|
|
|
GedcomX.AtomEntry = AtomEntry; |
536
|
|
|
|
537
|
|
|
}; |