EventBasedGatewayBehavior.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import inherits from 'inherits-browser';
  2. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  3. import { is } from '../../../util/ModelUtil';
  4. /**
  5. * @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
  6. * @typedef {import('../Modeling').default} Modeling
  7. */
  8. /**
  9. * @param {EventBus} eventBus
  10. * @param {Modeling} modeling
  11. */
  12. export default function EventBasedGatewayBehavior(eventBus, modeling) {
  13. CommandInterceptor.call(this, eventBus);
  14. /**
  15. * Remove incoming sequence flows of event-based target when creating
  16. * sequence flow.
  17. *
  18. * 1. If source is event-based gateway remove all incoming sequence flows
  19. * 2. If source is not event-based gateway remove all incoming sequence flows
  20. * whose source is event-based gateway
  21. */
  22. this.preExecuted('connection.create', function(event) {
  23. var context = event.context,
  24. connection = context.connection,
  25. source = context.source,
  26. target = context.target,
  27. hints = context.hints;
  28. if (hints && hints.createElementsBehavior === false) {
  29. return;
  30. }
  31. if (!isSequenceFlow(connection)) {
  32. return;
  33. }
  34. var sequenceFlows = [];
  35. if (is(source, 'bpmn:EventBasedGateway')) {
  36. sequenceFlows = target.incoming
  37. .filter(flow =>
  38. flow !== connection &&
  39. isSequenceFlow(flow)
  40. );
  41. } else {
  42. sequenceFlows = target.incoming
  43. .filter(flow =>
  44. flow !== connection &&
  45. isSequenceFlow(flow) &&
  46. is(flow.source, 'bpmn:EventBasedGateway')
  47. );
  48. }
  49. sequenceFlows.forEach(function(sequenceFlow) {
  50. modeling.removeConnection(sequenceFlow);
  51. });
  52. });
  53. /**
  54. * Remove incoming sequence flows of event-based targets when replacing source
  55. * with event-based gateway.
  56. */
  57. this.preExecuted('shape.replace', function(event) {
  58. var context = event.context,
  59. newShape = context.newShape;
  60. if (!is(newShape, 'bpmn:EventBasedGateway')) {
  61. return;
  62. }
  63. var targets = newShape.outgoing.filter(isSequenceFlow)
  64. .reduce(function(targets, sequenceFlow) {
  65. if (!targets.includes(sequenceFlow.target)) {
  66. return targets.concat(sequenceFlow.target);
  67. }
  68. return targets;
  69. }, []);
  70. targets.forEach(function(target) {
  71. target.incoming.filter(isSequenceFlow).forEach(function(sequenceFlow) {
  72. const sequenceFlowsFromNewShape = target.incoming.filter(isSequenceFlow).filter(function(sequenceFlow) {
  73. return sequenceFlow.source === newShape;
  74. });
  75. if (sequenceFlow.source !== newShape || sequenceFlowsFromNewShape.length > 1) {
  76. modeling.removeConnection(sequenceFlow);
  77. }
  78. });
  79. });
  80. });
  81. }
  82. EventBasedGatewayBehavior.$inject = [
  83. 'eventBus',
  84. 'modeling'
  85. ];
  86. inherits(EventBasedGatewayBehavior, CommandInterceptor);
  87. // helpers //////////
  88. function isSequenceFlow(connection) {
  89. return is(connection, 'bpmn:SequenceFlow');
  90. }