1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | 5226× 5226× 5226× 5226× 5226× 20217× 20217× 20217× 20217× 20217× 20217× 20217× 15548× 4669× 504× 4× 2756× 14× 14× 1951× 1951× 1951× 7381× 7381× 5809× 1756× 5809× 1951× 32485× 1× 1× 7996× 20× 20× 15× 20× 1× 1× 1× 1× 79× 79× 158× 84× 84× 84× 84× 5× 1× 20× 20× 20× 20× 116× 116× 20× 96× 96× 56× 20× 20× 1× 20× 20× 50× 15× 35× 13× 35× 35× 35× 20× 1× 35× 35× 1× 34× 6× 35× 1× 16052× 16052× 16052× 994× 15058× 16052× 172× 172× 15880× 15880× 15880× | import { cloneDeep, forEach, isArray, isBoolean, isNumber, isObject, isString, EventEmitter } from '../util' import Property from './Property' /* Base node implementation. @prop {String} id an id that is unique within this data */ class Node extends EventEmitter { /** @param {Object} properties */ constructor() { super() // NOTE: this indirection allows us to implement a overridable initializer // For instance, DocumentNode sets the document instance and the props this._initialize.apply(this, arguments) } _initialize(data) { const NodeClass = this.constructor let schema = NodeClass.schema for (var name in schema) { Iif (!schema.hasOwnProperty(name)) continue let prop = schema[name] // check integrity of provided data, such as type correctness, // and mandatory properties const propIsGiven = (data[name] !== undefined) const hasDefault = prop.hasDefault() const isOptional = prop.isOptional() Iif ( (!isOptional && !hasDefault) && !propIsGiven) { throw new Error('Property ' + name + ' is mandatory for node type ' + this.type) } if (propIsGiven) { this[name] = _checked(prop, data[name]) } else if (hasDefault) { this[name] = cloneDeep(_checked(prop, prop.getDefault())) } else { // property is optional } } } dispose() { // nothing by default } /** Check if the node is of a given type. @param {String} typeName @returns {Boolean} true if the node has a parent with given type, false otherwise. */ isInstanceOf(typeName) { return Node.isInstanceOf(this.constructor, typeName) } getSchema() { return this.constructor.schema } /** Get a the list of all polymorphic types. @returns {String[]} An array of type names. */ getTypeNames() { var typeNames = [] var NodeClass = this.constructor while (NodeClass.type !== "node") { typeNames.push(NodeClass.type) NodeClass = Object.getPrototypeOf(NodeClass) } return typeNames } /** * Get the type of a property. * * @param {String} propertyName * @returns The property's type. */ getPropertyType(propertyName) { var schema = this.constructor.schema return schema[propertyName].type } /** Convert node to JSON. @returns {Object} JSON representation of node. */ toJSON() { var data = { type: this.type } const schema = this.getSchema() forEach(schema, (prop, name) => { let val = this[name] if (prop.isOptional() && val === undefined) return if (isArray(val) || isObject(val)) { val = cloneDeep(val) } data[prop.name] = val }) return data } get type() { return this.constructor.type } } Node.prototype._isNode = true // NOTE: this code and its deps will always be included in the bundle as rollup considers this as global side-effect Object.defineProperty(Node, 'schema', { get() { return this._schema }, set(schema) { let NodeClass = this // TODO: discuss if we want this. Is a bit more convenient // ATM we transfer 'type' to the static property if (schema.type) { NodeClass.type = schema.type } // collects a full schema considering the schemas of parent class // we will use the unfolded schema, check integrity of the given props (mandatory, readonly) // or fill in default values for undefined properties. NodeClass._schema = compileSchema(NodeClass, schema) } }) Node.define = Node.defineSchema = function define(schema) { this.schema = schema } Node.schema = { type: "node", id: 'string' } /** Internal implementation of Node.prototype.isInstanceOf. @static @private @returns {Boolean} */ Node.isInstanceOf = function(NodeClass, typeName) { var type = NodeClass.type while (type !== "node") { if (type === typeName) return true var _super = Object.getPrototypeOf(NodeClass.prototype).constructor Eif (_super && _super.type) { NodeClass = _super type = NodeClass.type } else { break } } return false } // ### Internal implementation function compileSchema(NodeClass, schema) { let compiledSchema = _compileSchema(schema) let schemas = [compiledSchema] let clazz = NodeClass while(clazz) { var parentProto = Object.getPrototypeOf(clazz.prototype) if (!parentProto) { break } clazz = parentProto.constructor if (clazz && clazz._schema) { schemas.unshift(clazz._schema) } } schemas.unshift({}) return Object.assign.apply(null, schemas) } function _compileSchema(schema) { let compiledSchema = {} forEach(schema, function(definition, name) { // skip 'type' if (name === 'type') { return } if (isString(definition) || isArray(definition)) { definition = { type: definition } } definition = _compileDefintion(definition) definition.name = name compiledSchema[name] = new Property(definition) }) return compiledSchema } function _compileDefintion(definition) { let result = definition if (isArray(definition.type) && definition.type[0] !== "array") { definition.type = [ "array", definition.type[0] ] } else if (definition.type === 'text') { result = { type: "string", default: '', _isText: true } } return result } function _checked(prop, value) { let type let name = prop.name if (prop.isArray()) { type = "array" } else { type = prop.type } if (value === null) { Iif (prop.isNotNull()) { throw new Error('Value for property ' + name + ' is null.') } else { return value } } Iif (value === undefined) { throw new Error('Value for property ' + name + ' is undefined.') } Iif (type === "string" && !isString(value) || type === "boolean" && !isBoolean(value) || type === "number" && !isNumber(value) || type === "array" && !isArray(value) || type === "id" && !isString(value) || type === "object" && !isObject(value)) { throw new Error('Illegal value type for property ' + name + ': expected ' + type + ', was ' + (typeof value)) } return value } export default Node |