ConnectPreview.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { isReverse } from './Connect';
  2. var HIGH_PRIORITY = 1100,
  3. LOW_PRIORITY = 900;
  4. var MARKER_OK = 'connect-ok',
  5. MARKER_NOT_OK = 'connect-not-ok';
  6. /**
  7. * Shows connection preview during connect.
  8. *
  9. * @param {didi.Injector} injector
  10. * @param {EventBus} eventBus
  11. * @param {Canvas} canvas
  12. */
  13. export default function ConnectPreview(injector, eventBus, canvas) {
  14. var connectionPreview = injector.get('connectionPreview', false);
  15. connectionPreview && eventBus.on('connect.move', function(event) {
  16. var context = event.context,
  17. canConnect = context.canExecute,
  18. hover = context.hover,
  19. source = context.source,
  20. start = context.start,
  21. startPosition = context.startPosition,
  22. target = context.target,
  23. connectionStart = context.connectionStart || startPosition,
  24. connectionEnd = context.connectionEnd || {
  25. x: event.x,
  26. y: event.y
  27. },
  28. previewStart = connectionStart,
  29. previewEnd = connectionEnd;
  30. if (isReverse(context)) {
  31. previewStart = connectionEnd;
  32. previewEnd = connectionStart;
  33. }
  34. connectionPreview.drawPreview(context, canConnect, {
  35. source: source || start,
  36. target: target || hover,
  37. connectionStart: previewStart,
  38. connectionEnd: previewEnd
  39. });
  40. });
  41. eventBus.on('connect.hover', LOW_PRIORITY, function(event) {
  42. var context = event.context,
  43. hover = event.hover,
  44. canExecute = context.canExecute;
  45. // ignore hover
  46. if (canExecute === null) {
  47. return;
  48. }
  49. canvas.addMarker(hover, canExecute ? MARKER_OK : MARKER_NOT_OK);
  50. });
  51. eventBus.on([
  52. 'connect.out',
  53. 'connect.cleanup'
  54. ], HIGH_PRIORITY, function(event) {
  55. var hover = event.hover;
  56. if (hover) {
  57. canvas.removeMarker(hover, MARKER_OK);
  58. canvas.removeMarker(hover, MARKER_NOT_OK);
  59. }
  60. });
  61. connectionPreview && eventBus.on('connect.cleanup', function(event) {
  62. connectionPreview.cleanUp(event.context);
  63. });
  64. }
  65. ConnectPreview.$inject = [
  66. 'injector',
  67. 'eventBus',
  68. 'canvas'
  69. ];