X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=skyquake%2Fplugins%2Fcomposer%2Fsrc%2Fsrc%2Flibraries%2Fmodel%2FDescriptorModelMetaFactory.js;h=5488e7767e1256d5a59d3936ba3ebbfe05343a2b;hb=fc0265f43d6ca5d7d7b0240e0bd0c6f6f313d6f8;hp=d3ef200fb041000c0d2b1d1353755a674224a3c4;hpb=25ceeb3e3d86705229fad71f9b6738b97ead7e36;p=osm%2FUI.git diff --git a/skyquake/plugins/composer/src/src/libraries/model/DescriptorModelMetaFactory.js b/skyquake/plugins/composer/src/src/libraries/model/DescriptorModelMetaFactory.js index d3ef200fb..5488e7767 100644 --- a/skyquake/plugins/composer/src/src/libraries/model/DescriptorModelMetaFactory.js +++ b/skyquake/plugins/composer/src/src/libraries/model/DescriptorModelMetaFactory.js @@ -6,7 +6,7 @@ 'use strict'; -import _ from 'lodash' +import _cloneDeep from 'lodash/cloneDeep' import utils from './../utils' import DescriptorModelMetaProperty from './DescriptorModelMetaProperty' import CommonUtils from 'utils/utils'; @@ -64,15 +64,23 @@ export default { return cachedDescriptorModelMetaRequest; }, - createModelInstanceForType(typeOrPath) { + /** + * Create a new instance of the indicated property and, if relevent, use the given + * unique name for the instance's key (see generateItemUniqueName) + * + * @param {Object | string} typeOrPath a property definition object or a path to a property + * @param [{string}] uniqueName optional + * @returns + */ + createModelInstanceForType(typeOrPath, uniqueName) { const modelMeta = this.getModelMetaForType(typeOrPath); - return DescriptorModelMetaProperty.createModelInstance(modelMeta); + return DescriptorModelMetaProperty.createModelInstance(modelMeta, uniqueName); }, getModelMetaForType(typeOrPath, filterProperties = () => true) { // resolve paths like 'nsd' or 'vnfd.vdu' or 'nsd.constituent-vnfd' const found = utils.resolvePath(modelMetaByPropertyNameMap, getPathForType(typeOrPath)); if (found) { - const uiState = _.cloneDeep(found[':meta']); + const uiState = _cloneDeep(found[':meta']); uiState.properties = uiState.properties.filter(filterProperties); return uiState; } @@ -98,5 +106,46 @@ export default { return result; } console.warn('no model uiState found for type', typeOrPath); + }, + /** + * For a list with a single valued key that is of type string, generate a unique name + * for a new entry to be added to the indicated list. This name will use the provided + * prefix (or the list's name) followed by a number. The number will be based on the + * current length of the array but will insure there is no collision with an existing + * name. + * + * @param {Array} list the list model data + * @param {prooerty} property the schema definition of the list + * @param [{any} prefix] the perferred prefix for the name. If not provide property.name + * will be used. + * @returns {string} + */ + generateItemUniqueName (list, property, prefix) { + if ( property.type !== 'list' + || property.key.length !== 1 + || property.properties.find(prop => prop.name === property.key[0])['data-type'] !== 'string') { + // only support list with a single key of type string + return null; + } + if (!prefix) { + prefix = property.name; + } + let key = property.key[0]; + let suffix = list ? list.length + 1 : 1 + let keyValue = prefix + '-' + suffix; + function makeUniqueName() { + if (list) { + for (let i = 0; i < list.length; i = ++i) { + if (list[i][key] === keyValue) { + keyValue = keyValue + '-' + (i+1); + makeUniqueName(); // not worried about recursing too deep (chances ??) + break; + } + } + } + } + makeUniqueName(); + return keyValue; } + }