2014-06-11 13:08:45 +00:00
|
|
|
'use strict';
|
|
|
|
|
2014-06-24 13:34:57 +00:00
|
|
|
var _ = require('lodash');
|
|
|
|
|
2014-08-04 07:32:36 +00:00
|
|
|
var UpdateLabelHandler = require('./cmd/UpdateLabelHandler');
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2014-07-07 15:17:41 +00:00
|
|
|
var LabelUtil = require('./LabelUtil'),
|
|
|
|
DiUtil = require('../../util/Di');
|
2014-06-17 09:53:07 +00:00
|
|
|
|
|
|
|
|
2014-09-09 13:21:21 +00:00
|
|
|
var PADDING = 4;
|
|
|
|
|
|
|
|
var MIN_BOUNDS = {
|
2014-06-17 09:53:07 +00:00
|
|
|
width: 150,
|
2014-09-09 13:21:21 +00:00
|
|
|
height: 50
|
2014-06-17 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2014-07-16 14:15:23 +00:00
|
|
|
function LabelEditingProvider(eventBus, canvas, directEditing, commandStack) {
|
2014-06-11 13:08:45 +00:00
|
|
|
|
|
|
|
directEditing.registerProvider(this);
|
2014-08-04 07:32:36 +00:00
|
|
|
commandStack.registerHandler('element.updateLabel', UpdateLabelHandler);
|
2014-06-11 13:08:45 +00:00
|
|
|
|
|
|
|
// per default, listen to double click events
|
|
|
|
eventBus.on('shape.dblclick', function(event) {
|
|
|
|
directEditing.activate(event.element);
|
2014-06-24 13:34:57 +00:00
|
|
|
});
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2014-06-17 09:53:07 +00:00
|
|
|
// per default, listen to double click events
|
|
|
|
eventBus.on('connection.dblclick', function(event) {
|
|
|
|
directEditing.activate(event.element);
|
2014-06-24 13:34:57 +00:00
|
|
|
});
|
2014-06-17 09:53:07 +00:00
|
|
|
|
2014-07-07 15:17:41 +00:00
|
|
|
// intercept direct canvas clicks to deselect all selected shapes
|
2014-06-11 13:08:45 +00:00
|
|
|
eventBus.on('canvas.click', function(event) {
|
|
|
|
directEditing.complete();
|
|
|
|
});
|
|
|
|
|
2014-06-17 09:53:07 +00:00
|
|
|
eventBus.on('canvas.viewbox.changed', function(event) {
|
|
|
|
directEditing.complete();
|
|
|
|
});
|
|
|
|
|
2014-06-11 13:08:45 +00:00
|
|
|
this._canvas = canvas;
|
|
|
|
this._commandStack = commandStack;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LabelEditingProvider.prototype.activate = function(element) {
|
|
|
|
|
2014-07-16 14:15:23 +00:00
|
|
|
var semantic = element.businessObject,
|
|
|
|
di = semantic.di;
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2014-09-08 17:03:39 +00:00
|
|
|
var text = LabelUtil.getLabel(element);
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2014-06-17 09:53:07 +00:00
|
|
|
if (text === undefined) {
|
|
|
|
return;
|
2014-06-11 13:08:45 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 09:53:07 +00:00
|
|
|
var bbox = this.getEditingBBox(element);
|
|
|
|
|
|
|
|
// adjust for expanded pools / lanes
|
|
|
|
if ((semantic.$instanceOf('bpmn:Participant') && DiUtil.isExpandedPool(semantic)) ||
|
|
|
|
semantic.$instanceOf('bpmn:Lane')) {
|
|
|
|
|
2014-09-09 13:21:21 +00:00
|
|
|
bbox.width = MIN_BOUNDS.width;
|
|
|
|
bbox.height = MIN_BOUNDS.height;
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2014-06-17 09:53:07 +00:00
|
|
|
bbox.x = bbox.x + 10 - bbox.width / 2;
|
|
|
|
bbox.y = bbox.mid.y - bbox.height / 2;
|
2014-06-11 13:08:45 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 09:53:07 +00:00
|
|
|
// adjust for sub processes
|
|
|
|
if (semantic.$instanceOf('bpmn:SubProcess') && DiUtil.isExpanded(semantic, di)) {
|
|
|
|
|
2014-09-09 13:21:21 +00:00
|
|
|
bbox.height = MIN_BOUNDS.height;
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2014-06-17 09:53:07 +00:00
|
|
|
bbox.x = bbox.mid.x - bbox.width / 2;
|
|
|
|
bbox.y = bbox.y + 10 - bbox.height / 2;
|
2014-06-11 13:08:45 +00:00
|
|
|
}
|
2014-06-17 09:53:07 +00:00
|
|
|
|
|
|
|
return { bounds: bbox, text: text };
|
2014-06-11 13:08:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-06-17 09:53:07 +00:00
|
|
|
LabelEditingProvider.prototype.getEditingBBox = function(element, maxBounds) {
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2014-09-09 13:21:21 +00:00
|
|
|
var target = element.label || element;
|
|
|
|
|
|
|
|
var bbox = this._canvas.getAbsoluteBBox(target);
|
2014-06-11 13:08:45 +00:00
|
|
|
|
|
|
|
var mid = {
|
|
|
|
x: bbox.x + bbox.width / 2,
|
|
|
|
y: bbox.y + bbox.height / 2
|
|
|
|
};
|
|
|
|
|
2014-09-09 13:21:21 +00:00
|
|
|
// external label
|
|
|
|
if (target.labelTarget) {
|
|
|
|
bbox.width = Math.max(bbox.width, MIN_BOUNDS.width);
|
|
|
|
bbox.height = Math.max(bbox.height, MIN_BOUNDS.height);
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2014-09-09 13:21:21 +00:00
|
|
|
bbox.x = mid.x - bbox.width / 2;
|
|
|
|
}
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2014-06-17 09:53:07 +00:00
|
|
|
bbox.mid = mid;
|
|
|
|
|
2014-06-11 13:08:45 +00:00
|
|
|
return bbox;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-08-04 07:32:36 +00:00
|
|
|
LabelEditingProvider.prototype.update = function(element, newLabel) {
|
|
|
|
this._commandStack.execute('element.updateLabel', {
|
2014-06-11 13:08:45 +00:00
|
|
|
element: element,
|
2014-08-04 07:32:36 +00:00
|
|
|
newLabel: newLabel
|
2014-06-11 13:08:45 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-07-16 14:15:23 +00:00
|
|
|
LabelEditingProvider.$inject = [ 'eventBus', 'canvas', 'directEditing', 'commandStack' ];
|
2014-06-11 13:08:45 +00:00
|
|
|
|
|
|
|
module.exports = LabelEditingProvider;
|