aboutsummaryrefslogtreecommitdiffstats
path: root/workflow-designer-ui/src/main/frontend/src/features/version/composition/custom-modeler/index.js
blob: 86fbff6a2aa33d444c59b4840782388b75f19306 (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
import Modeler from 'bpmn-js/lib/Modeler';

import { assign, isArray } from 'min-dash';

import inherits from 'inherits';

import CustomModule from './custom';

export default function CustomModeler(options) {
    Modeler.call(this, options);

    this._customElements = [];
}

inherits(CustomModeler, Modeler);

CustomModeler.prototype._modules = [].concat(CustomModeler.prototype._modules, [
    CustomModule
]);

/**
 * Add a single custom element to the underlying diagram
 *
 * @param {Object} customElement
 */
CustomModeler.prototype._addCustomShape = function(customElement) {
    this._customElements.push(customElement);

    var canvas = this.get('canvas'),
        elementFactory = this.get('elementFactory');

    var customAttrs = assign({ businessObject: customElement }, customElement);

    var customShape = elementFactory.create('shape', customAttrs);

    return canvas.addShape(customShape);
};

CustomModeler.prototype._addCustomConnection = function(customElement) {
    this._customElements.push(customElement);

    var canvas = this.get('canvas'),
        elementFactory = this.get('elementFactory'),
        elementRegistry = this.get('elementRegistry');

    var customAttrs = assign({ businessObject: customElement }, customElement);

    var connection = elementFactory.create(
        'connection',
        assign(customAttrs, {
            source: elementRegistry.get(customElement.source),
            target: elementRegistry.get(customElement.target)
        }),
        elementRegistry.get(customElement.source).parent
    );

    return canvas.addConnection(connection);
};

/**
 * Add a number of custom elements and connections to the underlying diagram.
 *
 * @param {Array<Object>} customElements
 */
CustomModeler.prototype.addCustomElements = function(customElements) {
    if (!isArray(customElements)) {
        throw new Error('argument must be an array');
    }

    var shapes = [],
        connections = [];

    customElements.forEach(function(customElement) {
        if (isCustomConnection(customElement)) {
            connections.push(customElement);
        } else {
            shapes.push(customElement);
        }
    });

    // add shapes before connections so that connections
    // can already rely on the shapes being part of the diagram
    shapes.forEach(this._addCustomShape, this);

    connections.forEach(this._addCustomConnection, this);
};

/**
 * Get custom elements with their current status.
 *
 * @return {Array<Object>} custom elements on the diagram
 */
CustomModeler.prototype.getCustomElements = function() {
    return this._customElements;
};

function isCustomConnection(element) {
    return element.type === 'custom:connection';
}