X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FUI.git;a=blobdiff_plain;f=skyquake%2Fplugins%2Fcomposer%2Fsrc%2Fsrc%2Flibraries%2Fmodel%2FDescriptorModelMetaProperty.js;h=e064457a69c001ce126cb0b9b76b5cb903179494;hp=8fe51d9aca62838bf4a107cb06b0f65c6b4ba8bc;hb=7c0d1adb5981f247ae7c19e0ab0a000a607de191;hpb=73b4ff94fca62a769a0bc13971a05cd18e177b33 diff --git a/skyquake/plugins/composer/src/src/libraries/model/DescriptorModelMetaProperty.js b/skyquake/plugins/composer/src/src/libraries/model/DescriptorModelMetaProperty.js index 8fe51d9ac..e064457a6 100644 --- a/skyquake/plugins/composer/src/src/libraries/model/DescriptorModelMetaProperty.js +++ b/skyquake/plugins/composer/src/src/libraries/model/DescriptorModelMetaProperty.js @@ -21,9 +21,8 @@ * This class provides utility methods for interrogating an instance of model uiState object. */ -'use strict'; - -import _ from 'lodash' +import _includes from 'lodash/includes' +import _isArray from 'lodash/isArray' import guid from './../guid' import changeCase from 'change-case' import InstanceCounter from './../InstanceCounter' @@ -35,6 +34,9 @@ export default { isBoolean(property = {}) { return (typeof(property['data-type']) == 'string') && (property['data-type'].toLowerCase() == 'boolean') }, + isLeafEmpty(property = {}) { + return (typeof(property['data-type']) == 'string') && (property['data-type'].toLowerCase() == 'empty') + }, isLeaf(property = {}) { return /leaf|choice/.test(property.type); }, @@ -69,7 +71,7 @@ export default { return !/^(leaf|leaf_list)$/.test(property.type); }, isSimpleList(property = {}) { - return _.includes(DescriptorModelFields.simpleList, property.name); + return _includes(DescriptorModelFields.simpleList, property.name); }, isPrimativeDataType(property = {}) { const Property = this; @@ -146,22 +148,36 @@ export default { } return /uuid/.test(property['data-type']); }, - createModelInstance(property) { + /** + * 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} typeOrPath - property definition + * @param {any} uniqueName + * @returns + */ + createModelInstance(property, uniqueName) { const Property = this; const defaultValue = Property.defaultValue.bind(this); - function createModel(uiState, parentMeta) { + function createModel(uiState, parentMeta, uniqueName) { const model = {}; if (Property.isLeaf(uiState)) { if (uiState.name === 'name') { - return changeCase.param(parentMeta.name) + '-' + InstanceCounter.count(parentMeta[':qualified-type']); + return uniqueName || (changeCase.param(parentMeta.name) + '-' + InstanceCounter.count(parentMeta[':qualified-type'])); } - if (_.isArray(parentMeta.key) && _.includes(parentMeta.key, uiState.name)) { + if (_isArray(parentMeta.key) && _includes(parentMeta.key, uiState.name)) { if (/uuid/.test(uiState['data-type'])) { return guid(); } if (uiState['data-type'] === 'string') { - const prefix = uiState.name.replace('id', ''); - return (prefix ? changeCase.param(prefix) + '-' : '') + guid(5); + // if there is only one key property and we were given a + // unique name (probably because creating a list entry + // property) then use the unique name otherwise make one up. + if (parentMeta.key.length > 1 || !uniqueName) { + const prefix = uiState.name.replace('id', ''); + uniqueName = (prefix ? changeCase.param(prefix) + '-' : '') + guid(5); + } + return uniqueName; } if (/int/.test(uiState['data-type'])) { return InstanceCounter.count(uiState[':qualified-type']); @@ -172,7 +188,7 @@ export default { return []; } else { uiState.properties.forEach(p => { - model[p.name] = createModel(p, uiState); + model[p.name] = createModel(p, uiState, uniqueName); }); } return model; @@ -187,7 +203,7 @@ export default { if (/list/.test(property.type)) { property.type = 'container'; } - const modelInstance = createModel(property, property); + const modelInstance = createModel(property, property, uniqueName); modelInstance.uiState = {type: property.name}; const modelFragment = DescriptorTemplateFactory.createModelForType(property[':qualified-type'] || property.name) || {}; Object.assign(modelInstance, modelFragment);