FixHoverBehavior.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { getLanesRoot } from '../util/LaneUtil';
  2. import { is } from '../../../util/ModelUtil';
  3. import { isAny } from '../util/ModelingUtil';
  4. /**
  5. * @typedef {import('diagram-js/lib/core/ElementRegistry').default} ElementRegistry
  6. * @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
  7. * @typedef {import('diagram-js/lib/core/Canvas').default} Canvas
  8. */
  9. var HIGH_PRIORITY = 1500;
  10. var HIGHEST_PRIORITY = 2000;
  11. /**
  12. * Correct hover targets in certain situations to improve diagram interaction.
  13. *
  14. * @param {ElementRegistry} elementRegistry
  15. * @param {EventBus} eventBus
  16. * @param {Canvas} canvas
  17. */
  18. export default function FixHoverBehavior(elementRegistry, eventBus, canvas) {
  19. eventBus.on([
  20. 'create.hover',
  21. 'create.move',
  22. 'create.out',
  23. 'create.end',
  24. 'shape.move.hover',
  25. 'shape.move.move',
  26. 'shape.move.out',
  27. 'shape.move.end'
  28. ], HIGH_PRIORITY, function(event) {
  29. var context = event.context,
  30. shape = context.shape || event.shape,
  31. hover = event.hover;
  32. // ensure elements are not dropped onto a bpmn:Lane but onto
  33. // the underlying bpmn:Participant
  34. if (is(hover, 'bpmn:Lane') && !isAny(shape, [ 'bpmn:Lane', 'bpmn:Participant' ])) {
  35. event.hover = getLanesRoot(hover);
  36. event.hoverGfx = elementRegistry.getGraphics(event.hover);
  37. }
  38. var rootElement = canvas.getRootElement();
  39. // ensure bpmn:Group and label elements are dropped
  40. // always onto the root
  41. if (hover !== rootElement && (shape.labelTarget || is(shape, 'bpmn:Group'))) {
  42. event.hover = rootElement;
  43. event.hoverGfx = elementRegistry.getGraphics(event.hover);
  44. }
  45. });
  46. eventBus.on([
  47. 'connect.hover',
  48. 'connect.out',
  49. 'connect.end',
  50. 'connect.cleanup',
  51. 'global-connect.hover',
  52. 'global-connect.out',
  53. 'global-connect.end',
  54. 'global-connect.cleanup'
  55. ], HIGH_PRIORITY, function(event) {
  56. var hover = event.hover;
  57. // ensure connections start/end on bpmn:Participant,
  58. // not the underlying bpmn:Lane
  59. if (is(hover, 'bpmn:Lane')) {
  60. event.hover = getLanesRoot(hover) || hover;
  61. event.hoverGfx = elementRegistry.getGraphics(event.hover);
  62. }
  63. });
  64. eventBus.on([
  65. 'bendpoint.move.hover'
  66. ], HIGH_PRIORITY, function(event) {
  67. var context = event.context,
  68. hover = event.hover,
  69. type = context.type;
  70. // ensure reconnect start/end on bpmn:Participant,
  71. // not the underlying bpmn:Lane
  72. if (is(hover, 'bpmn:Lane') && /reconnect/.test(type)) {
  73. event.hover = getLanesRoot(hover) || hover;
  74. event.hoverGfx = elementRegistry.getGraphics(event.hover);
  75. }
  76. });
  77. eventBus.on([
  78. 'connect.start'
  79. ], HIGH_PRIORITY, function(event) {
  80. var context = event.context,
  81. start = context.start;
  82. // ensure connect start on bpmn:Participant,
  83. // not the underlying bpmn:Lane
  84. if (is(start, 'bpmn:Lane')) {
  85. context.start = getLanesRoot(start) || start;
  86. }
  87. });
  88. // allow movement of participants from lanes
  89. eventBus.on('shape.move.start', HIGHEST_PRIORITY, function(event) {
  90. var shape = event.shape;
  91. if (is(shape, 'bpmn:Lane')) {
  92. event.shape = getLanesRoot(shape) || shape;
  93. }
  94. });
  95. // ensure lanes aren't resized without their parent participant when using
  96. // space tool
  97. eventBus.on('spaceTool.move', HIGHEST_PRIORITY, function(event) {
  98. var hover = event.hover;
  99. if (hover && is(hover, 'bpmn:Lane')) {
  100. event.hover = getLanesRoot(hover);
  101. }
  102. });
  103. }
  104. FixHoverBehavior.$inject = [
  105. 'elementRegistry',
  106. 'eventBus',
  107. 'canvas'
  108. ];