2020-03-02 13:26:09 +00:00
|
|
|
import { is } from '../../util/ModelUtil';
|
|
|
|
import { isAny } from '../modeling/util/ModelingUtil';
|
|
|
|
|
|
|
|
import {
|
|
|
|
getMid,
|
|
|
|
asTRBL,
|
|
|
|
getOrientation
|
|
|
|
} from 'diagram-js/lib/layout/LayoutUtil';
|
|
|
|
|
|
|
|
import {
|
2020-03-09 09:41:35 +00:00
|
|
|
findFreePosition,
|
|
|
|
generateGetNextPosition,
|
2020-03-02 13:26:09 +00:00
|
|
|
getConnectedDistance
|
|
|
|
} from 'diagram-js/lib/features/auto-place/AutoPlaceUtil';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the new position for the target element to
|
|
|
|
* connect to source.
|
|
|
|
*
|
|
|
|
* @param {djs.model.Shape} source
|
|
|
|
* @param {djs.model.Shape} element
|
|
|
|
*
|
|
|
|
* @return {Point}
|
|
|
|
*/
|
|
|
|
export function getNewShapePosition(source, element) {
|
|
|
|
|
|
|
|
if (is(element, 'bpmn:TextAnnotation')) {
|
|
|
|
return getTextAnnotationPosition(source, element);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isAny(element, [ 'bpmn:DataObjectReference', 'bpmn:DataStoreReference' ])) {
|
|
|
|
return getDataElementPosition(source, element);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is(element, 'bpmn:FlowNode')) {
|
|
|
|
return getFlowNodePosition(source, element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Always try to place element right of source;
|
|
|
|
* compute actual distance from previous nodes in flow.
|
|
|
|
*/
|
|
|
|
export function getFlowNodePosition(source, element) {
|
|
|
|
|
|
|
|
var sourceTrbl = asTRBL(source);
|
|
|
|
var sourceMid = getMid(source);
|
|
|
|
|
2020-03-09 09:41:35 +00:00
|
|
|
var horizontalDistance = getConnectedDistance(source, {
|
|
|
|
filter: function(connection) {
|
|
|
|
return is(connection, 'bpmn:SequenceFlow');
|
|
|
|
}
|
2020-03-02 13:26:09 +00:00
|
|
|
});
|
|
|
|
|
2020-03-09 09:41:35 +00:00
|
|
|
var margin = 30,
|
|
|
|
minDistance = 80,
|
|
|
|
orientation = 'left';
|
2020-03-02 13:26:09 +00:00
|
|
|
|
|
|
|
if (is(source, 'bpmn:BoundaryEvent')) {
|
|
|
|
orientation = getOrientation(source, source.host, -25);
|
|
|
|
|
|
|
|
if (orientation.indexOf('top') !== -1) {
|
|
|
|
margin *= -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var position = {
|
|
|
|
x: sourceTrbl.right + horizontalDistance + element.width / 2,
|
2020-03-09 09:41:35 +00:00
|
|
|
y: sourceMid.y + getVerticalDistance(orientation, minDistance)
|
2020-03-02 13:26:09 +00:00
|
|
|
};
|
|
|
|
|
2020-03-09 09:41:35 +00:00
|
|
|
var nextPositionDirection = {
|
2020-03-02 13:26:09 +00:00
|
|
|
y: {
|
|
|
|
margin: margin,
|
2020-03-09 09:41:35 +00:00
|
|
|
minDistance: minDistance
|
2020-03-02 13:26:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-09 09:41:35 +00:00
|
|
|
return findFreePosition(source, element, position, generateGetNextPosition(nextPositionDirection));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getVerticalDistance(orientation, minDistance) {
|
|
|
|
if (orientation.indexOf('top') != -1) {
|
|
|
|
return -1 * minDistance;
|
|
|
|
} else if (orientation.indexOf('bottom') != -1) {
|
|
|
|
return minDistance;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-03-02 13:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Always try to place text annotations top right of source.
|
|
|
|
*/
|
|
|
|
export function getTextAnnotationPosition(source, element) {
|
|
|
|
|
|
|
|
var sourceTrbl = asTRBL(source);
|
|
|
|
|
|
|
|
var position = {
|
|
|
|
x: sourceTrbl.right + element.width / 2,
|
|
|
|
y: sourceTrbl.top - 50 - element.height / 2
|
|
|
|
};
|
|
|
|
|
2020-03-09 09:41:35 +00:00
|
|
|
var nextPositionDirection = {
|
2020-03-02 13:26:09 +00:00
|
|
|
y: {
|
|
|
|
margin: -30,
|
2020-03-09 09:41:35 +00:00
|
|
|
minDistance: 20
|
2020-03-02 13:26:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-09 09:41:35 +00:00
|
|
|
return findFreePosition(source, element, position, generateGetNextPosition(nextPositionDirection));
|
2020-03-02 13:26:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Always put element bottom right of source.
|
|
|
|
*/
|
|
|
|
export function getDataElementPosition(source, element) {
|
|
|
|
|
|
|
|
var sourceTrbl = asTRBL(source);
|
|
|
|
|
|
|
|
var position = {
|
|
|
|
x: sourceTrbl.right - 10 + element.width / 2,
|
|
|
|
y: sourceTrbl.bottom + 40 + element.width / 2
|
|
|
|
};
|
|
|
|
|
2020-03-09 09:41:35 +00:00
|
|
|
var nextPositionDirection = {
|
2020-03-02 13:26:09 +00:00
|
|
|
x: {
|
|
|
|
margin: 30,
|
2020-03-09 09:41:35 +00:00
|
|
|
minDistance: 30
|
2020-03-02 13:26:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-09 09:41:35 +00:00
|
|
|
return findFreePosition(source, element, position, generateGetNextPosition(nextPositionDirection));
|
2020-03-02 13:26:09 +00:00
|
|
|
}
|