all files / lib/features/snapping/ ResizeSnapping.js

90.57% Statements 48/53
75% Branches 15/20
100% Functions 14/14
90.57% Lines 48/53
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141                                                33×   33×     33×       12× 12× 12× 12× 12×   12×       12×       12×   12×       12×     33×                                               16×                   11×   11×   11×     11×         16×       16×       12×       12×  
import SnapContext from './SnapContext';
 
import {
  bottomLeft,
  bottomRight,
  getChildren,
  isSnapped,
  topLeft,
  topRight
} from './SnapUtil';
 
import { isCmd } from '../keyboard/KeyboardUtil';
 
import { forEach } from 'min-dash';
 
var HIGHER_PRIORITY = 1250;
 
 
/**
 * Snap during resize.
 *
 * @param {EventBus} eventBus
 * @param {Snapping} snapping
 */
export default function ResizeSnapping(eventBus, snapping) {
  var self = this;
 
  eventBus.on([ 'resize.start' ], function(event) {
    self.initSnap(event);
  });
 
  eventBus.on([
    'resize.move',
    'resize.end',
  ], HIGHER_PRIORITY, function(event) {
    var context = event.context,
        shape = context.shape,
        parent = shape.parent,
        direction = context.direction,
        snapContext = context.snapContext;
 
    Iif (event.originalEvent && isCmd(event.originalEvent)) {
      return;
    }
 
    Iif (isSnapped(event)) {
      return;
    }
 
    var snapPoints = snapContext.pointsForTarget(parent);
 
    if (!snapPoints.initialized) {
      snapPoints = self.addSnapTargetPoints(snapPoints, shape, parent, direction);
 
      snapPoints.initialized = true;
    }
 
    snapping.snap(event, snapPoints);
  });
 
  eventBus.on([ 'resize.cleanup' ], function() {
    snapping.hide();
  });
}
 
ResizeSnapping.prototype.initSnap = function(event) {
  var context = event.context,
      shape = context.shape,
      direction = context.direction,
      snapContext = context.snapContext;
 
  if (!snapContext) {
    snapContext = context.snapContext = new SnapContext();
  }
 
  var snapCorner = getCorner(shape, direction);
 
  snapContext.setSnapOrigin('corner', {
    x: snapCorner.x - event.x,
    y: snapCorner.y - event.y
  });
 
  return snapContext;
};
 
ResizeSnapping.prototype.addSnapTargetPoints = function(snapPoints, shape, target, direction) {
  var snapTargets = this.getSnapTargets(shape, target);
 
  forEach(snapTargets, function(snapTarget) {
    snapPoints.add('corner', bottomRight(snapTarget));
    snapPoints.add('corner', topLeft(snapTarget));
  });
 
  snapPoints.add('corner', getCorner(shape, direction));
 
  return snapPoints;
};
 
ResizeSnapping.$inject = [
  'eventBus',
  'snapping'
];
 
ResizeSnapping.prototype.getSnapTargets = function(shape, target) {
  return getChildren(target).filter(function(child) {
    return !isAttached(child, shape)
      && !isConnection(child)
      && !isHidden(child)
      && !isLabel(child);
  });
};
 
// helpers //////////
 
function getCorner(shape, direction) {
  Iif (direction === 'nw') {
    return topLeft(shape);
  } else Iif (direction === 'ne') {
    return topRight(shape);
  } else Iif (direction === 'sw') {
    return bottomLeft(shape);
  } else {
    return bottomRight(shape);
  }
}
 
function isAttached(element, host) {
  return element.host === host;
}
 
function isConnection(element) {
  return !!element.waypoints;
}
 
function isHidden(element) {
  return !!element.hidden;
}
 
function isLabel(element) {
  return !!element.labelTarget;
}