all files / Github/bpmn-js/lib/features/modeling/behavior/ EventBasedGatewayBehavior.js

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