all files / Github/diagram-js/lib/features/space-tool/ SpaceUtil.js

52.63% Statements 10/19
35.29% Branches 6/17
100% Functions 2/2
52.63% Lines 10/19
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                                                                                    34× 34×   34×     17×               13×                                                
/**
 * Get Resize direction given axis + offset
 *
 * @param {String} axis (x|y)
 * @param {Number} offset
 *
 * @return {String} (e|w|n|s)
 */
export function getDirection(axis, offset) {
 
  Eif (axis === 'x') {
    Eif (offset > 0) {
      return 'e';
    }
 
    if (offset < 0) {
      return 'w';
    }
  }
 
  if (axis === 'y') {
    if (offset > 0) {
      return 's';
    }
 
    if (offset < 0) {
      return 'n';
    }
  }
 
  return null;
}
 
 
/**
 * Resize the given bounds by the specified delta from a given anchor point.
 *
 * @param {Bounds} bounds the bounding box that should be resized
 * @param {String} direction in which the element is resized (n, s, e, w)
 * @param {Point} delta of the resize operation
 *
 * @return {Bounds} resized bounding box
 */
export function resizeBounds(bounds, direction, delta) {
 
  var dx = delta.x,
      dy = delta.y;
 
  switch (direction) {
 
  case 'n':
    return {
      x: bounds.x,
      y: bounds.y + dy,
      width: bounds.width,
      height: bounds.height - dy
    };
 
  case 's':
    return {
      x: bounds.x,
      y: bounds.y,
      width: bounds.width,
      height: bounds.height + dy
    };
 
  case 'w':
    return {
      x: bounds.x + dx,
      y: bounds.y,
      width: bounds.width - dx,
      height: bounds.height
    };
 
  case 'e':
    return {
      x: bounds.x,
      y: bounds.y,
      width: bounds.width + dx,
      height: bounds.height
    };
 
  default:
    throw new Error('unrecognized direction: ' + direction);
  }
}