01d4d27893de5371d8c3693991805346c2eb3ad2
[sdc/sdc-workflow-designer.git] /
1 import { assign } from 'min-dash';
2
3 import inherits from 'inherits';
4
5 import BpmnElementFactory from 'bpmn-js/lib/features/modeling/ElementFactory';
6 import { DEFAULT_LABEL_SIZE } from 'bpmn-js/lib/util/LabelUtil';
7
8 /**
9  * A custom factory that knows how to create BPMN _and_ custom elements.
10  */
11 export default function CustomElementFactory(bpmnFactory, moddle) {
12     BpmnElementFactory.call(this, bpmnFactory, moddle);
13
14     var self = this;
15
16     /**
17      * Create a diagram-js element with the given type (any of shape, connection, label).
18      *
19      * @param  {String} elementType
20      * @param  {Object} attrs
21      *
22      * @return {djs.model.Base}
23      */
24     this.create = function(elementType, attrs) {
25         var type = attrs.type;
26
27         if (elementType === 'label') {
28             return self.baseCreate(
29                 elementType,
30                 assign({ type: 'label' }, DEFAULT_LABEL_SIZE, attrs)
31             );
32         }
33
34         // add type to businessObject if custom
35         if (/^custom:/.test(type)) {
36             if (!attrs.businessObject) {
37                 attrs.businessObject = {
38                     type: type
39                 };
40
41                 if (attrs.id) {
42                     assign(attrs.businessObject, {
43                         id: attrs.id
44                     });
45                 }
46             }
47
48             // add width and height if shape
49             if (!/:connection$/.test(type)) {
50                 assign(attrs, self._getCustomElementSize(type));
51             }
52
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;
58                     }
59                 });
60             }
61
62             return self.baseCreate(elementType, attrs);
63         }
64
65         return self.createBpmnElement(elementType, attrs);
66     };
67 }
68
69 inherits(CustomElementFactory, BpmnElementFactory);
70
71 CustomElementFactory.$inject = ['bpmnFactory', 'moddle'];
72
73 /**
74  * Returns the default size of custom shapes.
75  *
76  * The following example shows an interface on how
77  * to setup the custom shapes's dimensions.
78  *
79  * @example
80  *
81  * var shapes = {
82  *   triangle: { width: 40, height: 40 },
83  *   rectangle: { width: 100, height: 20 }
84  * };
85  *
86  * return shapes[type];
87  *
88  *
89  * @param {String} type
90  *
91  * @return {Dimensions} a {width, height} object representing the size of the element
92  */
93 CustomElementFactory.prototype._getCustomElementSize = function(type) {
94     var shapes = {
95         __default: { width: 100, height: 80 },
96         'custom:triangle': { width: 40, height: 40 },
97         'custom:circle': { width: 140, height: 140 }
98     };
99
100     return shapes[type] || shapes.__default;
101 };