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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | 1× 1189× 1189× 1189× 1189× 398× 398× 398× 6189× 1189× 6259× 6259× 6259× 6259× 1092× 5167× 70× 1189× 6188× 6188× 1× 1× 1× 399× 399× 1× 399× 399× 399× 1× 398× 398× 398× 6189× 740× 6189× 6189× 6189× 2× 6187× 272× 398× 1× 6269× 6269× 6269× 1165× 5104× 16× 10× 16× 5088× 5088× 1697× 3391× 66× 80× 80× 78× 78× 2× 3325× 109× 109× 109× 109× 3216× 535× 10422× 223× 10199× 6188× | import { find, forEach, isArray, isDefined, isObject, matchPattern, reduce, has, sortBy } from 'min-dash'; var DISALLOWED_PROPERTIES = [ 'artifacts', 'dataInputAssociations', 'dataOutputAssociations', 'default', 'flowElements', 'lanes', 'incoming', 'outgoing' ]; /** * @typedef {Function} <moddleCopy.canCopyProperties> listener * * @param {Object} context * @param {Array<string>} context.propertyNames * @param {ModdleElement} context.sourceElement * @param {ModdleElement} context.targetElement * * @returns {Array<string>|boolean} - Return properties to be copied or false to disallow * copying. */ /** * @typedef {Function} <moddleCopy.canCopyProperty> listener * * @param {Object} context * @param {ModdleElement} context.parent * @param {*} context.property * @param {string} context.propertyName * * @returns {*|boolean} - Return copied property or false to disallow * copying. */ /** * @typedef {Function} <moddleCopy.canSetCopiedProperty> listener * * @param {Object} context * @param {ModdleElement} context.parent * @param {*} context.property * @param {string} context.propertyName * * @returns {boolean} - Return false to disallow * setting copied property. */ /** * Utility for copying model properties from source element to target element. * * @param {EventBus} eventBus * @param {BpmnFactory} bpmnFactory * @param {BpmnModdle} moddle */ export default function ModdleCopy(eventBus, bpmnFactory, moddle) { this._bpmnFactory = bpmnFactory; this._eventBus = eventBus; this._moddle = moddle; // copy extension elements last eventBus.on('moddleCopy.canCopyProperties', function(context) { var propertyNames = context.propertyNames; Iif (!propertyNames || !propertyNames.length) { return; } return sortBy(propertyNames, function(propertyName) { return propertyName === 'extensionElements'; }); }); // default check wether property can be copied eventBus.on('moddleCopy.canCopyProperty', function(context) { var parent = context.parent, parentDescriptor = isObject(parent) && parent.$descriptor, propertyName = context.propertyName; if (propertyName && DISALLOWED_PROPERTIES.indexOf(propertyName) !== -1) { // disallow copying property return false; } if (propertyName && parentDescriptor && !find(parentDescriptor.properties, matchPattern({ name: propertyName }))) { // disallow copying property return false; } }); // do NOT allow to copy empty extension elements eventBus.on('moddleCopy.canSetCopiedProperty', function(context) { var property = context.property; if (is(property, 'bpmn:ExtensionElements') && (!property.values || !property.values.length)) { // disallow setting copied property return false; } }); } ModdleCopy.$inject = [ 'eventBus', 'bpmnFactory', 'moddle' ]; /** * Copy model properties of source element to target element. * * @param {ModdleElement} sourceElement * @param {ModdleElement} targetElement * @param {Array<string>} [propertyNames] * * @param {ModdleElement} */ ModdleCopy.prototype.copyElement = function(sourceElement, targetElement, propertyNames) { var self = this; if (propertyNames && !isArray(propertyNames)) { propertyNames = [ propertyNames ]; } propertyNames = propertyNames || getPropertyNames(sourceElement.$descriptor); var canCopyProperties = this._eventBus.fire('moddleCopy.canCopyProperties', { propertyNames: propertyNames, sourceElement: sourceElement, targetElement: targetElement }); if (canCopyProperties === false) { return targetElement; } Eif (isArray(canCopyProperties)) { propertyNames = canCopyProperties; } // copy properties forEach(propertyNames, function(propertyName) { var sourceProperty; if (has(sourceElement, propertyName)) { sourceProperty = sourceElement.get(propertyName); } var copiedProperty = self.copyProperty(sourceProperty, targetElement, propertyName); var canSetProperty = self._eventBus.fire('moddleCopy.canSetCopiedProperty', { parent: parent, property: copiedProperty, propertyName: propertyName }); if (canSetProperty === false) { return; } if (isDefined(copiedProperty)) { targetElement.set(propertyName, copiedProperty); } }); return targetElement; }; /** * Copy model property. * * @param {*} property * @param {ModdleElement} parent * @param {string} propertyName * * @returns {*} */ ModdleCopy.prototype.copyProperty = function(property, parent, propertyName) { var self = this; // allow others to copy property var copiedProperty = this._eventBus.fire('moddleCopy.canCopyProperty', { parent: parent, property: property, propertyName: propertyName }); // return if copying is NOT allowed if (copiedProperty === false) { return; } if (copiedProperty) { if (isObject(copiedProperty) && copiedProperty.$type && !copiedProperty.$parent) { copiedProperty.$parent = parent; } return copiedProperty; } var propertyDescriptor = this._moddle.getPropertyDescriptor(parent, propertyName); // do NOT copy Ids and references if (propertyDescriptor.isId || propertyDescriptor.isReference) { return; } // copy arrays if (isArray(property)) { return reduce(property, function(childProperties, childProperty) { // recursion copiedProperty = self.copyProperty(childProperty, parent, propertyName); // copying might NOT be allowed if (copiedProperty) { copiedProperty.$parent = parent; return childProperties.concat(copiedProperty); } return childProperties; }, []); } // copy model elements if (isObject(property) && property.$type) { copiedProperty = self._bpmnFactory.create(property.$type); copiedProperty.$parent = parent; // recursion copiedProperty = self.copyElement(property, copiedProperty); return copiedProperty; } // copy primitive properties return property; }; // helpers ////////// export function getPropertyNames(descriptor, keepDefaultProperties) { return reduce(descriptor.properties, function(properties, property) { if (keepDefaultProperties && property.default) { return properties; } return properties.concat(property.name); }, []); } function is(element, type) { return element && (typeof element.$instanceOf === 'function') && element.$instanceOf(type); } |