12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import {
- forEach,
- bind
- } from 'min-dash';
- import Base from './base';
- /**
- * A model element factory.
- *
- * @param {Moddle} model
- * @param {Properties} properties
- */
- export default function Factory(model, properties) {
- this.model = model;
- this.properties = properties;
- }
- Factory.prototype.createType = function(descriptor) {
- var model = this.model;
- var props = this.properties,
- prototype = Object.create(Base.prototype);
- // initialize default values
- forEach(descriptor.properties, function(p) {
- if (!p.isMany && p.default !== undefined) {
- prototype[p.name] = p.default;
- }
- });
- props.defineModel(prototype, model);
- props.defineDescriptor(prototype, descriptor);
- var name = descriptor.ns.name;
- /**
- * The new type constructor
- */
- function ModdleElement(attrs) {
- props.define(this, '$type', { value: name, enumerable: true });
- props.define(this, '$attrs', { value: {} });
- props.define(this, '$parent', { writable: true });
- forEach(attrs, bind(function(val, key) {
- this.set(key, val);
- }, this));
- }
- ModdleElement.prototype = prototype;
- ModdleElement.hasType = prototype.$instanceOf = this.model.hasType;
- // static links
- props.defineModel(ModdleElement, model);
- props.defineDescriptor(ModdleElement, descriptor);
- return ModdleElement;
- };
|