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