all files / lib/layout/ CroppingConnectionDocking.js

96.55% Statements 28/29
100% Branches 10/10
87.5% Functions 7/8
96.55% Lines 28/29
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                        90×                     47× 47×                 45× 45×   45× 45×   45×   45× 45×   45×                 103×         103× 103×   103×   103×                     103× 103×   103×     103×     103×          
import {
  assign
} from 'min-dash';
 
import {
  getElementLineIntersection
} from './LayoutUtil';
 
 
function dockingToPoint(docking) {
  // use the dockings actual point and
  // retain the original docking
  return assign({ original: docking.point.original || docking.point }, docking.actual);
}
 
 
/**
 * A {@link ConnectionDocking} that crops connection waypoints based on
 * the path(s) of the connection source and target.
 *
 * @param {djs.core.ElementRegistry} elementRegistry
 */
export default function CroppingConnectionDocking(elementRegistry, graphicsFactory) {
  this._elementRegistry = elementRegistry;
  this._graphicsFactory = graphicsFactory;
}
 
CroppingConnectionDocking.$inject = [ 'elementRegistry', 'graphicsFactory' ];
 
 
/**
 * @inheritDoc ConnectionDocking#getCroppedWaypoints
 */
CroppingConnectionDocking.prototype.getCroppedWaypoints = function(connection, source, target) {
 
  source = source || connection.source;
  target = target || connection.target;
 
  var sourceDocking = this.getDockingPoint(connection, source, true),
      targetDocking = this.getDockingPoint(connection, target);
 
  var croppedWaypoints = connection.waypoints.slice(sourceDocking.idx + 1, targetDocking.idx);
 
  croppedWaypoints.unshift(dockingToPoint(sourceDocking));
  croppedWaypoints.push(dockingToPoint(targetDocking));
 
  return croppedWaypoints;
};
 
/**
 * Return the connection docking point on the specified shape
 *
 * @inheritDoc ConnectionDocking#getDockingPoint
 */
CroppingConnectionDocking.prototype.getDockingPoint = function(connection, shape, dockStart) {
 
  var waypoints = connection.waypoints,
      dockingIdx,
      dockingPoint,
      croppedPoint;
 
  dockingIdx = dockStart ? 0 : waypoints.length - 1;
  dockingPoint = waypoints[dockingIdx];
 
  croppedPoint = this._getIntersection(shape, connection, dockStart);
 
  return {
    point: dockingPoint,
    actual: croppedPoint || dockingPoint,
    idx: dockingIdx
  };
};
 
 
// helpers //////////////////////
 
CroppingConnectionDocking.prototype._getIntersection = function(shape, connection, takeFirst) {
 
  var shapePath = this._getShapePath(shape),
      connectionPath = this._getConnectionPath(connection);
 
  return getElementLineIntersection(shapePath, connectionPath, takeFirst);
};
 
CroppingConnectionDocking.prototype._getConnectionPath = function(connection) {
  return this._graphicsFactory.getConnectionPath(connection);
};
 
CroppingConnectionDocking.prototype._getShapePath = function(shape) {
  return this._graphicsFactory.getShapePath(shape);
};
 
CroppingConnectionDocking.prototype._getGfx = function(element) {
  return this._elementRegistry.getGraphics(element);
};