2018-04-02 19:01:53 +00:00
|
|
|
import { is } from '../../util/ModelUtil';
|
2017-12-08 20:06:08 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
getMid,
|
|
|
|
asTRBL,
|
|
|
|
getOrientation
|
|
|
|
} from 'diagram-js/lib/layout/LayoutUtil';
|
2017-12-08 20:06:08 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
find,
|
|
|
|
reduce
|
|
|
|
} from 'min-dash';
|
2017-12-08 20:06:08 +00:00
|
|
|
|
|
|
|
var DEFAULT_HORIZONTAL_DISTANCE = 50;
|
|
|
|
|
|
|
|
var MAX_HORIZONTAL_DISTANCE = 250;
|
|
|
|
|
|
|
|
// padding to detect element placement
|
|
|
|
var PLACEMENT_DETECTION_PAD = 10;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Always try to place element right of source;
|
|
|
|
* compute actual distance from previous nodes in flow.
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export function getFlowNodePosition(source, element) {
|
2017-12-08 20:06:08 +00:00
|
|
|
|
|
|
|
var sourceTrbl = asTRBL(source);
|
|
|
|
var sourceMid = getMid(source);
|
|
|
|
|
|
|
|
var horizontalDistance = getFlowNodeDistance(source, element);
|
|
|
|
|
2017-12-15 13:34:05 +00:00
|
|
|
var orientation = 'left',
|
|
|
|
rowSize = 80,
|
|
|
|
margin = 30;
|
|
|
|
|
|
|
|
if (is(source, 'bpmn:BoundaryEvent')) {
|
|
|
|
orientation = getOrientation(source, source.host, -25);
|
|
|
|
|
2018-05-03 15:29:23 +00:00
|
|
|
if (orientation.indexOf('top') !== -1) {
|
2017-12-15 13:34:05 +00:00
|
|
|
margin *= -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 15:29:23 +00:00
|
|
|
function getVerticalDistance(orient) {
|
|
|
|
if (orient.indexOf('top') != -1) {
|
|
|
|
return -1 * rowSize;
|
|
|
|
} else if (orient.indexOf('bottom') != -1) {
|
|
|
|
return rowSize;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 13:34:05 +00:00
|
|
|
|
2017-12-08 20:06:08 +00:00
|
|
|
var position = {
|
|
|
|
x: sourceTrbl.right + horizontalDistance + element.width / 2,
|
2018-05-03 15:29:23 +00:00
|
|
|
y: sourceMid.y + getVerticalDistance(orientation)
|
2017-12-08 20:06:08 +00:00
|
|
|
};
|
|
|
|
|
2017-12-15 08:35:18 +00:00
|
|
|
var escapeDirection = {
|
|
|
|
y: {
|
2017-12-15 13:34:05 +00:00
|
|
|
margin: margin,
|
|
|
|
rowSize: rowSize
|
2017-12-15 08:35:18 +00:00
|
|
|
}
|
|
|
|
};
|
2017-12-08 20:06:08 +00:00
|
|
|
|
2017-12-15 08:35:18 +00:00
|
|
|
return deconflictPosition(source, element, position, escapeDirection);
|
2017-12-08 20:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute best distance between source and target,
|
|
|
|
* based on existing connections to and from source.
|
|
|
|
*
|
|
|
|
* @param {djs.model.Shape} source
|
|
|
|
* @param {djs.model.Shape} element
|
|
|
|
*
|
|
|
|
* @return {Number} distance
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export function getFlowNodeDistance(source, element) {
|
2017-12-08 20:06:08 +00:00
|
|
|
|
|
|
|
var sourceTrbl = asTRBL(source);
|
|
|
|
|
|
|
|
// is connection a reference to consider?
|
2017-12-15 13:34:05 +00:00
|
|
|
function isReference(c) {
|
2017-12-08 20:06:08 +00:00
|
|
|
return is(c, 'bpmn:SequenceFlow');
|
2017-12-15 13:34:05 +00:00
|
|
|
}
|
2017-12-08 20:06:08 +00:00
|
|
|
|
2017-12-15 13:34:05 +00:00
|
|
|
function toTargetNode(weight) {
|
|
|
|
|
|
|
|
return function(shape) {
|
2017-12-08 20:06:08 +00:00
|
|
|
return {
|
2017-12-15 13:34:05 +00:00
|
|
|
shape: shape,
|
|
|
|
weight: weight,
|
2017-12-08 20:06:08 +00:00
|
|
|
distanceTo: function(shape) {
|
|
|
|
var shapeTrbl = asTRBL(shape);
|
|
|
|
|
|
|
|
return shapeTrbl.left - sourceTrbl.right;
|
|
|
|
}
|
|
|
|
};
|
2017-12-15 13:34:05 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function toSourceNode(weight) {
|
|
|
|
return function(shape) {
|
2017-12-08 20:06:08 +00:00
|
|
|
return {
|
2017-12-15 13:34:05 +00:00
|
|
|
shape: shape,
|
|
|
|
weight: weight,
|
2017-12-08 20:06:08 +00:00
|
|
|
distanceTo: function(shape) {
|
|
|
|
var shapeTrbl = asTRBL(shape);
|
|
|
|
|
|
|
|
return sourceTrbl.left - shapeTrbl.right;
|
|
|
|
}
|
|
|
|
};
|
2017-12-15 13:34:05 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// we create a list of nodes to take into consideration
|
|
|
|
// for calculating the optimal flow node distance
|
|
|
|
//
|
|
|
|
// * weight existing target nodes higher than source nodes
|
|
|
|
// * only take into account individual nodes once
|
|
|
|
//
|
|
|
|
var nodes = reduce([].concat(
|
|
|
|
getTargets(source, isReference).map(toTargetNode(5)),
|
|
|
|
getSources(source, isReference).map(toSourceNode(1))
|
2017-12-15 09:16:09 +00:00
|
|
|
), function(nodes, node) {
|
|
|
|
// filter out shapes connected twice via source or target
|
|
|
|
nodes[node.shape.id + '__weight_' + node.weight] = node;
|
|
|
|
|
|
|
|
return nodes;
|
|
|
|
}, {});
|
2017-12-08 20:06:08 +00:00
|
|
|
|
|
|
|
// compute distances between source and incoming nodes;
|
|
|
|
// group at the same time by distance and expose the
|
|
|
|
// favourite distance as { fav: { count, value } }.
|
|
|
|
var distancesGrouped = reduce(nodes, function(result, node) {
|
|
|
|
|
|
|
|
var shape = node.shape,
|
|
|
|
weight = node.weight,
|
|
|
|
distanceTo = node.distanceTo;
|
|
|
|
|
|
|
|
var fav = result.fav,
|
|
|
|
currentDistance,
|
|
|
|
currentDistanceCount,
|
|
|
|
currentDistanceEntry;
|
|
|
|
|
|
|
|
currentDistance = distanceTo(shape);
|
|
|
|
|
|
|
|
// ignore too far away peers
|
|
|
|
// or non-left to right modeled nodes
|
|
|
|
if (currentDistance < 0 || currentDistance > MAX_HORIZONTAL_DISTANCE) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentDistanceEntry = result[String(currentDistance)] =
|
|
|
|
result[String(currentDistance)] || {
|
|
|
|
value: currentDistance,
|
|
|
|
count: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
// inc diff count
|
|
|
|
currentDistanceCount = currentDistanceEntry.count += 1 * weight;
|
|
|
|
|
|
|
|
if (!fav || fav.count < currentDistanceCount) {
|
|
|
|
result.fav = currentDistanceEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}, { });
|
|
|
|
|
|
|
|
|
|
|
|
if (distancesGrouped.fav) {
|
|
|
|
return distancesGrouped.fav.value;
|
|
|
|
} else {
|
|
|
|
return DEFAULT_HORIZONTAL_DISTANCE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Always try to place text annotations top right of source.
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export function getTextAnnotationPosition(source, element) {
|
2017-12-08 20:06:08 +00:00
|
|
|
|
|
|
|
var sourceTrbl = asTRBL(source);
|
|
|
|
|
|
|
|
var position = {
|
2017-12-15 09:34:11 +00:00
|
|
|
x: sourceTrbl.right + element.width / 2,
|
2017-12-08 20:06:08 +00:00
|
|
|
y: sourceTrbl.top - 50 - element.height / 2
|
|
|
|
};
|
|
|
|
|
2017-12-15 08:35:18 +00:00
|
|
|
var escapeDirection = {
|
|
|
|
y: {
|
|
|
|
margin: -30,
|
2017-12-15 09:34:11 +00:00
|
|
|
rowSize: 20
|
2017-12-15 08:35:18 +00:00
|
|
|
}
|
|
|
|
};
|
2017-12-08 20:06:08 +00:00
|
|
|
|
2017-12-15 08:35:18 +00:00
|
|
|
return deconflictPosition(source, element, position, escapeDirection);
|
2017-12-08 20:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Always put element bottom right of source.
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export function getDataElementPosition(source, element) {
|
2017-12-08 20:06:08 +00:00
|
|
|
|
|
|
|
var sourceTrbl = asTRBL(source);
|
|
|
|
|
|
|
|
var position = {
|
2017-12-15 09:34:11 +00:00
|
|
|
x: sourceTrbl.right - 10 + element.width / 2,
|
2017-12-08 20:06:08 +00:00
|
|
|
y: sourceTrbl.bottom + 40 + element.width / 2
|
|
|
|
};
|
|
|
|
|
2017-12-15 08:35:18 +00:00
|
|
|
var escapeDirection = {
|
|
|
|
x: {
|
|
|
|
margin: 30,
|
2017-12-15 09:34:11 +00:00
|
|
|
rowSize: 30
|
2017-12-15 08:35:18 +00:00
|
|
|
}
|
|
|
|
};
|
2017-12-08 20:06:08 +00:00
|
|
|
|
2017-12-15 08:35:18 +00:00
|
|
|
return deconflictPosition(source, element, position, escapeDirection);
|
2017-12-08 20:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Always put element right of source per default.
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export function getDefaultPosition(source, element) {
|
2017-12-08 20:06:08 +00:00
|
|
|
|
|
|
|
var sourceTrbl = asTRBL(source);
|
|
|
|
|
|
|
|
var sourceMid = getMid(source);
|
|
|
|
|
|
|
|
// simply put element right next to source
|
|
|
|
return {
|
|
|
|
x: sourceTrbl.right + DEFAULT_HORIZONTAL_DISTANCE + element.width / 2,
|
|
|
|
y: sourceMid.y
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-15 13:34:05 +00:00
|
|
|
/**
|
|
|
|
* Returns all connected elements around the given source.
|
|
|
|
*
|
|
|
|
* This includes:
|
|
|
|
*
|
|
|
|
* - connected elements
|
|
|
|
* - host connected elements
|
|
|
|
* - attachers connected elements
|
|
|
|
*
|
|
|
|
* @param {djs.model.Shape} source
|
|
|
|
* @param {djs.model.Shape} element
|
|
|
|
*
|
|
|
|
* @return {Array<djs.model.Shape>}
|
|
|
|
*/
|
|
|
|
function getAutoPlaceClosure(source, element) {
|
|
|
|
|
|
|
|
var allConnected = getConnected(source);
|
|
|
|
|
|
|
|
if (source.host) {
|
|
|
|
allConnected = allConnected.concat(getConnected(source.host));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source.attachers) {
|
|
|
|
allConnected = allConnected.concat(source.attachers.reduce(function(shapes, attacher) {
|
|
|
|
return shapes.concat(getConnected(attacher));
|
|
|
|
}, []));
|
|
|
|
}
|
|
|
|
|
|
|
|
return allConnected;
|
|
|
|
}
|
|
|
|
|
2017-12-08 20:06:08 +00:00
|
|
|
/**
|
|
|
|
* Return target at given position, if defined.
|
2017-12-15 13:34:05 +00:00
|
|
|
*
|
|
|
|
* This takes connected elements from host and attachers
|
|
|
|
* into account, too.
|
2017-12-08 20:06:08 +00:00
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export function getConnectedAtPosition(source, position, element) {
|
2017-12-08 20:06:08 +00:00
|
|
|
|
|
|
|
var bounds = {
|
|
|
|
x: position.x - (element.width / 2),
|
|
|
|
y: position.y - (element.height / 2),
|
|
|
|
width: element.width,
|
|
|
|
height: element.height
|
|
|
|
};
|
|
|
|
|
2017-12-15 13:34:05 +00:00
|
|
|
var closure = getAutoPlaceClosure(source, element);
|
2017-12-08 20:06:08 +00:00
|
|
|
|
2017-12-15 13:34:05 +00:00
|
|
|
return find(closure, function(target) {
|
2017-12-08 20:06:08 +00:00
|
|
|
|
2017-12-15 13:34:05 +00:00
|
|
|
if (target === element) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-12-08 20:06:08 +00:00
|
|
|
|
|
|
|
var orientation = getOrientation(target, bounds, PLACEMENT_DETECTION_PAD);
|
|
|
|
|
|
|
|
return orientation === 'intersect';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-15 08:35:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new, position for the given element
|
|
|
|
* based on the given element that is not occupied
|
2018-03-22 09:22:37 +00:00
|
|
|
* by some element connected to source.
|
2017-12-15 08:35:18 +00:00
|
|
|
*
|
|
|
|
* Take into account the escapeDirection (where to move
|
|
|
|
* on positining clashes) in the computation.
|
|
|
|
*
|
|
|
|
* @param {djs.model.Shape} source
|
|
|
|
* @param {djs.model.Shape} element
|
|
|
|
* @param {Point} position
|
|
|
|
* @param {Object} escapeDelta
|
|
|
|
*
|
|
|
|
* @return {Point}
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export function deconflictPosition(source, element, position, escapeDelta) {
|
2017-12-15 08:35:18 +00:00
|
|
|
|
|
|
|
function nextPosition(existingElement) {
|
|
|
|
|
|
|
|
var newPosition = {
|
|
|
|
x: position.x,
|
|
|
|
y: position.y
|
|
|
|
};
|
|
|
|
|
|
|
|
[ 'x', 'y' ].forEach(function(axis) {
|
|
|
|
|
|
|
|
var axisDelta = escapeDelta[axis];
|
|
|
|
|
|
|
|
if (!axisDelta) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var dimension = axis === 'x' ? 'width' : 'height';
|
|
|
|
|
|
|
|
var margin = axisDelta.margin,
|
|
|
|
rowSize = axisDelta.rowSize;
|
|
|
|
|
|
|
|
if (margin < 0) {
|
|
|
|
newPosition[axis] = Math.min(
|
|
|
|
existingElement[axis] + margin - element[dimension] / 2,
|
|
|
|
position[axis] - rowSize + margin
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
newPosition[axis] = Math.max(
|
|
|
|
existingTarget[axis] + existingTarget[dimension] + margin + element[dimension] / 2,
|
|
|
|
position[axis] + rowSize + margin
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return newPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
var existingTarget;
|
|
|
|
|
|
|
|
// deconflict position until free slot is found
|
|
|
|
while ((existingTarget = getConnectedAtPosition(source, position, element))) {
|
|
|
|
position = nextPosition(existingTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
2017-12-15 13:34:05 +00:00
|
|
|
|
|
|
|
|
2018-02-27 08:57:22 +00:00
|
|
|
// helpers //////////////////////
|
2017-12-15 13:34:05 +00:00
|
|
|
|
|
|
|
function noneFilter() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getConnected(element, connectionFilter) {
|
|
|
|
return [].concat(
|
|
|
|
getTargets(element, connectionFilter),
|
|
|
|
getSources(element, connectionFilter)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSources(shape, connectionFilter) {
|
|
|
|
|
|
|
|
if (!connectionFilter) {
|
|
|
|
connectionFilter = noneFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return shape.incoming.filter(connectionFilter).map(function(c) {
|
|
|
|
return c.source;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTargets(shape, connectionFilter) {
|
|
|
|
|
|
|
|
if (!connectionFilter) {
|
|
|
|
connectionFilter = noneFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return shape.outgoing.filter(connectionFilter).map(function(c) {
|
|
|
|
return c.target;
|
|
|
|
});
|
|
|
|
}
|