2018-04-02 19:01:53 +00:00
|
|
|
import {
|
|
|
|
pick,
|
|
|
|
assign,
|
|
|
|
filter,
|
|
|
|
has
|
|
|
|
} from 'min-dash';
|
|
|
|
|
|
|
|
import {
|
|
|
|
is,
|
2018-06-27 13:11:34 +00:00
|
|
|
getBusinessObject
|
2018-04-02 19:01:53 +00:00
|
|
|
} from '../../util/ModelUtil';
|
|
|
|
|
|
|
|
import {
|
|
|
|
isAny
|
|
|
|
} from '../modeling/util/ModelingUtil';
|
|
|
|
|
|
|
|
import {
|
|
|
|
isExpanded,
|
|
|
|
isEventSubProcess
|
|
|
|
} from '../../util/DiUtil';
|
|
|
|
|
|
|
|
import {
|
|
|
|
getProperties,
|
|
|
|
IGNORED_PROPERTIES
|
|
|
|
} from '../../util/model/ModelCloneUtils';
|
|
|
|
|
|
|
|
import ModelCloneHelper from '../../util/model/ModelCloneHelper';
|
2015-08-04 15:52:43 +00:00
|
|
|
|
2015-07-23 11:59:47 +00:00
|
|
|
var CUSTOM_PROPERTIES = [
|
|
|
|
'cancelActivity',
|
|
|
|
'instantiate',
|
2015-08-04 15:52:43 +00:00
|
|
|
'eventGatewayType',
|
|
|
|
'triggeredByEvent',
|
|
|
|
'isInterrupting'
|
2015-07-23 11:59:47 +00:00
|
|
|
];
|
|
|
|
|
2017-01-11 14:22:32 +00:00
|
|
|
|
2016-06-16 11:57:32 +00:00
|
|
|
function toggeling(element, target) {
|
|
|
|
|
2018-01-05 13:32:52 +00:00
|
|
|
var oldCollapsed = (
|
2018-03-22 09:22:37 +00:00
|
|
|
element && has(element, 'collapsed') ? element.collapsed : !isExpanded(element)
|
2018-01-05 13:32:52 +00:00
|
|
|
);
|
2016-06-16 11:57:32 +00:00
|
|
|
|
|
|
|
var targetCollapsed;
|
|
|
|
|
2018-03-22 09:22:37 +00:00
|
|
|
if (target && (has(target, 'collapsed') || has(target, 'isExpanded'))) {
|
2016-06-16 11:57:32 +00:00
|
|
|
// property is explicitly set so use it
|
2018-01-05 13:32:52 +00:00
|
|
|
targetCollapsed = (
|
2018-03-22 09:22:37 +00:00
|
|
|
has(target, 'collapsed') ? target.collapsed : !target.isExpanded
|
2018-01-05 13:32:52 +00:00
|
|
|
);
|
2016-06-16 11:57:32 +00:00
|
|
|
} else {
|
|
|
|
// keep old state
|
|
|
|
targetCollapsed = oldCollapsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldCollapsed !== targetCollapsed) {
|
|
|
|
element.collapsed = oldCollapsed;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-23 11:59:47 +00:00
|
|
|
|
2017-01-11 14:22:32 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
/**
|
2015-12-10 11:48:40 +00:00
|
|
|
* This module takes care of replacing BPMN elements
|
2015-03-11 15:17:19 +00:00
|
|
|
*/
|
2018-04-02 19:01:53 +00:00
|
|
|
export default function BpmnReplace(
|
2019-04-12 12:36:20 +00:00
|
|
|
bpmnFactory, elementFactory, replace,
|
|
|
|
selection, modeling, eventBus
|
|
|
|
) {
|
2017-01-11 14:22:32 +00:00
|
|
|
|
2017-12-07 22:12:00 +00:00
|
|
|
var helper = new ModelCloneHelper(eventBus, bpmnFactory);
|
2015-03-11 15:17:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares a new business object for the replacement element
|
|
|
|
* and triggers the replace operation.
|
|
|
|
*
|
|
|
|
* @param {djs.model.Base} element
|
|
|
|
* @param {Object} target
|
2015-08-20 15:52:49 +00:00
|
|
|
* @param {Object} [hints]
|
2015-12-10 11:48:40 +00:00
|
|
|
*
|
2015-03-11 15:17:19 +00:00
|
|
|
* @return {djs.model.Base} the newly created element
|
|
|
|
*/
|
2015-08-20 15:52:49 +00:00
|
|
|
function replaceElement(element, target, hints) {
|
|
|
|
|
|
|
|
hints = hints || {};
|
2015-03-11 15:17:19 +00:00
|
|
|
|
|
|
|
var type = target.type,
|
2016-06-16 11:57:32 +00:00
|
|
|
oldBusinessObject = element.businessObject;
|
|
|
|
|
2018-02-14 21:06:22 +00:00
|
|
|
if (isSubProcess(oldBusinessObject)) {
|
2016-06-16 11:57:32 +00:00
|
|
|
if (type === 'bpmn:SubProcess') {
|
|
|
|
if (toggeling(element, target)) {
|
|
|
|
// expanding or collapsing process
|
|
|
|
modeling.toggleCollapse(element);
|
|
|
|
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var newBusinessObject = bpmnFactory.create(type);
|
2015-03-11 15:17:19 +00:00
|
|
|
|
|
|
|
var newElement = {
|
|
|
|
type: type,
|
2016-01-20 10:31:01 +00:00
|
|
|
businessObject: newBusinessObject
|
2015-03-11 15:17:19 +00:00
|
|
|
};
|
|
|
|
|
2017-01-11 14:22:32 +00:00
|
|
|
var elementProps = getProperties(oldBusinessObject.$descriptor),
|
|
|
|
newElementProps = getProperties(newBusinessObject.$descriptor, true),
|
2018-06-27 13:11:34 +00:00
|
|
|
copyProps = intersection(elementProps, newElementProps);
|
2017-01-11 14:22:32 +00:00
|
|
|
|
2017-01-19 15:12:25 +00:00
|
|
|
// initialize special properties defined in target definition
|
|
|
|
assign(newBusinessObject, pick(target, CUSTOM_PROPERTIES));
|
|
|
|
|
2018-06-27 13:11:34 +00:00
|
|
|
var properties = filter(copyProps, function(property) {
|
2017-01-19 15:12:25 +00:00
|
|
|
var propName = property.replace(/bpmn:/, '');
|
|
|
|
|
2018-06-27 13:11:34 +00:00
|
|
|
// copying event definitions, unless we replace
|
|
|
|
if (propName === 'eventDefinitions') {
|
|
|
|
return hasEventDefinition(element, target.eventDefinitionType);
|
2017-01-19 15:12:25 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 13:11:34 +00:00
|
|
|
// retain loop characteristics if the target element
|
|
|
|
// is not an event sub process
|
2017-01-19 15:12:25 +00:00
|
|
|
if (propName === 'loopCharacteristics') {
|
|
|
|
return !isEventSubProcess(newBusinessObject);
|
|
|
|
}
|
|
|
|
|
2018-06-27 13:11:34 +00:00
|
|
|
// so the applied properties from 'target' don't get lost
|
|
|
|
if (property in newBusinessObject) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (propName === 'processRef' && target.isExpanded === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (propName === 'triggeredByEvent') {
|
2017-01-19 15:12:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IGNORED_PROPERTIES.indexOf(propName) === -1;
|
2017-01-11 14:22:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
newBusinessObject = helper.clone(oldBusinessObject, newBusinessObject, properties);
|
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
// initialize custom BPMN extensions
|
2016-01-20 10:31:01 +00:00
|
|
|
if (target.eventDefinitionType) {
|
2018-06-27 13:11:34 +00:00
|
|
|
|
|
|
|
// only initialize with new eventDefinition
|
|
|
|
// if we did not set an event definition yet,
|
|
|
|
// i.e. because we cloned it
|
|
|
|
if (!hasEventDefinition(newBusinessObject, target.eventDefinitionType)) {
|
|
|
|
newElement.eventDefinitionType = target.eventDefinitionType;
|
|
|
|
}
|
2015-03-11 15:17:19 +00:00
|
|
|
}
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2016-04-13 15:13:10 +00:00
|
|
|
if (is(oldBusinessObject, 'bpmn:Activity')) {
|
|
|
|
|
2018-02-14 21:06:22 +00:00
|
|
|
if (isSubProcess(oldBusinessObject)) {
|
2016-06-16 11:57:32 +00:00
|
|
|
// no toggeling, so keep old state
|
|
|
|
newElement.isExpanded = isExpanded(oldBusinessObject);
|
|
|
|
}
|
|
|
|
// else if property is explicitly set, use it
|
2018-03-22 09:22:37 +00:00
|
|
|
else if (target && has(target, 'isExpanded')) {
|
2016-06-16 11:57:32 +00:00
|
|
|
newElement.isExpanded = target.isExpanded;
|
2016-04-13 15:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: need also to respect min/max Size
|
|
|
|
// copy size, from an expanded subprocess to an expanded alternative subprocess
|
|
|
|
// except bpmn:Task, because Task is always expanded
|
2017-02-03 13:58:41 +00:00
|
|
|
if ((isExpanded(oldBusinessObject) && !is(oldBusinessObject, 'bpmn:Task')) && newElement.isExpanded) {
|
2016-04-13 15:13:10 +00:00
|
|
|
newElement.width = element.width;
|
|
|
|
newElement.height = element.height;
|
|
|
|
}
|
2015-06-24 09:43:07 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 15:58:46 +00:00
|
|
|
// remove children if not expanding sub process
|
2018-02-14 21:06:22 +00:00
|
|
|
if (isSubProcess(oldBusinessObject) && !isSubProcess(newBusinessObject)) {
|
2018-02-13 15:58:46 +00:00
|
|
|
hints.moveChildren = false;
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:21:41 +00:00
|
|
|
// transform collapsed/expanded pools
|
|
|
|
if (is(oldBusinessObject, 'bpmn:Participant')) {
|
|
|
|
|
2017-01-19 15:12:25 +00:00
|
|
|
// create expanded pool
|
2016-06-07 06:46:45 +00:00
|
|
|
if (target.isExpanded === true) {
|
|
|
|
newBusinessObject.processRef = bpmnFactory.create('bpmn:Process');
|
|
|
|
} else {
|
2017-01-19 15:12:25 +00:00
|
|
|
// remove children when transforming to collapsed pool
|
2016-06-07 06:46:45 +00:00
|
|
|
hints.moveChildren = false;
|
|
|
|
}
|
2016-03-08 16:21:41 +00:00
|
|
|
|
2019-04-12 12:36:20 +00:00
|
|
|
// apply same width and default height
|
2016-06-07 06:46:45 +00:00
|
|
|
newElement.width = element.width;
|
2019-04-12 12:36:20 +00:00
|
|
|
newElement.height = elementFactory._getDefaultSize(newBusinessObject).height;
|
2016-03-08 16:21:41 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 10:31:01 +00:00
|
|
|
newBusinessObject.name = oldBusinessObject.name;
|
2015-08-04 15:52:43 +00:00
|
|
|
|
2016-01-07 08:03:20 +00:00
|
|
|
// retain default flow's reference between inclusive <-> exclusive gateways and activities
|
2018-02-14 21:06:22 +00:00
|
|
|
if (
|
|
|
|
isAny(oldBusinessObject, [
|
|
|
|
'bpmn:ExclusiveGateway',
|
|
|
|
'bpmn:InclusiveGateway',
|
|
|
|
'bpmn:Activity'
|
|
|
|
]) &&
|
|
|
|
isAny(newBusinessObject, [
|
|
|
|
'bpmn:ExclusiveGateway',
|
|
|
|
'bpmn:InclusiveGateway',
|
|
|
|
'bpmn:Activity'
|
|
|
|
])
|
|
|
|
) {
|
2016-01-20 10:31:01 +00:00
|
|
|
newBusinessObject.default = oldBusinessObject.default;
|
|
|
|
}
|
|
|
|
|
2017-02-03 09:34:16 +00:00
|
|
|
if ('fill' in oldBusinessObject.di || 'stroke' in oldBusinessObject.di) {
|
|
|
|
assign(newElement, { colors: pick(oldBusinessObject.di, [ 'fill', 'stroke' ]) });
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:21:41 +00:00
|
|
|
newElement = replace.replaceElement(element, newElement, hints);
|
2015-03-11 15:31:42 +00:00
|
|
|
|
2015-08-20 15:52:49 +00:00
|
|
|
if (hints.select !== false) {
|
|
|
|
selection.select(newElement);
|
|
|
|
}
|
2015-03-11 15:31:42 +00:00
|
|
|
|
|
|
|
return newElement;
|
2015-03-11 15:17:19 +00:00
|
|
|
}
|
2015-01-19 11:13:39 +00:00
|
|
|
|
2015-03-11 15:17:19 +00:00
|
|
|
this.replaceElement = replaceElement;
|
|
|
|
}
|
|
|
|
|
2018-02-14 21:06:22 +00:00
|
|
|
BpmnReplace.$inject = [
|
|
|
|
'bpmnFactory',
|
2019-04-12 12:36:20 +00:00
|
|
|
'elementFactory',
|
2018-02-14 21:06:22 +00:00
|
|
|
'replace',
|
|
|
|
'selection',
|
|
|
|
'modeling',
|
|
|
|
'eventBus'
|
|
|
|
];
|
2015-03-11 15:17:19 +00:00
|
|
|
|
2018-02-14 21:06:22 +00:00
|
|
|
|
|
|
|
function isSubProcess(bo) {
|
|
|
|
return is(bo, 'bpmn:SubProcess');
|
2018-06-27 13:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function hasEventDefinition(element, type) {
|
|
|
|
|
|
|
|
var bo = getBusinessObject(element);
|
|
|
|
|
|
|
|
return type && bo.get('eventDefinitions').some(function(definition) {
|
|
|
|
return is(definition, type);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute intersection between two arrays.
|
|
|
|
*/
|
|
|
|
function intersection(a1, a2) {
|
|
|
|
return a1.filter(function(el) {
|
|
|
|
return a2.indexOf(el) !== -1;
|
|
|
|
});
|
2019-04-12 12:36:20 +00:00
|
|
|
}
|