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

100% Statements 19/19
92.86% Branches 13/14
100% Functions 4/4
100% Lines 19/19

    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

    1128×

    1128×

    347×

    347×
    330×

    17×
    17×

    17×
    2×

    15×

    15×

    15×

    1×

    1×

    78×

    15×

    15×
    6×

    9×

     import inherits from 'inherits';

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

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

    import lineIntersect from './util/LineIntersect';

    export default function RemoveElementBehavior(eventBus, bpmnRules, modeling) {

      CommandInterceptor.call(this, eventBus);

      /**
       * Combine sequence flows when deleting an element
       * if there is one incoming and one outgoing
       * sequence flow
       */
      this.preExecute('shape.delete', function(e) {

        var shape = e.context.shape;

        // only handle [a] -> [shape] -> [b] patterns
        if (shape.incoming.length !== 1 || shape.outgoing.length !== 1) {
          return;
        }

        var inConnection = shape.incoming[0],
            outConnection = shape.outgoing[0];

        // only handle sequence flows
        if (!is(inConnection, 'bpmn:SequenceFlow') || !is(outConnection, 'bpmn:SequenceFlow')) {
          return;
        }

        Eif (bpmnRules.canConnect(inConnection.source, outConnection.target, inConnection)) {

          // compute new, combined waypoints
          var newWaypoints = getNewWaypoints(inConnection.waypoints, outConnection.waypoints);

          modeling.reconnectEnd(inConnection, outConnection.target, newWaypoints);
        }
      });

    }

    inherits(RemoveElementBehavior, CommandInterceptor);

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

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

    function getDocking(point) {
      return point.original || point;
    }

    function getNewWaypoints(inWaypoints, outWaypoints) {

      var intersection = lineIntersect(
        getDocking(inWaypoints[inWaypoints.length - 2]),
        getDocking(inWaypoints[inWaypoints.length - 1]),
        getDocking(outWaypoints[1]),
        getDocking(outWaypoints[0]));

      if (intersection) {
        return [].concat(
          inWaypoints.slice(0, inWaypoints.length - 1),
          [ intersection ],
          outWaypoints.slice(1));
      } else {
        return [
          getDocking(inWaypoints[0]),
          getDocking(outWaypoints[outWaypoints.length - 1])
        ];
      }
    }