BendpointMovePreview.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import {
  2. classes as svgClasses,
  3. remove as svgRemove
  4. } from 'tiny-svg';
  5. import { addBendpoint } from './BendpointUtil';
  6. import { translate } from '../../util/SvgTransformUtil';
  7. import { isReverse } from './BendpointMove';
  8. var RECONNECT_START = 'reconnectStart',
  9. RECONNECT_END = 'reconnectEnd',
  10. UPDATE_WAYPOINTS = 'updateWaypoints';
  11. var MARKER_OK = 'connect-ok',
  12. MARKER_NOT_OK = 'connect-not-ok',
  13. MARKER_CONNECT_HOVER = 'connect-hover',
  14. MARKER_CONNECT_UPDATING = 'djs-updating',
  15. MARKER_ELEMENT_HIDDEN = 'djs-element-hidden';
  16. var HIGH_PRIORITY = 1100;
  17. /**
  18. * Preview connection while moving bendpoints.
  19. */
  20. export default function BendpointMovePreview(bendpointMove, injector, eventBus, canvas) {
  21. this._injector = injector;
  22. var connectionPreview = injector.get('connectionPreview', false);
  23. eventBus.on('bendpoint.move.start', function(event) {
  24. var context = event.context,
  25. bendpointIndex = context.bendpointIndex,
  26. connection = context.connection,
  27. insert = context.insert,
  28. waypoints = connection.waypoints,
  29. newWaypoints = waypoints.slice();
  30. context.waypoints = waypoints;
  31. if (insert) {
  32. // insert placeholder for new bendpoint
  33. newWaypoints.splice(bendpointIndex, 0, { x: event.x, y: event.y });
  34. }
  35. connection.waypoints = newWaypoints;
  36. // add dragger gfx
  37. var draggerGfx = context.draggerGfx = addBendpoint(canvas.getLayer('overlays'));
  38. svgClasses(draggerGfx).add('djs-dragging');
  39. canvas.addMarker(connection, MARKER_ELEMENT_HIDDEN);
  40. canvas.addMarker(connection, MARKER_CONNECT_UPDATING);
  41. });
  42. eventBus.on('bendpoint.move.hover', function(event) {
  43. var context = event.context,
  44. allowed = context.allowed,
  45. hover = context.hover,
  46. type = context.type;
  47. if (hover) {
  48. canvas.addMarker(hover, MARKER_CONNECT_HOVER);
  49. if (type === UPDATE_WAYPOINTS) {
  50. return;
  51. }
  52. if (allowed) {
  53. canvas.removeMarker(hover, MARKER_NOT_OK);
  54. canvas.addMarker(hover, MARKER_OK);
  55. } else if (allowed === false) {
  56. canvas.removeMarker(hover, MARKER_OK);
  57. canvas.addMarker(hover, MARKER_NOT_OK);
  58. }
  59. }
  60. });
  61. eventBus.on([
  62. 'bendpoint.move.out',
  63. 'bendpoint.move.cleanup'
  64. ], HIGH_PRIORITY, function(event) {
  65. var context = event.context,
  66. hover = context.hover,
  67. target = context.target;
  68. if (hover) {
  69. canvas.removeMarker(hover, MARKER_CONNECT_HOVER);
  70. canvas.removeMarker(hover, target ? MARKER_OK : MARKER_NOT_OK);
  71. }
  72. });
  73. eventBus.on('bendpoint.move.move', function(event) {
  74. var context = event.context,
  75. allowed = context.allowed,
  76. bendpointIndex = context.bendpointIndex,
  77. draggerGfx = context.draggerGfx,
  78. hover = context.hover,
  79. type = context.type,
  80. connection = context.connection,
  81. source = connection.source,
  82. target = connection.target,
  83. newWaypoints = connection.waypoints.slice(),
  84. bendpoint = { x: event.x, y: event.y },
  85. hints = context.hints || {},
  86. drawPreviewHints = {};
  87. if (connectionPreview) {
  88. if (hints.connectionStart) {
  89. drawPreviewHints.connectionStart = hints.connectionStart;
  90. }
  91. if (hints.connectionEnd) {
  92. drawPreviewHints.connectionEnd = hints.connectionEnd;
  93. }
  94. if (type === RECONNECT_START) {
  95. if (isReverse(context)) {
  96. drawPreviewHints.connectionEnd = drawPreviewHints.connectionEnd || bendpoint;
  97. drawPreviewHints.source = target;
  98. drawPreviewHints.target = hover || source;
  99. newWaypoints = newWaypoints.reverse();
  100. } else {
  101. drawPreviewHints.connectionStart = drawPreviewHints.connectionStart || bendpoint;
  102. drawPreviewHints.source = hover || source;
  103. drawPreviewHints.target = target;
  104. }
  105. } else if (type === RECONNECT_END) {
  106. if (isReverse(context)) {
  107. drawPreviewHints.connectionStart = drawPreviewHints.connectionStart || bendpoint;
  108. drawPreviewHints.source = hover || target;
  109. drawPreviewHints.target = source;
  110. newWaypoints = newWaypoints.reverse();
  111. } else {
  112. drawPreviewHints.connectionEnd = drawPreviewHints.connectionEnd || bendpoint;
  113. drawPreviewHints.source = source;
  114. drawPreviewHints.target = hover || target;
  115. }
  116. } else {
  117. drawPreviewHints.noCropping = true;
  118. drawPreviewHints.noLayout = true;
  119. newWaypoints[ bendpointIndex ] = bendpoint;
  120. }
  121. if (type === UPDATE_WAYPOINTS) {
  122. newWaypoints = bendpointMove.cropWaypoints(connection, newWaypoints);
  123. }
  124. drawPreviewHints.waypoints = newWaypoints;
  125. connectionPreview.drawPreview(context, allowed, drawPreviewHints);
  126. }
  127. translate(draggerGfx, event.x, event.y);
  128. }, this);
  129. eventBus.on([
  130. 'bendpoint.move.end',
  131. 'bendpoint.move.cancel'
  132. ], HIGH_PRIORITY, function(event) {
  133. var context = event.context,
  134. connection = context.connection,
  135. draggerGfx = context.draggerGfx,
  136. hover = context.hover,
  137. target = context.target,
  138. waypoints = context.waypoints;
  139. connection.waypoints = waypoints;
  140. // remove dragger gfx
  141. svgRemove(draggerGfx);
  142. canvas.removeMarker(connection, MARKER_CONNECT_UPDATING);
  143. canvas.removeMarker(connection, MARKER_ELEMENT_HIDDEN);
  144. if (hover) {
  145. canvas.removeMarker(hover, MARKER_OK);
  146. canvas.removeMarker(hover, target ? MARKER_OK : MARKER_NOT_OK);
  147. }
  148. if (connectionPreview) {
  149. connectionPreview.cleanUp(context);
  150. }
  151. });
  152. }
  153. BendpointMovePreview.$inject = [
  154. 'bendpointMove',
  155. 'injector',
  156. 'eventBus',
  157. 'canvas'
  158. ];