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
|
|
|
|
2019-06-05 14:17:21 +00:00
|
|
|
export default function ReplaceConnectionBehavior(eventBus, modeling, bpmnRules, injector) {
|
2015-07-20 09:59:41 +00:00
|
|
|
|
|
|
|
CommandInterceptor.call(this, eventBus);
|
|
|
|
|
2019-06-05 14:17:21 +00:00
|
|
|
var dragging = injector.get('dragging', false);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 14:16:40 +00:00
|
|
|
function replaceReconnectedConnection(event) {
|
|
|
|
|
|
|
|
var context = event.context,
|
|
|
|
connection = context.connection,
|
|
|
|
allowed,
|
|
|
|
replacement;
|
|
|
|
|
|
|
|
if (context.newTarget) {
|
|
|
|
allowed = bpmnRules.canConnect(connection.source, context.newTarget);
|
|
|
|
} else {
|
|
|
|
allowed = bpmnRules.canConnect(context.newSource, connection.target);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!allowed || allowed.type === connection.type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// temporarily connect old shapes with new connection
|
|
|
|
replacement = modeling.connect(connection.source, connection.target, {
|
|
|
|
type: allowed.type,
|
|
|
|
waypoints: connection.waypoints.slice()
|
|
|
|
});
|
|
|
|
|
|
|
|
// remove old connection
|
|
|
|
modeling.removeConnection(connection);
|
|
|
|
|
|
|
|
// replace connection in context to reconnect end/start
|
|
|
|
context.connection = replacement;
|
2019-06-05 14:17:21 +00:00
|
|
|
|
|
|
|
if (dragging) {
|
|
|
|
cleanDraggingSelection(connection, replacement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// monkey-patch selection saved in dragging in order to not re-select non-existing connection
|
|
|
|
function cleanDraggingSelection(oldConnection, newConnection) {
|
|
|
|
var context = dragging.context(),
|
|
|
|
previousSelection = context && context.payload.previousSelection,
|
|
|
|
index;
|
|
|
|
|
|
|
|
// do nothing if not dragging or no selection was present
|
|
|
|
if (!previousSelection || !previousSelection.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = previousSelection.indexOf(oldConnection);
|
|
|
|
|
|
|
|
if (index === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
previousSelection.splice(index, 1, newConnection);
|
2019-06-05 14:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// lifecycle hooks
|
|
|
|
|
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);
|
|
|
|
|
2019-06-05 14:16:40 +00:00
|
|
|
this.preExecute([
|
2015-07-20 09:59:41 +00:00
|
|
|
'connection.reconnectStart',
|
|
|
|
'connection.reconnectEnd'
|
2019-06-05 14:16:40 +00:00
|
|
|
], replaceReconnectedConnection);
|
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',
|
2019-06-05 14:17:21 +00:00
|
|
|
'bpmnRules',
|
|
|
|
'injector'
|
2018-04-02 19:01:53 +00:00
|
|
|
];
|