2014-06-11 13:08:45 +00:00
|
|
|
'use strict';
|
|
|
|
|
2014-06-17 09:53:07 +00:00
|
|
|
var LabelUtil = require('../LabelUtil');
|
|
|
|
|
2017-02-13 12:47:00 +00:00
|
|
|
var TextUtil = require('diagram-js/lib/util/Text');
|
|
|
|
|
|
|
|
var hasExternalLabel = require('../../../util/LabelUtil').hasExternalLabel;
|
|
|
|
|
2017-06-27 07:37:58 +00:00
|
|
|
var getBusinessObject = require('../../../util/ModelUtil').getBusinessObject,
|
|
|
|
is = require('../../../util/ModelUtil').is;
|
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
|
|
|
*/
|
2017-02-13 12:47:00 +00:00
|
|
|
function UpdateLabelHandler(modeling) {
|
|
|
|
|
|
|
|
var textUtil = new TextUtil();
|
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;
|
|
|
|
|
|
|
|
LabelUtil.setLabel(label, text, labelTarget !== label);
|
|
|
|
|
|
|
|
return [ label, labelTarget ];
|
2014-06-11 13:08:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function execute(ctx) {
|
2014-09-08 17:03:39 +00:00
|
|
|
ctx.oldLabel = LabelUtil.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-01-24 19:25:28 +00:00
|
|
|
newBounds = ctx.newBounds;
|
2017-02-13 12:47:00 +00:00
|
|
|
|
2017-06-27 07:37:58 +00:00
|
|
|
// ignore internal labels for elements except text annotations
|
|
|
|
if (!hasExternalLabel(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') {
|
|
|
|
newBounds = getLayoutedBounds(label, text, textUtil);
|
|
|
|
}
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-13 12:47:00 +00:00
|
|
|
UpdateLabelHandler.$inject = [ 'modeling' ];
|
|
|
|
|
|
|
|
module.exports = UpdateLabelHandler;
|
|
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
};
|
|
|
|
}
|