1 import { assign } from 'min-dash';
3 import inherits from 'inherits';
5 import BpmnElementFactory from 'bpmn-js/lib/features/modeling/ElementFactory';
6 import { DEFAULT_LABEL_SIZE } from 'bpmn-js/lib/util/LabelUtil';
9 * A custom factory that knows how to create BPMN _and_ custom elements.
11 export default function CustomElementFactory(bpmnFactory, moddle) {
12 BpmnElementFactory.call(this, bpmnFactory, moddle);
17 * Create a diagram-js element with the given type (any of shape, connection, label).
19 * @param {String} elementType
20 * @param {Object} attrs
22 * @return {djs.model.Base}
24 this.create = function(elementType, attrs) {
25 var type = attrs.type;
27 if (elementType === 'label') {
28 return self.baseCreate(
30 assign({ type: 'label' }, DEFAULT_LABEL_SIZE, attrs)
34 // add type to businessObject if custom
35 if (/^custom:/.test(type)) {
36 if (!attrs.businessObject) {
37 attrs.businessObject = {
42 assign(attrs.businessObject, {
48 // add width and height if shape
49 if (!/:connection$/.test(type)) {
50 assign(attrs, self._getCustomElementSize(type));
53 if (!('$instanceOf' in attrs.businessObject)) {
54 // ensure we can use ModelUtil#is for type checks
55 Object.defineProperty(attrs.businessObject, '$instanceOf', {
56 value: function(type) {
57 return this.type === type;
62 return self.baseCreate(elementType, attrs);
65 return self.createBpmnElement(elementType, attrs);
69 inherits(CustomElementFactory, BpmnElementFactory);
71 CustomElementFactory.$inject = ['bpmnFactory', 'moddle'];
74 * Returns the default size of custom shapes.
76 * The following example shows an interface on how
77 * to setup the custom shapes's dimensions.
82 * triangle: { width: 40, height: 40 },
83 * rectangle: { width: 100, height: 20 }
86 * return shapes[type];
89 * @param {String} type
91 * @return {Dimensions} a {width, height} object representing the size of the element
93 CustomElementFactory.prototype._getCustomElementSize = function(type) {
95 __default: { width: 100, height: 80 },
96 'custom:triangle': { width: 40, height: 40 },
97 'custom:circle': { width: 140, height: 140 }
100 return shapes[type] || shapes.__default;