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 6f7e57b..548c676 100644 (file)
@@ -23,10 +23,11 @@ import {Component, Input, Output, EventEmitter, ViewChild, ComponentRef} from "@
 import { PropertyFEModel, DerivedFEProperty, DerivedPropertyType } from "app/models";
 import { PROPERTY_TYPES } from 'app/utils';
 import { DataTypeService } from "../../../../services/data-type.service";
-import { trigger, state, style, transition, animate } from '@angular/core';
+import { trigger, state, style, transition, animate } from '@angular/animations';
 import {PropertiesUtils} from "../../../../pages/properties-assignment/services/properties.utils";
 import {IUiElementChangeEvent} from "../../../ui/form-components/ui-element-base.component";
 import {DynamicElementComponent} from "../../../ui/dynamic-element/dynamic-element.component";
+import {SubPropertyToscaFunction} from "app/models/sub-property-tosca-function";
 
 @Component({
     selector: 'dynamic-property',
@@ -41,6 +42,8 @@ export class DynamicPropertyComponent {
     propPath: string;
     isPropertyFEModel: boolean;
     nestedLevel: number;
+    propertyTestsId: string;
+    constraints:string[];
 
     @Input() canBeDeclared: boolean;
     @Input() property: PropertyFEModel | DerivedFEProperty;
@@ -50,10 +53,12 @@ export class DynamicPropertyComponent {
     @Input() readonly: boolean;
     @Input() hasChildren: boolean;
     @Input() hasDeclareOption:boolean;
+    @Input() rootProperty: PropertyFEModel;
 
     @Output('propertyChanged') emitter: EventEmitter<void> = new EventEmitter<void>();
     @Output() expandChild: EventEmitter<string> = new EventEmitter<string>();
     @Output() checkProperty: EventEmitter<string> = new EventEmitter<string>();
+    @Output() toggleTosca: EventEmitter<DerivedFEProperty> = new EventEmitter<DerivedFEProperty>();
     @Output() deleteItem: EventEmitter<string> = new EventEmitter<string>();
     @Output() clickOnPropertyRow: EventEmitter<PropertyFEModel | DerivedFEProperty> = new EventEmitter<PropertyFEModel | DerivedFEProperty>();
     @Output() mapKeyChanged: EventEmitter<string> = new EventEmitter<string>();
@@ -69,6 +74,29 @@ export class DynamicPropertyComponent {
         this.propType = this.property.derivedDataType;
         this.propPath = (this.property instanceof PropertyFEModel) ? this.property.name : this.property.propertiesName;
         this.nestedLevel = (this.property.propertiesName.match(/#/g) || []).length;
+        this.rootProperty = (this.rootProperty) ? this.rootProperty : <PropertyFEModel>this.property;
+        this.propertyTestsId = this.getPropertyTestsId();
+
+        this.initConsraintsValues();
+    }
+
+    initConsraintsValues(){
+        let primitiveProperties = ['string', 'integer', 'float', 'boolean', PROPERTY_TYPES.TIMESTAMP];
+
+        //Property has constraints
+        if(this.property.constraints && this.property.constraints[0]){
+            this.constraints = this.property.constraints[0].validValues
+        }
+
+        //Complex Type
+        else if (primitiveProperties.indexOf(this.rootProperty.type) == -1 && primitiveProperties.indexOf(this.property.type) >= 0 ){
+            this.constraints = this.dataTypeService.getConstraintsByParentTypeAndUniqueID(this.rootProperty.type, this.property.name);           
+        }
+        else{
+            this.constraints = null;
+        }
+        
     }
 
     ngDoCheck() {
@@ -82,6 +110,12 @@ export class DynamicPropertyComponent {
         }
     }
 
+    ngOnChanges() {
+        this.propType = this.property.derivedDataType;
+        this.propPath = (this.property instanceof PropertyFEModel) ? this.property.name : this.property.propertiesName;
+        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
@@ -89,7 +123,6 @@ export class DynamicPropertyComponent {
         this.clickOnPropertyRow.emit(property);
     }
 
-
     expandChildById = (id: string) => {
         this.expandedChildId = id;
         this.expandChild.emit(id);
@@ -99,12 +132,20 @@ export class DynamicPropertyComponent {
         this.checkProperty.emit(propName);
     }
 
+    toggleToscaFunction = (prop: DerivedFEProperty) => {
+        this.toggleTosca.emit(prop);
+    }
+
     getHasChildren = (property:DerivedFEProperty): boolean => {// enter to this function only from base property (PropertyFEModel) and check for child property if it has children
         return _.filter((<PropertyFEModel>this.property).flattenedChildren,(prop:DerivedFEProperty)=>{
             return _.startsWith(prop.propertiesName + '#', property.propertiesName);
         }).length > 1;
     }
 
+    getPropertyTestsId = () => {
+        return [this.rootProperty.name].concat(this.rootProperty.getParentNamesArray(this.property.propertiesName, [], true)).join('.');
+    };
+
     onElementChanged = (event: IUiElementChangeEvent) => {
         this.property.updateValueObj(event.value, event.isValid);
         this.emitter.emit();
@@ -112,7 +153,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) {
             this.addChildProps(newProps, this.property.name);
@@ -129,7 +187,6 @@ export class DynamicPropertyComponent {
             this.expandChildById(newProps[0].propertiesName);
 
             this.updateMapKeyValueOnMainParent(newProps);
-            this.emitter.emit();
         }
     }
 
@@ -177,9 +234,17 @@ export class DynamicPropertyComponent {
             if (!itemParent) {
                 return;
             }
-
-            if (item.derivedDataType == DerivedPropertyType.MAP) {
-                const oldKey = item.getActualMapKey();
+            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) {
                 delete itemParent.valueObj[oldKey];
                 if (itemParent instanceof PropertyFEModel) {
                     delete itemParent.valueObjValidation[oldKey];
@@ -204,13 +269,26 @@ export class DynamicPropertyComponent {
 
     updateChildKeyInParent(childProp: DerivedFEProperty, newMapKey: string) {
         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){
+                        item.subPropertyPath = [newMapKey];
+                    }
+                });
+            }
             this.emitter.emit();
         }
     }
 
     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;