2014-07-23 16:53:33 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _ = require('lodash');
|
|
|
|
|
|
|
|
var LabelUtil = require('../../util/Label');
|
|
|
|
|
|
|
|
var hasExternalLabel = LabelUtil.hasExternalLabel,
|
|
|
|
getExternalLabelMid = LabelUtil.getExternalLabelMid;
|
|
|
|
|
|
|
|
|
|
|
|
function LabelSupport(eventBus, modeling, bpmnFactory) {
|
|
|
|
|
2014-09-11 15:22:59 +00:00
|
|
|
// create external labels on shape creation
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
eventBus.on([
|
|
|
|
'commandStack.shape.create.postExecute',
|
|
|
|
'commandStack.connection.create.postExecute'
|
|
|
|
], function(e) {
|
|
|
|
var context = e.context;
|
|
|
|
|
|
|
|
var element = context.shape || context.connection,
|
|
|
|
businessObject = element.businessObject;
|
|
|
|
|
|
|
|
var position;
|
|
|
|
|
|
|
|
if (hasExternalLabel(businessObject)) {
|
|
|
|
position = getExternalLabelMid(element);
|
|
|
|
modeling.createLabel(element, position, {
|
|
|
|
id: businessObject.id + '_label',
|
|
|
|
businessObject: businessObject
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-09-11 15:22:59 +00:00
|
|
|
|
|
|
|
// indicate label is dragged during move
|
|
|
|
|
|
|
|
eventBus.on('shape.move.start', 50000, function(e) {
|
|
|
|
|
|
|
|
var dragContext = e.dragContext,
|
2014-11-21 08:19:35 +00:00
|
|
|
shapes = dragContext.shapes;
|
2014-09-11 15:22:59 +00:00
|
|
|
|
2014-11-21 08:19:35 +00:00
|
|
|
var labels = [];
|
2014-09-11 15:22:59 +00:00
|
|
|
|
2014-11-21 08:19:35 +00:00
|
|
|
_.forEach(shapes, function(element) {
|
|
|
|
var label = element.label;
|
|
|
|
|
|
|
|
if (label && !label.hidden && dragContext.shapes.indexOf(label) === -1) {
|
|
|
|
labels.push(label);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_.forEach(labels, function(label) {
|
|
|
|
shapes.push(label);
|
|
|
|
});
|
2014-09-11 15:22:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// move labels with shapes
|
|
|
|
|
|
|
|
eventBus.on([
|
|
|
|
'commandStack.shape.move.postExecute'
|
|
|
|
], function(e) {
|
|
|
|
var context = e.context,
|
|
|
|
shape = context.shape;
|
|
|
|
|
|
|
|
if (shape.label && context.hints.layout !== false) {
|
|
|
|
modeling.moveShape(shape.label, context.delta);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// update di information on label movement and creation
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
eventBus.on([
|
|
|
|
'commandStack.label.create.executed',
|
2014-09-11 15:22:59 +00:00
|
|
|
'commandStack.shape.moved.executed'
|
2014-07-23 16:53:33 +00:00
|
|
|
], function(e) {
|
|
|
|
|
|
|
|
var element = e.context.shape,
|
|
|
|
businessObject = element.businessObject,
|
|
|
|
di = businessObject.di;
|
|
|
|
|
2014-09-11 15:22:59 +00:00
|
|
|
// we want to trigger on real labels only
|
|
|
|
if (!element.labelTarget) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-23 16:53:33 +00:00
|
|
|
if (!di.label) {
|
|
|
|
di.label = bpmnFactory.create('bpmndi:BPMNLabel', {
|
|
|
|
bounds: bpmnFactory.create('dc:Bounds')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_.extend(di.label.bounds, {
|
|
|
|
x: element.x,
|
|
|
|
y: element.y,
|
|
|
|
width: element.width,
|
|
|
|
height: element.height
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
LabelSupport.$inject = [ 'eventBus', 'modeling', 'bpmnFactory' ];
|
|
|
|
|
2014-10-27 11:07:57 +00:00
|
|
|
module.exports = LabelSupport;
|