2018-04-02 19:01:53 +00:00
|
|
|
import inherits from 'inherits';
|
2017-01-25 16:27:30 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
assign,
|
2019-08-29 09:55:34 +00:00
|
|
|
filter,
|
2018-07-24 14:30:42 +00:00
|
|
|
find,
|
2019-08-29 09:55:34 +00:00
|
|
|
isNumber
|
2018-04-02 19:01:53 +00:00
|
|
|
} from 'min-dash';
|
2017-01-25 16:27:30 +00:00
|
|
|
|
2019-08-29 09:55:34 +00:00
|
|
|
import { getMid } from 'diagram-js/lib/layout/LayoutUtil';
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
|
2017-01-25 16:27:30 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
getApproxIntersection
|
|
|
|
} from 'diagram-js/lib/util/LineIntersection';
|
2017-01-25 16:27:30 +00:00
|
|
|
|
2017-07-24 13:07:44 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
export default function DropOnFlowBehavior(eventBus, bpmnRules, modeling) {
|
2017-01-25 16:27:30 +00:00
|
|
|
|
|
|
|
CommandInterceptor.call(this, eventBus);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reconnect start / end of a connection after
|
|
|
|
* dropping an element on a flow.
|
|
|
|
*/
|
|
|
|
|
2019-08-29 09:55:34 +00:00
|
|
|
function insertShape(shape, targetFlow, positionOrBounds) {
|
2017-01-25 16:27:30 +00:00
|
|
|
var waypoints = targetFlow.waypoints,
|
2018-07-24 14:30:42 +00:00
|
|
|
waypointsBefore,
|
|
|
|
waypointsAfter,
|
|
|
|
dockingPoint,
|
|
|
|
source,
|
|
|
|
target,
|
|
|
|
incomingConnection,
|
|
|
|
outgoingConnection,
|
|
|
|
oldOutgoing = shape.outgoing.slice(),
|
|
|
|
oldIncoming = shape.incoming.slice();
|
2017-01-25 16:27:30 +00:00
|
|
|
|
2019-08-29 09:55:34 +00:00
|
|
|
var mid;
|
|
|
|
|
|
|
|
if (isNumber(positionOrBounds.width)) {
|
|
|
|
mid = getMid(positionOrBounds);
|
|
|
|
} else {
|
|
|
|
mid = positionOrBounds;
|
|
|
|
}
|
|
|
|
|
|
|
|
var intersection = getApproxIntersection(waypoints, mid);
|
2017-01-25 16:27:30 +00:00
|
|
|
|
|
|
|
if (intersection) {
|
|
|
|
waypointsBefore = waypoints.slice(0, intersection.index);
|
|
|
|
waypointsAfter = waypoints.slice(intersection.index + (intersection.bendpoint ? 1 : 0));
|
|
|
|
|
2018-02-14 12:36:30 +00:00
|
|
|
// due to inaccuracy intersection might have been found
|
|
|
|
if (!waypointsBefore.length || !waypointsAfter.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-29 09:55:34 +00:00
|
|
|
dockingPoint = intersection.bendpoint ? waypoints[intersection.index] : mid;
|
2017-01-25 16:27:30 +00:00
|
|
|
|
2017-07-24 13:07:44 +00:00
|
|
|
// if last waypointBefore is inside shape's bounds, ignore docking point
|
|
|
|
if (!isPointInsideBBox(shape, waypointsBefore[waypointsBefore.length-1])) {
|
|
|
|
waypointsBefore.push(copy(dockingPoint));
|
|
|
|
}
|
|
|
|
|
|
|
|
// if first waypointAfter is inside shape's bounds, ignore docking point
|
|
|
|
if (!isPointInsideBBox(shape, waypointsAfter[0])) {
|
|
|
|
waypointsAfter.unshift(copy(dockingPoint));
|
|
|
|
}
|
2017-01-25 16:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
source = targetFlow.source;
|
|
|
|
target = targetFlow.target;
|
|
|
|
|
|
|
|
if (bpmnRules.canConnect(source, shape, targetFlow)) {
|
2019-08-19 08:39:20 +00:00
|
|
|
|
2017-01-25 16:27:30 +00:00
|
|
|
// reconnect source -> inserted shape
|
2019-08-29 09:55:34 +00:00
|
|
|
modeling.reconnectEnd(targetFlow, shape, waypointsBefore || mid);
|
2017-01-25 16:27:30 +00:00
|
|
|
|
2018-07-24 14:30:42 +00:00
|
|
|
incomingConnection = targetFlow;
|
2017-01-25 16:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bpmnRules.canConnect(shape, target, targetFlow)) {
|
|
|
|
|
2018-07-24 14:30:42 +00:00
|
|
|
if (!incomingConnection) {
|
2019-08-19 08:39:20 +00:00
|
|
|
|
2017-01-25 16:27:30 +00:00
|
|
|
// reconnect inserted shape -> end
|
2019-08-29 09:55:34 +00:00
|
|
|
modeling.reconnectStart(targetFlow, shape, waypointsAfter || mid);
|
2018-07-24 14:30:42 +00:00
|
|
|
|
|
|
|
outgoingConnection = targetFlow;
|
2017-01-25 16:27:30 +00:00
|
|
|
} else {
|
2018-07-24 14:30:42 +00:00
|
|
|
outgoingConnection = modeling.connect(
|
|
|
|
shape, target, { type: targetFlow.type, waypoints: waypointsAfter }
|
|
|
|
);
|
2017-01-25 16:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-24 14:30:42 +00:00
|
|
|
|
|
|
|
var duplicateConnections = [].concat(
|
|
|
|
|
|
|
|
incomingConnection && filter(oldIncoming, function(connection) {
|
|
|
|
return connection.source === incomingConnection.source;
|
|
|
|
}) || [],
|
|
|
|
|
|
|
|
outgoingConnection && filter(oldOutgoing, function(connection) {
|
|
|
|
return connection.source === outgoingConnection.source;
|
|
|
|
}) || []
|
|
|
|
);
|
|
|
|
|
|
|
|
if (duplicateConnections.length) {
|
|
|
|
modeling.removeElements(duplicateConnections);
|
|
|
|
}
|
2017-01-25 16:27:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 10:25:33 +00:00
|
|
|
this.preExecute('elements.move', function(context) {
|
2017-01-25 16:27:30 +00:00
|
|
|
|
2017-07-27 11:40:21 +00:00
|
|
|
var newParent = context.newParent,
|
2017-03-01 10:25:33 +00:00
|
|
|
shapes = context.shapes,
|
2017-07-27 11:40:21 +00:00
|
|
|
delta = context.delta,
|
|
|
|
shape = shapes[0];
|
2017-03-01 10:25:33 +00:00
|
|
|
|
2017-07-27 11:40:21 +00:00
|
|
|
if (!shape || !newParent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the new parent is a connection,
|
|
|
|
// change it to the new parent's parent
|
|
|
|
if (newParent && newParent.waypoints) {
|
|
|
|
context.newParent = newParent = newParent.parent;
|
|
|
|
}
|
2017-03-01 10:25:33 +00:00
|
|
|
|
2017-07-27 11:40:21 +00:00
|
|
|
var shapeMid = getMid(shape);
|
|
|
|
var newShapeMid = {
|
|
|
|
x: shapeMid.x + delta.x,
|
|
|
|
y: shapeMid.y + delta.y
|
|
|
|
};
|
2017-03-01 10:25:33 +00:00
|
|
|
|
2017-07-27 11:40:21 +00:00
|
|
|
// find a connection which intersects with the
|
|
|
|
// element's mid point
|
|
|
|
var connection = find(newParent.children, function(element) {
|
|
|
|
var canInsert = bpmnRules.canInsert(shapes, element);
|
|
|
|
|
|
|
|
return canInsert && getApproxIntersection(element.waypoints, newShapeMid);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (connection) {
|
|
|
|
context.targetFlow = connection;
|
|
|
|
context.position = newShapeMid;
|
2017-01-25 16:27:30 +00:00
|
|
|
}
|
2017-07-27 11:40:21 +00:00
|
|
|
|
2017-03-01 10:25:33 +00:00
|
|
|
}, true);
|
2017-01-25 16:27:30 +00:00
|
|
|
|
2017-03-01 10:25:33 +00:00
|
|
|
this.postExecuted('elements.move', function(context) {
|
2017-01-25 16:27:30 +00:00
|
|
|
|
2017-03-01 10:25:33 +00:00
|
|
|
var shapes = context.shapes,
|
2017-01-25 16:27:30 +00:00
|
|
|
targetFlow = context.targetFlow,
|
2017-03-01 10:25:33 +00:00
|
|
|
position = context.position;
|
2017-01-25 16:27:30 +00:00
|
|
|
|
|
|
|
if (targetFlow) {
|
2017-03-01 10:25:33 +00:00
|
|
|
insertShape(shapes[0], targetFlow, position);
|
2017-01-25 16:27:30 +00:00
|
|
|
}
|
2017-03-01 10:25:33 +00:00
|
|
|
|
2017-01-25 16:27:30 +00:00
|
|
|
}, true);
|
|
|
|
|
|
|
|
this.preExecute('shape.create', function(context) {
|
|
|
|
|
|
|
|
var parent = context.parent,
|
|
|
|
shape = context.shape;
|
|
|
|
|
|
|
|
if (bpmnRules.canInsert(shape, parent)) {
|
|
|
|
context.targetFlow = parent;
|
|
|
|
context.parent = parent.parent;
|
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
|
2017-12-09 23:32:11 +00:00
|
|
|
this.postExecuted('shape.create', function(context) {
|
2017-01-25 16:27:30 +00:00
|
|
|
|
|
|
|
var shape = context.shape,
|
|
|
|
targetFlow = context.targetFlow,
|
2019-08-29 09:55:34 +00:00
|
|
|
positionOrBounds = context.position;
|
2017-01-25 16:27:30 +00:00
|
|
|
|
|
|
|
if (targetFlow) {
|
2019-08-29 09:55:34 +00:00
|
|
|
insertShape(shape, targetFlow, positionOrBounds);
|
2017-01-25 16:27:30 +00:00
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
inherits(DropOnFlowBehavior, CommandInterceptor);
|
|
|
|
|
|
|
|
DropOnFlowBehavior.$inject = [
|
|
|
|
'eventBus',
|
|
|
|
'bpmnRules',
|
|
|
|
'modeling'
|
|
|
|
];
|
|
|
|
|
2017-01-25 16:27:30 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
// helpers /////////////////////
|
|
|
|
|
|
|
|
function isPointInsideBBox(bbox, point) {
|
|
|
|
var x = point.x,
|
|
|
|
y = point.y;
|
|
|
|
|
|
|
|
return x >= bbox.x &&
|
|
|
|
x <= bbox.x + bbox.width &&
|
|
|
|
y >= bbox.y &&
|
|
|
|
y <= bbox.y + bbox.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
function copy(obj) {
|
|
|
|
return assign({}, obj);
|
|
|
|
}
|
|
|
|
|