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

100% Statements 22/22
100% Branches 9/9
100% Functions 5/5
100% Lines 22/22
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× 24× 24×   24×     1128× 404× 404×     404× 403×           1128× 137× 137×     137×         134×                                      
import inherits from 'inherits';
 
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
 
import { is } from '../../../util/ModelUtil';
import { isExpanded } from '../../../util/DiUtil.js';
 
/**
 * Add start event child by default when creating an expanded subprocess
 * with create.start or replacing a task with an expanded subprocess.
 */
export default function SubProcessStartEventBehavior(eventBus, modeling) {
  CommandInterceptor.call(this, eventBus);
 
  eventBus.on('create.start', function(event) {
    var shape = event.context.shape,
        hints = event.context.hints;
 
    hints.shouldAddStartEvent = is(shape, 'bpmn:SubProcess') && isExpanded(shape);
  });
 
  this.postExecuted('shape.create', function(event) {
    var shape = event.context.shape,
        hints = event.context.hints,
        position;
 
    if (!hints.shouldAddStartEvent) {
      return;
    }
 
    position = calculatePositionRelativeToShape(shape);
 
    modeling.createShape({ type: 'bpmn:StartEvent' }, position, shape);
  });
 
  this.postExecuted('shape.replace', function(event) {
    var oldShape = event.context.oldShape,
        newShape = event.context.newShape,
        position;
 
    if (
      !is(newShape, 'bpmn:SubProcess') ||
      !is(oldShape, 'bpmn:Task') ||
      !isExpanded(newShape)
    ) {
      return;
    }
 
    position = calculatePositionRelativeToShape(newShape);
 
    modeling.createShape({ type: 'bpmn:StartEvent' }, position, newShape);
  });
}
 
SubProcessStartEventBehavior.$inject = [
  'eventBus',
  'modeling'
];
 
inherits(SubProcessStartEventBehavior, CommandInterceptor);
 
// helpers //////////
 
function calculatePositionRelativeToShape(shape) {
  return {
    x: shape.x + shape.width / 6,
    y: shape.y + shape.height / 2
  };
}