aboutsummaryrefslogtreecommitdiffstats
path: root/sdc-workflow-designer-ui/src/main/frontend/src/features/version/composition/custom-modeler/custom/CustomRules.js
blob: 1dce143da14fc89f5b2f9391f37a270e79967f81 (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 { reduce } from 'min-dash';

import inherits from 'inherits';

import { is } from 'bpmn-js/lib/util/ModelUtil';

import RuleProvider from 'diagram-js/lib/features/rules/RuleProvider';

var HIGH_PRIORITY = 1500;

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

/**
 * Specific rules for custom elements
 */
export default function CustomRules(eventBus) {
    RuleProvider.call(this, eventBus);
}

inherits(CustomRules, RuleProvider);

CustomRules.$inject = ['eventBus'];

CustomRules.prototype.init = function() {
    /**
     * Can shape be created on target container?
     */
    function canCreate(shape, target) {
        // only judge about custom elements
        if (!isCustom(shape)) {
            return;
        }

        // allow creation on processes
        return (
            is(target, 'bpmn:Process') ||
            is(target, 'bpmn:Participant') ||
            is(target, 'bpmn:Collaboration')
        );
    }

    /**
     * Can source and target be connected?
     */
    function canConnect(source, target) {
        // only judge about custom elements
        if (!isCustom(source) && !isCustom(target)) {
            return;
        }

        // allow connection between custom shape and task
        if (isCustom(source)) {
            if (is(target, 'bpmn:Task')) {
                return { type: 'custom:connection' };
            } else {
                return false;
            }
        } else if (isCustom(target)) {
            if (is(source, 'bpmn:Task')) {
                return { type: 'custom:connection' };
            } else {
                return false;
            }
        }
    }

    this.addRule('elements.move', HIGH_PRIORITY, function(context) {
        var target = context.target,
            shapes = context.shapes;

        var type;

        // do not allow mixed movements of custom / BPMN shapes
        // if any shape cannot be moved, the group cannot be moved, too
        var allowed = reduce(
            shapes,
            function(result, s) {
                if (type === undefined) {
                    type = isCustom(s);
                }

                if (type !== isCustom(s) || result === false) {
                    return false;
                }

                return canCreate(s, target);
            },
            undefined
        );

        // reject, if we have at least one
        // custom element that cannot be moved
        return allowed;
    });

    this.addRule('shape.create', HIGH_PRIORITY, function(context) {
        var target = context.target,
            shape = context.shape;

        return canCreate(shape, target);
    });

    this.addRule('shape.resize', HIGH_PRIORITY, function(context) {
        var shape = context.shape;

        if (isCustom(shape)) {
            // cannot resize custom elements
            return false;
        }
    });

    this.addRule('connection.create', HIGH_PRIORITY, function(context) {
        var source = context.source,
            target = context.target;

        return canConnect(source, target);
    });

    this.addRule('connection.reconnectStart', HIGH_PRIORITY, function(context) {
        var connection = context.connection,
            source = context.hover || context.source,
            target = connection.target;

        return canConnect(source, target, connection);
    });

    this.addRule('connection.reconnectEnd', HIGH_PRIORITY, function(context) {
        var connection = context.connection,
            source = connection.source,
            target = context.hover || context.target;

        return canConnect(source, target, connection);
    });
};