|
1
|
|
|
var Subject = require('./Subject'), |
|
2
|
|
|
ResourceReference = require('./ResourceReference'), |
|
3
|
|
|
TextValue = require('./TextValue'), |
|
4
|
|
|
GDate = require('./Date'), |
|
5
|
|
|
utils = require('./utils'); |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* A description of a place |
|
9
|
|
|
* |
|
10
|
|
|
* @constructor |
|
11
|
|
|
* @param {Object} [json] |
|
|
|
|
|
|
12
|
|
|
*/ |
|
13
|
|
|
var PlaceDescription = function(json){ |
|
14
|
|
|
|
|
15
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
|
16
|
|
|
if(!(this instanceof PlaceDescription)){ |
|
17
|
|
|
return new PlaceDescription(json); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
|
21
|
|
|
if(PlaceDescription.isInstance(json)){ |
|
22
|
|
|
return json; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
Subject.call(this, json); |
|
26
|
|
|
|
|
27
|
|
|
if(json){ |
|
|
|
|
|
|
28
|
|
|
this.setType(json.type); |
|
29
|
|
|
this.setNames(json.names); |
|
30
|
|
|
this.setPlace(json.place); |
|
31
|
|
|
this.setJurisdiction(json.jurisdiction); |
|
32
|
|
|
this.setTemporalDescription(json.temporalDescription); |
|
33
|
|
|
this.setSpatialDescription(json.spatialDescription); |
|
34
|
|
|
this.setLatitude(json.latitude); |
|
35
|
|
|
this.setLongitude(json.longitude); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
}; |
|
38
|
|
|
|
|
39
|
|
|
PlaceDescription.prototype = Object.create(Subject.prototype); |
|
40
|
|
|
|
|
41
|
|
|
PlaceDescription._gedxClass = PlaceDescription.prototype._gedxClass = 'GedcomX.PlaceDescription'; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Check whether the given object is an instance of this class. |
|
45
|
|
|
* |
|
46
|
|
|
* @param {Object} obj |
|
47
|
|
|
* @returns {Boolean} |
|
48
|
|
|
*/ |
|
49
|
|
|
PlaceDescription.isInstance = function(obj){ |
|
50
|
|
|
return utils.isInstance(obj, this._gedxClass); |
|
51
|
|
|
}; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get the type |
|
55
|
|
|
* |
|
56
|
|
|
* @returns {String} |
|
57
|
|
|
*/ |
|
58
|
|
|
PlaceDescription.prototype.getType = function(){ |
|
59
|
|
|
return this.type; |
|
60
|
|
|
}; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Set the type |
|
64
|
|
|
* |
|
65
|
|
|
* @param {String} type |
|
66
|
|
|
* @returns {PlaceDescription} |
|
67
|
|
|
*/ |
|
68
|
|
|
PlaceDescription.prototype.setType = function(type){ |
|
69
|
|
|
this.type = type; |
|
70
|
|
|
return this; |
|
71
|
|
|
}; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the names |
|
75
|
|
|
* |
|
76
|
|
|
* @returns {TextValue[]} |
|
77
|
|
|
*/ |
|
78
|
|
|
PlaceDescription.prototype.getNames = function(){ |
|
79
|
|
|
return this.names || []; |
|
80
|
|
|
}; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Set the names |
|
84
|
|
|
* |
|
85
|
|
|
* @param {TextValue[]|Object[]} names |
|
86
|
|
|
* @returns {PlaceDescription} |
|
87
|
|
|
*/ |
|
88
|
|
|
PlaceDescription.prototype.setNames = function(names){ |
|
89
|
|
|
if(Array.isArray(names)){ |
|
90
|
|
|
var place = this; |
|
91
|
|
|
place.names = []; |
|
92
|
|
|
names.forEach(function(n){ |
|
93
|
|
|
place.addName(n); |
|
94
|
|
|
}); |
|
95
|
|
|
} |
|
96
|
|
|
return this; |
|
97
|
|
|
}; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Add the name |
|
101
|
|
|
* |
|
102
|
|
|
* @param {TextValue|Object} name |
|
103
|
|
|
* @returns {PlaceDescription} |
|
104
|
|
|
*/ |
|
105
|
|
|
PlaceDescription.prototype.addName = function(name){ |
|
106
|
|
|
if(name){ |
|
107
|
|
|
if(!Array.isArray(this.names)){ |
|
108
|
|
|
this.names = []; |
|
109
|
|
|
} |
|
110
|
|
|
this.names.push(TextValue(name)); |
|
111
|
|
|
} |
|
112
|
|
|
return this; |
|
113
|
|
|
}; |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get the place |
|
117
|
|
|
* |
|
118
|
|
|
* @returns {ResourceReference} |
|
119
|
|
|
*/ |
|
120
|
|
|
PlaceDescription.prototype.getPlace = function(){ |
|
121
|
|
|
return this.place; |
|
122
|
|
|
}; |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Set the place |
|
126
|
|
|
* |
|
127
|
|
|
* @param {ResourceReference} place |
|
128
|
|
|
* @returns {PlaceDescription} |
|
129
|
|
|
*/ |
|
130
|
|
|
PlaceDescription.prototype.setPlace = function(place){ |
|
131
|
|
|
if(place){ |
|
132
|
|
|
this.place = ResourceReference(place); |
|
133
|
|
|
} |
|
134
|
|
|
return this; |
|
135
|
|
|
}; |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Get the jurisdiction |
|
139
|
|
|
* |
|
140
|
|
|
* @returns {ResourceReference} |
|
141
|
|
|
*/ |
|
142
|
|
|
PlaceDescription.prototype.getJurisdiction = function(){ |
|
143
|
|
|
return this.jurisdiction; |
|
144
|
|
|
}; |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Set the jurisdiction |
|
148
|
|
|
* |
|
149
|
|
|
* @param {ResourceReference} jurisdiction |
|
150
|
|
|
* @returns {PlaceDescription} |
|
151
|
|
|
*/ |
|
152
|
|
|
PlaceDescription.prototype.setJurisdiction = function(jurisdiction){ |
|
153
|
|
|
if(jurisdiction){ |
|
154
|
|
|
this.jurisdiction = ResourceReference(jurisdiction); |
|
155
|
|
|
} |
|
156
|
|
|
return this; |
|
157
|
|
|
}; |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Get the latitude |
|
161
|
|
|
* |
|
162
|
|
|
* @returns {Number} |
|
163
|
|
|
*/ |
|
164
|
|
|
PlaceDescription.prototype.getLatitude = function(){ |
|
165
|
|
|
return this.latitude; |
|
166
|
|
|
}; |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Set the latutide |
|
170
|
|
|
* |
|
171
|
|
|
* @param {Number} latitude |
|
172
|
|
|
* @returns {PlaceDescription} |
|
173
|
|
|
*/ |
|
174
|
|
|
PlaceDescription.prototype.setLatitude = function(latitude){ |
|
175
|
|
|
this.latitude = latitude; |
|
176
|
|
|
return this; |
|
177
|
|
|
}; |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Get the longitude |
|
181
|
|
|
* |
|
182
|
|
|
* @returns {Number} |
|
183
|
|
|
*/ |
|
184
|
|
|
PlaceDescription.prototype.getLongitude = function(){ |
|
185
|
|
|
return this.longitude; |
|
186
|
|
|
}; |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Set the latutide |
|
190
|
|
|
* |
|
191
|
|
|
* @param {Number} longitude |
|
192
|
|
|
* @returns {PlaceDescription} |
|
193
|
|
|
*/ |
|
194
|
|
|
PlaceDescription.prototype.setLongitude = function(longitude){ |
|
195
|
|
|
this.longitude = longitude; |
|
196
|
|
|
return this; |
|
197
|
|
|
}; |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Get the temporal description |
|
201
|
|
|
* |
|
202
|
|
|
* @returns {Date} |
|
203
|
|
|
*/ |
|
204
|
|
|
PlaceDescription.prototype.getTemporalDescription = function(){ |
|
205
|
|
|
return this.temporalDescription; |
|
206
|
|
|
}; |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Set the temporal description |
|
210
|
|
|
* |
|
211
|
|
|
* @param {Date} date |
|
212
|
|
|
* @returns {PlaceDescription} |
|
213
|
|
|
*/ |
|
214
|
|
|
PlaceDescription.prototype.setTemporalDescription = function(date){ |
|
215
|
|
|
if(date){ |
|
216
|
|
|
this.temporalDescription = GDate(date); |
|
217
|
|
|
} |
|
218
|
|
|
return this; |
|
219
|
|
|
}; |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Get the spatial description |
|
223
|
|
|
* |
|
224
|
|
|
* @returns {ResourceReference} |
|
225
|
|
|
*/ |
|
226
|
|
|
PlaceDescription.prototype.getSpatialDescription = function(){ |
|
227
|
|
|
return this.spatialDescription; |
|
228
|
|
|
}; |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Set the spatial description |
|
232
|
|
|
* |
|
233
|
|
|
* @param {ResourceReference} spatial |
|
234
|
|
|
* @returns {PlaceDescription} |
|
235
|
|
|
*/ |
|
236
|
|
|
PlaceDescription.prototype.setSpatialDescription = function(spatial){ |
|
237
|
|
|
if(spatial){ |
|
238
|
|
|
this.spatialDescription = ResourceReference(spatial); |
|
239
|
|
|
} |
|
240
|
|
|
return this; |
|
241
|
|
|
}; |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Export the object as JSON |
|
245
|
|
|
* |
|
246
|
|
|
* @return {Object} JSON object |
|
247
|
|
|
*/ |
|
248
|
|
|
PlaceDescription.prototype.toJSON = function(){ |
|
249
|
|
|
var json = Subject.prototype.toJSON.call(this); |
|
250
|
|
|
|
|
251
|
|
|
if(this.type){ |
|
252
|
|
|
json.type = this.type; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
if(this.names){ |
|
256
|
|
|
json.names = this.names.map(function(n){ |
|
257
|
|
|
return n.toJSON(); |
|
258
|
|
|
}); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
if(this.place){ |
|
262
|
|
|
json.place = this.place.toJSON(); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
if(this.jurisdiction){ |
|
266
|
|
|
json.jurisdiction = this.jurisdiction.toJSON(); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
if(this.latitude){ |
|
270
|
|
|
json.latitude = this.latitude; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
if(this.longitude){ |
|
274
|
|
|
json.longitude = this.longitude; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
if(this.temporalDescription){ |
|
278
|
|
|
json.temporalDescription = this.temporalDescription.toJSON(); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
if(this.spatialDescription){ |
|
282
|
|
|
json.spatialDescription = this.spatialDescription.toJSON(); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
return json; |
|
286
|
|
|
}; |
|
287
|
|
|
|
|
288
|
|
|
module.exports = PlaceDescription; |