2019-05-17 12:26:13 +00:00
|
|
|
import inherits from 'inherits';
|
|
|
|
|
|
|
|
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
|
|
|
|
|
2019-05-22 08:42:07 +00:00
|
|
|
import { getBusinessObject } from '../../../util/ModelUtil';
|
2019-05-17 12:26:13 +00:00
|
|
|
|
2019-10-11 14:39:29 +00:00
|
|
|
import { isAny } from '../util/ModelingUtil';
|
|
|
|
|
|
|
|
import { isLabel } from '../../../util/LabelUtil';
|
|
|
|
|
|
|
|
var LOW_PRIORITY = 500;
|
|
|
|
|
2019-05-17 12:26:13 +00:00
|
|
|
|
|
|
|
/**
|
2019-10-11 14:39:29 +00:00
|
|
|
* Replace intermediate event with boundary event when creating or moving results in attached event.
|
2019-05-17 12:26:13 +00:00
|
|
|
*/
|
2019-10-11 14:39:29 +00:00
|
|
|
export default function AttachEventBehavior(bpmnReplace, injector) {
|
|
|
|
injector.invoke(CommandInterceptor, this);
|
2019-05-17 12:26:13 +00:00
|
|
|
|
2019-10-11 14:39:29 +00:00
|
|
|
this._bpmnReplace = bpmnReplace;
|
2019-05-17 12:26:13 +00:00
|
|
|
|
2019-10-11 14:39:29 +00:00
|
|
|
var self = this;
|
2019-05-17 12:26:13 +00:00
|
|
|
|
2019-10-11 14:39:29 +00:00
|
|
|
this.postExecuted('elements.create', LOW_PRIORITY, function(context) {
|
|
|
|
var elements = context.elements;
|
2019-05-17 12:26:13 +00:00
|
|
|
|
2019-10-11 14:39:29 +00:00
|
|
|
elements = elements.filter(function(shape) {
|
|
|
|
var host = shape.host;
|
|
|
|
|
|
|
|
return shouldReplace(shape, host);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (elements.length !== 1) {
|
2019-05-17 12:26:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:39:29 +00:00
|
|
|
elements.map(function(element) {
|
|
|
|
return elements.indexOf(element);
|
|
|
|
}).forEach(function(index) {
|
|
|
|
var host = elements[ index ];
|
2019-05-17 12:26:13 +00:00
|
|
|
|
2019-10-11 14:39:29 +00:00
|
|
|
context.elements[ index ] = self.replaceShape(elements[ index ], host);
|
|
|
|
});
|
|
|
|
}, true);
|
2019-05-17 12:26:13 +00:00
|
|
|
|
|
|
|
|
2019-10-11 14:39:29 +00:00
|
|
|
this.preExecute('elements.move', LOW_PRIORITY, function(context) {
|
|
|
|
var shapes = context.shapes,
|
|
|
|
host = context.newHost;
|
2019-05-17 12:26:13 +00:00
|
|
|
|
2019-10-11 14:39:29 +00:00
|
|
|
if (shapes.length !== 1) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-22 08:42:07 +00:00
|
|
|
|
2019-10-11 14:39:29 +00:00
|
|
|
var shape = shapes[0];
|
2019-05-17 12:26:13 +00:00
|
|
|
|
2019-10-11 14:39:29 +00:00
|
|
|
if (shouldReplace(shape, host)) {
|
|
|
|
context.shapes = [ self.replaceShape(shape, host) ];
|
2019-05-17 12:26:13 +00:00
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
AttachEventBehavior.$inject = [
|
2019-10-11 14:39:29 +00:00
|
|
|
'bpmnReplace',
|
|
|
|
'injector'
|
2019-05-17 12:26:13 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
inherits(AttachEventBehavior, CommandInterceptor);
|
2019-05-22 08:42:07 +00:00
|
|
|
|
2019-10-11 14:39:29 +00:00
|
|
|
AttachEventBehavior.prototype.replaceShape = function(shape, host) {
|
|
|
|
var eventDefinition = getEventDefinition(shape);
|
|
|
|
|
|
|
|
var boundaryEvent = {
|
|
|
|
type: 'bpmn:BoundaryEvent',
|
|
|
|
host: host
|
|
|
|
};
|
2019-05-22 08:42:07 +00:00
|
|
|
|
2019-10-11 14:39:29 +00:00
|
|
|
if (eventDefinition) {
|
|
|
|
boundaryEvent.eventDefinitionType = eventDefinition.$type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._bpmnReplace.replaceElement(shape, boundaryEvent, { layoutConnection: false });
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// helpers //////////
|
2019-05-22 08:42:07 +00:00
|
|
|
|
|
|
|
function getEventDefinition(element) {
|
2019-10-11 14:39:29 +00:00
|
|
|
var businessObject = getBusinessObject(element),
|
|
|
|
eventDefinitions = businessObject.eventDefinitions;
|
|
|
|
|
|
|
|
return eventDefinitions && eventDefinitions[0];
|
|
|
|
}
|
2019-05-22 08:42:07 +00:00
|
|
|
|
2019-10-11 14:39:29 +00:00
|
|
|
function shouldReplace(shape, host) {
|
|
|
|
return !isLabel(shape) &&
|
|
|
|
isAny(shape, [ 'bpmn:IntermediateThrowEvent', 'bpmn:IntermediateCatchEvent' ]) && !!host;
|
2019-05-22 08:42:07 +00:00
|
|
|
}
|