AnchorsHelper.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import {
  2. getNewAttachPoint
  3. } from '../../../../util/AttachUtil';
  4. import {
  5. getOrientation
  6. } from '../../../../layout/LayoutUtil';
  7. import {
  8. filter,
  9. map
  10. } from 'min-dash';
  11. export function getResizedSourceAnchor(connection, shape, oldBounds) {
  12. var waypoints = safeGetWaypoints(connection),
  13. waypointsInsideNewBounds = getWaypointsInsideBounds(waypoints, shape),
  14. oldAnchor = waypoints[0];
  15. // new anchor is the last waypoint enclosed be resized source
  16. if (waypointsInsideNewBounds.length) {
  17. return waypointsInsideNewBounds[ waypointsInsideNewBounds.length - 1 ];
  18. }
  19. return getNewAttachPoint(oldAnchor.original || oldAnchor, oldBounds, shape);
  20. }
  21. export function getResizedTargetAnchor(connection, shape, oldBounds) {
  22. var waypoints = safeGetWaypoints(connection),
  23. waypointsInsideNewBounds = getWaypointsInsideBounds(waypoints, shape),
  24. oldAnchor = waypoints[waypoints.length - 1];
  25. // new anchor is the first waypoint enclosed be resized target
  26. if (waypointsInsideNewBounds.length) {
  27. return waypointsInsideNewBounds[ 0 ];
  28. }
  29. return getNewAttachPoint(oldAnchor.original || oldAnchor, oldBounds, shape);
  30. }
  31. export function getMovedSourceAnchor(connection, source, moveDelta) {
  32. var waypoints = safeGetWaypoints(connection),
  33. oldBounds = subtract(source, moveDelta),
  34. oldAnchor = waypoints[ 0 ];
  35. return getNewAttachPoint(oldAnchor.original || oldAnchor, oldBounds, source);
  36. }
  37. export function getMovedTargetAnchor(connection, target, moveDelta) {
  38. var waypoints = safeGetWaypoints(connection),
  39. oldBounds = subtract(target, moveDelta),
  40. oldAnchor = waypoints[ waypoints.length - 1 ];
  41. return getNewAttachPoint(oldAnchor.original || oldAnchor, oldBounds, target);
  42. }
  43. // helpers //////////////////////
  44. function subtract(bounds, delta) {
  45. return {
  46. x: bounds.x - delta.x,
  47. y: bounds.y - delta.y,
  48. width: bounds.width,
  49. height: bounds.height
  50. };
  51. }
  52. /**
  53. * Return waypoints of given connection; throw if non exists (should not happen!!).
  54. *
  55. * @param {Connection} connection
  56. *
  57. * @return {Array<Point>}
  58. */
  59. function safeGetWaypoints(connection) {
  60. var waypoints = connection.waypoints;
  61. if (!waypoints.length) {
  62. throw new Error('connection#' + connection.id + ': no waypoints');
  63. }
  64. return waypoints;
  65. }
  66. function getWaypointsInsideBounds(waypoints, bounds) {
  67. var originalWaypoints = map(waypoints, getOriginal);
  68. return filter(originalWaypoints, function(waypoint) {
  69. return isInsideBounds(waypoint, bounds);
  70. });
  71. }
  72. /**
  73. * Checks if point is inside bounds, incl. edges.
  74. *
  75. * @param {Point} point
  76. * @param {Bounds} bounds
  77. */
  78. function isInsideBounds(point, bounds) {
  79. return getOrientation(bounds, point, 1) === 'intersect';
  80. }
  81. function getOriginal(point) {
  82. return point.original || point;
  83. }