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

98.91% Statements 91/92
97.06% Branches 33/34
100% Functions 19/19
98.91% Lines 91/92
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255                                                  1189×     1189×       81× 81× 81×   81×     71×       10× 66×           10×                     1189× 29× 29× 29× 29×         29× 10×   10× 10×           20×     20×   20×         20×     20×     20×       1189× 420× 420×   420× 11×       1189× 471× 471×   471× 12×     12×       1189× 85× 85×   85×           1189× 420× 420×   420×     11×   11×           1189× 48× 48×     48×   48×               1189×               1189× 48× 48×     48×                                                                                 44×       69× 138×    
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,
  find
} 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);
    });
 
    // ensure for available children to calculate bounds
    if (!children.length) {
      return;
    }
 
    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);
      });
    }
  });
 
  function ensureCollaboration(context) {
    var parent = context.parent,
        collaboration;
 
    var rootElement = canvas.getRootElement();
 
    Iif (is(rootElement, 'bpmn:Collaboration')) {
      collaboration = rootElement;
    } else {
 
      // update root element by making collaboration
      collaboration = modeling.makeCollaboration();
 
      // re-use process when creating first participant
      context.process = parent;
    }
 
    context.parent = collaboration;
  }
 
  // turn process into collaboration before adding participant
  this.preExecute('shape.create', function(context) {
    var parent = context.parent,
        shape = context.shape;
 
    if (is(shape, 'bpmn:Participant') && is(parent, 'bpmn:Process')) {
      ensureCollaboration(context);
    }
  }, true);
 
  this.execute('shape.create', function(context) {
    var process = context.process,
        shape = context.shape;
 
    if (process) {
      context.oldProcessRef = shape.businessObject.processRef;
 
      // re-use process when creating first participant
      shape.businessObject.processRef = process.businessObject;
    }
  }, true);
 
  this.revert('shape.create', function(context) {
    var process = context.process,
        shape = context.shape;
 
    if (process) {
 
      // re-use process when creating first participant
      shape.businessObject.processRef = context.oldProcessRef;
    }
  }, true);
 
  this.postExecute('shape.create', function(context) {
    var process = context.process,
        shape = context.shape;
 
    if (process) {
 
      // move children from process to participant
      var processChildren = process.children.slice();
 
      modeling.moveElements(processChildren, { x: 0, y: 0 }, shape);
    }
 
  }, true);
 
  // turn process into collaboration when creating participants
  this.preExecute('elements.create', HIGH_PRIORITY, function(context) {
    var elements = context.elements,
        parent = context.parent,
        participant;
 
    var hasParticipants = findParticipant(elements);
 
    if (hasParticipants && is(parent, 'bpmn:Process')) {
      ensureCollaboration(context);
 
      participant = findParticipant(elements);
 
      context.oldProcessRef = participant.businessObject.processRef;
 
      // re-use process when creating first participant
      participant.businessObject.processRef = parent.businessObject;
    }
  }, true);
 
  this.revert('elements.create', function(context) {
    var elements = context.elements,
        process = context.process,
        participant;
 
    if (process) {
      participant = findParticipant(elements);
 
      // re-use process when creating first participant
      participant.businessObject.processRef = context.oldProcessRef;
    }
  }, true);
 
  this.postExecute('elements.create', function(context) {
    var elements = context.elements,
        process = context.process,
        participant;
 
    if (process) {
      participant = findParticipant(elements);
 
      // move children from process to first participant
      var processChildren = process.children.slice();
 
      modeling.moveElements(processChildren, { x: 0, y: 0 }, participant);
    }
 
  }, 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
  };
 
  var width = Math.max(shape.width, childrenBBox.width),
      height = Math.max(shape.height, childrenBBox.height);
 
  return {
    x: -width / 2,
    y: -height / 2,
    width: width,
    height: 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;
}
 
function findParticipant(elements) {
  return find(elements, function(element) {
    return is(element, 'bpmn:Participant');
  });
}