2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
assign
|
|
|
|
} from 'min-dash';
|
2015-07-13 08:37:43 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import inherits from 'inherits';
|
2015-07-13 08:37:43 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
is,
|
|
|
|
getBusinessObject
|
|
|
|
} from '../../../util/ModelUtil';
|
2015-07-13 08:37:43 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
2018-04-30 09:06:26 +00:00
|
|
|
isLabelExternal,
|
2018-04-02 19:01:53 +00:00
|
|
|
getExternalLabelMid,
|
2019-06-11 09:13:33 +00:00
|
|
|
hasExternalLabel
|
2018-04-02 19:01:53 +00:00
|
|
|
} from '../../../util/LabelUtil';
|
2015-07-13 08:37:43 +00:00
|
|
|
|
2019-07-01 10:48:49 +00:00
|
|
|
import {
|
|
|
|
getLabel
|
|
|
|
} from '../../label-editing/LabelUtil';
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
getLabelAdjustment
|
|
|
|
} from './util/LabelLayoutUtil';
|
|
|
|
|
|
|
|
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
|
|
|
|
|
2019-06-11 09:13:33 +00:00
|
|
|
import {
|
|
|
|
getNewAttachPoint
|
|
|
|
} from 'diagram-js/lib/util/AttachUtil';
|
|
|
|
|
|
|
|
import {
|
|
|
|
getMid,
|
|
|
|
roundPoint
|
|
|
|
} from 'diagram-js/lib/layout/LayoutUtil';
|
|
|
|
|
|
|
|
import {
|
|
|
|
delta
|
|
|
|
} from 'diagram-js/lib/util/PositionUtil';
|
|
|
|
|
|
|
|
import {
|
|
|
|
sortBy
|
|
|
|
} from 'min-dash';
|
|
|
|
|
|
|
|
import {
|
|
|
|
getDistancePointLine,
|
|
|
|
perpendicularFoot
|
|
|
|
} from './util/GeometricUtil';
|
|
|
|
|
2017-03-07 08:30:48 +00:00
|
|
|
var DEFAULT_LABEL_DIMENSIONS = {
|
|
|
|
width: 90,
|
|
|
|
height: 20
|
|
|
|
};
|
|
|
|
|
2018-06-06 12:31:31 +00:00
|
|
|
var NAME_PROPERTY = 'name';
|
2018-06-13 09:08:46 +00:00
|
|
|
var TEXT_PROPERTY = 'text';
|
2015-07-13 08:37:43 +00:00
|
|
|
|
2016-03-08 13:12:52 +00:00
|
|
|
/**
|
|
|
|
* A component that makes sure that external labels are added
|
|
|
|
* together with respective elements and properly updated (DI wise)
|
|
|
|
* during move.
|
|
|
|
*
|
|
|
|
* @param {EventBus} eventBus
|
|
|
|
* @param {Modeling} modeling
|
|
|
|
* @param {BpmnFactory} bpmnFactory
|
2018-05-24 11:42:40 +00:00
|
|
|
* @param {TextRenderer} textRenderer
|
2016-03-08 13:12:52 +00:00
|
|
|
*/
|
2018-05-24 11:42:40 +00:00
|
|
|
export default function LabelBehavior(
|
|
|
|
eventBus, modeling, bpmnFactory,
|
|
|
|
textRenderer) {
|
2015-07-13 08:37:43 +00:00
|
|
|
|
|
|
|
CommandInterceptor.call(this, eventBus);
|
|
|
|
|
2018-04-30 09:06:26 +00:00
|
|
|
// update label if name property was updated
|
|
|
|
this.postExecute('element.updateProperties', function(e) {
|
|
|
|
var context = e.context,
|
|
|
|
element = context.element,
|
|
|
|
properties = context.properties;
|
2016-07-05 13:54:14 +00:00
|
|
|
|
2018-06-06 12:31:31 +00:00
|
|
|
if (NAME_PROPERTY in properties) {
|
|
|
|
modeling.updateLabel(element, properties[NAME_PROPERTY]);
|
2018-04-30 09:06:26 +00:00
|
|
|
}
|
2018-06-13 07:01:16 +00:00
|
|
|
|
2018-06-13 09:08:46 +00:00
|
|
|
if (TEXT_PROPERTY in properties
|
2018-06-13 07:01:16 +00:00
|
|
|
&& is(element, 'bpmn:TextAnnotation')) {
|
|
|
|
|
|
|
|
var newBounds = textRenderer.getTextAnnotationBounds(
|
|
|
|
{
|
|
|
|
x: element.x,
|
|
|
|
y: element.y,
|
|
|
|
width: element.width,
|
|
|
|
height: element.height
|
|
|
|
},
|
2018-06-13 09:08:46 +00:00
|
|
|
properties[TEXT_PROPERTY] || ''
|
2018-06-13 07:01:16 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
modeling.updateLabel(element, properties.text, newBounds);
|
|
|
|
}
|
2018-04-30 09:06:26 +00:00
|
|
|
});
|
2015-07-13 08:37:43 +00:00
|
|
|
|
2018-04-30 09:06:26 +00:00
|
|
|
// create label shape after shape/connection was created
|
2015-07-13 08:37:43 +00:00
|
|
|
this.postExecute([ 'shape.create', 'connection.create' ], function(e) {
|
2019-08-07 13:17:28 +00:00
|
|
|
var context = e.context,
|
|
|
|
hints = context.hints || {};
|
|
|
|
|
|
|
|
if (hints.createElementsBehavior === false) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-13 08:37:43 +00:00
|
|
|
|
|
|
|
var element = context.shape || context.connection,
|
|
|
|
businessObject = element.businessObject;
|
|
|
|
|
2018-04-30 09:06:26 +00:00
|
|
|
if (!isLabelExternal(element)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-01 10:48:49 +00:00
|
|
|
// only create label if attribute available
|
|
|
|
if (!getLabel(element)) {
|
2017-03-07 08:30:48 +00:00
|
|
|
return;
|
2015-07-13 08:37:43 +00:00
|
|
|
}
|
2017-03-07 08:30:48 +00:00
|
|
|
|
|
|
|
var labelCenter = getExternalLabelMid(element);
|
|
|
|
|
|
|
|
// we don't care about x and y
|
2018-06-13 07:01:16 +00:00
|
|
|
var labelDimensions = textRenderer.getExternalLabelBounds(
|
2017-03-07 08:30:48 +00:00
|
|
|
DEFAULT_LABEL_DIMENSIONS,
|
2019-07-01 10:48:49 +00:00
|
|
|
getLabel(element)
|
2017-03-07 08:30:48 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
modeling.createLabel(element, labelCenter, {
|
|
|
|
id: businessObject.id + '_label',
|
|
|
|
businessObject: businessObject,
|
|
|
|
width: labelDimensions.width,
|
|
|
|
height: labelDimensions.height
|
|
|
|
});
|
2015-07-13 08:37:43 +00:00
|
|
|
});
|
|
|
|
|
2018-04-30 09:06:26 +00:00
|
|
|
// update label after label shape was deleted
|
|
|
|
this.postExecute('shape.delete', function(event) {
|
|
|
|
var context = event.context,
|
2018-06-08 14:01:40 +00:00
|
|
|
labelTarget = context.labelTarget,
|
|
|
|
hints = context.hints || {};
|
2016-04-19 15:12:48 +00:00
|
|
|
|
2018-04-30 09:06:26 +00:00
|
|
|
// check if label
|
2018-06-08 14:01:40 +00:00
|
|
|
if (labelTarget && hints.unsetLabel !== false) {
|
2018-06-08 14:13:18 +00:00
|
|
|
modeling.updateLabel(labelTarget, null, null, { removeShape: false });
|
2018-04-30 09:06:26 +00:00
|
|
|
}
|
|
|
|
});
|
2016-07-05 13:54:14 +00:00
|
|
|
|
2018-04-30 09:06:26 +00:00
|
|
|
// update di information on label creation
|
|
|
|
this.postExecute([ 'label.create' ], function(event) {
|
2016-07-05 13:54:14 +00:00
|
|
|
|
2018-04-30 09:06:26 +00:00
|
|
|
var context = event.context,
|
|
|
|
element = context.shape,
|
2016-07-05 13:54:14 +00:00
|
|
|
businessObject,
|
|
|
|
di;
|
|
|
|
|
|
|
|
// we want to trigger on real labels only
|
|
|
|
if (!element.labelTarget) {
|
2016-04-19 15:12:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-05 13:54:14 +00:00
|
|
|
// we want to trigger on BPMN elements only
|
|
|
|
if (!is(element.labelTarget || element, 'bpmn:BaseElement')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
businessObject = element.businessObject,
|
|
|
|
di = businessObject.di;
|
2016-04-19 15:12:48 +00:00
|
|
|
|
|
|
|
|
2016-07-05 13:54:14 +00:00
|
|
|
if (!di.label) {
|
|
|
|
di.label = bpmnFactory.create('bpmndi:BPMNLabel', {
|
|
|
|
bounds: bpmnFactory.create('dc:Bounds')
|
|
|
|
});
|
2016-04-19 15:12:48 +00:00
|
|
|
}
|
2016-07-05 13:54:14 +00:00
|
|
|
|
|
|
|
assign(di.label.bounds, {
|
|
|
|
x: element.x,
|
|
|
|
y: element.y,
|
|
|
|
width: element.width,
|
|
|
|
height: element.height
|
|
|
|
});
|
2016-04-19 15:12:48 +00:00
|
|
|
});
|
|
|
|
|
2016-07-05 13:54:14 +00:00
|
|
|
function getVisibleLabelAdjustment(event) {
|
|
|
|
|
2019-04-18 13:09:44 +00:00
|
|
|
var context = event.context,
|
2016-07-05 13:54:14 +00:00
|
|
|
connection = context.connection,
|
|
|
|
label = connection.label,
|
|
|
|
hints = assign({}, context.hints),
|
|
|
|
newWaypoints = context.newWaypoints || connection.waypoints,
|
|
|
|
oldWaypoints = context.oldWaypoints;
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof hints.startChanged === 'undefined') {
|
2019-04-18 13:09:44 +00:00
|
|
|
hints.startChanged = !!hints.connectionStart;
|
2016-04-22 13:44:56 +00:00
|
|
|
}
|
|
|
|
|
2016-07-05 13:54:14 +00:00
|
|
|
if (typeof hints.endChanged === 'undefined') {
|
2019-04-18 13:09:44 +00:00
|
|
|
hints.endChanged = !!hints.connectionEnd;
|
2016-07-05 13:54:14 +00:00
|
|
|
}
|
2016-04-22 13:44:56 +00:00
|
|
|
|
2016-07-05 13:54:14 +00:00
|
|
|
return getLabelAdjustment(label, newWaypoints, oldWaypoints, hints);
|
|
|
|
}
|
2016-04-22 13:44:56 +00:00
|
|
|
|
|
|
|
this.postExecute([
|
2016-07-05 13:54:14 +00:00
|
|
|
'connection.layout',
|
2016-04-22 13:44:56 +00:00
|
|
|
'connection.updateWaypoints'
|
2016-07-05 13:54:14 +00:00
|
|
|
], function(event) {
|
2016-04-22 13:44:56 +00:00
|
|
|
|
2016-07-05 13:54:14 +00:00
|
|
|
var label = event.context.connection.label,
|
|
|
|
labelAdjustment;
|
2016-04-22 13:44:56 +00:00
|
|
|
|
2019-10-22 13:17:36 +00:00
|
|
|
// handle missing label as well as the case
|
|
|
|
// that the label parent does not exist (yet),
|
|
|
|
// because it is being pasted / created via multi element create
|
|
|
|
//
|
|
|
|
// Cf. https://github.com/bpmn-io/bpmn-js/pull/1227
|
|
|
|
if (!label || !label.parent) {
|
2016-04-22 13:44:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-30 09:06:26 +00:00
|
|
|
labelAdjustment = getVisibleLabelAdjustment(event);
|
2016-04-22 13:44:56 +00:00
|
|
|
|
2016-07-05 13:54:14 +00:00
|
|
|
modeling.moveShape(label, labelAdjustment);
|
2016-04-22 13:44:56 +00:00
|
|
|
});
|
|
|
|
|
2016-07-05 13:54:14 +00:00
|
|
|
|
2018-02-27 09:08:31 +00:00
|
|
|
// keep label position on shape replace
|
2016-07-05 13:54:14 +00:00
|
|
|
this.postExecute([ 'shape.replace' ], function(event) {
|
|
|
|
var context = event.context,
|
2016-04-18 11:04:32 +00:00
|
|
|
newShape = context.newShape,
|
|
|
|
oldShape = context.oldShape;
|
|
|
|
|
|
|
|
var businessObject = getBusinessObject(newShape);
|
|
|
|
|
2018-04-30 09:06:26 +00:00
|
|
|
if (businessObject
|
|
|
|
&& isLabelExternal(businessObject)
|
|
|
|
&& oldShape.label
|
|
|
|
&& newShape.label) {
|
2016-04-18 11:04:32 +00:00
|
|
|
newShape.label.x = oldShape.label.x;
|
|
|
|
newShape.label.y = oldShape.label.y;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-11 09:13:33 +00:00
|
|
|
|
|
|
|
// move external label after resizing
|
|
|
|
this.postExecute('shape.resize', function(event) {
|
|
|
|
|
|
|
|
var context = event.context,
|
|
|
|
shape = context.shape,
|
|
|
|
newBounds = context.newBounds,
|
|
|
|
oldBounds = context.oldBounds;
|
|
|
|
|
|
|
|
if (hasExternalLabel(shape)) {
|
|
|
|
|
|
|
|
var label = shape.label,
|
|
|
|
labelMid = getMid(label),
|
|
|
|
edges = asEdges(oldBounds);
|
|
|
|
|
|
|
|
// get nearest border point to label as reference point
|
|
|
|
var referencePoint = getReferencePoint(labelMid, edges);
|
|
|
|
|
|
|
|
var delta = getReferencePointDelta(referencePoint, oldBounds, newBounds);
|
|
|
|
|
|
|
|
modeling.moveShape(label, delta);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-07-13 08:37:43 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
inherits(LabelBehavior, CommandInterceptor);
|
2015-07-13 08:37:43 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
LabelBehavior.$inject = [
|
|
|
|
'eventBus',
|
|
|
|
'modeling',
|
2018-05-24 11:42:40 +00:00
|
|
|
'bpmnFactory',
|
|
|
|
'textRenderer'
|
2019-06-11 09:13:33 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// helpers //////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates a reference point delta relative to a new position
|
|
|
|
* of a certain element's bounds
|
|
|
|
*
|
|
|
|
* @param {Point} point
|
|
|
|
* @param {Bounds} oldBounds
|
|
|
|
* @param {Bounds} newBounds
|
|
|
|
*
|
|
|
|
* @return {Delta} delta
|
|
|
|
*/
|
|
|
|
export function getReferencePointDelta(referencePoint, oldBounds, newBounds) {
|
|
|
|
|
|
|
|
var newReferencePoint = getNewAttachPoint(referencePoint, oldBounds, newBounds);
|
|
|
|
|
|
|
|
return roundPoint(delta(newReferencePoint, referencePoint));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the nearest point (reference point) for a given point
|
|
|
|
* onto given set of lines
|
|
|
|
*
|
|
|
|
* @param {Array<Point, Point>} lines
|
|
|
|
* @param {Point} point
|
|
|
|
*
|
|
|
|
* @param {Point}
|
|
|
|
*/
|
|
|
|
export function getReferencePoint(point, lines) {
|
|
|
|
|
|
|
|
if (!lines.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var nearestLine = getNearestLine(point, lines);
|
|
|
|
|
|
|
|
return perpendicularFoot(point, nearestLine);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert the given bounds to a lines array containing all edges
|
|
|
|
*
|
|
|
|
* @param {Bounds|Point} bounds
|
|
|
|
*
|
|
|
|
* @return Array<Point>
|
|
|
|
*/
|
|
|
|
export function asEdges(bounds) {
|
|
|
|
return [
|
|
|
|
[ // top
|
|
|
|
{
|
|
|
|
x: bounds.x,
|
|
|
|
y: bounds.y
|
|
|
|
},
|
|
|
|
{
|
|
|
|
x: bounds.x + (bounds.width || 0),
|
|
|
|
y: bounds.y
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[ // right
|
|
|
|
{
|
|
|
|
x: bounds.x + (bounds.width || 0),
|
|
|
|
y: bounds.y
|
|
|
|
},
|
|
|
|
{
|
|
|
|
x: bounds.x + (bounds.width || 0),
|
|
|
|
y: bounds.y + (bounds.height || 0)
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[ // bottom
|
|
|
|
{
|
|
|
|
x: bounds.x,
|
|
|
|
y: bounds.y + (bounds.height || 0)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
x: bounds.x + (bounds.width || 0),
|
|
|
|
y: bounds.y + (bounds.height || 0)
|
|
|
|
}
|
|
|
|
],
|
|
|
|
[ // left
|
|
|
|
{
|
|
|
|
x: bounds.x,
|
|
|
|
y: bounds.y
|
|
|
|
},
|
|
|
|
{
|
|
|
|
x: bounds.x,
|
|
|
|
y: bounds.y + (bounds.height || 0)
|
|
|
|
}
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the nearest line for a given point by distance
|
|
|
|
* @param {Point} point
|
|
|
|
* @param Array<Point> lines
|
|
|
|
*
|
|
|
|
* @return Array<Point>
|
|
|
|
*/
|
|
|
|
function getNearestLine(point, lines) {
|
|
|
|
|
|
|
|
var distances = lines.map(function(l) {
|
|
|
|
return {
|
|
|
|
line: l,
|
|
|
|
distance: getDistancePointLine(point, l)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
var sorted = sortBy(distances, 'distance');
|
|
|
|
|
|
|
|
return sorted[0].line;
|
2019-10-22 13:17:36 +00:00
|
|
|
}
|