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

100% Statements 18/18
100% Branches 11/11
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                          1128×             1128× 406× 406×           406× 206×     200×   200×   10×   10×         10×     10×   10×                           10×   10×    
import inherits from 'inherits';
 
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
 
import { isAny } from '../util/ModelingUtil';
import { getBusinessObject } from '../../../util/ModelUtil';
 
 
/**
 * BPMN specific attach event behavior
 */
export default function AttachEventBehavior(eventBus, bpmnReplace) {
 
  CommandInterceptor.call(this, eventBus);
 
  /**
   * replace intermediate event with boundary event when
   * attaching it to a shape
   */
 
  this.preExecute('elements.move', function(context) {
    var shapes = context.shapes,
        host = context.newHost,
        shape,
        eventDefinition,
        boundaryEvent,
        newShape;
 
    if (shapes.length !== 1) {
      return;
    }
 
    shape = shapes[0];
 
    if (host && isAny(shape, [ 'bpmn:IntermediateThrowEvent', 'bpmn:IntermediateCatchEvent' ])) {
 
      eventDefinition = getEventDefinition(shape);
 
      boundaryEvent = {
        type: 'bpmn:BoundaryEvent',
        host: host
      };
 
      if (eventDefinition) {
        boundaryEvent.eventDefinitionType = eventDefinition.$type;
      }
 
      newShape = bpmnReplace.replaceElement(shape, boundaryEvent, { layoutConnection: false });
 
      context.shapes = [ newShape ];
    }
  }, true);
}
 
AttachEventBehavior.$inject = [
  'eventBus',
  'bpmnReplace'
];
 
inherits(AttachEventBehavior, CommandInterceptor);
 
 
 
// helper /////
function getEventDefinition(element) {
  var bo = getBusinessObject(element);
 
  return bo && bo.eventDefinitions && bo.eventDefinitions[0];
}