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');
|
|
|
|
|
|
|
|
var is = require('../../util/ModelUtil').is,
|
|
|
|
isExpanded = require('../../util/DiUtil').isExpanded;
|
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
|
|
|
|
2015-10-20 13:34:16 +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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-20 13:34:16 +00:00
|
|
|
LabelEditingProvider.$inject = [ 'eventBus', 'canvas', 'directEditing', 'commandStack' ];
|
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);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-04-27 14:50:09 +00:00
|
|
|
// adjust for expanded sub processes
|
|
|
|
if (is(element, 'bpmn:SubProcess') && isExpanded(element)) {
|
2014-06-17 09:53:07 +00:00
|
|
|
|
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-09-15 12:42:43 +00:00
|
|
|
};
|