all files / lib/model/ index.js

97.22% Statements 35/36
83.33% Branches 5/6
100% Functions 8/8
97.22% Lines 35/36
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                                                          2527×                     2527×   22×                               2527×               2527×               2527×               2527×                         2213×               2213×           2213×           2213×                           463×                           101×               101×                           314×               314×               314×                                                         2527× 2527×     2527×  
import { assign } from 'min-dash';
import inherits from 'inherits';
 
import Refs from 'object-refs';
 
var parentRefs = new Refs({ name: 'children', enumerable: true, collection: true }, { name: 'parent' }),
    labelRefs = new Refs({ name: 'labels', enumerable: true, collection: true }, { name: 'labelTarget' }),
    attacherRefs = new Refs({ name: 'attachers', collection: true }, { name: 'host' }),
    outgoingRefs = new Refs({ name: 'outgoing', collection: true }, { name: 'source' }),
    incomingRefs = new Refs({ name: 'incoming', collection: true }, { name: 'target' });
 
/**
 * @namespace djs.model
 */
 
/**
 * @memberOf djs.model
 */
 
/**
 * The basic graphical representation
 *
 * @class
 *
 * @abstract
 */
export function Base() {
 
  /**
   * The object that backs up the shape
   *
   * @name Base#businessObject
   * @type Object
   */
  Object.defineProperty(this, 'businessObject', {
    writable: true
  });
 
 
  /**
   * Single label support, will mapped to multi label array
   *
   * @name Base#label
   * @type Object
   */
  Object.defineProperty(this, 'label', {
    get: function() {
      return this.labels[0];
    },
    set: function(newLabel) {
 
      var label = this.label,
          labels = this.labels;
 
      if (!newLabel && label) {
        labels.remove(label);
      } else {
        labels.add(newLabel, 0);
      }
    }
  });
 
  /**
   * The parent shape
   *
   * @name Base#parent
   * @type Shape
   */
  parentRefs.bind(this, 'parent');
 
  /**
   * The list of labels
   *
   * @name Base#labels
   * @type Label
   */
  labelRefs.bind(this, 'labels');
 
  /**
   * The list of outgoing connections
   *
   * @name Base#outgoing
   * @type Array<Connection>
   */
  outgoingRefs.bind(this, 'outgoing');
 
  /**
   * The list of incoming connections
   *
   * @name Base#incoming
   * @type Array<Connection>
   */
  incomingRefs.bind(this, 'incoming');
}
 
 
/**
 * A graphical object
 *
 * @class
 * @constructor
 *
 * @extends Base
 */
export function Shape() {
  Base.call(this);
 
  /**
   * The list of children
   *
   * @name Shape#children
   * @type Array<Base>
   */
  parentRefs.bind(this, 'children');
 
  /**
   * @name Shape#host
   * @type Shape
   */
  attacherRefs.bind(this, 'host');
 
  /**
   * @name Shape#attachers
   * @type Shape
   */
  attacherRefs.bind(this, 'attachers');
}
 
inherits(Shape, Base);
 
 
/**
 * A root graphical object
 *
 * @class
 * @constructor
 *
 * @extends Shape
 */
export function Root() {
  Shape.call(this);
}
 
inherits(Root, Shape);
 
 
/**
 * A label for an element
 *
 * @class
 * @constructor
 *
 * @extends Shape
 */
export function Label() {
  Shape.call(this);
 
  /**
   * The labeled element
   *
   * @name Label#labelTarget
   * @type Base
   */
  labelRefs.bind(this, 'labelTarget');
}
 
inherits(Label, Shape);
 
 
/**
 * A connection between two elements
 *
 * @class
 * @constructor
 *
 * @extends Base
 */
export function Connection() {
  Base.call(this);
 
  /**
   * The element this connection originates from
   *
   * @name Connection#source
   * @type Base
   */
  outgoingRefs.bind(this, 'source');
 
  /**
   * The element this connection points to
   *
   * @name Connection#target
   * @type Base
   */
  incomingRefs.bind(this, 'target');
}
 
inherits(Connection, Base);
 
 
var types = {
  connection: Connection,
  shape: Shape,
  label: Label,
  root: Root
};
 
/**
 * Creates a new model element of the specified type
 *
 * @method create
 *
 * @example
 *
 * var shape1 = Model.create('shape', { x: 10, y: 10, width: 100, height: 100 });
 * var shape2 = Model.create('shape', { x: 210, y: 210, width: 100, height: 100 });
 *
 * var connection = Model.create('connection', { waypoints: [ { x: 110, y: 55 }, {x: 210, y: 55 } ] });
 *
 * @param  {String} type lower-cased model name
 * @param  {Object} attrs attributes to initialize the new model instance with
 *
 * @return {Base} the new model instance
 */
export function create(type, attrs) {
  var Type = types[type];
  Iif (!Type) {
    throw new Error('unknown type: <' + type + '>');
  }
  return assign(new Type(), attrs);
}