2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
assign
|
|
|
|
} from 'min-dash';
|
2014-07-18 12:39:15 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import { is } from './ModelUtil';
|
2014-07-18 12:39:15 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
|
|
|
|
export var DEFAULT_LABEL_SIZE = {
|
2014-08-03 12:30:53 +00:00
|
|
|
width: 90,
|
2014-09-15 12:41:26 +00:00
|
|
|
height: 20
|
2014-08-03 12:30:53 +00:00
|
|
|
};
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
export var FLOW_LABEL_INDENT = 15;
|
2016-04-20 13:40:40 +00:00
|
|
|
|
2014-08-03 12:30:53 +00:00
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the given semantic has an external label
|
|
|
|
*
|
|
|
|
* @param {BpmnElement} semantic
|
|
|
|
* @return {Boolean} true if has label
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export function hasExternalLabel(semantic) {
|
2015-09-08 12:38:50 +00:00
|
|
|
return is(semantic, 'bpmn:Event') ||
|
|
|
|
is(semantic, 'bpmn:Gateway') ||
|
|
|
|
is(semantic, 'bpmn:DataStoreReference') ||
|
|
|
|
is(semantic, 'bpmn:DataObjectReference') ||
|
|
|
|
is(semantic, 'bpmn:SequenceFlow') ||
|
|
|
|
is(semantic, 'bpmn:MessageFlow');
|
2018-04-02 19:01:53 +00:00
|
|
|
}
|
2014-07-18 12:39:15 +00:00
|
|
|
|
2016-04-19 14:50:42 +00:00
|
|
|
/**
|
|
|
|
* Get the position for sequence flow labels
|
|
|
|
*
|
|
|
|
* @param {Array<Point>} waypoints
|
|
|
|
* @return {Point} the label position
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export function getFlowLabelPosition(waypoints) {
|
2016-04-19 14:50:42 +00:00
|
|
|
|
|
|
|
// get the waypoints mid
|
|
|
|
var mid = waypoints.length / 2 - 1;
|
|
|
|
|
|
|
|
var first = waypoints[Math.floor(mid)];
|
|
|
|
var second = waypoints[Math.ceil(mid + 0.01)];
|
|
|
|
|
|
|
|
// get position
|
|
|
|
var position = getWaypointsMid(waypoints);
|
|
|
|
|
|
|
|
// calculate angle
|
2018-02-27 09:08:31 +00:00
|
|
|
var angle = Math.atan((second.y - first.y) / (second.x - first.x));
|
2016-04-19 14:50:42 +00:00
|
|
|
|
|
|
|
var x = position.x,
|
|
|
|
y = position.y;
|
|
|
|
|
2018-02-27 09:08:31 +00:00
|
|
|
if (Math.abs(angle) < Math.PI / 2) {
|
2016-04-20 13:40:40 +00:00
|
|
|
y -= FLOW_LABEL_INDENT;
|
2016-04-19 14:50:42 +00:00
|
|
|
} else {
|
2016-04-20 13:40:40 +00:00
|
|
|
x += FLOW_LABEL_INDENT;
|
2016-04-19 14:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return { x: x, y: y };
|
|
|
|
}
|
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the middle of a number of waypoints
|
|
|
|
*
|
|
|
|
* @param {Array<Point>} waypoints
|
|
|
|
* @return {Point} the mid point
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export function getWaypointsMid(waypoints) {
|
2014-07-18 12:39:15 +00:00
|
|
|
|
|
|
|
var mid = waypoints.length / 2 - 1;
|
|
|
|
|
|
|
|
var first = waypoints[Math.floor(mid)];
|
|
|
|
var second = waypoints[Math.ceil(mid + 0.01)];
|
|
|
|
|
|
|
|
return {
|
|
|
|
x: first.x + (second.x - first.x) / 2,
|
|
|
|
y: first.y + (second.y - first.y) / 2
|
|
|
|
};
|
2015-09-08 12:38:50 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
export function getExternalLabelMid(element) {
|
2014-07-23 16:53:33 +00:00
|
|
|
|
|
|
|
if (element.waypoints) {
|
2016-04-19 14:50:42 +00:00
|
|
|
return getFlowLabelPosition(element.waypoints);
|
2014-07-23 16:53:33 +00:00
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
x: element.x + element.width / 2,
|
2014-09-15 12:41:26 +00:00
|
|
|
y: element.y + element.height + DEFAULT_LABEL_SIZE.height / 2
|
2014-07-23 16:53:33 +00:00
|
|
|
};
|
|
|
|
}
|
2015-09-08 12:38:50 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
|
2014-07-18 12:39:15 +00:00
|
|
|
/**
|
|
|
|
* Returns the bounds of an elements label, parsed from the elements DI or
|
|
|
|
* generated from its bounds.
|
|
|
|
*
|
|
|
|
* @param {BpmnElement} semantic
|
|
|
|
* @param {djs.model.Base} element
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export function getExternalLabelBounds(semantic, element) {
|
2014-07-18 12:39:15 +00:00
|
|
|
|
|
|
|
var mid,
|
|
|
|
size,
|
|
|
|
bounds,
|
|
|
|
di = semantic.di,
|
|
|
|
label = di.label;
|
|
|
|
|
|
|
|
if (label && label.bounds) {
|
|
|
|
bounds = label.bounds;
|
|
|
|
|
|
|
|
size = {
|
2014-09-15 12:41:26 +00:00
|
|
|
width: Math.max(DEFAULT_LABEL_SIZE.width, bounds.width),
|
2014-07-18 12:39:15 +00:00
|
|
|
height: bounds.height
|
|
|
|
};
|
|
|
|
|
|
|
|
mid = {
|
|
|
|
x: bounds.x + bounds.width / 2,
|
2014-10-30 11:07:28 +00:00
|
|
|
y: bounds.y + bounds.height / 2
|
2014-07-18 12:39:15 +00:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
mid = getExternalLabelMid(element);
|
2014-07-18 12:39:15 +00:00
|
|
|
|
2014-08-03 12:30:53 +00:00
|
|
|
size = DEFAULT_LABEL_SIZE;
|
2014-07-18 12:39:15 +00:00
|
|
|
}
|
|
|
|
|
2015-02-02 13:46:21 +00:00
|
|
|
return assign({
|
2014-07-18 12:39:15 +00:00
|
|
|
x: mid.x - size.width / 2,
|
2014-09-08 17:03:39 +00:00
|
|
|
y: mid.y - size.height / 2
|
2014-07-18 12:39:15 +00:00
|
|
|
}, size);
|
2018-04-02 19:01:53 +00:00
|
|
|
}
|