Bug fix for list of map values in properties
[sdc.git] / catalog-ui / src / app / ng2 / components / logic / properties-table / dynamic-property / dynamic-property.component.ts
index 360cfac..548c676 100644 (file)
@@ -113,17 +113,16 @@ export class DynamicPropertyComponent {
     ngOnChanges() {
         this.propType = this.property.derivedDataType;
         this.propPath = (this.property instanceof PropertyFEModel) ? this.property.name : this.property.propertiesName;
-        this.propertyTestsId = this.getPropertyTestsId(); 
+        this.rootProperty = (this.rootProperty) ? this.rootProperty : <PropertyFEModel>this.property;
+        this.propertyTestsId = this.getPropertyTestsId();
     }
 
-
     onClickPropertyRow = (property, event) => {
         // Because DynamicPropertyComponent is recrusive second time the event is fire event.stopPropagation = undefined
         event && event.stopPropagation && event.stopPropagation();
         this.clickOnPropertyRow.emit(property);
     }
 
-
     expandChildById = (id: string) => {
         this.expandedChildId = id;
         this.expandChild.emit(id);
@@ -153,8 +152,24 @@ export class DynamicPropertyComponent {
     };
 
     createNewChildProperty = (): void => {
-       
-        let newProps: Array<DerivedFEProperty> = this.propertiesUtils.createListOrMapChildren(this.property, "", null);
+
+        let mapKeyValue = this.property instanceof DerivedFEProperty ? this.property.mapKey : "";
+        if (this.property.type == PROPERTY_TYPES.LIST && mapKeyValue === "") {
+            if (this.property.schemaType != PROPERTY_TYPES.MAP) {
+                if (this.property.value != null) {
+                    const valueJson = JSON.parse(this.property.value);
+                    if (this.property instanceof PropertyFEModel && this.property.expandedChildPropertyId != null) {
+                        let indexNumber = Number(Object.keys(valueJson).sort().reverse()[0]) + 1;
+                        mapKeyValue = indexNumber.toString();
+                    }else{
+                        mapKeyValue = Object.keys(valueJson).sort().reverse()[0];
+                    }
+                }else {
+                    mapKeyValue = "0";
+                }
+            }
+        }
+        let newProps: Array<DerivedFEProperty> = this.propertiesUtils.createListOrMapChildren(this.property, mapKeyValue, null);
 
         this.propertiesUtils.assignFlattenedChildrenValues(this.property.valueObj, [newProps[0]], this.property.propertiesName);
         if (this.property instanceof PropertyFEModel) {
@@ -172,7 +187,6 @@ export class DynamicPropertyComponent {
             this.expandChildById(newProps[0].propertiesName);
 
             this.updateMapKeyValueOnMainParent(newProps);
-            this.emitter.emit();
         }
     }
 
@@ -220,23 +234,22 @@ export class DynamicPropertyComponent {
             if (!itemParent) {
                 return;
             }
-
+            const oldKey = item.getActualMapKey();
+            if (this.property.subPropertyToscaFunctions !== null) {
+                let tempSubToscaFunction: SubPropertyToscaFunction[] = [];
+                this.property.subPropertyToscaFunctions.forEach((item : SubPropertyToscaFunction, index) => {
+                    if(item.subPropertyPath[0] != oldKey){
+                        tempSubToscaFunction.push(item);
+                    }
+                });
+                this.property.subPropertyToscaFunctions = tempSubToscaFunction;
+            }
             if (item.derivedDataType == DerivedPropertyType.MAP && !item.mapInlist) {
-                const oldKey = item.getActualMapKey();
                 delete itemParent.valueObj[oldKey];
                 if (itemParent instanceof PropertyFEModel) {
                     delete itemParent.valueObjValidation[oldKey];
                     itemParent.valueObjIsValid = itemParent.calculateValueObjIsValid();
                 }
-                if (this.property.subPropertyToscaFunctions !== null) {
-                    let tempSubToscaFunction: SubPropertyToscaFunction[] = [];
-                    this.property.subPropertyToscaFunctions.forEach((item : SubPropertyToscaFunction, index) => {
-                        if(item.subPropertyPath[0] != oldKey){
-                            tempSubToscaFunction.push(item);
-                        }
-                    });
-                    this.property.subPropertyToscaFunctions = tempSubToscaFunction;
-                }
                 this.property.childPropMapKeyUpdated(item, null);  // remove map key
             } else {
                 const itemIndex: number = this.property.flattenedChildren.filter(prop => prop.parentName == item.parentName).map(prop => prop.propertiesName).indexOf(item.propertiesName);
@@ -258,6 +271,11 @@ export class DynamicPropertyComponent {
         if (this.property instanceof PropertyFEModel) {
             let oldKey = childProp.getActualMapKey();
             this.property.childPropMapKeyUpdated(childProp, newMapKey);
+            this.property.flattenedChildren.forEach(tempDervObj => {
+                if (childProp.propertiesName === tempDervObj.parentName) {
+                    tempDervObj.mapKey = newMapKey;
+                }
+            });
             if (this.property.subPropertyToscaFunctions != null) {
                 this.property.subPropertyToscaFunctions.forEach((item : SubPropertyToscaFunction) => {
                     if(item.subPropertyPath[0] === oldKey){
@@ -270,7 +288,7 @@ export class DynamicPropertyComponent {
     }
 
     preventInsertItem = (property:DerivedFEProperty):boolean => {
-        if(property.type == PROPERTY_TYPES.MAP && Object.keys(property.valueObj).indexOf('') > -1 ){
+        if(property.type == PROPERTY_TYPES.MAP && property.valueObj != null && Object.keys(property.valueObj).indexOf('') > -1 ){
             return true;
         }
         return false;