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

100% Statements 30/30
100% Branches 11/11
100% Functions 5/5
100% Lines 30/30
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                            963×             963×   346× 346× 346×   346×   346×                 13×         13× 13×   13×         963×   408× 408×   408× 17×     17×         963× 91× 91×   91×           963×   346× 346×   346×   13× 13×                          
import inherits from 'inherits';
 
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
 
import { is } from '../../../util/ModelUtil';
 
 
/**
 * BPMN specific create participant behavior
 */
export default function CreateParticipantBehavior(
    eventBus, modeling, elementFactory,
    bpmnFactory, canvas) {
 
  CommandInterceptor.call(this, eventBus);
 
  /**
   * morph process into collaboration before adding
   * participant onto collaboration
   */
 
  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 = [
  'eventBus',
  'modeling',
  'elementFactory',
  'bpmnFactory',
  'canvas'
];
 
inherits(CreateParticipantBehavior, CommandInterceptor);