ElementFactory.js 988 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import {
  2. create
  3. } from '../model';
  4. import { assign } from 'min-dash';
  5. /**
  6. * A factory for diagram-js shapes
  7. */
  8. export default function ElementFactory() {
  9. this._uid = 12;
  10. }
  11. ElementFactory.prototype.createRoot = function(attrs) {
  12. return this.create('root', attrs);
  13. };
  14. ElementFactory.prototype.createLabel = function(attrs) {
  15. return this.create('label', attrs);
  16. };
  17. ElementFactory.prototype.createShape = function(attrs) {
  18. return this.create('shape', attrs);
  19. };
  20. ElementFactory.prototype.createConnection = function(attrs) {
  21. return this.create('connection', attrs);
  22. };
  23. /**
  24. * Create a model element with the given type and
  25. * a number of pre-set attributes.
  26. *
  27. * @param {string} type
  28. * @param {Object} attrs
  29. * @return {djs.model.Base} the newly created model instance
  30. */
  31. ElementFactory.prototype.create = function(type, attrs) {
  32. attrs = assign({}, attrs || {});
  33. if (!attrs.id) {
  34. attrs.id = type + '_' + (this._uid++);
  35. }
  36. return create(type, attrs);
  37. };