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,
|
2016-05-18 05:54:58 +00:00
|
|
|
isExpanded = require('../../util/DiUtil').isExpanded;
|
2014-06-17 09:53:07 +00:00
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
var LINE_HEIGHT = 14,
|
|
|
|
PADDING = 6;
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2016-05-18 05:54:58 +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
|
|
|
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-05-18 05:54:58 +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
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
/**
|
|
|
|
* Activate direct editing for activities and text annotations.
|
|
|
|
*
|
|
|
|
* @param {djs.model.Base} element
|
|
|
|
*
|
|
|
|
* @return {Object} an object with properties bounds (position and size) and text
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
var properties = this.getEditingBBox(element);
|
2016-05-18 05:54:58 +00:00
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
properties.text = text;
|
2014-06-17 09:53:07 +00:00
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
return properties;
|
|
|
|
};
|
2014-06-11 13:08:45 +00:00
|
|
|
|
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
/**
|
|
|
|
* Get the editing bounding box based on the element's size and position
|
|
|
|
*
|
|
|
|
* @param {djs.model.Base} element
|
|
|
|
*
|
|
|
|
* @return {Object} an object containing information about position and size (fixed or minimum and/or maximum)
|
|
|
|
*/
|
|
|
|
LabelEditingProvider.prototype.getEditingBBox = function(element) {
|
2016-03-07 16:06:24 +00:00
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
var target = element.label || element;
|
2016-03-07 16:06:24 +00:00
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
var bbox = this._canvas.getAbsoluteBBox(target);
|
|
|
|
|
|
|
|
var mid = {
|
|
|
|
x: bbox.x + bbox.width / 2,
|
|
|
|
y: bbox.y + bbox.height / 2
|
|
|
|
};
|
2016-03-07 16:06:24 +00:00
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
// default position
|
|
|
|
var bounds = { x: bbox.x, y: bbox.y };
|
2014-06-17 09:53:07 +00:00
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
var style = {};
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
// adjust for expanded pools AND lanes
|
|
|
|
if ((is(element, 'bpmn:Participant') && isExpanded(element)) || is(element, 'bpmn:Lane')) {
|
|
|
|
|
|
|
|
bounds.width = 150;
|
|
|
|
bounds.minHeight = LINE_HEIGHT + PADDING;
|
|
|
|
bounds.maxHeight = LINE_HEIGHT * 2 + PADDING;
|
|
|
|
bounds.x = bbox.x - bounds.width / 2;
|
|
|
|
bounds.y = mid.y - bounds.minHeight / 2;
|
2014-06-11 13:08:45 +00:00
|
|
|
}
|
2014-06-17 09:53:07 +00:00
|
|
|
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
// internal labels for tasks and collapsed call activities, sub processes and participants
|
|
|
|
if (
|
|
|
|
is(element, 'bpmn:Task') ||
|
|
|
|
(is(element, 'bpmn:CallActivity') && !isExpanded(element)) ||
|
|
|
|
(is(element, 'bpmn:SubProcess') && !isExpanded(element)) ||
|
|
|
|
(is(element, 'bpmn:Participant') && !isExpanded(element))
|
|
|
|
) {
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
// fixed size for internal labels
|
|
|
|
bounds.width = bbox.width;
|
|
|
|
bounds.height = bbox.height;
|
|
|
|
}
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2016-05-18 05:54:58 +00:00
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
// internal labels for expanded sub processes
|
|
|
|
if (is(element, 'bpmn:SubProcess') && isExpanded(element)) {
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
bounds.width = element.width;
|
|
|
|
bounds.maxHeight = 3 * LINE_HEIGHT + PADDING; // maximum 3 lines
|
|
|
|
bounds.x = mid.x - element.width / 2;
|
|
|
|
}
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
|
|
|
|
// external labels for events, data elements, gateways and connections
|
2014-09-09 13:21:21 +00:00
|
|
|
if (target.labelTarget) {
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
bounds.width = 150;
|
|
|
|
bounds.minHeight = LINE_HEIGHT + PADDING; // 1 line
|
|
|
|
bounds.x = mid.x - bounds.width / 2;
|
2014-09-09 13:21:21 +00:00
|
|
|
}
|
2014-06-11 13:08:45 +00:00
|
|
|
|
2014-06-17 09:53:07 +00:00
|
|
|
|
2016-08-19 14:16:37 +00:00
|
|
|
// text annotations
|
|
|
|
if (is(element, 'bpmn:TextAnnotation')) {
|
|
|
|
bounds.minWidth = 100;
|
|
|
|
bounds.height = element.height;
|
|
|
|
|
|
|
|
style.textAlign = 'left';
|
|
|
|
}
|
|
|
|
|
|
|
|
return { bounds: bounds, style: style };
|
2014-06-11 13:08:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-05-18 05:54:58 +00:00
|
|
|
LabelEditingProvider.prototype.update = function(element, newLabel) {
|
2014-08-04 07:32:36 +00:00
|
|
|
this._commandStack.execute('element.updateLabel', {
|
2014-06-11 13:08:45 +00:00
|
|
|
element: element,
|
2016-05-18 05:54:58 +00:00
|
|
|
newLabel: newLabel
|
2014-06-11 13:08:45 +00:00
|
|
|
});
|
2016-03-07 16:06:24 +00:00
|
|
|
};
|