2017-01-11 07:12:34 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var forEach = require('lodash/collection/forEach'),
|
|
|
|
filter = require('lodash/collection/filter'),
|
2017-01-19 15:13:31 +00:00
|
|
|
any = require('lodash/collection/any'),
|
2017-01-23 13:12:43 +00:00
|
|
|
sort = require('lodash/collection/sortBy'),
|
2017-01-19 15:13:31 +00:00
|
|
|
isArray = require('lodash/lang/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;
|
|
|
|
|
|
|
|
// '*' is a wildcard, which means any element is allowed to use this property
|
|
|
|
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) {
|
|
|
|
return any(types, function(type) {
|
|
|
|
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-01-23 13:12:43 +00:00
|
|
|
function ModelCloneHelper(eventBus) {
|
|
|
|
this._eventBus = eventBus;
|
|
|
|
}
|
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) {
|
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
|
|
|
|
properties = sort(properties, function(prop) {
|
|
|
|
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-02-02 10:28:40 +00:00
|
|
|
if (newElementProp === refElementProp ||
|
2017-01-19 15:13:31 +00:00
|
|
|
(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;
|
|
|
|
|
|
|
|
newProp = this._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;
|
2017-01-11 07:12:34 +00:00
|
|
|
}, this);
|
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;
|
|
|
|
|
2017-02-01 09:42:49 +00:00
|
|
|
newProperty = this._deepClone(refElementProp, context);
|
|
|
|
|
|
|
|
if (context.hasNestedProperty) {
|
|
|
|
newElement[name] = newProperty;
|
|
|
|
}
|
|
|
|
|
|
|
|
context.hasNestedProperty = false;
|
2017-01-11 07:12:34 +00:00
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
return newElement;
|
|
|
|
};
|
|
|
|
|
2017-02-02 10:28:40 +00:00
|
|
|
ModelCloneHelper.prototype._deepClone = function _deepClone(propertyElement, context) {
|
2017-01-23 13:12:43 +00:00
|
|
|
var eventBus = this._eventBus;
|
|
|
|
|
2017-02-02 10:28:40 +00:00
|
|
|
var newProp = propertyElement.$model.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
|
|
|
}
|
|
|
|
|
2017-02-02 10:28:40 +00:00
|
|
|
newDeepProp = this._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);
|
2017-01-11 07:12:34 +00:00
|
|
|
}, this);
|
|
|
|
|
2017-02-02 10:28:40 +00:00
|
|
|
} else if (propertyElement[propName].$type) {
|
|
|
|
newProp[propName] = this._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
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
return newProp;
|
|
|
|
};
|