all files / lib/features/auto-place/ AutoPlace.js

93.75% Statements 15/16
75% Branches 6/8
100% Functions 4/4
93.75% Lines 15/16
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                                          38×                       54×     19×         19× 19×     19×     19×       19×                                             19×     17×     15× 15×          
import { is } from '../../util/ModelUtil';
import { isAny } from '../modeling/util/ModelingUtil';
 
import {
  getTextAnnotationPosition,
  getDataElementPosition,
  getFlowNodePosition,
  getDefaultPosition
} from './AutoPlaceUtil';
 
 
/**
 * A service that places elements connected to existing ones
 * to an appropriate position in an _automated_ fashion.
 *
 * @param {EventBus} eventBus
 * @param {Modeling} modeling
 */
export default function AutoPlace(eventBus, modeling) {
 
  function emit(event, payload) {
    return eventBus.fire(event, payload);
  }
 
 
  /**
   * Append shape to source at appropriate position.
   *
   * @param {djs.model.Shape} source
   * @param {djs.model.Shape} shape
   *
   * @return {djs.model.Shape} appended shape
   */
  this.append = function(source, shape) {
 
    // allow others to provide the position
    var position = emit('autoPlace', {
      source: source,
      shape: shape
    });
 
    Eif (!position) {
      position = getNewShapePosition(source, shape);
    }
 
    var newShape = modeling.appendShape(source, shape, position, source.parent);
 
    // notify interested parties on new shape placed
    emit('autoPlace.end', {
      shape: newShape
    });
 
    return newShape;
  };
 
}
 
AutoPlace.$inject = [
  'eventBus',
  'modeling'
];
 
 
// helpers //////////////////////
 
/**
 * Find the new position for the target element to
 * connect to source.
 *
 * @param  {djs.model.Shape} source
 * @param  {djs.model.Shape} element
 *
 * @return {Point}
 */
function getNewShapePosition(source, element) {
 
  if (is(element, 'bpmn:TextAnnotation')) {
    return getTextAnnotationPosition(source, element);
  }
 
  if (isAny(element, [ 'bpmn:DataObjectReference', 'bpmn:DataStoreReference' ])) {
    return getDataElementPosition(source, element);
  }
 
  Eif (is(element, 'bpmn:FlowNode')) {
    return getFlowNodePosition(source, element);
  }
 
  return getDefaultPosition(source, element);
}