2014-06-11 13:08:45 +00:00
|
|
|
'use strict';
|
|
|
|
|
2014-08-04 07:32:36 +00:00
|
|
|
var UpdateLabelHandler = require('./cmd/UpdateLabelHandler');
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2015-04-27 14:50:09 +00:00
|
|
|
var LabelUtil = require('./LabelUtil');
|
|
|
|
|
2016-03-15 14:03:11 +00:00
|
|
|
var minBoundsLabel = require('../../util/LabelUtil').DEFAULT_LABEL_SIZE;
|
|
|
|
|
2015-04-27 14:50:09 +00:00
|
|
|
var is = require('../../util/ModelUtil').is,
|
2016-03-15 14:03:11 +00:00
|
|
|
isExpanded = require('../../util/DiUtil').isExpanded,
|
|
|
|
assign = require('lodash/object/assign');
|
2014-06-17 09:53:07 +00:00
|
|
|
|
|
|
|
|
2014-09-09 13:21:21 +00:00
|
|
|
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
|
|
|
|
2016-03-15 14:03:11 +00:00
|
|
|
|
|
|
|
function LabelEditingProvider(eventBus, canvas, directEditing, commandStack, elementFactory) {
|
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
|
|
|
|
2014-12-17 20:54:25 +00:00
|
|
|
// listen to dblclick on non-root elements
|
|
|
|
eventBus.on('element.dblclick', function(event) {
|
2014-06-17 09:53:07 +00:00
|
|
|
directEditing.activate(event.element);
|
2014-06-24 13:34:57 +00:00
|
|
|
});
|
2014-06-17 09:53:07 +00:00
|
|
|
|
2014-12-23 15:49:28 +00:00
|
|
|
// complete on followup canvas operation
|
2016-03-08 13:41:32 +00:00
|
|
|
eventBus.on([ 'element.mousedown', 'drag.init', 'canvas.viewbox.changed' ], function(event) {
|
2014-06-11 13:08:45 +00:00
|
|
|
directEditing.complete();
|
|
|
|
});
|
|
|
|
|
2014-12-23 15:49:28 +00:00
|
|
|
// cancel on command stack changes
|
|
|
|
eventBus.on([ 'commandStack.changed' ], function() {
|
|
|
|
directEditing.cancel();
|
2014-06-17 09:53:07 +00:00
|
|
|
});
|
|
|
|
|
2014-09-15 12:42:43 +00:00
|
|
|
|
2014-12-23 15:49:28 +00:00
|
|
|
// activate direct editing for activities and text annotations
|
2014-09-15 12:42:43 +00:00
|
|
|
|
|
|
|
|
2014-12-23 15:49:28 +00:00
|
|
|
if ('ontouchstart' in document.documentElement) {
|
|
|
|
// we deactivate automatic label editing on mobile devices
|
|
|
|
// as it breaks the user interaction workflow
|
2014-09-15 12:42:43 +00:00
|
|
|
|
2014-12-23 15:49:28 +00:00
|
|
|
// TODO(nre): we should temporarily focus the edited element here
|
|
|
|
// and release the focused viewport after the direct edit operation is finished
|
|
|
|
} else {
|
|
|
|
eventBus.on('create.end', 500, function(e) {
|
2014-09-15 12:42:43 +00:00
|
|
|
|
2014-12-23 15:49:28 +00:00
|
|
|
var element = e.shape,
|
2015-04-27 14:50:09 +00:00
|
|
|
canExecute = e.context.canExecute;
|
|
|
|
|
|
|
|
if (!canExecute) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-15 12:42:43 +00:00
|
|
|
|
2015-04-27 14:50:09 +00:00
|
|
|
if (is(element, 'bpmn:Task') || is(element, 'bpmn:TextAnnotation') ||
|
|
|
|
(is(element, 'bpmn:SubProcess') && !isExpanded(element))) {
|
2014-09-15 12:42:43 +00:00
|
|
|
|
2014-12-23 15:49:28 +00:00
|
|
|
directEditing.activate(element);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-09-15 12:42:43 +00:00
|
|
|
|
2014-06-11 13:08:45 +00:00
|
|
|
this._canvas = canvas;
|
|
|
|
this._commandStack = commandStack;
|
2016-03-15 14:03:11 +00:00
|
|
|
this._elementFactory = elementFactory;
|
2014-06-11 13:08:45 +00:00
|
|
|
}
|
|
|
|
|
2016-03-15 14:03:11 +00:00
|
|
|
LabelEditingProvider.$inject = [ 'eventBus', 'canvas', 'directEditing', 'commandStack', 'elementFactory' ];
|
2014-09-15 12:42:43 +00:00
|
|
|
|
|
|
|
module.exports = LabelEditingProvider;
|
|
|
|
|
2014-06-11 13:08:45 +00:00
|
|
|
|
|
|
|
LabelEditingProvider.prototype.activate = function(element) {
|
|
|
|
|
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);
|
2016-03-15 14:03:11 +00:00
|
|
|
var options = {};
|
2015-04-27 14:50:09 +00:00
|
|
|
// adjust for expanded pools AND lanes
|
|
|
|
if ((is(element, 'bpmn:Participant') && isExpanded(element)) || is(element, 'bpmn:Lane')) {
|
2014-06-17 09:53:07 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-03-07 16:06:24 +00:00
|
|
|
// ajust minumum size for task and activities
|
|
|
|
if ((is(element, 'bpmn:Task') || is(element, 'bpmn:Activity') )) {
|
|
|
|
|
|
|
|
if (bbox.width < 100) {
|
|
|
|
bbox.width = 100;
|
|
|
|
bbox.x = bbox.mid.x - bbox.width / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bbox.height < 80) {
|
|
|
|
bbox.height = 80;
|
|
|
|
bbox.y = bbox.mid.y - bbox.height / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust for expanded sub processes and collapsed pools
|
|
|
|
if ((is(element, 'bpmn:SubProcess') && isExpanded(element)) ||
|
|
|
|
(is(element, 'bpmn:Participant') && !isExpanded(element))) {
|
2014-06-17 09:53:07 +00:00
|
|
|
|
2016-03-07 16:06:24 +00:00
|
|
|
bbox.width = element.width;
|
2014-09-09 13:21:21 +00:00
|
|
|
bbox.height = MIN_BOUNDS.height;
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2016-03-07 16:06:24 +00:00
|
|
|
bbox.x = bbox.mid.x - element.width / 2;
|
2014-06-11 13:08:45 +00:00
|
|
|
}
|
2014-06-17 09:53:07 +00:00
|
|
|
|
2016-03-15 14:03:11 +00:00
|
|
|
// autosizing for TextAnnotation
|
|
|
|
if (is(element, 'bpmn:TextAnnotation')) {
|
|
|
|
options.autosizing = true;
|
|
|
|
options.textAlignment = 'left';
|
|
|
|
options.defaultHeight = this._elementFactory._getDefaultSize(element).height;
|
|
|
|
options.maxHeight = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
// and external label
|
|
|
|
if(element.label || element.type === 'label') {
|
|
|
|
options.autosizing = true;
|
|
|
|
options.defaultHeight = 50;
|
|
|
|
options.maxHeight = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
return { bounds: bbox, text: text, options: options };
|
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;
|
|
|
|
};
|
|
|
|
|
2016-03-15 14:03:11 +00:00
|
|
|
LabelEditingProvider.prototype.update = function(element, newLabel, newSize) {
|
|
|
|
var newBounds = {};
|
|
|
|
|
|
|
|
var target = element.label || element;
|
|
|
|
|
|
|
|
if(is(target, 'bpmn:TextAnnotation') || target.type === 'label'){
|
|
|
|
var newX = null;
|
|
|
|
if (target.type === 'label') {
|
|
|
|
// newSize-obj carries dimensions of the textarea which have to be adapted
|
|
|
|
newSize.width = newSize.width <= MIN_BOUNDS.width ? minBoundsLabel.width : newSize.width;
|
|
|
|
newSize.height = Math.max(minBoundsLabel.height, newSize.height);
|
|
|
|
|
|
|
|
// x coordinate gets calculated related to the old position
|
|
|
|
var deltaX = target.width - newSize.width;
|
|
|
|
newX = target.x + deltaX / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
assign(newBounds, {
|
|
|
|
x: newX || target.x,
|
|
|
|
y: target.y,
|
|
|
|
width: newSize.width,
|
|
|
|
height: newSize.height
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
newBounds = null;
|
|
|
|
}
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2014-08-04 07:32:36 +00:00
|
|
|
this._commandStack.execute('element.updateLabel', {
|
2014-06-11 13:08:45 +00:00
|
|
|
element: element,
|
2016-03-15 14:03:11 +00:00
|
|
|
newLabel: newLabel,
|
|
|
|
newBounds: newBounds
|
2014-06-11 13:08:45 +00:00
|
|
|
});
|
2016-03-07 16:06:24 +00:00
|
|
|
};
|