aboutsummaryrefslogtreecommitdiffstats
path: root/workflow-designer-ui/src/main/frontend/src/features/version/composition/custom-modeler/custom/CustomElementFactory.js
blob: 01d4d27893de5371d8c3693991805346c2eb3ad2 (plain)
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
import { assign } from 'min-dash';

import inherits from 'inherits';

import BpmnElementFactory from 'bpmn-js/lib/features/modeling/ElementFactory';
import { DEFAULT_LABEL_SIZE } from 'bpmn-js/lib/util/LabelUtil';

/**
 * A custom factory that knows how to create BPMN _and_ custom elements.
 */
export default function CustomElementFactory(bpmnFactory, moddle) {
    BpmnElementFactory.call(this, bpmnFactory, moddle);

    var self = this;

    /**
     * Create a diagram-js element with the given type (any of shape, connection, label).
     *
     * @param  {String} elementType
     * @param  {Object} attrs
     *
     * @return {djs.model.Base}
     */
    this.create = function(elementType, attrs) {
        var type = attrs.type;

        if (elementType === 'label') {
            return self.baseCreate(
                elementType,
                assign({ type: 'label' }, DEFAULT_LABEL_SIZE, attrs)
            );
        }

        // add type to businessObject if custom
        if (/^custom:/.test(type)) {
            if (!attrs.businessObject) {
                attrs.businessObject = {
                    type: type
                };

                if (attrs.id) {
                    assign(attrs.businessObject, {
                        id: attrs.id
                    });
                }
            }

            // add width and height if shape
            if (!/:connection$/.test(type)) {
                assign(attrs, self._getCustomElementSize(type));
            }

            if (!('$instanceOf' in attrs.businessObject)) {
                // ensure we can use ModelUtil#is for type checks
                Object.defineProperty(attrs.businessObject, '$instanceOf', {
                    value: function(type) {
                        return this.type === type;
                    }
                });
            }

            return self.baseCreate(elementType, attrs);
        }

        return self.createBpmnElement(elementType, attrs);
    };
}

inherits(CustomElementFactory, BpmnElementFactory);

CustomElementFactory.$inject = ['bpmnFactory', 'moddle'];

/**
 * Returns the default size of custom shapes.
 *
 * The following example shows an interface on how
 * to setup the custom shapes's dimensions.
 *
 * @example
 *
 * var shapes = {
 *   triangle: { width: 40, height: 40 },
 *   rectangle: { width: 100, height: 20 }
 * };
 *
 * return shapes[type];
 *
 *
 * @param {String} type
 *
 * @return {Dimensions} a {width, height} object representing the size of the element
 */
CustomElementFactory.prototype._getCustomElementSize = function(type) {
    var shapes = {
        __default: { width: 100, height: 80 },
        'custom:triangle': { width: 40, height: 40 },
        'custom:circle': { width: 140, height: 140 }
    };

    return shapes[type] || shapes.__default;
};