2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
setLabel,
|
|
|
|
getLabel
|
|
|
|
} from '../LabelUtil';
|
2014-06-17 09:53:07 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
2018-04-30 09:06:26 +00:00
|
|
|
getExternalLabelMid,
|
|
|
|
isLabelExternal,
|
|
|
|
hasExternalLabel,
|
|
|
|
isLabel
|
2018-04-02 19:01:53 +00:00
|
|
|
} from '../../../util/LabelUtil';
|
2017-02-13 12:47:00 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
getBusinessObject,
|
|
|
|
is
|
|
|
|
} from '../../../util/ModelUtil';
|
2017-02-13 12:47:00 +00:00
|
|
|
|
|
|
|
var NULL_DIMENSIONS = {
|
|
|
|
width: 0,
|
|
|
|
height: 0
|
|
|
|
};
|
|
|
|
|
2014-06-17 09:53:07 +00:00
|
|
|
|
2014-06-11 13:08:45 +00:00
|
|
|
/**
|
2014-09-11 14:44:56 +00:00
|
|
|
* A handler that updates the text of a BPMN element.
|
2014-06-11 13:08:45 +00:00
|
|
|
*/
|
2018-05-24 11:42:40 +00:00
|
|
|
export default function UpdateLabelHandler(modeling, textRenderer) {
|
2016-03-03 16:09:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the label and return the changed elements.
|
|
|
|
*
|
|
|
|
* Element parameter can be label itself or connection (i.e. sequence flow).
|
|
|
|
*
|
|
|
|
* @param {djs.model.Base} element
|
|
|
|
* @param {String} text
|
|
|
|
*/
|
2014-06-11 13:08:45 +00:00
|
|
|
function setText(element, text) {
|
|
|
|
|
2016-03-03 16:09:43 +00:00
|
|
|
// external label if present
|
|
|
|
var label = element.label || element;
|
|
|
|
|
|
|
|
var labelTarget = element.labelTarget || element;
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
setLabel(label, text, labelTarget !== label);
|
2016-03-03 16:09:43 +00:00
|
|
|
|
|
|
|
return [ label, labelTarget ];
|
2014-06-11 13:08:45 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 09:06:26 +00:00
|
|
|
function preExecute(ctx) {
|
|
|
|
var element = ctx.element,
|
|
|
|
businessObject = element.businessObject,
|
|
|
|
newLabel = ctx.newLabel;
|
|
|
|
|
|
|
|
if (!isLabel(element)
|
|
|
|
&& isLabelExternal(element)
|
|
|
|
&& !hasExternalLabel(element)
|
2018-06-08 14:01:40 +00:00
|
|
|
&& !isEmptyText(newLabel)) {
|
2018-04-30 09:06:26 +00:00
|
|
|
|
|
|
|
// create label
|
|
|
|
var paddingTop = 7;
|
|
|
|
|
|
|
|
var labelCenter = getExternalLabelMid(element);
|
|
|
|
|
|
|
|
labelCenter = {
|
|
|
|
x: labelCenter.x,
|
|
|
|
y: labelCenter.y + paddingTop
|
|
|
|
};
|
|
|
|
|
|
|
|
modeling.createLabel(element, labelCenter, {
|
|
|
|
id: businessObject.id + '_label',
|
|
|
|
businessObject: businessObject
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 13:08:45 +00:00
|
|
|
function execute(ctx) {
|
2018-04-02 19:01:53 +00:00
|
|
|
ctx.oldLabel = getLabel(ctx.element);
|
2014-09-11 14:44:56 +00:00
|
|
|
return setText(ctx.element, ctx.newLabel);
|
2014-06-11 13:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function revert(ctx) {
|
2014-09-11 14:44:56 +00:00
|
|
|
return setText(ctx.element, ctx.oldLabel);
|
2014-06-11 13:08:45 +00:00
|
|
|
}
|
|
|
|
|
2017-02-13 12:47:00 +00:00
|
|
|
function postExecute(ctx) {
|
|
|
|
var element = ctx.element,
|
2017-06-27 07:37:58 +00:00
|
|
|
label = element.label || element,
|
2018-04-30 09:06:26 +00:00
|
|
|
newLabel = ctx.newLabel,
|
2018-06-08 14:01:40 +00:00
|
|
|
newBounds = ctx.newBounds,
|
|
|
|
hints = ctx.hints || {};
|
2017-02-13 12:47:00 +00:00
|
|
|
|
2018-06-08 14:01:40 +00:00
|
|
|
if (isLabel(label) && isEmptyText(newLabel)) {
|
|
|
|
|
|
|
|
if (hints.removeShape !== false) {
|
|
|
|
modeling.removeShape(label, { unsetLabel: false });
|
|
|
|
}
|
2018-04-30 09:06:26 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-27 07:37:58 +00:00
|
|
|
// ignore internal labels for elements except text annotations
|
2018-04-30 09:06:26 +00:00
|
|
|
if (!isLabelExternal(element) && !is(element, 'bpmn:TextAnnotation')) {
|
2017-02-13 12:47:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-27 07:37:58 +00:00
|
|
|
var bo = getBusinessObject(label);
|
|
|
|
|
|
|
|
var text = bo.name || bo.text;
|
2017-02-13 12:47:00 +00:00
|
|
|
|
2018-01-24 19:25:28 +00:00
|
|
|
// don't resize without text
|
2017-02-13 12:47:00 +00:00
|
|
|
if (!text) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-24 19:25:28 +00:00
|
|
|
// resize element based on label _or_ pre-defined bounds
|
|
|
|
if (typeof newBounds === 'undefined') {
|
2018-05-24 11:42:40 +00:00
|
|
|
newBounds = textRenderer.getLayoutedBounds(label, text);
|
2018-01-24 19:25:28 +00:00
|
|
|
}
|
2017-02-13 12:47:00 +00:00
|
|
|
|
2018-01-24 19:25:28 +00:00
|
|
|
// setting newBounds to false or _null_ will
|
|
|
|
// disable the postExecute resize operation
|
|
|
|
if (newBounds) {
|
|
|
|
modeling.resizeShape(label, newBounds, NULL_DIMENSIONS);
|
|
|
|
}
|
2017-02-13 12:47:00 +00:00
|
|
|
}
|
|
|
|
|
2016-03-15 14:03:11 +00:00
|
|
|
// API
|
2016-05-18 05:54:58 +00:00
|
|
|
|
2018-04-30 09:06:26 +00:00
|
|
|
this.preExecute = preExecute;
|
2014-06-11 13:08:45 +00:00
|
|
|
this.execute = execute;
|
|
|
|
this.revert = revert;
|
2017-02-13 12:47:00 +00:00
|
|
|
this.postExecute = postExecute;
|
2014-06-11 13:08:45 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 11:42:40 +00:00
|
|
|
UpdateLabelHandler.$inject = [
|
|
|
|
'modeling',
|
|
|
|
'textRenderer'
|
2018-06-08 14:01:40 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
// helpers ///////////////////////
|
|
|
|
|
|
|
|
function isEmptyText(label) {
|
|
|
|
return !label || !label.trim();
|
|
|
|
}
|