aboutsummaryrefslogtreecommitdiffstats
path: root/sdc-workflow-designer-ui/src/main/frontend/src/features/version/composition/custom-modeler/custom/CustomUpdater.js
blob: 532c24f3b36851587216c80e1145e6b09292761d (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import inherits from 'inherits';

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

import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';

import {
    add as collectionAdd,
    remove as collectionRemove
} from 'diagram-js/lib/util/Collections';

/**
 * A handler responsible for updating the custom element's businessObject
 * once changes on the diagram happen.
 */
export default function CustomUpdater(eventBus, bpmnjs) {
    CommandInterceptor.call(this, eventBus);

    function updateCustomElement(e) {
        var context = e.context,
            shape = context.shape,
            businessObject = shape.businessObject;

        if (!isCustom(shape)) {
            return;
        }

        var parent = shape.parent;

        var customElements = bpmnjs._customElements;

        // make sure element is added / removed from bpmnjs.customElements
        if (!parent) {
            collectionRemove(customElements, businessObject);
        } else {
            collectionAdd(customElements, businessObject);
        }

        // save custom element position
        assign(businessObject, pick(shape, ['x', 'y']));
    }

    function updateCustomConnection(e) {
        var context = e.context,
            connection = context.connection,
            source = connection.source,
            target = connection.target,
            businessObject = connection.businessObject;

        var parent = connection.parent;

        var customElements = bpmnjs._customElements;

        // make sure element is added / removed from bpmnjs.customElements
        if (!parent) {
            collectionRemove(customElements, businessObject);
        } else {
            collectionAdd(customElements, businessObject);
        }

        // update waypoints
        assign(businessObject, {
            waypoints: copyWaypoints(connection)
        });

        if (source && target) {
            assign(businessObject, {
                source: source.id,
                target: target.id
            });
        }
    }

    this.executed(
        ['shape.create', 'shape.move', 'shape.delete'],
        ifCustomElement(updateCustomElement)
    );

    this.reverted(
        ['shape.create', 'shape.move', 'shape.delete'],
        ifCustomElement(updateCustomElement)
    );

    this.executed(
        [
            'connection.create',
            'connection.reconnectStart',
            'connection.reconnectEnd',
            'connection.updateWaypoints',
            'connection.delete',
            'connection.layout',
            'connection.move'
        ],
        ifCustomElement(updateCustomConnection)
    );

    this.reverted(
        [
            'connection.create',
            'connection.reconnectStart',
            'connection.reconnectEnd',
            'connection.updateWaypoints',
            'connection.delete',
            'connection.layout',
            'connection.move'
        ],
        ifCustomElement(updateCustomConnection)
    );
}

inherits(CustomUpdater, CommandInterceptor);

CustomUpdater.$inject = ['eventBus', 'bpmnjs'];

/////// helpers ///////////////////////////////////

function copyWaypoints(connection) {
    return connection.waypoints.map(function(p) {
        return { x: p.x, y: p.y };
    });
}

function isCustom(element) {
    return element && /custom:/.test(element.type);
}

function ifCustomElement(fn) {
    return function(event) {
        var context = event.context,
            element = context.shape || context.connection;

        if (isCustom(element)) {
            fn(event);
        }
    };
}