LayoutConnectionBehavior.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {
  2. assign
  3. } from 'min-dash';
  4. import inherits from 'inherits-browser';
  5. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  6. import { getConnectionAdjustment as getConnectionAnchorPoint } from './util/ConnectionLayoutUtil';
  7. /**
  8. * @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
  9. * @typedef {import('../Modeling').default} Modeling
  10. */
  11. /**
  12. * A component that makes sure that Associations connected to Connections
  13. * are updated together with the Connection.
  14. *
  15. * @param {EventBus} eventBus
  16. * @param {Modeling} modeling
  17. */
  18. export default function LayoutConnectionBehavior(eventBus, modeling) {
  19. CommandInterceptor.call(this, eventBus);
  20. function getnewAnchorPoint(event, point) {
  21. var context = event.context,
  22. connection = context.connection,
  23. hints = assign({}, context.hints),
  24. newWaypoints = context.newWaypoints || connection.waypoints,
  25. oldWaypoints = context.oldWaypoints;
  26. if (typeof hints.startChanged === 'undefined') {
  27. hints.startChanged = !!hints.connectionStart;
  28. }
  29. if (typeof hints.endChanged === 'undefined') {
  30. hints.endChanged = !!hints.connectionEnd;
  31. }
  32. return getConnectionAnchorPoint(point, newWaypoints, oldWaypoints, hints);
  33. }
  34. this.postExecute([
  35. 'connection.layout',
  36. 'connection.updateWaypoints'
  37. ], function(event) {
  38. var context = event.context;
  39. var connection = context.connection,
  40. outgoing = connection.outgoing,
  41. incoming = connection.incoming;
  42. incoming.forEach(function(connection) {
  43. var endPoint = connection.waypoints[connection.waypoints.length - 1];
  44. var newEndpoint = getnewAnchorPoint(event, endPoint);
  45. var newWaypoints = [].concat(connection.waypoints.slice(0, -1), [ newEndpoint ]);
  46. modeling.updateWaypoints(connection, newWaypoints);
  47. });
  48. outgoing.forEach(function(connection) {
  49. var startpoint = connection.waypoints[0];
  50. var newStartpoint = getnewAnchorPoint(event, startpoint);
  51. var newWaypoints = [].concat([ newStartpoint ], connection.waypoints.slice(1));
  52. modeling.updateWaypoints(connection, newWaypoints);
  53. });
  54. });
  55. this.postExecute([
  56. 'connection.move'
  57. ], function(event) {
  58. var context = event.context;
  59. var connection = context.connection,
  60. outgoing = connection.outgoing,
  61. incoming = connection.incoming,
  62. delta = context.delta;
  63. incoming.forEach(function(connection) {
  64. var endPoint = connection.waypoints[connection.waypoints.length - 1];
  65. var newEndpoint = {
  66. x: endPoint.x + delta.x,
  67. y: endPoint.y + delta.y
  68. };
  69. var newWaypoints = [].concat(connection.waypoints.slice(0, -1), [ newEndpoint ]);
  70. modeling.updateWaypoints(connection, newWaypoints);
  71. });
  72. outgoing.forEach(function(connection) {
  73. var startpoint = connection.waypoints[0];
  74. var newStartpoint = {
  75. x: startpoint.x + delta.x,
  76. y: startpoint.y + delta.y
  77. };
  78. var newWaypoints = [].concat([ newStartpoint ], connection.waypoints.slice(1));
  79. modeling.updateWaypoints(connection, newWaypoints);
  80. });
  81. });
  82. }
  83. inherits(LayoutConnectionBehavior, CommandInterceptor);
  84. LayoutConnectionBehavior.$inject = [
  85. 'eventBus',
  86. 'modeling'
  87. ];