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,
|
|
|
|
} from '../../../util/LabelUtil';
|
2015-07-13 08:37:43 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
getLabelAdjustment
|
|
|
|
} from './util/LabelLayoutUtil';
|
|
|
|
|
|
|
|
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
|
|
|
|
|
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';
|
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
|
|
|
}
|
|
|
|
});
|
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) {
|
|
|
|
var context = e.context;
|
|
|
|
|
|
|
|
var element = context.shape || context.connection,
|
|
|
|
businessObject = element.businessObject;
|
|
|
|
|
2018-04-30 09:06:26 +00:00
|
|
|
if (!isLabelExternal(element)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only create label if name
|
|
|
|
if (!businessObject.name) {
|
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-05-24 11:42:40 +00:00
|
|
|
var labelDimensions = textRenderer.getLayoutedBounds(
|
2017-03-07 08:30:48 +00:00
|
|
|
DEFAULT_LABEL_DIMENSIONS,
|
2018-05-24 11:42:40 +00:00
|
|
|
businessObject.name || ''
|
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) {
|
|
|
|
modeling.updateLabel(labelTarget, '', 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) {
|
|
|
|
|
|
|
|
var command = event.command,
|
|
|
|
context = event.context,
|
|
|
|
connection = context.connection,
|
|
|
|
label = connection.label,
|
|
|
|
hints = assign({}, context.hints),
|
|
|
|
newWaypoints = context.newWaypoints || connection.waypoints,
|
|
|
|
oldWaypoints = context.oldWaypoints;
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof hints.startChanged === 'undefined') {
|
|
|
|
hints.startChanged = (command === 'connection.reconnectStart');
|
2016-04-22 13:44:56 +00:00
|
|
|
}
|
|
|
|
|
2016-07-05 13:54:14 +00:00
|
|
|
if (typeof hints.endChanged === 'undefined') {
|
|
|
|
hints.endChanged = (command === 'connection.reconnectEnd');
|
|
|
|
}
|
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',
|
|
|
|
'connection.reconnectEnd',
|
|
|
|
'connection.reconnectStart',
|
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
|
|
|
|
|
|
|
if (!label) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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'
|
|
|
|
];
|