2018-04-02 19:01:53 +00:00
|
|
|
import inherits from 'inherits';
|
2015-03-31 13:02:04 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
|
|
|
|
|
|
|
|
import { is } from '../../../util/ModelUtil';
|
2015-03-31 13:02:04 +00:00
|
|
|
|
2019-06-03 08:46:52 +00:00
|
|
|
import { isLabel } from '../../../util/LabelUtil';
|
|
|
|
|
|
|
|
import { getBBox } from 'diagram-js/lib/util/Elements';
|
|
|
|
|
|
|
|
import { assign } from 'min-dash';
|
|
|
|
|
2019-06-03 13:31:54 +00:00
|
|
|
import { asTRBL } from 'diagram-js/lib/layout/LayoutUtil';
|
|
|
|
|
2019-06-03 08:46:52 +00:00
|
|
|
var HORIZONTAL_PARTICIPANT_PADDING = 20,
|
2019-06-03 13:31:54 +00:00
|
|
|
VERTICAL_PARTICIPANT_PADDING = 20;
|
|
|
|
|
|
|
|
export var PARTICIPANT_BORDER_WIDTH = 30;
|
2019-06-03 08:46:52 +00:00
|
|
|
|
|
|
|
var HIGH_PRIORITY = 2000;
|
|
|
|
|
2015-03-31 13:02:04 +00:00
|
|
|
|
|
|
|
/**
|
2019-06-03 08:46:52 +00:00
|
|
|
* BPMN-specific behavior for creating participants.
|
2015-03-31 13:02:04 +00:00
|
|
|
*/
|
2019-06-03 08:46:52 +00:00
|
|
|
export default function CreateParticipantBehavior(canvas, eventBus, modeling) {
|
2015-03-31 13:02:04 +00:00
|
|
|
CommandInterceptor.call(this, eventBus);
|
|
|
|
|
2019-06-03 08:46:52 +00:00
|
|
|
// fit participant
|
|
|
|
eventBus.on([
|
|
|
|
'create.start',
|
|
|
|
'shape.move.start'
|
|
|
|
], HIGH_PRIORITY, function(event) {
|
|
|
|
var context = event.context,
|
|
|
|
shape = context.shape,
|
|
|
|
rootElement = canvas.getRootElement();
|
2015-03-31 13:02:04 +00:00
|
|
|
|
2019-06-03 08:46:52 +00:00
|
|
|
if (!is(shape, 'bpmn:Participant') ||
|
|
|
|
!is(rootElement, 'bpmn:Process') ||
|
|
|
|
!rootElement.children.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore connections, groups and labels
|
|
|
|
var children = rootElement.children.filter(function(element) {
|
|
|
|
return !is(element, 'bpmn:Group') &&
|
|
|
|
!isLabel(element) &&
|
|
|
|
!isConnection(element);
|
|
|
|
});
|
|
|
|
|
|
|
|
var childrenBBox = getBBox(children);
|
|
|
|
|
|
|
|
var participantBounds = getParticipantBounds(shape, childrenBBox);
|
|
|
|
|
|
|
|
// assign width and height
|
|
|
|
assign(shape, participantBounds);
|
|
|
|
|
|
|
|
// assign create constraints
|
|
|
|
context.createConstraints = getParticipantCreateConstraints(shape, childrenBBox);
|
|
|
|
});
|
|
|
|
|
|
|
|
// force hovering process when creating first participant
|
|
|
|
eventBus.on('create.start', HIGH_PRIORITY, function(event) {
|
|
|
|
var context = event.context,
|
|
|
|
shape = context.shape,
|
|
|
|
rootElement = canvas.getRootElement(),
|
|
|
|
rootElementGfx = canvas.getGraphics(rootElement);
|
|
|
|
|
|
|
|
function ensureHoveringProcess(event) {
|
|
|
|
event.element = rootElement;
|
|
|
|
event.gfx = rootElementGfx;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is(shape, 'bpmn:Participant') && is(rootElement, 'bpmn:Process')) {
|
|
|
|
eventBus.on('element.hover', HIGH_PRIORITY, ensureHoveringProcess);
|
2015-03-31 13:02:04 +00:00
|
|
|
|
2019-06-03 08:46:52 +00:00
|
|
|
eventBus.once('create.cleanup', function() {
|
|
|
|
eventBus.off('element.hover', ensureHoveringProcess);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// turn process into collaboration before adding participant
|
|
|
|
this.preExecute('shape.create', function(context) {
|
2015-03-31 13:02:04 +00:00
|
|
|
var parent = context.parent,
|
|
|
|
shape = context.shape,
|
|
|
|
position = context.position;
|
|
|
|
|
2016-04-20 15:31:42 +00:00
|
|
|
var rootElement = canvas.getRootElement();
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
if (
|
|
|
|
is(parent, 'bpmn:Process') &&
|
|
|
|
is(shape, 'bpmn:Participant') &&
|
|
|
|
!is(rootElement, 'bpmn:Collaboration')
|
|
|
|
) {
|
2015-03-31 13:02:04 +00:00
|
|
|
|
|
|
|
// this is going to detach the process root
|
|
|
|
// and set the returned collaboration element
|
|
|
|
// as the new root element
|
|
|
|
var collaborationElement = modeling.makeCollaboration();
|
|
|
|
|
|
|
|
// monkey patch the create context
|
|
|
|
// so that the participant is being dropped
|
|
|
|
// onto the new collaboration root instead
|
|
|
|
context.position = position;
|
|
|
|
context.parent = collaborationElement;
|
|
|
|
|
|
|
|
context.processRoot = parent;
|
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
this.execute('shape.create', function(context) {
|
|
|
|
var processRoot = context.processRoot,
|
|
|
|
shape = context.shape;
|
|
|
|
|
|
|
|
if (processRoot) {
|
|
|
|
context.oldProcessRef = shape.businessObject.processRef;
|
|
|
|
|
|
|
|
// assign the participant processRef
|
|
|
|
shape.businessObject.processRef = processRoot.businessObject;
|
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
this.revert('shape.create', function(context) {
|
|
|
|
var processRoot = context.processRoot,
|
|
|
|
shape = context.shape;
|
|
|
|
|
|
|
|
if (processRoot) {
|
2019-06-03 08:46:52 +00:00
|
|
|
|
2015-03-31 13:02:04 +00:00
|
|
|
// assign the participant processRef
|
|
|
|
shape.businessObject.processRef = context.oldProcessRef;
|
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
this.postExecute('shape.create', function(context) {
|
|
|
|
var processRoot = context.processRoot,
|
|
|
|
shape = context.shape;
|
|
|
|
|
|
|
|
if (processRoot) {
|
2019-06-03 08:46:52 +00:00
|
|
|
|
2015-03-31 13:02:04 +00:00
|
|
|
// process root is already detached at this point
|
|
|
|
var processChildren = processRoot.children.slice();
|
2019-06-03 08:46:52 +00:00
|
|
|
|
2015-08-13 08:51:52 +00:00
|
|
|
modeling.moveElements(processChildren, { x: 0, y: 0 }, shape);
|
2015-03-31 13:02:04 +00:00
|
|
|
}
|
2015-07-13 08:37:43 +00:00
|
|
|
|
2015-03-31 13:02:04 +00:00
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
CreateParticipantBehavior.$inject = [
|
2019-06-03 08:46:52 +00:00
|
|
|
'canvas',
|
2018-04-02 19:01:53 +00:00
|
|
|
'eventBus',
|
2019-06-03 08:46:52 +00:00
|
|
|
'modeling'
|
2018-04-02 19:01:53 +00:00
|
|
|
];
|
2015-03-31 13:02:04 +00:00
|
|
|
|
2019-06-03 08:46:52 +00:00
|
|
|
inherits(CreateParticipantBehavior, CommandInterceptor);
|
|
|
|
|
|
|
|
// helpers //////////
|
|
|
|
|
|
|
|
function getParticipantBounds(shape, childrenBBox) {
|
|
|
|
childrenBBox = {
|
|
|
|
width: childrenBBox.width + HORIZONTAL_PARTICIPANT_PADDING * 2 + PARTICIPANT_BORDER_WIDTH,
|
|
|
|
height: childrenBBox.height + VERTICAL_PARTICIPANT_PADDING * 2
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
width: Math.max(shape.width, childrenBBox.width),
|
|
|
|
height: Math.max(shape.height, childrenBBox.height)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getParticipantCreateConstraints(shape, childrenBBox) {
|
2019-06-03 13:31:54 +00:00
|
|
|
childrenBBox = asTRBL(childrenBBox);
|
|
|
|
|
2019-06-03 08:46:52 +00:00
|
|
|
return {
|
2019-06-03 13:31:54 +00:00
|
|
|
bottom: childrenBBox.top + shape.height / 2 - VERTICAL_PARTICIPANT_PADDING,
|
|
|
|
left: childrenBBox.right - shape.width / 2 + HORIZONTAL_PARTICIPANT_PADDING,
|
|
|
|
top: childrenBBox.bottom - shape.height / 2 + VERTICAL_PARTICIPANT_PADDING,
|
|
|
|
right: childrenBBox.left + shape.width / 2 - HORIZONTAL_PARTICIPANT_PADDING - PARTICIPANT_BORDER_WIDTH
|
2019-06-03 08:46:52 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function isConnection(element) {
|
|
|
|
return !!element.waypoints;
|
|
|
|
}
|