all files / model/data/ Node.js

85.58% Statements 89/104
89.55% Branches 60/67
88.24% Functions 15/17
86.14% Lines 87/101
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                                          6202×   6202×   6202× 6202× 25133× 25133×     25133× 25133× 25133× 25133×     25133× 22043× 3090× 1286×                                     3710×                                                                     2857×     2857× 2857× 11963× 11963× 11104× 3252×   11104×   2857×       32963×             10797×   19×     19× 14×         19×                                 74× 74× 146× 74× 74× 74× 74×                 19× 19× 19× 19× 108× 108× 19×   89× 89× 51×     19× 19×     19× 19×   48× 14×   34× 13×   34× 34× 34×   19×     34× 34×   34×           34×     23329× 23329× 23329× 901×   22428×   23329× 173×     173×     23156×     23156×               23156×        
import cloneDeep from '../../util/cloneDeep'
import forEach from '../../util/forEach'
import isArray from '../../util/isArray'
import isBoolean from '../../util/isBoolean'
import isNumber from '../../util/isNumber'
import isObject from '../../util/isObject'
import isString from '../../util/isString'
import EventEmitter from '../../util/EventEmitter'
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(data) {
    super()
 
    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() {}
 
  /**
    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
  Iif (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