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 {
|
|
|
|
hasExternalLabel,
|
|
|
|
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';
|
|
|
|
|
|
|
|
import TextUtil from 'diagram-js/lib/util/Text';
|
2017-03-07 08:30:48 +00:00
|
|
|
|
|
|
|
var DEFAULT_LABEL_DIMENSIONS = {
|
|
|
|
width: 90,
|
|
|
|
height: 20
|
|
|
|
};
|
|
|
|
|
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-04-02 19:01:53 +00:00
|
|
|
export default function LabelBehavior(eventBus, modeling, bpmnFactory) {
|
2015-07-13 08:37:43 +00:00
|
|
|
|
|
|
|
CommandInterceptor.call(this, eventBus);
|
|
|
|
|
2017-03-07 08:30:48 +00:00
|
|
|
var textUtil = new TextUtil();
|
|
|
|
|
2016-07-05 13:54:14 +00:00
|
|
|
|
2018-02-27 09:08:31 +00:00
|
|
|
// create external labels on shape creation
|
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;
|
|
|
|
|
2017-12-09 23:32:11 +00:00
|
|
|
if (!hasExternalLabel(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
|
|
|
|
var labelDimensions = getLayoutedBounds(
|
|
|
|
DEFAULT_LABEL_DIMENSIONS,
|
|
|
|
businessObject.name || '',
|
|
|
|
textUtil
|
|
|
|
);
|
|
|
|
|
|
|
|
modeling.createLabel(element, labelCenter, {
|
|
|
|
id: businessObject.id + '_label',
|
|
|
|
hidden: !businessObject.name,
|
|
|
|
businessObject: businessObject,
|
|
|
|
width: labelDimensions.width,
|
|
|
|
height: labelDimensions.height
|
|
|
|
});
|
2015-07-13 08:37:43 +00:00
|
|
|
});
|
|
|
|
|
2016-04-19 15:12:48 +00:00
|
|
|
|
2018-02-27 09:08:31 +00:00
|
|
|
// update di information on label creation
|
2016-07-05 13:54:14 +00:00
|
|
|
|
|
|
|
this.executed([ 'label.create' ], function(event) {
|
|
|
|
|
|
|
|
var element = event.context.shape,
|
|
|
|
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-04-22 13:44:56 +00:00
|
|
|
|
2018-02-27 09:08:31 +00:00
|
|
|
// update label position on connection change
|
2016-04-22 13:44:56 +00:00
|
|
|
|
2016-07-05 13:54:14 +00:00
|
|
|
function getHiddenLabelAdjustment(event) {
|
2016-04-22 13:44:56 +00:00
|
|
|
|
2016-07-05 13:54:14 +00:00
|
|
|
var context = event.context,
|
|
|
|
connection = context.connection,
|
|
|
|
label = connection.label;
|
|
|
|
|
|
|
|
var labelPosition = getExternalLabelMid(connection);
|
|
|
|
|
|
|
|
return {
|
|
|
|
x: labelPosition.x - label.x - label.width / 2,
|
|
|
|
y: labelPosition.y - label.y - label.height / 2
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-07-05 13:54:14 +00:00
|
|
|
if (label.hidden) {
|
|
|
|
labelAdjustment = getHiddenLabelAdjustment(event);
|
|
|
|
} else {
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (businessObject && hasExternalLabel(businessObject)) {
|
|
|
|
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',
|
|
|
|
'bpmnFactory'
|
|
|
|
];
|
2017-03-07 08:30:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
// TODO(nikku): repeating code (search for <getLayoutedBounds>)
|
|
|
|
|
|
|
|
var EXTERNAL_LABEL_STYLE = {
|
|
|
|
fontFamily: 'Arial, sans-serif',
|
|
|
|
fontSize: '11px'
|
|
|
|
};
|
|
|
|
|
|
|
|
function getLayoutedBounds(bounds, text, textUtil) {
|
|
|
|
|
|
|
|
var layoutedLabelDimensions = textUtil.getDimensions(text, {
|
|
|
|
box: {
|
|
|
|
width: 90,
|
|
|
|
height: 30,
|
|
|
|
x: bounds.width / 2 + bounds.x,
|
|
|
|
y: bounds.height / 2 + bounds.y
|
|
|
|
},
|
|
|
|
style: EXTERNAL_LABEL_STYLE
|
|
|
|
});
|
|
|
|
|
|
|
|
// resize label shape to fit label text
|
|
|
|
return {
|
|
|
|
x: Math.round(bounds.x + bounds.width / 2 - layoutedLabelDimensions.width / 2),
|
|
|
|
y: Math.round(bounds.y),
|
|
|
|
width: Math.ceil(layoutedLabelDimensions.width),
|
|
|
|
height: Math.ceil(layoutedLabelDimensions.height)
|
|
|
|
};
|
|
|
|
}
|