EventBasedGatewayBehavior.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import inherits from 'inherits';
  2. import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
  3. import { is } from '../../../util/ModelUtil';
  4. export default function EventBasedGatewayBehavior(eventBus, modeling) {
  5. CommandInterceptor.call(this, eventBus);
  6. /**
  7. * Remove existing sequence flows of event-based target before connecting
  8. * from event-based gateway.
  9. */
  10. this.preExecuted('connection.create', function(event) {
  11. var context = event.context,
  12. source = context.source,
  13. target = context.target,
  14. existingIncomingConnections = target.incoming.slice();
  15. if (context.hints && context.hints.createElementsBehavior === false) {
  16. return;
  17. }
  18. if (
  19. is(source, 'bpmn:EventBasedGateway') &&
  20. target.incoming.length
  21. ) {
  22. existingIncomingConnections.filter(isSequenceFlow)
  23. .forEach(function(sequenceFlow) {
  24. modeling.removeConnection(sequenceFlow);
  25. });
  26. }
  27. });
  28. /**
  29. * After replacing shape with event-based gateway, remove incoming sequence
  30. * flows of event-based targets which do not belong to event-based gateway
  31. * source.
  32. */
  33. this.preExecuted('shape.replace', function(event) {
  34. var newShape = event.context.newShape,
  35. newShapeTargets,
  36. newShapeTargetsIncomingSequenceFlows;
  37. if (!is(newShape, 'bpmn:EventBasedGateway')) {
  38. return;
  39. }
  40. newShapeTargets = newShape.outgoing.filter(isSequenceFlow)
  41. .map(function(sequenceFlow) {
  42. return sequenceFlow.target;
  43. });
  44. newShapeTargetsIncomingSequenceFlows = newShapeTargets.reduce(function(sequenceFlows, target) {
  45. var incomingSequenceFlows = target.incoming.filter(isSequenceFlow);
  46. return sequenceFlows.concat(incomingSequenceFlows);
  47. }, []);
  48. newShapeTargetsIncomingSequenceFlows.forEach(function(sequenceFlow) {
  49. if (sequenceFlow.source !== newShape) {
  50. modeling.removeConnection(sequenceFlow);
  51. }
  52. });
  53. });
  54. }
  55. EventBasedGatewayBehavior.$inject = [
  56. 'eventBus',
  57. 'modeling'
  58. ];
  59. inherits(EventBasedGatewayBehavior, CommandInterceptor);
  60. // helpers //////////////////////
  61. function isSequenceFlow(connection) {
  62. return is(connection, 'bpmn:SequenceFlow');
  63. }