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