2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
forEach,
|
|
|
|
find,
|
|
|
|
matchPattern
|
|
|
|
} from 'min-dash';
|
2018-03-22 09:22:37 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import inherits from 'inherits';
|
2015-07-20 09:59:41 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
|
2015-07-20 09:59:41 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import { is } from '../../../util/ModelUtil';
|
2015-07-20 09:59:41 +00:00
|
|
|
|
2018-03-22 09:22:37 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
export default function ReplaceConnectionBehavior(eventBus, modeling, bpmnRules) {
|
2015-07-20 09:59:41 +00:00
|
|
|
|
|
|
|
CommandInterceptor.call(this, eventBus);
|
|
|
|
|
2015-08-19 09:08:29 +00:00
|
|
|
function fixConnection(connection) {
|
2015-07-20 09:59:41 +00:00
|
|
|
|
|
|
|
var source = connection.source,
|
2015-08-19 09:08:29 +00:00
|
|
|
target = connection.target,
|
|
|
|
parent = connection.parent;
|
|
|
|
|
|
|
|
// do not do anything if connection
|
|
|
|
// is already deleted (may happen due to other
|
|
|
|
// behaviors plugged-in before)
|
|
|
|
if (!parent) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-20 09:59:41 +00:00
|
|
|
|
|
|
|
var replacementType,
|
|
|
|
remove;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if incoming or outgoing connections
|
|
|
|
* can stay or could be substituted with an
|
|
|
|
* appropriate replacement.
|
|
|
|
*
|
|
|
|
* This holds true for SequenceFlow <> MessageFlow.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (is(connection, 'bpmn:SequenceFlow')) {
|
|
|
|
if (!bpmnRules.canConnectSequenceFlow(source, target)) {
|
|
|
|
remove = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bpmnRules.canConnectMessageFlow(source, target)) {
|
|
|
|
replacementType = 'bpmn:MessageFlow';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// transform message flows into sequence flows, if possible
|
|
|
|
|
|
|
|
if (is(connection, 'bpmn:MessageFlow')) {
|
|
|
|
|
|
|
|
if (!bpmnRules.canConnectMessageFlow(source, target)) {
|
|
|
|
remove = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bpmnRules.canConnectSequenceFlow(source, target)) {
|
|
|
|
replacementType = 'bpmn:SequenceFlow';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is(connection, 'bpmn:Association') && !bpmnRules.canConnectAssociation(source, target)) {
|
|
|
|
remove = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-19 09:08:29 +00:00
|
|
|
// remove invalid connection,
|
|
|
|
// unless it has been removed already
|
2015-07-20 09:59:41 +00:00
|
|
|
if (remove) {
|
|
|
|
modeling.removeConnection(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace SequenceFlow <> MessageFlow
|
|
|
|
|
|
|
|
if (replacementType) {
|
2015-08-21 12:21:56 +00:00
|
|
|
modeling.connect(source, target, {
|
2015-07-20 09:59:41 +00:00
|
|
|
type: replacementType,
|
|
|
|
waypoints: connection.waypoints.slice()
|
2015-08-21 12:21:56 +00:00
|
|
|
});
|
2015-07-20 09:59:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 08:51:52 +00:00
|
|
|
this.postExecuted('elements.move', function(context) {
|
2015-07-20 09:59:41 +00:00
|
|
|
|
|
|
|
var closure = context.closure,
|
|
|
|
allConnections = closure.allConnections;
|
|
|
|
|
2015-08-19 09:08:29 +00:00
|
|
|
forEach(allConnections, fixConnection);
|
2015-07-20 09:59:41 +00:00
|
|
|
}, true);
|
|
|
|
|
2015-07-29 15:56:14 +00:00
|
|
|
this.postExecuted([
|
2015-07-20 09:59:41 +00:00
|
|
|
'connection.reconnectStart',
|
|
|
|
'connection.reconnectEnd'
|
2016-06-07 06:46:45 +00:00
|
|
|
], function(event) {
|
2015-07-20 09:59:41 +00:00
|
|
|
|
|
|
|
var connection = event.context.connection;
|
|
|
|
|
2015-08-19 09:08:29 +00:00
|
|
|
fixConnection(connection);
|
2015-07-20 09:59:41 +00:00
|
|
|
});
|
|
|
|
|
2016-01-07 10:54:10 +00:00
|
|
|
this.postExecuted('element.updateProperties', function(event) {
|
|
|
|
var context = event.context,
|
|
|
|
properties = context.properties,
|
|
|
|
element = context.element,
|
|
|
|
businessObject = element.businessObject,
|
|
|
|
connection;
|
|
|
|
|
|
|
|
// remove condition expression when morphing to default flow
|
|
|
|
if (properties.default) {
|
2018-03-22 09:22:37 +00:00
|
|
|
connection = find(
|
|
|
|
element.outgoing,
|
|
|
|
matchPattern({ id: element.businessObject.default.id })
|
|
|
|
);
|
2016-01-07 10:54:10 +00:00
|
|
|
|
|
|
|
if (connection) {
|
|
|
|
modeling.updateProperties(connection, { conditionExpression: undefined });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove default property from source when morphing to conditional flow
|
|
|
|
if (properties.conditionExpression && businessObject.sourceRef.default === businessObject) {
|
|
|
|
modeling.updateProperties(element.source, { default: undefined });
|
|
|
|
}
|
|
|
|
});
|
2015-07-20 09:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inherits(ReplaceConnectionBehavior, CommandInterceptor);
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
ReplaceConnectionBehavior.$inject = [
|
|
|
|
'eventBus',
|
|
|
|
'modeling',
|
|
|
|
'bpmnRules'
|
|
|
|
];
|