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

100% Statements 18/18
100% Branches 12/12
100% Functions 3/3
100% Lines 18/18

    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

    1189×

    1189×
    414×
    414×

    414×
    208×

    206×

    206×

    9×

    9×
    4×

    5×

    9×

    9×

    1×

    1×

    9×

    9×

     import inherits from 'inherits';

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

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

    import { isLabel } from '../../../util/LabelUtil';

    /**
     * BPMN specific detach event behavior
     */
    export default function DetachEventBehavior(eventBus, bpmnReplace) {

      CommandInterceptor.call(this, eventBus);

      /**
       * replace boundary event with intermediate event when
       * detaching from a shape
       */

      this.preExecute('elements.move', function(context) {
        var shapes = context.shapes,
            host = context.newHost,
            shape,
            eventDefinition,
            intermediateEvent,
            newShape;

        if (shapes.length !== 1) {
          return;
        }

        shape = shapes[0];

        if (!isLabel(shape) && !host && is(shape, 'bpmn:BoundaryEvent')) {

          eventDefinition = getEventDefinition(shape);

          if (eventDefinition) {
            intermediateEvent = {
              type: 'bpmn:IntermediateCatchEvent',
              eventDefinitionType: eventDefinition.$type
            };
          } else {
            intermediateEvent = {
              type: 'bpmn:IntermediateThrowEvent'
            };
          }

          newShape = bpmnReplace.replaceElement(shape, intermediateEvent, { layoutConnection: false });

          context.shapes = [ newShape ];
        }
      }, true);
    }

    DetachEventBehavior.$inject = [
      'eventBus',
      'bpmnReplace'
    ];

    inherits(DetachEventBehavior, CommandInterceptor);

    // helper /////
    function getEventDefinition(element) {
      var bo = getBusinessObject(element);

      return bo && bo.eventDefinitions && bo.eventDefinitions[0];
    }