Improve auto generation of list key values. (RIFT-15923)
[osm/UI.git] / skyquake / plugins / composer / src / src / libraries / model / DescriptorModelMetaProperty.js
index 4318c5b..2955e55 100644 (file)
@@ -147,22 +147,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 (/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']);
@@ -173,7 +187,7 @@ export default {
                                return [];
                        } else {
                                uiState.properties.forEach(p => {
-                                       model[p.name] = createModel(p, uiState);
+                                       model[p.name] = createModel(p, uiState, uniqueName);
                                });
                        }
                        return model;
@@ -188,7 +202,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);