2017-01-25 16:27:30 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var inherits = require('inherits');
|
|
|
|
|
|
|
|
var assign = require('lodash/object/assign');
|
|
|
|
|
|
|
|
var CommandInterceptor = require('diagram-js/lib/command/CommandInterceptor');
|
|
|
|
|
|
|
|
var getApproxIntersection = require('diagram-js/lib/util/LineIntersection').getApproxIntersection;
|
|
|
|
|
2017-07-24 13:07:44 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-25 16:27:30 +00:00
|
|
|
function copy(obj) {
|
|
|
|
return assign({}, obj);
|
|
|
|
}
|
|
|
|
|
2017-03-01 10:25:33 +00:00
|
|
|
function getMid(bounds) {
|
|
|
|
|
|
|
|
return {
|
|
|
|
x: Math.round(bounds.x + bounds.width / 2),
|
|
|
|
y: Math.round(bounds.y + bounds.height / 2)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-01-25 16:27:30 +00:00
|
|
|
function DropOnFlow(eventBus, bpmnRules, modeling) {
|
|
|
|
|
|
|
|
CommandInterceptor.call(this, eventBus);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reconnect start / end of a connection after
|
|
|
|
* dropping an element on a flow.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function insertShape(shape, targetFlow, position) {
|
|
|
|
var waypoints = targetFlow.waypoints,
|
|
|
|
waypointsBefore, waypointsAfter, dockingPoint, source, target, reconnected;
|
|
|
|
|
|
|
|
var intersection = getApproxIntersection(waypoints, position);
|
|
|
|
|
|
|
|
if (intersection) {
|
|
|
|
waypointsBefore = waypoints.slice(0, intersection.index);
|
|
|
|
waypointsAfter = waypoints.slice(intersection.index + (intersection.bendpoint ? 1 : 0));
|
|
|
|
|
|
|
|
dockingPoint = intersection.bendpoint ? waypoints[intersection.index] : position;
|
|
|
|
|
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)) {
|
|
|
|
// reconnect source -> inserted shape
|
|
|
|
modeling.reconnectEnd(targetFlow, shape, waypointsBefore || position);
|
|
|
|
|
|
|
|
reconnected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bpmnRules.canConnect(shape, target, targetFlow)) {
|
|
|
|
|
|
|
|
if (!reconnected) {
|
|
|
|
// reconnect inserted shape -> end
|
|
|
|
modeling.reconnectStart(targetFlow, shape, waypointsAfter || position);
|
|
|
|
} else {
|
|
|
|
modeling.connect(shape, target, { type: targetFlow.type, waypoints: waypointsAfter });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 10:25:33 +00:00
|
|
|
this.preExecute('elements.move', function(context) {
|
2017-01-25 16:27:30 +00:00
|
|
|
|
2017-03-01 10:25:33 +00:00
|
|
|
var parent = context.newParent,
|
|
|
|
shapes = context.shapes,
|
|
|
|
shape,
|
|
|
|
shapeMid,
|
|
|
|
delta = context.delta;
|
|
|
|
|
|
|
|
if (bpmnRules.canInsert(shapes, parent)) {
|
|
|
|
|
|
|
|
shape = shapes[0];
|
|
|
|
shapeMid = getMid(shape);
|
|
|
|
|
|
|
|
context.targetFlow = parent;
|
|
|
|
context.newParent = parent.parent;
|
|
|
|
context.position = {
|
|
|
|
x: shapeMid.x + delta.x,
|
|
|
|
y: shapeMid.y + delta.y
|
|
|
|
};
|
2017-01-25 16:27:30 +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);
|
|
|
|
|
|
|
|
|
|
|
|
this.postExecute('shape.create', function(context) {
|
|
|
|
|
|
|
|
var shape = context.shape,
|
|
|
|
targetFlow = context.targetFlow,
|
|
|
|
position = context.position;
|
|
|
|
|
|
|
|
if (targetFlow) {
|
|
|
|
insertShape(shape, targetFlow, position);
|
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
inherits(DropOnFlow, CommandInterceptor);
|
|
|
|
|
|
|
|
DropOnFlow.$inject = [ 'eventBus', 'bpmnRules', 'modeling' ];
|
|
|
|
|
|
|
|
module.exports = DropOnFlow;
|