CroppingConnectionDocking.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import {
  2. assign
  3. } from 'min-dash';
  4. import {
  5. getElementLineIntersection
  6. } from './LayoutUtil';
  7. function dockingToPoint(docking) {
  8. // use the dockings actual point and
  9. // retain the original docking
  10. return assign({ original: docking.point.original || docking.point }, docking.actual);
  11. }
  12. /**
  13. * A {@link ConnectionDocking} that crops connection waypoints based on
  14. * the path(s) of the connection source and target.
  15. *
  16. * @param {djs.core.ElementRegistry} elementRegistry
  17. */
  18. export default function CroppingConnectionDocking(elementRegistry, graphicsFactory) {
  19. this._elementRegistry = elementRegistry;
  20. this._graphicsFactory = graphicsFactory;
  21. }
  22. CroppingConnectionDocking.$inject = [ 'elementRegistry', 'graphicsFactory' ];
  23. /**
  24. * @inheritDoc ConnectionDocking#getCroppedWaypoints
  25. */
  26. CroppingConnectionDocking.prototype.getCroppedWaypoints = function(connection, source, target) {
  27. source = source || connection.source;
  28. target = target || connection.target;
  29. var sourceDocking = this.getDockingPoint(connection, source, true),
  30. targetDocking = this.getDockingPoint(connection, target);
  31. var croppedWaypoints = connection.waypoints.slice(sourceDocking.idx + 1, targetDocking.idx);
  32. croppedWaypoints.unshift(dockingToPoint(sourceDocking));
  33. croppedWaypoints.push(dockingToPoint(targetDocking));
  34. return croppedWaypoints;
  35. };
  36. /**
  37. * Return the connection docking point on the specified shape
  38. *
  39. * @inheritDoc ConnectionDocking#getDockingPoint
  40. */
  41. CroppingConnectionDocking.prototype.getDockingPoint = function(connection, shape, dockStart) {
  42. var waypoints = connection.waypoints,
  43. dockingIdx,
  44. dockingPoint,
  45. croppedPoint;
  46. dockingIdx = dockStart ? 0 : waypoints.length - 1;
  47. dockingPoint = waypoints[dockingIdx];
  48. croppedPoint = this._getIntersection(shape, connection, dockStart);
  49. return {
  50. point: dockingPoint,
  51. actual: croppedPoint || dockingPoint,
  52. idx: dockingIdx
  53. };
  54. };
  55. // helpers //////////////////////
  56. CroppingConnectionDocking.prototype._getIntersection = function(shape, connection, takeFirst) {
  57. var shapePath = this._getShapePath(shape),
  58. connectionPath = this._getConnectionPath(connection);
  59. return getElementLineIntersection(shapePath, connectionPath, takeFirst);
  60. };
  61. CroppingConnectionDocking.prototype._getConnectionPath = function(connection) {
  62. return this._graphicsFactory.getConnectionPath(connection);
  63. };
  64. CroppingConnectionDocking.prototype._getShapePath = function(shape) {
  65. return this._graphicsFactory.getShapePath(shape);
  66. };
  67. CroppingConnectionDocking.prototype._getGfx = function(element) {
  68. return this._elementRegistry.getGraphics(element);
  69. };