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'),
|
2016-04-22 13:44:56 +00:00
|
|
|
LabelLayoutUtil = require('./util/LabelLayoutUtil'),
|
2016-04-18 11:04:32 +00:00
|
|
|
ModelUtil = require('../../../util/ModelUtil'),
|
|
|
|
is = ModelUtil.is,
|
|
|
|
getBusinessObject = ModelUtil.getBusinessObject;
|
2015-07-13 08:37:43 +00:00
|
|
|
|
|
|
|
var hasExternalLabel = LabelUtil.hasExternalLabel,
|
2016-04-22 13:44:56 +00:00
|
|
|
getExternalLabelMid = LabelUtil.getExternalLabelMid,
|
|
|
|
getLabelAdjustment = LabelLayoutUtil.getLabelAdjustment;
|
2015-07-13 08:37:43 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2016-07-05 13:54:14 +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;
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-07-05 13:54:14 +00:00
|
|
|
///// update di information on label creation
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-07-05 13:54:14 +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
|
|
|
|
|
|
|
////// keep label position on shape replace
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
inherits(LabelSupport, CommandInterceptor);
|
|
|
|
|
|
|
|
LabelSupport.$inject = [ 'eventBus', 'modeling', 'bpmnFactory' ];
|
|
|
|
|
|
|
|
module.exports = LabelSupport;
|