1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | 19× 19× 3× 16× 924× 2672× 1252× 1252× 1× 306× 306× 306× 4202× 306× 4202× 4202× 4202× 4202× 1652× 2550× 1626× 924× 105× 105× 819× 793× 41× 41× 41× 41× 41× 41× 26× 26× 26× 26× 25× 25× 26× 306× 1× 115× 115× 115× 115× 115× 227× 227× 4× 223× 2× 221× 115× 31× 115× 106× 34× 25× 25× 41× 41× 41× 41× 36× 2× 39× 39× 39× 39× 39× 9× 9× 9× 9× 9× 72× 72× 115× | import { forEach, filter, some, sortBy, isArray } from 'min-dash'; import { IGNORED_PROPERTIES } from './ModelCloneUtils'; 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; } function isType(element, types) { return some(types, function(type) { return typeof element === type; }); } /** * A bpmn properties cloning interface * */ export default function ModelCloneHelper(eventBus, bpmnFactory) { this._eventBus = eventBus; this._bpmnFactory = bpmnFactory; } ModelCloneHelper.prototype.clone = function(refElement, newElement, properties) { var self = this; // hasNestedProperty: property allows us to avoid ending up with empty (xml) tags // f.ex: if extensionElements.values is empty, don't set it var context = { newElement: newElement, hasNestedProperty: false }; // we want the extensionElements to be cloned last // so that they can check certain properties properties = sortBy(properties, function(prop) { return prop === 'bpmn:extensionElements'; }); forEach(properties, function(propName) { var refElementProp = refElement.get(propName), newElementProp = newElement.get(propName), propDescriptor = newElement.$model.getPropertyDescriptor(newElement, propName), newProperty, name; // we're not interested in cloning: // - same values from simple types // - cloning id's // - cloning reference elements if (newElementProp === refElementProp) { return; } if (propDescriptor && (propDescriptor.isId || propDescriptor.isReference)) { return; } // if the property is of type 'boolean', 'string', 'number' or 'null', just set it if (isType(refElementProp, [ 'boolean', 'string', 'number' ]) || refElementProp === null) { newElement.set(propName, refElementProp); return; } if (isArray(refElementProp)) { forEach(refElementProp, function(extElement) { var newProp; context.refTopLevelProperty = extElement; newProp = self._deepClone(extElement, context); Eif (context.hasNestedProperty) { newProp.$parent = newElement; newElementProp.push(newProp); } context.hasNestedProperty = false; }); } else { name = propName.replace(/bpmn:/, ''); context.refTopLevelProperty = refElementProp; newProperty = self._deepClone(refElementProp, context); if (context.hasNestedProperty) { newProperty.$parent = newElement; newElement.set(name, newProperty); } context.hasNestedProperty = false; } }); return newElement; }; ModelCloneHelper.prototype._deepClone = function _deepClone(propertyElement, context) { var self = this; var eventBus = this._eventBus; var bpmnFactory = this._bpmnFactory; var newProp = bpmnFactory.create(propertyElement.$type); var properties = filter(Object.keys(propertyElement), function(prop) { 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'; }); if (!properties.length) { context.hasNestedProperty = true; } forEach(properties, function(propName) { // check if the propertyElement has this property defined if (propertyElement[propName] !== undefined && (propertyElement[propName].$type || isArray(propertyElement[propName]))) { if (isArray(propertyElement[propName])) { newProp[propName] = []; forEach(propertyElement[propName], function(property) { var extProp = propertyElement.$model.getTypeDescriptor(property.$type), newDeepProp; // we're not going to copy undefined types Iif (!extProp) { return; } var canClone = eventBus.fire('property.clone', { newElement: context.newElement, refTopLevelProperty: context.refTopLevelProperty, propertyDescriptor: extProp }); if (!canClone) { // if can clone is 'undefined' or 'false' // check for the meta information if it is allowed if (propertyElement.$type === 'bpmn:ExtensionElements' && extProp.meta && extProp.meta.allowedIn && !isAllowedIn(extProp, context.newElement.$type)) { return false; } } newDeepProp = self._deepClone(property, context); newDeepProp.$parent = newProp; Iif (!newProp[propName]) { newProp[propName] = []; } context.hasNestedProperty = true; newProp[propName].push(newDeepProp); }); } else Eif (propertyElement[propName].$type) { newProp[propName] = self._deepClone(propertyElement[propName], context); Eif (newProp[propName]) { context.hasNestedProperty = true; newProp[propName].$parent = newProp; } } } else { context.hasNestedProperty = true; // just assign directly if it's a value newProp[propName] = propertyElement[propName]; } }); return newProp; }; |