all files / diagram-js/lib/core/ ElementFactory.js

100% Statements 14/14
75% Branches 3/4
100% Functions 6/6
100% Lines 14/14
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                    1357×       1364×     2749×     11387×     3830×                       19798×   19798× 19×     19798×  
import {
  create
} from '../model';
 
import { assign } from 'min-dash';
 
/**
 * A factory for diagram-js shapes
 */
export default function ElementFactory() {
  this._uid = 12;
}
 
 
ElementFactory.prototype.createRoot = function(attrs) {
  return this.create('root', attrs);
};
 
ElementFactory.prototype.createLabel = function(attrs) {
  return this.create('label', attrs);
};
 
ElementFactory.prototype.createShape = function(attrs) {
  return this.create('shape', attrs);
};
 
ElementFactory.prototype.createConnection = function(attrs) {
  return this.create('connection', attrs);
};
 
/**
 * Create a model element with the given type and
 * a number of pre-set attributes.
 *
 * @param  {String} type
 * @param  {Object} attrs
 * @return {djs.model.Base} the newly created model instance
 */
ElementFactory.prototype.create = function(type, attrs) {
 
  attrs = assign({}, attrs || {});
 
  if (!attrs.id) {
    attrs.id = type + '_' + (this._uid++);
  }
 
  return create(type, attrs);
};