2015-01-19 11:13:39 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-12-10 11:48:40 +00:00
|
|
|
var pick = require('lodash/object/pick'),
|
2016-06-16 11:57:32 +00:00
|
|
|
assign = require('lodash/object/assign'),
|
2017-01-11 14:22:32 +00:00
|
|
|
intersection = require('lodash/array/intersection'),
|
|
|
|
filter = require('lodash/collection/filter'),
|
2016-06-16 11:57:32 +00:00
|
|
|
has = require('lodash/object/has');
|
2015-03-11 15:17:19 +00:00
|
|
|
|
2015-06-12 13:46:28 +00:00
|
|
|
var is = require('../../util/ModelUtil').is,
|
2015-08-04 15:52:43 +00:00
|
|
|
isExpanded = require('../../util/DiUtil').isExpanded,
|
2017-01-11 14:22:32 +00:00
|
|
|
isEventSubProcess = require('../../util/DiUtil').isEventSubProcess,
|
2017-01-19 15:12:25 +00:00
|
|
|
ModelCloneUtils = require('../../util/model/ModelCloneUtils'),
|
|
|
|
getProperties = ModelCloneUtils.getProperties;
|
|
|
|
|
|
|
|
var IGNORED_PROPERTIES = ModelCloneUtils.IGNORED_PROPERTIES;
|
2017-01-11 14:22:32 +00:00
|
|
|
|
|
|
|
var ModelCloneHelper = require('../../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) {
|
|
|
|
|
|
|
|
var oldCollapsed = has(element, 'collapsed') ?
|
|
|
|
element.collapsed : !isExpanded(element);
|
|
|
|
|
|
|
|
var targetCollapsed;
|
|
|
|
|
|
|
|
if (has(target, 'collapsed') || has(target, 'isExpanded')) {
|
|
|
|
// property is explicitly set so use it
|
|
|
|
targetCollapsed = has(target, 'collapsed') ?
|
|
|
|
target.collapsed : !target.isExpanded;
|
|
|
|
} 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
|
|
|
*/
|
2017-01-23 13:12:43 +00:00
|
|
|
function BpmnReplace(bpmnFactory, 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;
|
|
|
|
|
|
|
|
if (is(oldBusinessObject, 'bpmn:SubProcess')) {
|
|
|
|
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),
|
|
|
|
properties = intersection(elementProps, newElementProps);
|
|
|
|
|
2017-01-19 15:12:25 +00:00
|
|
|
// initialize special properties defined in target definition
|
|
|
|
assign(newBusinessObject, pick(target, CUSTOM_PROPERTIES));
|
|
|
|
|
2017-01-11 14:22:32 +00:00
|
|
|
properties = filter(properties, function(property) {
|
2017-01-19 15:12:25 +00:00
|
|
|
var propName = property.replace(/bpmn:/, '');
|
|
|
|
|
|
|
|
// so the applied properties from 'target' don't get lost
|
|
|
|
if (newBusinessObject[property] !== undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// retain loop characteristics if the target element is not an event sub process
|
|
|
|
if (propName === 'loopCharacteristics') {
|
|
|
|
return !isEventSubProcess(newBusinessObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((propName === 'processRef' && target.isExpanded === false) ||
|
|
|
|
propName === 'triggeredByEvent' ||
|
|
|
|
propName === 'eventDefinitions') {
|
|
|
|
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) {
|
|
|
|
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')) {
|
|
|
|
|
2016-06-16 11:57:32 +00:00
|
|
|
if (is(oldBusinessObject, 'bpmn:SubProcess')) {
|
|
|
|
// no toggeling, so keep old state
|
|
|
|
newElement.isExpanded = isExpanded(oldBusinessObject);
|
|
|
|
}
|
|
|
|
// else if property is explicitly set, use it
|
|
|
|
else if (has(target, 'isExpanded')) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// apply same size
|
2016-06-07 06:46:45 +00:00
|
|
|
newElement.width = element.width;
|
|
|
|
newElement.height = element.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
|
|
|
|
if ((is(oldBusinessObject, 'bpmn:ExclusiveGateway') || is(oldBusinessObject, 'bpmn:InclusiveGateway') ||
|
|
|
|
is(oldBusinessObject, 'bpmn:Activity')) &&
|
2016-01-20 10:31:01 +00:00
|
|
|
(is(newBusinessObject, 'bpmn:ExclusiveGateway') || is(newBusinessObject, 'bpmn:InclusiveGateway') ||
|
|
|
|
is(newBusinessObject, 'bpmn:Activity')))
|
2015-10-06 08:50:47 +00:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-23 13:12:43 +00:00
|
|
|
BpmnReplace.$inject = [ 'bpmnFactory', 'replace', 'selection', 'modeling', 'eventBus' ];
|
2015-03-11 15:17:19 +00:00
|
|
|
|
2015-06-12 13:46:28 +00:00
|
|
|
module.exports = BpmnReplace;
|