2015-07-13 08:37:43 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var assign = require('lodash/object/assign'),
|
|
|
|
inherits = require('inherits');
|
|
|
|
|
2016-03-08 13:12:52 +00:00
|
|
|
var LabelUtil = require('../../../util/LabelUtil'),
|
|
|
|
is = require('../../../util/ModelUtil').is;
|
2015-07-13 08:37:43 +00:00
|
|
|
|
|
|
|
var hasExternalLabel = LabelUtil.hasExternalLabel,
|
|
|
|
getExternalLabelMid = LabelUtil.getExternalLabelMid;
|
|
|
|
|
|
|
|
var CommandInterceptor = require('diagram-js/lib/command/CommandInterceptor');
|
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2015-07-13 08:37:43 +00:00
|
|
|
function LabelSupport(eventBus, modeling, bpmnFactory) {
|
|
|
|
|
|
|
|
CommandInterceptor.call(this, eventBus);
|
|
|
|
|
|
|
|
// create external labels on shape creation
|
|
|
|
|
|
|
|
this.postExecute([ 'shape.create', 'connection.create' ], function(e) {
|
|
|
|
var context = e.context;
|
|
|
|
|
|
|
|
var element = context.shape || context.connection,
|
|
|
|
businessObject = element.businessObject;
|
|
|
|
|
|
|
|
var position;
|
|
|
|
|
|
|
|
if (hasExternalLabel(businessObject)) {
|
|
|
|
position = getExternalLabelMid(element);
|
2016-01-04 10:12:53 +00:00
|
|
|
|
2015-07-13 08:37:43 +00:00
|
|
|
modeling.createLabel(element, position, {
|
|
|
|
id: businessObject.id + '_label',
|
2016-01-04 10:12:53 +00:00
|
|
|
hidden: !businessObject.name,
|
2015-07-13 08:37:43 +00:00
|
|
|
businessObject: businessObject
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-04-19 15:12:48 +00:00
|
|
|
// update label position on waypoints change if still hidden
|
|
|
|
this.postExecute([ 'connection.updateWaypoints' ], function(e) {
|
|
|
|
var context = e.context,
|
|
|
|
connection = context.connection,
|
|
|
|
label = connection.label;
|
|
|
|
|
|
|
|
if (!label) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (label.hidden) {
|
|
|
|
var position = getExternalLabelMid(connection);
|
|
|
|
|
|
|
|
var delta = {
|
|
|
|
x: position.x - label.x - label.width / 2,
|
|
|
|
y: position.y - label.y - label.height / 2
|
|
|
|
};
|
|
|
|
|
|
|
|
modeling.moveShape(label, delta);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-03-08 13:12:52 +00:00
|
|
|
// update di information on label creation
|
|
|
|
this.executed([ 'label.create' ], function(e) {
|
|
|
|
|
2015-07-13 08:37:43 +00:00
|
|
|
var element = e.context.shape,
|
2015-09-02 13:13:18 +00:00
|
|
|
businessObject,
|
|
|
|
di;
|
2015-07-13 08:37:43 +00:00
|
|
|
|
|
|
|
// we want to trigger on real labels only
|
|
|
|
if (!element.labelTarget) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-02 13:13:18 +00:00
|
|
|
// we want to trigger on BPMN elements only
|
|
|
|
if (!is(element.labelTarget || element, 'bpmn:BaseElement')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
businessObject = element.businessObject,
|
|
|
|
di = businessObject.di;
|
|
|
|
|
|
|
|
|
2015-07-13 08:37:43 +00:00
|
|
|
if (!di.label) {
|
|
|
|
di.label = bpmnFactory.create('bpmndi:BPMNLabel', {
|
|
|
|
bounds: bpmnFactory.create('dc:Bounds')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
assign(di.label.bounds, {
|
|
|
|
x: element.x,
|
|
|
|
y: element.y,
|
|
|
|
width: element.width,
|
|
|
|
height: element.height
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
inherits(LabelSupport, CommandInterceptor);
|
|
|
|
|
|
|
|
LabelSupport.$inject = [ 'eventBus', 'modeling', 'bpmnFactory' ];
|
|
|
|
|
|
|
|
module.exports = LabelSupport;
|