1
|
|
|
var ExtensibleData = require('./ExtensibleData'), |
2
|
|
|
ResourceReference = require('./ResourceReference'), |
3
|
|
|
utils = require('./utils'); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Define who is contributing information, when they contributed it, |
7
|
|
|
* and why they are making the contribution. |
8
|
|
|
* |
9
|
|
|
* @constructor |
10
|
|
|
* @param {Object} [json] |
|
|
|
|
11
|
|
|
*/ |
12
|
|
|
var Attribution = function(json){ |
13
|
|
|
|
14
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
15
|
|
|
if(!(this instanceof Attribution)){ |
16
|
|
|
return new Attribution(json); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
20
|
|
|
if(Attribution.isInstance(json)){ |
21
|
|
|
return json; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
ExtensibleData.call(this, json); |
25
|
|
|
|
26
|
|
|
if(json){ |
|
|
|
|
27
|
|
|
this.setChangeMessage(json.changeMessage); |
28
|
|
|
this.setContributor(json.contributor); |
29
|
|
|
this.setCreated(json.created); |
30
|
|
|
this.setCreator(json.creator); |
31
|
|
|
this.setModified(json.modified); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
}; |
34
|
|
|
|
35
|
|
|
Attribution.prototype = Object.create(ExtensibleData.prototype); |
36
|
|
|
|
37
|
|
|
Attribution._gedxClass = Attribution.prototype._gedxClass = 'GedcomX.Attribution'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Check whether the given object is an instance of this class. |
41
|
|
|
* |
42
|
|
|
* @param {Object} obj |
43
|
|
|
* @returns {Boolean} |
44
|
|
|
*/ |
45
|
|
|
Attribution.isInstance = function(obj){ |
46
|
|
|
return utils.isInstance(obj, this._gedxClass); |
47
|
|
|
}; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the change message. |
51
|
|
|
* |
52
|
|
|
* @returns {String} Change message |
53
|
|
|
*/ |
54
|
|
|
Attribution.prototype.getChangeMessage = function(){ |
55
|
|
|
return this.changeMessage; |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set the change message. |
60
|
|
|
* |
61
|
|
|
* @param {String} changeMessage |
62
|
|
|
* @returns {Attribution} This object |
63
|
|
|
*/ |
64
|
|
|
Attribution.prototype.setChangeMessage = function(changeMessage){ |
65
|
|
|
this.changeMessage = changeMessage; |
66
|
|
|
return this; |
67
|
|
|
}; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the contributor. |
71
|
|
|
* |
72
|
|
|
* @returns {ResourceReference} contributor |
73
|
|
|
*/ |
74
|
|
|
Attribution.prototype.getContributor = function(){ |
75
|
|
|
return this.contributor; |
76
|
|
|
}; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set the contributor |
80
|
|
|
* |
81
|
|
|
* @param {Object|ResourceReference} contributor ResourceReference representing the contributor. |
82
|
|
|
* @returns {Attribution} This instance |
83
|
|
|
*/ |
84
|
|
|
Attribution.prototype.setContributor = function(contributor){ |
85
|
|
|
if(contributor){ |
86
|
|
|
this.contributor = ResourceReference(contributor); |
87
|
|
|
} |
88
|
|
|
return this; |
89
|
|
|
}; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the created timestamp |
93
|
|
|
* |
94
|
|
|
* @returns {Date} created |
95
|
|
|
*/ |
96
|
|
|
Attribution.prototype.getCreated = function(){ |
97
|
|
|
return this.created; |
98
|
|
|
}; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set the created timestamp |
102
|
|
|
* |
103
|
|
|
* @param {Date|Number} date Integer timestamp (milliseconds since epoch) or JavaScript Date instance |
104
|
|
|
* @returns {Attribution} This instance |
105
|
|
|
*/ |
106
|
|
|
Attribution.prototype.setCreated = function(date){ |
107
|
|
|
if(date){ |
108
|
|
|
this.created = new Date(date); |
109
|
|
|
} |
110
|
|
|
return this; |
111
|
|
|
}; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the creator |
115
|
|
|
* |
116
|
|
|
* @returns {ResourceReference} Creator |
117
|
|
|
*/ |
118
|
|
|
Attribution.prototype.getCreator = function(){ |
119
|
|
|
return this.creator; |
120
|
|
|
}; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set the creator |
124
|
|
|
* |
125
|
|
|
* @param {Object|ResourceReference} creator ResourceReference representing the creator. |
126
|
|
|
* @returns {Attribution} This instance |
127
|
|
|
*/ |
128
|
|
|
Attribution.prototype.setCreator = function(creator){ |
129
|
|
|
if(creator){ |
130
|
|
|
this.creator = new ResourceReference(creator); |
131
|
|
|
} |
132
|
|
|
return this; |
133
|
|
|
}; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the modified timestamp |
137
|
|
|
* |
138
|
|
|
* @returns {Date} modified |
139
|
|
|
*/ |
140
|
|
|
Attribution.prototype.getModified = function(){ |
141
|
|
|
return this.modified; |
142
|
|
|
}; |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set the modified timestamp |
146
|
|
|
* |
147
|
|
|
* @param {Date|Number} date Integer timestamp (milliseconds since epoch) or JavaScript Date instance |
148
|
|
|
* @returns {Attribution} This instance |
149
|
|
|
*/ |
150
|
|
|
Attribution.prototype.setModified = function(date){ |
151
|
|
|
if(date){ |
152
|
|
|
this.modified = new Date(date); |
153
|
|
|
} |
154
|
|
|
return this; |
155
|
|
|
}; |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Export the object as JSON |
159
|
|
|
* |
160
|
|
|
* @return {Object} JSON object |
161
|
|
|
*/ |
162
|
|
|
Attribution.prototype.toJSON = function(){ |
163
|
|
|
var json = ExtensibleData.prototype.toJSON.call(this); |
164
|
|
|
|
165
|
|
|
if(this.changeMessage){ |
166
|
|
|
json.changeMessage = this.changeMessage; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if(this.contributor){ |
170
|
|
|
json.contributor = this.contributor.toJSON(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if(this.created){ |
174
|
|
|
json.created = this.created.getTime(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if(this.creator){ |
178
|
|
|
json.creator = this.creator.toJSON(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if(this.modified){ |
182
|
|
|
json.modified = this.modified.getTime(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return json; |
186
|
|
|
}; |
187
|
|
|
|
188
|
|
|
module.exports = Attribution; |