1
|
|
|
module.exports = function(GedcomX){ |
2
|
|
|
|
3
|
|
|
var utils = require('../utils'), |
4
|
|
|
Base = require('../Base'); |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* A representation of an available transition from one application state to another. |
8
|
|
|
* |
9
|
|
|
* @constructor |
10
|
|
|
* @param {Object} [json] |
|
|
|
|
11
|
|
|
*/ |
12
|
|
|
var Link = function(json){ |
13
|
|
|
|
14
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
15
|
|
|
if(!(this instanceof Link)){ |
16
|
|
|
return new Link(json); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
20
|
|
|
if(Link.isInstance(json)){ |
21
|
|
|
return json; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
// TODO: Enforce spec constraint that requires either an href or a template? |
25
|
|
|
|
26
|
|
|
this.init(json); |
|
|
|
|
27
|
|
|
}; |
28
|
|
|
|
29
|
|
|
Link.prototype = Object.create(Base.prototype); |
30
|
|
|
|
31
|
|
|
Link._gedxClass = Link.prototype._gedxClass = 'GedcomX.Link'; |
32
|
|
|
|
33
|
|
|
Link.jsonProps = [ |
34
|
|
|
'rel', |
35
|
|
|
'href', |
36
|
|
|
'template', |
37
|
|
|
'type', |
38
|
|
|
'accept', |
39
|
|
|
'allow', |
40
|
|
|
'hreflang', |
41
|
|
|
'title' |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Check whether the given object is an instance of this class. |
46
|
|
|
* |
47
|
|
|
* @param {Object} obj |
48
|
|
|
* @returns {Boolean} |
49
|
|
|
*/ |
50
|
|
|
Link.isInstance = function(obj){ |
51
|
|
|
return utils.isInstance(obj, this._gedxClass); |
52
|
|
|
}; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Initialize from JSON |
56
|
|
|
* |
57
|
|
|
* @param {Object} |
|
|
|
|
58
|
|
|
* @return {Link} this |
59
|
|
|
*/ |
60
|
|
|
Link.prototype.init = function(json){ |
61
|
|
|
|
62
|
|
|
Base.prototype.init.call(this, json); |
63
|
|
|
|
64
|
|
|
if(json){ |
65
|
|
|
this.setRel(json.rel); |
66
|
|
|
this.setHref(json.href); |
67
|
|
|
this.setTemplate(json.template); |
68
|
|
|
this.setType(json.type); |
69
|
|
|
this.setAccept(json.accept); |
70
|
|
|
this.setAllow(json.allow); |
71
|
|
|
this.setHrefLang(json.hreflang); |
72
|
|
|
this.setTitle(json.title); |
73
|
|
|
} |
74
|
|
|
return this; |
75
|
|
|
}; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the Link's rel |
79
|
|
|
* |
80
|
|
|
* @returns {String} rel |
81
|
|
|
*/ |
82
|
|
|
Link.prototype.getRel = function(){ |
83
|
|
|
return this.rel; |
84
|
|
|
}; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set the Link's rel |
88
|
|
|
* |
89
|
|
|
* @param {String} rel |
90
|
|
|
* @returns {Object} this |
91
|
|
|
*/ |
92
|
|
|
Link.prototype.setRel = function(rel){ |
93
|
|
|
if(rel){ |
94
|
|
|
this.rel = rel; |
95
|
|
|
} |
96
|
|
|
return this; |
97
|
|
|
}; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the Link's href |
101
|
|
|
* |
102
|
|
|
* @returns {String} href |
103
|
|
|
*/ |
104
|
|
|
Link.prototype.getHref = function(){ |
105
|
|
|
return this.href; |
106
|
|
|
}; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set the Link's href |
110
|
|
|
* |
111
|
|
|
* @param {String} href |
112
|
|
|
* @returns {Object} this |
113
|
|
|
*/ |
114
|
|
|
Link.prototype.setHref = function(href){ |
115
|
|
|
if(href){ |
116
|
|
|
this.href = href; |
117
|
|
|
} |
118
|
|
|
return this; |
119
|
|
|
}; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the Link's template |
123
|
|
|
* |
124
|
|
|
* @returns {String} template |
125
|
|
|
*/ |
126
|
|
|
Link.prototype.getTemplate = function(){ |
127
|
|
|
return this.template; |
128
|
|
|
}; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set the Link's template |
132
|
|
|
* |
133
|
|
|
* @param {String} template |
134
|
|
|
* @returns {Object} this |
135
|
|
|
*/ |
136
|
|
|
Link.prototype.setTemplate = function(template){ |
137
|
|
|
if(template){ |
138
|
|
|
this.template = template; |
139
|
|
|
} |
140
|
|
|
return this; |
141
|
|
|
}; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get the Link's type |
145
|
|
|
* |
146
|
|
|
* @returns {String} type |
147
|
|
|
*/ |
148
|
|
|
Link.prototype.getType = function(){ |
149
|
|
|
return this.type; |
150
|
|
|
}; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Set the Link's type |
154
|
|
|
* |
155
|
|
|
* @param {String} type |
156
|
|
|
* @returns {Object} this |
157
|
|
|
*/ |
158
|
|
|
Link.prototype.setType = function(type){ |
159
|
|
|
if(type){ |
160
|
|
|
this.type = type; |
161
|
|
|
} |
162
|
|
|
return this; |
163
|
|
|
}; |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get the Link's accept |
167
|
|
|
* |
168
|
|
|
* @returns {String} accept |
169
|
|
|
*/ |
170
|
|
|
Link.prototype.getAccept = function(){ |
171
|
|
|
return this.accept; |
172
|
|
|
}; |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Set the Link's accept |
176
|
|
|
* |
177
|
|
|
* @param {String} accept |
178
|
|
|
* @returns {Object} this |
179
|
|
|
*/ |
180
|
|
|
Link.prototype.setAccept = function(accept){ |
181
|
|
|
if(accept){ |
182
|
|
|
this.accept = accept; |
183
|
|
|
} |
184
|
|
|
return this; |
185
|
|
|
}; |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get the Link's allow |
189
|
|
|
* |
190
|
|
|
* @returns {String} allow |
191
|
|
|
*/ |
192
|
|
|
Link.prototype.getAllow = function(){ |
193
|
|
|
return this.allow; |
194
|
|
|
}; |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Set the Link's allow |
198
|
|
|
* |
199
|
|
|
* @param {String} allow |
200
|
|
|
* @returns {Object} this |
201
|
|
|
*/ |
202
|
|
|
Link.prototype.setAllow = function(allow){ |
203
|
|
|
if(allow){ |
204
|
|
|
this.allow = allow; |
205
|
|
|
} |
206
|
|
|
return this; |
207
|
|
|
}; |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get the Link's hreflang |
211
|
|
|
* |
212
|
|
|
* @returns {String} hreflang |
213
|
|
|
*/ |
214
|
|
|
Link.prototype.getHrefLang = function(){ |
215
|
|
|
return this.hreflang; |
216
|
|
|
}; |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Set the Link's hreflang |
220
|
|
|
* |
221
|
|
|
* @param {String} hreflang |
222
|
|
|
* @returns {Object} this |
223
|
|
|
*/ |
224
|
|
|
Link.prototype.setHrefLang = function(hreflang){ |
225
|
|
|
if(hreflang){ |
226
|
|
|
this.hreflang = hreflang; |
227
|
|
|
} |
228
|
|
|
return this; |
229
|
|
|
}; |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Get the Link's title |
233
|
|
|
* |
234
|
|
|
* @returns {String} title |
235
|
|
|
*/ |
236
|
|
|
Link.prototype.getTitle = function(){ |
237
|
|
|
return this.title; |
238
|
|
|
}; |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Set the Link's title |
242
|
|
|
* |
243
|
|
|
* @param {String} title |
244
|
|
|
* @returns {Object} this |
245
|
|
|
*/ |
246
|
|
|
Link.prototype.setTitle = function(title){ |
247
|
|
|
if(title){ |
248
|
|
|
this.title = title; |
249
|
|
|
} |
250
|
|
|
return this; |
251
|
|
|
}; |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Export the object as JSON |
255
|
|
|
* |
256
|
|
|
* @return {Object} JSON object |
257
|
|
|
*/ |
258
|
|
|
Link.prototype.toJSON = function(){ |
259
|
|
|
return this._toJSON(Base, Link.jsonProps); |
260
|
|
|
}; |
261
|
|
|
|
262
|
|
|
GedcomX.Link = Link; |
263
|
|
|
|
264
|
|
|
}; |