2017-01-11 07:12:34 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-03-14 20:43:53 +00:00
|
|
|
var forEach = require('min-dash').forEach,
|
|
|
|
filter = require('min-dash').filter,
|
2018-03-22 09:22:37 +00:00
|
|
|
some = require('min-dash').some,
|
2018-03-14 20:43:53 +00:00
|
|
|
sortBy = require('min-dash').sortBy,
|
|
|
|
isArray = require('min-dash').isArray;
|
2017-01-11 14:22:32 +00:00
|
|
|
|
2017-01-19 15:13:31 +00:00
|
|
|
var IGNORED_PROPERTIES = require('./ModelCloneUtils').IGNORED_PROPERTIES;
|
2017-01-11 14:22:32 +00:00
|
|
|
|
2017-01-11 14:22:32 +00:00
|
|
|
function isAllowedIn(extProp, type) {
|
|
|
|
var allowedIn = extProp.meta.allowedIn;
|
|
|
|
|
2018-03-22 11:43:36 +00:00
|
|
|
// '*' is a wildcard, which means any element is allowed to use this property
|
2017-01-11 14:22:32 +00:00
|
|
|
if (allowedIn.length === 1 && allowedIn[0] === '*') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return allowedIn.indexOf(type) !== -1;
|
|
|
|
}
|
|
|
|
|
2017-01-19 15:13:31 +00:00
|
|
|
function isType(element, types) {
|
2018-03-22 09:22:37 +00:00
|
|
|
return some(types, function(type) {
|
2017-01-19 15:13:31 +00:00
|
|
|
return typeof element === type;
|
|
|
|
});
|
2017-01-11 14:22:32 +00:00
|
|
|
}
|
2017-01-11 07:12:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A bpmn properties cloning interface
|
|
|
|
*
|
|
|
|
*/
|
2017-12-07 22:12:00 +00:00
|
|
|
function ModelCloneHelper(eventBus, bpmnFactory) {
|
2017-01-23 13:12:43 +00:00
|
|
|
this._eventBus = eventBus;
|
2017-12-07 22:12:00 +00:00
|
|
|
this._bpmnFactory = bpmnFactory;
|
2017-01-23 13:12:43 +00:00
|
|
|
}
|
2017-01-11 07:12:34 +00:00
|
|
|
|
2017-01-11 14:22:32 +00:00
|
|
|
module.exports = ModelCloneHelper;
|
2017-01-11 07:12:34 +00:00
|
|
|
|
|
|
|
|
2017-02-02 10:28:40 +00:00
|
|
|
ModelCloneHelper.prototype.clone = function(refElement, newElement, properties) {
|
2018-03-22 09:22:37 +00:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2017-02-01 09:42:49 +00:00
|
|
|
// hasNestedProperty: property allows us to avoid ending up with empty (xml) tags
|
|
|
|
// f.ex: if extensionElements.values is empty, don't set it
|
2017-02-02 10:28:40 +00:00
|
|
|
var context = {
|
2017-02-01 09:42:49 +00:00
|
|
|
newElement: newElement,
|
|
|
|
hasNestedProperty: false
|
2017-02-02 10:28:40 +00:00
|
|
|
};
|
2017-01-11 07:12:34 +00:00
|
|
|
|
2017-01-23 13:12:43 +00:00
|
|
|
// we want the extensionElements to be cloned last
|
|
|
|
// so that they can check certain properties
|
2018-03-14 20:43:53 +00:00
|
|
|
properties = sortBy(properties, function(prop) {
|
2017-01-23 13:12:43 +00:00
|
|
|
return prop === 'bpmn:extensionElements';
|
|
|
|
});
|
|
|
|
|
2017-01-11 07:12:34 +00:00
|
|
|
forEach(properties, function(propName) {
|
2017-02-02 10:28:40 +00:00
|
|
|
var refElementProp = refElement.get(propName),
|
2017-01-19 15:13:31 +00:00
|
|
|
newElementProp = newElement.get(propName),
|
|
|
|
propDescriptor = newElement.$model.getPropertyDescriptor(newElement, propName),
|
2017-02-01 09:42:49 +00:00
|
|
|
newProperty, name;
|
2017-01-19 15:13:31 +00:00
|
|
|
|
|
|
|
// we're not interested in cloning:
|
|
|
|
// - same values from simple types
|
|
|
|
// - cloning id's
|
|
|
|
// - cloning reference elements
|
2017-12-07 22:12:00 +00:00
|
|
|
if (newElementProp === refElementProp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (propDescriptor && (propDescriptor.isId || propDescriptor.isReference)) {
|
2017-01-11 07:12:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-19 15:13:31 +00:00
|
|
|
// if the property is of type 'boolean', 'string', 'number' or 'null', just set it
|
2017-02-02 10:28:40 +00:00
|
|
|
if (isType(refElementProp, [ 'boolean', 'string', 'number' ]) || refElementProp === null) {
|
|
|
|
newElement.set(propName, refElementProp);
|
2017-01-11 07:12:34 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-02 10:28:40 +00:00
|
|
|
if (isArray(refElementProp)) {
|
2017-01-11 07:12:34 +00:00
|
|
|
|
2017-02-02 10:28:40 +00:00
|
|
|
forEach(refElementProp, function(extElement) {
|
|
|
|
var newProp;
|
|
|
|
|
|
|
|
context.refTopLevelProperty = extElement;
|
|
|
|
|
2018-03-22 09:22:37 +00:00
|
|
|
newProp = self._deepClone(extElement, context);
|
2017-01-11 07:12:34 +00:00
|
|
|
|
2017-02-01 09:42:49 +00:00
|
|
|
if (context.hasNestedProperty) {
|
|
|
|
newProp.$parent = newElement;
|
2017-01-11 14:22:32 +00:00
|
|
|
|
2017-02-01 09:42:49 +00:00
|
|
|
newElementProp.push(newProp);
|
|
|
|
}
|
|
|
|
|
|
|
|
context.hasNestedProperty = false;
|
2018-03-22 09:22:37 +00:00
|
|
|
});
|
2017-02-01 09:42:49 +00:00
|
|
|
|
2017-01-19 15:13:31 +00:00
|
|
|
} else {
|
|
|
|
name = propName.replace(/bpmn:/, '');
|
|
|
|
|
2017-02-02 10:28:40 +00:00
|
|
|
context.refTopLevelProperty = refElementProp;
|
|
|
|
|
2018-03-22 09:22:37 +00:00
|
|
|
newProperty = self._deepClone(refElementProp, context);
|
2017-02-01 09:42:49 +00:00
|
|
|
|
|
|
|
if (context.hasNestedProperty) {
|
|
|
|
newElement[name] = newProperty;
|
|
|
|
}
|
|
|
|
|
|
|
|
context.hasNestedProperty = false;
|
2017-01-11 07:12:34 +00:00
|
|
|
}
|
2018-03-22 09:22:37 +00:00
|
|
|
});
|
2017-01-11 07:12:34 +00:00
|
|
|
|
|
|
|
return newElement;
|
|
|
|
};
|
|
|
|
|
2017-02-02 10:28:40 +00:00
|
|
|
ModelCloneHelper.prototype._deepClone = function _deepClone(propertyElement, context) {
|
2018-03-22 09:22:37 +00:00
|
|
|
var self = this;
|
|
|
|
|
2017-01-23 13:12:43 +00:00
|
|
|
var eventBus = this._eventBus;
|
2017-12-07 22:12:00 +00:00
|
|
|
var bpmnFactory = this._bpmnFactory;
|
2017-01-23 13:12:43 +00:00
|
|
|
|
2017-12-07 22:12:00 +00:00
|
|
|
var newProp = bpmnFactory.create(propertyElement.$type);
|
2017-01-19 15:13:31 +00:00
|
|
|
|
2017-02-02 10:28:40 +00:00
|
|
|
var properties = filter(Object.keys(propertyElement), function(prop) {
|
2017-01-19 15:13:31 +00:00
|
|
|
var descriptor = newProp.$model.getPropertyDescriptor(newProp, prop);
|
|
|
|
|
|
|
|
if (descriptor && (descriptor.isId || descriptor.isReference)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we need to make sure we don't clone certain properties
|
|
|
|
// which we cannot easily know if they hold references or not
|
|
|
|
if (IGNORED_PROPERTIES.indexOf(prop) !== -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we don't copy the type
|
|
|
|
return prop !== '$type';
|
|
|
|
});
|
2017-01-11 07:12:34 +00:00
|
|
|
|
2017-02-01 09:42:49 +00:00
|
|
|
if (!properties.length) {
|
|
|
|
context.hasNestedProperty = true;
|
|
|
|
}
|
|
|
|
|
2017-01-11 07:12:34 +00:00
|
|
|
forEach(properties, function(propName) {
|
2017-02-02 10:28:40 +00:00
|
|
|
// check if the propertyElement has this property defined
|
|
|
|
if (propertyElement[propName] !== undefined &&
|
|
|
|
(propertyElement[propName].$type || isArray(propertyElement[propName]))) {
|
2017-01-11 07:12:34 +00:00
|
|
|
|
2017-02-02 10:28:40 +00:00
|
|
|
if (isArray(propertyElement[propName])) {
|
2017-02-01 09:42:49 +00:00
|
|
|
newProp[propName] = [];
|
2017-01-11 07:12:34 +00:00
|
|
|
|
2017-02-02 10:28:40 +00:00
|
|
|
forEach(propertyElement[propName], function(property) {
|
|
|
|
var extProp = propertyElement.$model.getTypeDescriptor(property.$type),
|
2017-01-19 15:13:31 +00:00
|
|
|
newDeepProp;
|
|
|
|
|
|
|
|
// we're not going to copy undefined types
|
|
|
|
if (!extProp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-23 13:12:43 +00:00
|
|
|
var canClone = eventBus.fire('property.clone', {
|
2017-02-02 10:28:40 +00:00
|
|
|
newElement: context.newElement,
|
|
|
|
refTopLevelProperty: context.refTopLevelProperty,
|
2017-01-23 13:12:43 +00:00
|
|
|
propertyDescriptor: extProp
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!canClone) {
|
|
|
|
// if can clone is 'undefined' or 'false'
|
|
|
|
// check for the meta information if it is allowed
|
2017-02-02 10:28:40 +00:00
|
|
|
if (propertyElement.$type === 'bpmn:ExtensionElements' &&
|
2017-01-23 13:12:43 +00:00
|
|
|
extProp.meta && extProp.meta.allowedIn &&
|
2017-02-02 10:28:40 +00:00
|
|
|
!isAllowedIn(extProp, context.newElement.$type)) {
|
2017-01-23 13:12:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-19 15:13:31 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 09:22:37 +00:00
|
|
|
newDeepProp = self._deepClone(property, context);
|
2017-01-11 14:22:32 +00:00
|
|
|
|
|
|
|
newDeepProp.$parent = newProp;
|
|
|
|
|
2017-02-01 09:42:49 +00:00
|
|
|
if (!newProp[propName]) {
|
|
|
|
newProp[propName] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
context.hasNestedProperty = true;
|
|
|
|
|
2017-01-11 14:22:32 +00:00
|
|
|
newProp[propName].push(newDeepProp);
|
2018-03-22 09:22:37 +00:00
|
|
|
});
|
2017-01-11 07:12:34 +00:00
|
|
|
|
2017-02-02 10:28:40 +00:00
|
|
|
} else if (propertyElement[propName].$type) {
|
2018-03-22 09:22:37 +00:00
|
|
|
newProp[propName] = self._deepClone(propertyElement[propName], context);
|
2017-01-11 14:22:32 +00:00
|
|
|
|
2017-02-01 09:42:49 +00:00
|
|
|
if (newProp[propName]) {
|
|
|
|
context.hasNestedProperty = true;
|
|
|
|
|
|
|
|
newProp[propName].$parent = newProp;
|
|
|
|
}
|
2017-01-11 07:12:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-02-01 09:42:49 +00:00
|
|
|
context.hasNestedProperty = true;
|
|
|
|
|
2017-01-11 07:12:34 +00:00
|
|
|
// just assign directly if it's a value
|
2017-02-02 10:28:40 +00:00
|
|
|
newProp[propName] = propertyElement[propName];
|
2017-01-11 07:12:34 +00:00
|
|
|
}
|
2018-03-22 09:22:37 +00:00
|
|
|
});
|
2017-01-11 07:12:34 +00:00
|
|
|
|
|
|
|
return newProp;
|
|
|
|
};
|