lib/layout/ManhattanLayout.js
function _tryRepairConnectionSide(moved, other, newDocking, points) {

  function needsRelayout(moved, other, points) {

    if (points.length < 3) {
      return true;
    }

    if (points.length > 4) {
      return false;
    }

    // relayout if two points overlap
    // this is most likely due to
    return !!find(points, function(p, idx) {
      var q = points[idx - 1];

      return q && pointDistance(p, q) < 3;
    });
  }

  function repairBendpoint(candidate, oldPeer, newPeer) {

    var alignment = pointsAligned(oldPeer, candidate);

    switch (alignment) {
    case 'v':
      // repair vertical alignment
      return { x: candidate.x, y: newPeer.y };
    case 'h':
      // repair horizontal alignment
      return { x: newPeer.x, y: candidate.y };
    }

    return { x: candidate.x, y: candidate. y };
  }

  function removeOverlapping(points, a, b) {
    var i;

    for (i = points.length - 2; i !== 0; i--) {

      // intersects (?) break, remove all bendpoints up to this one and relayout
      if (pointInRect(points[i], a, INTERSECTION_THRESHOLD) ||
          pointInRect(points[i], b, INTERSECTION_THRESHOLD)) {

        // return sliced old connection
        return points.slice(i);
      }
    }

    return points;
  }


  // (0) only repair what has layoutable bendpoints

  // (1) if only one bendpoint and on shape moved onto other shapes axis
  //     (horizontally / vertically), relayout

  if (needsRelayout(moved, other, points)) {
    return null;
  }

  var oldDocking = points[0],
      newPoints = points.slice(),
      slicedPoints;

  // (2) repair only last line segment and only if it was layouted before

  newPoints[0] = newDocking;
  newPoints[1] = repairBendpoint(newPoints[1], oldDocking, newDocking);


  // (3) if shape intersects with any bendpoint after repair,
  //     remove all segments up to this bendpoint and repair from there

  slicedPoints = removeOverlapping(newPoints, moved, other);

  if (slicedPoints !== newPoints) {
    return _tryRepairConnectionSide(moved, other, newDocking, slicedPoints);
  }

  return newPoints;
}
function _tryRepairConnectionSide(moved, other, newDocking, points) {

  function needsRelayout(moved, other, points) {

    if (points.length < 3) {
      return true;
    }

    if (points.length > 4) {
      return false;
    }

    // relayout if two points overlap
    // this is most likely due to
    return !!find(points, function(p, idx) {
      var q = points[idx - 1];

      return q && pointDistance(p, q) < 3;
    });
  }

  function repairBendpoint(candidate, oldPeer, newPeer) {

    var alignment = pointsAligned(oldPeer, candidate);

    switch (alignment) {
    case 'v':
      // repair vertical alignment
      return { x: candidate.x, y: newPeer.y };
    case 'h':
      // repair horizontal alignment
      return { x: newPeer.x, y: candidate.y };
    }

    return { x: candidate.x, y: candidate. y };
  }

  function removeOverlapping(points, a, b) {
    var i;

    for (i = points.length - 2; i !== 0; i--) {

      // intersects (?) break, remove all bendpoints up to this one and relayout
      if (pointInRect(points[i], a, INTERSECTION_THRESHOLD) ||
          pointInRect(points[i], b, INTERSECTION_THRESHOLD)) {

        // return sliced old connection
        return points.slice(i);
      }
    }

    return points;
  }


  // (0) only repair what has layoutable bendpoints

  // (1) if only one bendpoint and on shape moved onto other shapes axis
  //     (horizontally / vertically), relayout

  if (needsRelayout(moved, other, points)) {
    return null;
  }

  var oldDocking = points[0],
      newPoints = points.slice(),
      slicedPoints;

  // (2) repair only last line segment and only if it was layouted before

  newPoints[0] = newDocking;
  newPoints[1] = repairBendpoint(newPoints[1], oldDocking, newDocking);


  // (3) if shape intersects with any bendpoint after repair,
  //     remove all segments up to this bendpoint and repair from there

  slicedPoints = removeOverlapping(newPoints, moved, other);

  if (slicedPoints !== newPoints) {
    return _tryRepairConnectionSide(moved, other, newDocking, slicedPoints);
  }

  return newPoints;
}
function _tryRepairConnectionSide ( moved , other , newDocking , points ) { function needsRelayout ( moved , other , points ) { if ( points . length < 3 ) { return true ; } if ( points . length > 4 ) { return false ; } // relayout if two points overlap // this is most likely due to return ! ! find ( points , function ( p , idx ) { var q = points [ idx - 1 ] ; return q && pointDistance ( p , q ) < 3 ; } ) ; } function repairBendpoint ( candidate , oldPeer , newPeer ) { var alignment = pointsAligned ( oldPeer , candidate ) ; switch ( alignment ) { case 'v' : // repair vertical alignment return { x : candidate . x , y : newPeer . y } ; case 'h' : // repair horizontal alignment return { x : newPeer . x , y : candidate . y } ; } return { x : candidate . x , y : candidate . y } ; } function removeOverlapping ( points , a , b ) { var i ; for ( i = points . length - 2 ; i !== 0 ; i -- ) { // intersects (?) break, remove all bendpoints up to this one and relayout if ( pointInRect ( points [ i ] , a , INTERSECTION_THRESHOLD ) || pointInRect ( points [ i ] , b , INTERSECTION_THRESHOLD ) ) { // return sliced old connection return points . slice ( i ) ; } } return points ; } // (0) only repair what has layoutable bendpoints // (1) if only one bendpoint and on shape moved onto other shapes axis //     (horizontally / vertically), relayout if ( needsRelayout ( moved , other , points ) ) { return null ; } var oldDocking = points [ 0 ] , newPoints = points . slice ( ) , slicedPoints ; // (2) repair only last line segment and only if it was layouted before newPoints [ 0 ] = newDocking ; newPoints [ 1 ] = repairBendpoint ( newPoints [ 1 ] , oldDocking , newDocking ) ; // (3) if shape intersects with any bendpoint after repair, //     remove all segments up to this bendpoint and repair from there slicedPoints = removeOverlapping ( newPoints , moved , other ) ; if ( slicedPoints !== newPoints ) { return _tryRepairConnectionSide ( moved , other , newDocking , slicedPoints ) ; } return newPoints ; }
Repair a connection from one side that moved.

@param {Bounds} moved
@param {Bounds} other
@param {Point} newDocking
@param {Array<Point>} points originalPoints from moved to other

@return {Array<Point>} the repaired points between the two rectangles
Repair a connection from one side that moved .