MessageFlowBehavior.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import inherits from 'inherits-browser';
  2. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  3. import { is } from '../../../util/ModelUtil';
  4. import { isExpanded } from '../../../util/DiUtil';
  5. import { selfAndAllChildren } from 'diagram-js/lib/util/Elements';
  6. import {
  7. getResizedSourceAnchor,
  8. getResizedTargetAnchor
  9. } from 'diagram-js/lib/features/modeling/cmd/helper/AnchorsHelper';
  10. /**
  11. * @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
  12. * @typedef {import('../Modeling').default} Modeling
  13. */
  14. /**
  15. * BPMN-specific message flow behavior.
  16. *
  17. * @param {EventBus} eventBus
  18. * @param {Modeling} modeling
  19. */
  20. export default function MessageFlowBehavior(eventBus, modeling) {
  21. CommandInterceptor.call(this, eventBus);
  22. this.postExecute('shape.replace', function(context) {
  23. var oldShape = context.oldShape,
  24. newShape = context.newShape;
  25. if (!isParticipantCollapse(oldShape, newShape)) {
  26. return;
  27. }
  28. var messageFlows = getMessageFlows(oldShape);
  29. messageFlows.incoming.forEach(function(incoming) {
  30. var anchor = getResizedTargetAnchor(incoming, newShape, oldShape);
  31. modeling.reconnectEnd(incoming, newShape, anchor);
  32. });
  33. messageFlows.outgoing.forEach(function(outgoing) {
  34. var anchor = getResizedSourceAnchor(outgoing, newShape, oldShape);
  35. modeling.reconnectStart(outgoing, newShape, anchor);
  36. });
  37. }, true);
  38. }
  39. MessageFlowBehavior.$inject = [ 'eventBus', 'modeling' ];
  40. inherits(MessageFlowBehavior, CommandInterceptor);
  41. // helpers //////////
  42. function isParticipantCollapse(oldShape, newShape) {
  43. return is(oldShape, 'bpmn:Participant')
  44. && isExpanded(oldShape)
  45. && is(newShape, 'bpmn:Participant')
  46. && !isExpanded(newShape);
  47. }
  48. function getMessageFlows(parent) {
  49. var elements = selfAndAllChildren([ parent ], false);
  50. var incoming = [],
  51. outgoing = [];
  52. elements.forEach(function(element) {
  53. if (element === parent) {
  54. return;
  55. }
  56. element.incoming.forEach(function(connection) {
  57. if (is(connection, 'bpmn:MessageFlow')) {
  58. incoming.push(connection);
  59. }
  60. });
  61. element.outgoing.forEach(function(connection) {
  62. if (is(connection, 'bpmn:MessageFlow')) {
  63. outgoing.push(connection);
  64. }
  65. });
  66. }, []);
  67. return {
  68. incoming: incoming,
  69. outgoing: outgoing
  70. };
  71. }