2018-04-02 19:01:53 +00:00
|
|
|
import inherits from 'inherits';
|
2015-09-17 07:05:09 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
|
2015-09-17 07:05:09 +00:00
|
|
|
|
2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
forEach
|
|
|
|
} from 'min-dash';
|
|
|
|
|
|
|
|
import {
|
|
|
|
isEventSubProcess
|
|
|
|
} from '../../../util/DiUtil';
|
|
|
|
|
|
|
|
import { is } from '../../../util/ModelUtil';
|
2015-09-17 07:05:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the behaviour of what happens to the elements inside a container
|
|
|
|
* that morphs into another BPMN element
|
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export default function ReplaceElementBehaviour(
|
2018-03-22 09:22:37 +00:00
|
|
|
eventBus, bpmnReplace, bpmnRules,
|
2018-04-02 19:01:53 +00:00
|
|
|
elementRegistry, selection, modeling) {
|
|
|
|
|
2015-09-17 07:05:09 +00:00
|
|
|
CommandInterceptor.call(this, eventBus);
|
|
|
|
|
|
|
|
this._bpmnReplace = bpmnReplace;
|
|
|
|
this._elementRegistry = elementRegistry;
|
|
|
|
this._selection = selection;
|
|
|
|
this._modeling = modeling;
|
|
|
|
|
|
|
|
this.postExecuted([ 'elements.move' ], 500, function(event) {
|
|
|
|
|
|
|
|
var context = event.context,
|
|
|
|
target = context.newParent,
|
|
|
|
newHost = context.newHost,
|
|
|
|
elements = [];
|
|
|
|
|
|
|
|
forEach(context.closure.topLevel, function(topLevelElements) {
|
|
|
|
if (isEventSubProcess(topLevelElements)) {
|
|
|
|
elements = elements.concat(topLevelElements.children);
|
|
|
|
} else {
|
|
|
|
elements = elements.concat(topLevelElements);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Change target to host when the moving element is a `bpmn:BoundaryEvent`
|
|
|
|
if (elements.length === 1 && newHost) {
|
|
|
|
target = newHost;
|
|
|
|
}
|
|
|
|
|
|
|
|
var canReplace = bpmnRules.canReplace(elements, target);
|
|
|
|
|
|
|
|
if (canReplace) {
|
|
|
|
this.replaceElements(elements, canReplace.replacements, newHost);
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
// update attachments if the host is replaced
|
|
|
|
this.postExecute([ 'shape.replace' ], 1500, function(e) {
|
|
|
|
|
|
|
|
var context = e.context,
|
|
|
|
oldShape = context.oldShape,
|
|
|
|
newShape = context.newShape,
|
|
|
|
attachers = oldShape.attachers,
|
|
|
|
canReplace;
|
|
|
|
|
|
|
|
if (attachers && attachers.length) {
|
|
|
|
canReplace = bpmnRules.canReplace(attachers, newShape);
|
|
|
|
|
|
|
|
this.replaceElements(attachers, canReplace.replacements);
|
|
|
|
}
|
|
|
|
|
|
|
|
}, this);
|
2016-06-07 14:45:47 +00:00
|
|
|
|
2018-02-27 09:08:31 +00:00
|
|
|
this.postExecuted([ 'shape.replace' ], 1500, function(e) {
|
2016-06-07 14:45:47 +00:00
|
|
|
var context = e.context,
|
|
|
|
oldShape = context.oldShape,
|
|
|
|
newShape = context.newShape;
|
|
|
|
|
|
|
|
modeling.unclaimId(oldShape.businessObject.id, oldShape.businessObject);
|
|
|
|
modeling.updateProperties(newShape, { id: oldShape.id });
|
|
|
|
});
|
2015-09-17 07:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inherits(ReplaceElementBehaviour, CommandInterceptor);
|
|
|
|
|
|
|
|
|
|
|
|
ReplaceElementBehaviour.prototype.replaceElements = function(elements, newElements, newHost) {
|
|
|
|
var elementRegistry = this._elementRegistry,
|
|
|
|
bpmnReplace = this._bpmnReplace,
|
|
|
|
selection = this._selection,
|
|
|
|
modeling = this._modeling;
|
|
|
|
|
|
|
|
forEach(newElements, function(replacement) {
|
|
|
|
|
|
|
|
var newElement = {
|
|
|
|
type: replacement.newElementType
|
|
|
|
};
|
|
|
|
|
|
|
|
var oldElement = elementRegistry.get(replacement.oldElementId);
|
|
|
|
|
|
|
|
if (newHost && is(oldElement, 'bpmn:BoundaryEvent')) {
|
|
|
|
modeling.updateAttachment(oldElement, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
var idx = elements.indexOf(oldElement);
|
|
|
|
|
|
|
|
elements[idx] = bpmnReplace.replaceElement(oldElement, newElement, { select: false });
|
|
|
|
|
|
|
|
if (newHost && is(elements[idx], 'bpmn:BoundaryEvent')) {
|
|
|
|
modeling.updateAttachment(elements[idx], newHost);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (newElements) {
|
|
|
|
selection.select(elements);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-18 07:47:04 +00:00
|
|
|
ReplaceElementBehaviour.$inject = [
|
2018-04-02 19:01:53 +00:00
|
|
|
'eventBus',
|
|
|
|
'bpmnReplace',
|
|
|
|
'bpmnRules',
|
|
|
|
'elementRegistry',
|
|
|
|
'selection',
|
|
|
|
'modeling'
|
2017-05-18 07:47:04 +00:00
|
|
|
];
|