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

100% Statements 62/62
100% Branches 23/23
100% Functions 13/13
100% Lines 62/62

    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

    1×
    1×

    1×

    1×

    1128×

    1128×

    71×
    71×
    71×

    71×

    62×

    9×
    65×

    9×

    9×

    9×

    9×

    1128×
    24×
    24×
    24×
    24×

    1×
    1×

    24×
    9×

    9×
    9×

    1128×
    404×
    404×
    404×

    404×

    404×

    19×

    19×
    19×

    19×

    1128×
    467×
    467×

    467×
    23×

    23×

    1128×
    93×
    93×

    93×

    7×

    1128×
    404×
    404×

    404×

    19×

    19×

    1×

    1×

    9×

    9×

    9×

    9×

    44×
     import inherits from 'inherits';

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

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

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

    import { getBBox } from 'diagram-js/lib/util/Elements';

    import { assign } from 'min-dash';

    import { asTRBL } from 'diagram-js/lib/layout/LayoutUtil';

    var HORIZONTAL_PARTICIPANT_PADDING = 20,
        VERTICAL_PARTICIPANT_PADDING = 20;

    export var PARTICIPANT_BORDER_WIDTH = 30;

    var HIGH_PRIORITY = 2000;

    /**
     * BPMN-specific behavior for creating participants.
     */
    export default function CreateParticipantBehavior(canvas, eventBus, modeling) {
      CommandInterceptor.call(this, eventBus);

      // fit participant
      eventBus.on([
        'create.start',
        'shape.move.start'
      ], HIGH_PRIORITY, function(event) {
        var context = event.context,
            shape = context.shape,
            rootElement = canvas.getRootElement();

        if (!is(shape, 'bpmn:Participant') ||
          !is(rootElement, 'bpmn:Process') ||
          !rootElement.children.length) {
          return;
        }

        // ignore connections, groups and labels
        var children = rootElement.children.filter(function(element) {
          return !is(element, 'bpmn:Group') &&
            !isLabel(element) &&
            !isConnection(element);
        });

        var childrenBBox = getBBox(children);

        var participantBounds = getParticipantBounds(shape, childrenBBox);

        // assign width and height
        assign(shape, participantBounds);

        // assign create constraints
        context.createConstraints = getParticipantCreateConstraints(shape, childrenBBox);
      });

      // force hovering process when creating first participant
      eventBus.on('create.start', HIGH_PRIORITY, function(event) {
        var context = event.context,
            shape = context.shape,
            rootElement = canvas.getRootElement(),
            rootElementGfx = canvas.getGraphics(rootElement);

        function ensureHoveringProcess(event) {
          event.element = rootElement;
          event.gfx = rootElementGfx;
        }

        if (is(shape, 'bpmn:Participant') && is(rootElement, 'bpmn:Process')) {
          eventBus.on('element.hover', HIGH_PRIORITY, ensureHoveringProcess);

          eventBus.once('create.cleanup', function() {
            eventBus.off('element.hover', ensureHoveringProcess);
          });
        }
      });

      // turn process into collaboration before adding participant
      this.preExecute('shape.create', function(context) {
        var parent = context.parent,
            shape = context.shape,
            position = context.position;

        var rootElement = canvas.getRootElement();

        if (
          is(parent, 'bpmn:Process') &&
          is(shape, 'bpmn:Participant') &&
          !is(rootElement, 'bpmn:Collaboration')
        ) {

          // this is going to detach the process root
          // and set the returned collaboration element
          // as the new root element
          var collaborationElement = modeling.makeCollaboration();

          // monkey patch the create context
          // so that the participant is being dropped
          // onto the new collaboration root instead
          context.position = position;
          context.parent = collaborationElement;

          context.processRoot = parent;
        }
      }, true);

      this.execute('shape.create', function(context) {
        var processRoot = context.processRoot,
            shape = context.shape;

        if (processRoot) {
          context.oldProcessRef = shape.businessObject.processRef;

          // assign the participant processRef
          shape.businessObject.processRef = processRoot.businessObject;
        }
      }, true);

      this.revert('shape.create', function(context) {
        var processRoot = context.processRoot,
            shape = context.shape;

        if (processRoot) {

          // assign the participant processRef
          shape.businessObject.processRef = context.oldProcessRef;
        }
      }, true);

      this.postExecute('shape.create', function(context) {
        var processRoot = context.processRoot,
            shape = context.shape;

        if (processRoot) {

          // process root is already detached at this point
          var processChildren = processRoot.children.slice();

          modeling.moveElements(processChildren, { x: 0, y: 0 }, shape);
        }

      }, true);
    }

    CreateParticipantBehavior.$inject = [
      'canvas',
      'eventBus',
      'modeling'
    ];

    inherits(CreateParticipantBehavior, CommandInterceptor);

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

    function getParticipantBounds(shape, childrenBBox) {
      childrenBBox = {
        width: childrenBBox.width + HORIZONTAL_PARTICIPANT_PADDING * 2 + PARTICIPANT_BORDER_WIDTH,
        height: childrenBBox.height + VERTICAL_PARTICIPANT_PADDING * 2
      };

      return {
        width: Math.max(shape.width, childrenBBox.width),
        height: Math.max(shape.height, childrenBBox.height)
      };
    }

    function getParticipantCreateConstraints(shape, childrenBBox) {
      childrenBBox = asTRBL(childrenBBox);

      return {
        bottom: childrenBBox.top + shape.height / 2 - VERTICAL_PARTICIPANT_PADDING,
        left: childrenBBox.right - shape.width / 2 + HORIZONTAL_PARTICIPANT_PADDING,
        top: childrenBBox.bottom - shape.height / 2 + VERTICAL_PARTICIPANT_PADDING,
        right: childrenBBox.left + shape.width / 2 - HORIZONTAL_PARTICIPANT_PADDING - PARTICIPANT_BORDER_WIDTH
      };
    }

    function isConnection(element) {
      return !!element.waypoints;
    }