| @@ 1-189 (lines=189) @@ | ||
| 1 | var GedcomX = require('../'), |
|
| 2 | utils = require('../utils'); |
|
| 3 | ||
| 4 | /** |
|
| 5 | * A fact for a person or relationship. |
|
| 6 | * |
|
| 7 | * @constructor |
|
| 8 | * @param {Object} [json] |
|
| 9 | */ |
|
| 10 | var Fact = function(json){ |
|
| 11 | ||
| 12 | // Protect against forgetting the new keyword when calling the constructor |
|
| 13 | if(!(this instanceof Fact)){ |
|
| 14 | return new Fact(json); |
|
| 15 | } |
|
| 16 | ||
| 17 | // If the given object is already an instance then just return it. DON'T copy it. |
|
| 18 | if(Fact.isInstance(json)){ |
|
| 19 | return json; |
|
| 20 | } |
|
| 21 | ||
| 22 | this.init(json); |
|
| 23 | }; |
|
| 24 | ||
| 25 | Fact.prototype = Object.create(GedcomX.Conclusion.prototype); |
|
| 26 | ||
| 27 | Fact._gedxClass = Fact.prototype._gedxClass = 'GedcomX.Fact'; |
|
| 28 | ||
| 29 | Fact.jsonProps = [ |
|
| 30 | 'type', |
|
| 31 | 'date', |
|
| 32 | 'place', |
|
| 33 | 'value', |
|
| 34 | 'qualifiers' |
|
| 35 | ]; |
|
| 36 | ||
| 37 | /** |
|
| 38 | * Check whether the given object is an instance of this class. |
|
| 39 | * |
|
| 40 | * @param {Object} obj |
|
| 41 | * @returns {Boolean} |
|
| 42 | */ |
|
| 43 | Fact.isInstance = function(obj){ |
|
| 44 | return utils.isInstance(obj, this._gedxClass); |
|
| 45 | }; |
|
| 46 | ||
| 47 | /** |
|
| 48 | * Initialize from JSON |
|
| 49 | * |
|
| 50 | * @param {Object} |
|
| 51 | * @return {Fact} this |
|
| 52 | */ |
|
| 53 | Fact.prototype.init = function(json){ |
|
| 54 | ||
| 55 | GedcomX.Conclusion.prototype.init.call(this, json); |
|
| 56 | ||
| 57 | if(json){ |
|
| 58 | this.setType(json.type); |
|
| 59 | this.setDate(json.date); |
|
| 60 | this.setPlace(json.place); |
|
| 61 | this.setValue(json.value); |
|
| 62 | this.setQualifiers(json.qualifiers); |
|
| 63 | } |
|
| 64 | return this; |
|
| 65 | }; |
|
| 66 | ||
| 67 | /** |
|
| 68 | * Get the fact type |
|
| 69 | * |
|
| 70 | * @returns {String} type |
|
| 71 | */ |
|
| 72 | Fact.prototype.getType = function(){ |
|
| 73 | return this.type; |
|
| 74 | }; |
|
| 75 | ||
| 76 | /** |
|
| 77 | * Set the fact type |
|
| 78 | * |
|
| 79 | * @param {String} type |
|
| 80 | * @returns {Fact} This instance |
|
| 81 | */ |
|
| 82 | Fact.prototype.setType = function(type){ |
|
| 83 | this.type = type; |
|
| 84 | return this; |
|
| 85 | }; |
|
| 86 | ||
| 87 | /** |
|
| 88 | * Get the date |
|
| 89 | * |
|
| 90 | * @returns {Date} date |
|
| 91 | */ |
|
| 92 | Fact.prototype.getDate = function(){ |
|
| 93 | return this.date; |
|
| 94 | }; |
|
| 95 | ||
| 96 | /** |
|
| 97 | * Set the date |
|
| 98 | * |
|
| 99 | * @param {Date|Object} date |
|
| 100 | * @returns {Fact} This instance |
|
| 101 | */ |
|
| 102 | Fact.prototype.setDate = function(date){ |
|
| 103 | if(date){ |
|
| 104 | this.date = GedcomX.Date(date); |
|
| 105 | } |
|
| 106 | return this; |
|
| 107 | }; |
|
| 108 | ||
| 109 | /** |
|
| 110 | * Get the place reference |
|
| 111 | * |
|
| 112 | * @returns {PlaceReference} |
|
| 113 | */ |
|
| 114 | Fact.prototype.getPlace = function(){ |
|
| 115 | return this.place; |
|
| 116 | }; |
|
| 117 | ||
| 118 | /** |
|
| 119 | * Set the place reference |
|
| 120 | * |
|
| 121 | * @param {PlaceReference|Object} place |
|
| 122 | * @returns {Fact} This instance |
|
| 123 | */ |
|
| 124 | Fact.prototype.setPlace = function(place){ |
|
| 125 | if(place){ |
|
| 126 | this.place = GedcomX.PlaceReference(place); |
|
| 127 | } |
|
| 128 | return this; |
|
| 129 | }; |
|
| 130 | ||
| 131 | /** |
|
| 132 | * Get the value |
|
| 133 | * |
|
| 134 | * @returns {String} |
|
| 135 | */ |
|
| 136 | Fact.prototype.getValue = function(){ |
|
| 137 | return this.value; |
|
| 138 | }; |
|
| 139 | ||
| 140 | /** |
|
| 141 | * Set the value |
|
| 142 | * |
|
| 143 | * @param {String} value |
|
| 144 | * @returns {Fact} This instance |
|
| 145 | */ |
|
| 146 | Fact.prototype.setValue = function(value){ |
|
| 147 | this.value = value; |
|
| 148 | return this; |
|
| 149 | }; |
|
| 150 | ||
| 151 | /** |
|
| 152 | * Get qualifiers |
|
| 153 | * |
|
| 154 | * @return {Qualifer[]} |
|
| 155 | */ |
|
| 156 | Fact.prototype.getQualifiers = function(){ |
|
| 157 | return this.qualifiers || []; |
|
| 158 | }; |
|
| 159 | ||
| 160 | /** |
|
| 161 | * Set the qualifiers |
|
| 162 | * |
|
| 163 | * @param {Qualifer[]|Object[]} qualifiers |
|
| 164 | * @returns {Fact} This instance |
|
| 165 | */ |
|
| 166 | Fact.prototype.setQualifiers = function(qualifiers){ |
|
| 167 | return this._setArray(qualifiers, 'qualifiers', 'addQualifier'); |
|
| 168 | }; |
|
| 169 | ||
| 170 | /** |
|
| 171 | * Add a qualifier |
|
| 172 | * |
|
| 173 | * @param {Qualifier|Object} qualifier |
|
| 174 | * @returns {Fact} This instance |
|
| 175 | */ |
|
| 176 | Fact.prototype.addQualifier = function(qualifier){ |
|
| 177 | return this._arrayPush(qualifier, 'qualifiers', GedcomX.Qualifier); |
|
| 178 | }; |
|
| 179 | ||
| 180 | /** |
|
| 181 | * Export the object as JSON |
|
| 182 | * |
|
| 183 | * @return {Object} JSON object |
|
| 184 | */ |
|
| 185 | Fact.prototype.toJSON = function(){ |
|
| 186 | return this._toJSON(GedcomX.Conclusion, Fact.jsonProps); |
|
| 187 | }; |
|
| 188 | ||
| 189 | module.exports = Fact; |
|
| @@ 1-167 (lines=167) @@ | ||
| 1 | var GedcomX = require('../'), |
|
| 2 | utils = require('../utils'); |
|
| 3 | ||
| 4 | /** |
|
| 5 | * An event. |
|
| 6 | * |
|
| 7 | * @constructor |
|
| 8 | * @param {Object} [json] |
|
| 9 | */ |
|
| 10 | var Event = function(json){ |
|
| 11 | ||
| 12 | // Protect against forgetting the new keyword when calling the constructor |
|
| 13 | if(!(this instanceof Event)){ |
|
| 14 | return new Event(json); |
|
| 15 | } |
|
| 16 | ||
| 17 | // If the given object is already an instance then just return it. DON'T copy it. |
|
| 18 | if(Event.isInstance(json)){ |
|
| 19 | return json; |
|
| 20 | } |
|
| 21 | ||
| 22 | this.init(json); |
|
| 23 | }; |
|
| 24 | ||
| 25 | Event.prototype = Object.create(GedcomX.Subject.prototype); |
|
| 26 | ||
| 27 | Event._gedxClass = Event.prototype._gedxClass = 'GedcomX.Event'; |
|
| 28 | ||
| 29 | Event.jsonProps = [ |
|
| 30 | 'type', |
|
| 31 | 'date', |
|
| 32 | 'place', |
|
| 33 | 'roles' |
|
| 34 | ]; |
|
| 35 | ||
| 36 | /** |
|
| 37 | * Check whether the given object is an instance of this class. |
|
| 38 | * |
|
| 39 | * @param {Object} obj |
|
| 40 | * @returns {Boolean} |
|
| 41 | */ |
|
| 42 | Event.isInstance = function(obj){ |
|
| 43 | return utils.isInstance(obj, this._gedxClass); |
|
| 44 | }; |
|
| 45 | ||
| 46 | /** |
|
| 47 | * Initialize from JSON |
|
| 48 | * |
|
| 49 | * @param {Object} |
|
| 50 | * @return {Event} this |
|
| 51 | */ |
|
| 52 | Event.prototype.init = function(json){ |
|
| 53 | ||
| 54 | GedcomX.Subject.prototype.init.call(this, json); |
|
| 55 | ||
| 56 | if(json){ |
|
| 57 | this.setType(json.type); |
|
| 58 | this.setDate(json.date); |
|
| 59 | this.setPlace(json.place); |
|
| 60 | this.setRoles(json.roles); |
|
| 61 | } |
|
| 62 | return this; |
|
| 63 | }; |
|
| 64 | ||
| 65 | /** |
|
| 66 | * Get the type |
|
| 67 | * |
|
| 68 | * @returns {String} |
|
| 69 | */ |
|
| 70 | Event.prototype.getType = function(){ |
|
| 71 | return this.type; |
|
| 72 | }; |
|
| 73 | ||
| 74 | /** |
|
| 75 | * Set the type |
|
| 76 | * |
|
| 77 | * @param {String} type |
|
| 78 | * @returns {Event} |
|
| 79 | */ |
|
| 80 | Event.prototype.setType = function(type){ |
|
| 81 | this.type = type; |
|
| 82 | return this; |
|
| 83 | }; |
|
| 84 | ||
| 85 | /** |
|
| 86 | * Get the date |
|
| 87 | * |
|
| 88 | * @returns {Date} |
|
| 89 | */ |
|
| 90 | Event.prototype.getDate = function(){ |
|
| 91 | return this.date; |
|
| 92 | }; |
|
| 93 | ||
| 94 | /** |
|
| 95 | * Set the date |
|
| 96 | * |
|
| 97 | * @param {Date} date |
|
| 98 | * @returns {Event} |
|
| 99 | */ |
|
| 100 | Event.prototype.setDate = function(date){ |
|
| 101 | if(date){ |
|
| 102 | this.date = GedcomX.Date(date); |
|
| 103 | } |
|
| 104 | return this; |
|
| 105 | }; |
|
| 106 | ||
| 107 | /** |
|
| 108 | * Get the place |
|
| 109 | * |
|
| 110 | * @returns {PlaceReference} |
|
| 111 | */ |
|
| 112 | Event.prototype.getPlace = function(){ |
|
| 113 | return this.place; |
|
| 114 | }; |
|
| 115 | ||
| 116 | /** |
|
| 117 | * Set the place |
|
| 118 | * |
|
| 119 | * @param {PlaceReference} place |
|
| 120 | * @returns {Event} |
|
| 121 | */ |
|
| 122 | Event.prototype.setPlace = function(place){ |
|
| 123 | if(place){ |
|
| 124 | this.place = GedcomX.PlaceReference(place); |
|
| 125 | } |
|
| 126 | return this; |
|
| 127 | }; |
|
| 128 | ||
| 129 | /** |
|
| 130 | * Get the roles |
|
| 131 | * |
|
| 132 | * @returns {EventRole[]} |
|
| 133 | */ |
|
| 134 | Event.prototype.getRoles = function(){ |
|
| 135 | return this.roles || []; |
|
| 136 | }; |
|
| 137 | ||
| 138 | /** |
|
| 139 | * Set the roles |
|
| 140 | * |
|
| 141 | * @param {EventRole[]|Object[]} roles |
|
| 142 | * @returns {Event} |
|
| 143 | */ |
|
| 144 | Event.prototype.setRoles = function(roles){ |
|
| 145 | return this._setArray(roles, 'roles', 'addRole'); |
|
| 146 | }; |
|
| 147 | ||
| 148 | /** |
|
| 149 | * Add a role |
|
| 150 | * |
|
| 151 | * @param {EventRole|Object} role |
|
| 152 | * @returns {Event} |
|
| 153 | */ |
|
| 154 | Event.prototype.addRole = function(role){ |
|
| 155 | return this._arrayPush(role, 'roles', GedcomX.EventRole); |
|
| 156 | }; |
|
| 157 | ||
| 158 | /** |
|
| 159 | * Export the object as JSON |
|
| 160 | * |
|
| 161 | * @return {Object} JSON object |
|
| 162 | */ |
|
| 163 | Event.prototype.toJSON = function(){ |
|
| 164 | return this._toJSON(GedcomX.Subject, Event.jsonProps); |
|
| 165 | }; |
|
| 166 | ||
| 167 | module.exports = Event; |
|
| @@ 1-167 (lines=167) @@ | ||
| 1 | var GedcomX = require('../'), |
|
| 2 | utils = require('../utils'); |
|
| 3 | ||
| 4 | /** |
|
| 5 | * A relationship. |
|
| 6 | * |
|
| 7 | * @constructor |
|
| 8 | * @param {Object} [json] |
|
| 9 | */ |
|
| 10 | var Relationship = function(json){ |
|
| 11 | ||
| 12 | // Protect against forgetting the new keyword when calling the constructor |
|
| 13 | if(!(this instanceof Relationship)){ |
|
| 14 | return new Relationship(json); |
|
| 15 | } |
|
| 16 | ||
| 17 | // If the given object is already an instance then just return it. DON'T copy it. |
|
| 18 | if(Relationship.isInstance(json)){ |
|
| 19 | return json; |
|
| 20 | } |
|
| 21 | ||
| 22 | this.init(json); |
|
| 23 | }; |
|
| 24 | ||
| 25 | Relationship.prototype = Object.create(GedcomX.Subject.prototype); |
|
| 26 | ||
| 27 | Relationship._gedxClass = Relationship.prototype._gedxClass = 'GedcomX.Relationship'; |
|
| 28 | ||
| 29 | Relationship.jsonProps = [ |
|
| 30 | 'type', |
|
| 31 | 'person1', |
|
| 32 | 'person2', |
|
| 33 | 'facts' |
|
| 34 | ]; |
|
| 35 | ||
| 36 | /** |
|
| 37 | * Check whether the given object is an instance of this class. |
|
| 38 | * |
|
| 39 | * @param {Object} obj |
|
| 40 | * @returns {Boolean} |
|
| 41 | */ |
|
| 42 | Relationship.isInstance = function(obj){ |
|
| 43 | return utils.isInstance(obj, this._gedxClass); |
|
| 44 | }; |
|
| 45 | ||
| 46 | /** |
|
| 47 | * Initialize from JSON |
|
| 48 | * |
|
| 49 | * @param {Object} |
|
| 50 | * @return {Relationship} this |
|
| 51 | */ |
|
| 52 | Relationship.prototype.init = function(json){ |
|
| 53 | ||
| 54 | GedcomX.Subject.prototype.init.call(this, json); |
|
| 55 | ||
| 56 | if(json){ |
|
| 57 | this.setType(json.type); |
|
| 58 | this.setPerson1(json.person1); |
|
| 59 | this.setPerson2(json.person2); |
|
| 60 | this.setFacts(json.facts); |
|
| 61 | } |
|
| 62 | return this; |
|
| 63 | }; |
|
| 64 | ||
| 65 | /** |
|
| 66 | * Get the relationship type |
|
| 67 | * |
|
| 68 | * @returns {String} type |
|
| 69 | */ |
|
| 70 | Relationship.prototype.getType = function(){ |
|
| 71 | return this.type; |
|
| 72 | }; |
|
| 73 | ||
| 74 | /** |
|
| 75 | * Set the relationship type |
|
| 76 | * |
|
| 77 | * @param {String} type |
|
| 78 | * @returns {Relationship} This instance |
|
| 79 | */ |
|
| 80 | Relationship.prototype.setType = function(type){ |
|
| 81 | this.type = type; |
|
| 82 | return this; |
|
| 83 | }; |
|
| 84 | ||
| 85 | /** |
|
| 86 | * Get person1 reference |
|
| 87 | * |
|
| 88 | * @returns {ResourceReference} |
|
| 89 | */ |
|
| 90 | Relationship.prototype.getPerson1 = function(){ |
|
| 91 | return this.person1; |
|
| 92 | }; |
|
| 93 | ||
| 94 | /** |
|
| 95 | * Set the person1 reference |
|
| 96 | * |
|
| 97 | * @param {ResourceReference} person1 |
|
| 98 | * @returns {Relationship} This instance |
|
| 99 | */ |
|
| 100 | Relationship.prototype.setPerson1 = function(person1){ |
|
| 101 | if(person1){ |
|
| 102 | this.person1 = GedcomX.ResourceReference(person1); |
|
| 103 | } |
|
| 104 | return this; |
|
| 105 | }; |
|
| 106 | ||
| 107 | /** |
|
| 108 | * Get person2 reference |
|
| 109 | * |
|
| 110 | * @returns {ResourceReference} |
|
| 111 | */ |
|
| 112 | Relationship.prototype.getPerson2 = function(){ |
|
| 113 | return this.person2; |
|
| 114 | }; |
|
| 115 | ||
| 116 | /** |
|
| 117 | * Set the person1 reference |
|
| 118 | * |
|
| 119 | * @param {ResourceReference} person2 |
|
| 120 | * @returns {Relationship} This instance |
|
| 121 | */ |
|
| 122 | Relationship.prototype.setPerson2 = function(person2){ |
|
| 123 | if(person2){ |
|
| 124 | this.person2 = GedcomX.ResourceReference(person2); |
|
| 125 | } |
|
| 126 | return this; |
|
| 127 | }; |
|
| 128 | ||
| 129 | /** |
|
| 130 | * Get the facts |
|
| 131 | * |
|
| 132 | * @return {Fact[]} |
|
| 133 | */ |
|
| 134 | Relationship.prototype.getFacts = function(){ |
|
| 135 | return this.facts || []; |
|
| 136 | }; |
|
| 137 | ||
| 138 | /** |
|
| 139 | * Set the facts |
|
| 140 | * |
|
| 141 | * @param {Fact[]|Object[]} facts |
|
| 142 | * @returns {Relationship} This instance |
|
| 143 | */ |
|
| 144 | Relationship.prototype.setFacts = function(facts){ |
|
| 145 | return this._setArray(facts, 'facts', 'addFact'); |
|
| 146 | }; |
|
| 147 | ||
| 148 | /** |
|
| 149 | * Add a fact |
|
| 150 | * |
|
| 151 | * @param {Fact|Object} fact |
|
| 152 | * @returns {Relationship} This instance |
|
| 153 | */ |
|
| 154 | Relationship.prototype.addFact = function(fact){ |
|
| 155 | return this._arrayPush(fact, 'facts', GedcomX.Fact); |
|
| 156 | }; |
|
| 157 | ||
| 158 | /** |
|
| 159 | * Export the object as JSON |
|
| 160 | * |
|
| 161 | * @return {Object} JSON object |
|
| 162 | */ |
|
| 163 | Relationship.prototype.toJSON = function(){ |
|
| 164 | return this._toJSON(GedcomX.Subject, Relationship.jsonProps); |
|
| 165 | }; |
|
| 166 | ||
| 167 | module.exports = Relationship; |
|