# all files / Github/bpmn-js/lib/features/modeling/behavior/ ReplaceConnectionBehavior.js

100% Statements 61/61
95.65% Branches 44/46
100% Functions 6/6
100% Lines 61/61

    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
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191

    1128×

    1128×

    351×
    351×
    351×

    351×
    2×

    349×
    234×
    24×

    234×
    9×

    349×

    61×
    8×

    61×
    4×

    349×
    2×

    349×
    34×

    349×
    13×

    164×
    164×

    164×
    86×

    78×

    164×
    155×

    9×

    9×

    9×

    9×
    9×

    9×
    9×

    9×
    7×

    2×

    2×
    1×

    1×

    1128×

    406×
    406×

    406×

    1128×

    1128×
    326×
    326×
    326×
    326×

    326×
    29×

    29×
    29×

    326×
    2×

    1×

    1×

     import {
      forEach,
      find,
      matchPattern
    } from 'min-dash';

    import inherits from 'inherits';

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

    import { is } from '../../../util/ModelUtil';

    export default function ReplaceConnectionBehavior(eventBus, modeling, bpmnRules, injector) {

      CommandInterceptor.call(this, eventBus);

      var dragging = injector.get('dragging', false);

      function fixConnection(connection) {

        var source = connection.source,
            target = connection.target,
            parent = connection.parent;

        // do not do anything if connection
        // is already deleted (may happen due to other
        // behaviors plugged-in before)
        if (!parent) {
          return;
        }

        var replacementType,
            remove;

        /**
         * Check if incoming or outgoing connections
         * can stay or could be substituted with an
         * appropriate replacement.
         *
         * This holds true for SequenceFlow <> MessageFlow.
         */

        if (is(connection, 'bpmn:SequenceFlow')) {
          if (!bpmnRules.canConnectSequenceFlow(source, target)) {
            remove = true;
          }

          if (bpmnRules.canConnectMessageFlow(source, target)) {
            replacementType = 'bpmn:MessageFlow';
          }
        }

        // transform message flows into sequence flows, if possible

        if (is(connection, 'bpmn:MessageFlow')) {

          if (!bpmnRules.canConnectMessageFlow(source, target)) {
            remove = true;
          }

          if (bpmnRules.canConnectSequenceFlow(source, target)) {
            replacementType = 'bpmn:SequenceFlow';
          }
        }

        if (is(connection, 'bpmn:Association') && !bpmnRules.canConnectAssociation(source, target)) {
          remove = true;
        }

        // remove invalid connection,
        // unless it has been removed already
        if (remove) {
          modeling.removeConnection(connection);
        }

        // replace SequenceFlow <> MessageFlow

        if (replacementType) {
          modeling.connect(source, target, {
            type: replacementType,
            waypoints: connection.waypoints.slice()
          });
        }
      }

      function replaceReconnectedConnection(event) {

        var context = event.context,
            connection = context.connection,
            allowed,
            replacement;

        if (context.newTarget) {
          allowed = bpmnRules.canConnect(connection.source, context.newTarget);
        } else {
          allowed = bpmnRules.canConnect(context.newSource, connection.target);
        }

        if (!allowed || allowed.type === connection.type) {
          return;
        }

        // temporarily connect old shapes with new connection
        replacement = modeling.connect(connection.source, connection.target, {
          type: allowed.type,
          waypoints: connection.waypoints.slice()
        });

        // remove old connection
        modeling.removeConnection(connection);

        // replace connection in context to reconnect end/start
        context.connection = replacement;

        Eif (dragging) {
          cleanDraggingSelection(connection, replacement);
        }
      }

      // monkey-patch selection saved in dragging in order to not re-select non-existing connection
      function cleanDraggingSelection(oldConnection, newConnection) {
        var context = dragging.context(),
            previousSelection = context && context.payload.previousSelection,
            index;

        // do nothing if not dragging or no selection was present
        if (!previousSelection || !previousSelection.length) {
          return;
        }

        index = previousSelection.indexOf(oldConnection);

        if (index === -1) {
          return;
        }

        previousSelection.splice(index, 1, newConnection);
      }

      // lifecycle hooks

      this.postExecuted('elements.move', function(context) {

        var closure = context.closure,
            allConnections = closure.allConnections;

        forEach(allConnections, fixConnection);
      }, true);

      this.preExecute([
        'connection.reconnectStart',
        'connection.reconnectEnd'
      ], replaceReconnectedConnection);

      this.postExecuted('element.updateProperties', function(event) {
        var context = event.context,
            properties = context.properties,
            element = context.element,
            businessObject = element.businessObject,
            connection;

        // remove condition expression when morphing to default flow
        if (properties.default) {
          connection = find(
            element.outgoing,
            matchPattern({ id: element.businessObject.default.id })
          );

          Eif (connection) {
            modeling.updateProperties(connection, { conditionExpression: undefined });
          }
        }

        // remove default property from source when morphing to conditional flow
        if (properties.conditionExpression && businessObject.sourceRef.default === businessObject) {
          modeling.updateProperties(element.source, { default: undefined });
        }
      });
    }

    inherits(ReplaceConnectionBehavior, CommandInterceptor);

    ReplaceConnectionBehavior.$inject = [
      'eventBus',
      'modeling',
      'bpmnRules',
      'injector'
    ];