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

100% Statements 26/26
91.3% Branches 21/23
100% Functions 6/6
100% Lines 26/26

    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

    1073×

    1073×
    119×
    119×

    119×

    117×

    2×

    1073×
    25×
    25×
    25×

    25×

    24×

    1×

    1×

    1×

    1×

    51×

    51×
    17×

    34×

    17×

    17×
    14×

    3×

    31×
     import inherits from 'inherits';

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

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

    import { expandedBounds } from './ToggleElementCollapseBehaviour';

    export default function SubProcessBehavior(elementFactory, eventBus, modeling) {
      CommandInterceptor.call(this, eventBus);

      /**
       * Adjust position of sub process after it replaces a shape with incoming
       * sequence flows and no outgoing sequence flows to prevent overlap.
       */
      this.postExecuted('shape.replace', function(event) {
        var oldShape = event.context.oldShape,
            newShape = event.context.newShape;

        if (!is(newShape, 'bpmn:SubProcess') ||
          !hasIncomingSequenceFlows(newShape) ||
          hasOutgoingSequenceFlows(newShape)) {
          return;
        }

        modeling.moveShape(newShape, {
          x: oldShape.x - newShape.x,
          y: 0
        });
      });

      /**
       * Adjust position of sub process with incoming sequence flows and no outgoing
       * sequence flows after toggling to prevent overlap.
       */
      this.postExecuted('shape.toggleCollapse', function(event) {
        var context = event.context,
            shape = context.shape,
            defaultSize = elementFactory._getDefaultSize(shape),
            newBounds;

        if (!is(shape, 'bpmn:SubProcess') ||
          shape.collapsed ||
          !hasIncomingSequenceFlows(shape) ||
          hasOutgoingSequenceFlows(shape)) {
          return;
        }

        newBounds = expandedBounds(shape, defaultSize);

        modeling.moveShape(shape, {
          x: shape.x - newBounds.x,
          y: 0
        });
      });
    }

    SubProcessBehavior.$inject = [
      'elementFactory',
      'eventBus',
      'modeling'
    ];

    inherits(SubProcessBehavior, CommandInterceptor);

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

    function hasIncomingSequenceFlows(shape) {
      shape = shape || {};

      if (shape.incoming && shape.incoming.length) {
        return shape.incoming.some(isSequenceFlow);
      }

      return false;
    }

    function hasOutgoingSequenceFlows(shape) {
      shape = shape || {};

      if (shape.outgoing && shape.outgoing.length) {
        return shape.outgoing.some(isSequenceFlow);
      }

      return false;
    }

    function isSequenceFlow(connection) {
      return is(connection, 'bpmn:SequenceFlow');
    }