all files / bpmn-js/lib/features/grid-snapping/behavior/ AutoPlaceBehavior.js

100% Statements 20/20
100% Branches 8/8
100% Functions 4/4
100% Lines 20/20
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              75×     18×     18×     16× 13×       16×             16×                              
import { getNewShapePosition } from '../../auto-place/AutoPlaceUtil';
 
import { getMid } from 'diagram-js/lib/layout/LayoutUtil';
import { is } from '../../../util/ModelUtil';
 
 
export default function AutoPlaceBehavior(eventBus, gridSnapping) {
  eventBus.on('autoPlace', function(context) {
    var source = context.source,
        sourceMid = getMid(source),
        shape = context.shape;
 
    var position = getNewShapePosition(source, shape);
 
    [ 'x', 'y' ].forEach(function(axis) {
      var options = {};
 
      // do not snap if x/y equal
      if (position[ axis ] === sourceMid[ axis ]) {
        return;
      }
 
      if (position[ axis ] > sourceMid[ axis ]) {
        options.min = position[ axis ];
      } else {
        options.max = position[ axis ];
      }
 
      if (is(shape, 'bpmn:TextAnnotation')) {
 
        if (isHorizontal(axis)) {
          options.offset = -shape.width / 2;
        } else {
          options.offset = -shape.height / 2;
        }
 
      }
 
      position[ axis ] = gridSnapping.snapValue(position[ axis ], options);
 
    });
 
    // must be returned to be considered by auto place
    return position;
  });
}
 
AutoPlaceBehavior.$inject = [
  'eventBus',
  'gridSnapping'
];
 
// helpers //////////
 
function isHorizontal(axis) {
  return axis === 'x';
}