Setting Tosca Function on top of unsaved value causes problems
[sdc.git] / catalog-ui / src / app / ng2 / components / logic / properties-table / dynamic-property / dynamic-property.component.ts
index 6107e8a..e53cf01 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/animations';
+import { trigger, 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',
@@ -43,6 +44,7 @@ export class DynamicPropertyComponent {
     nestedLevel: number;
     propertyTestsId: string;
     constraints:string[];
+    checkboxDisabled: boolean = false;
 
     @Input() canBeDeclared: boolean;
     @Input() property: PropertyFEModel | DerivedFEProperty;
@@ -57,6 +59,7 @@ export class DynamicPropertyComponent {
     @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>();
@@ -73,11 +76,9 @@ export class DynamicPropertyComponent {
         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.propertyTestsId = this.getPropertyTestsId();
+
         this.initConsraintsValues();
-        
-        
     }
 
     initConsraintsValues(){
@@ -110,6 +111,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
@@ -117,7 +124,6 @@ export class DynamicPropertyComponent {
         this.clickOnPropertyRow.emit(property);
     }
 
-
     expandChildById = (id: string) => {
         this.expandedChildId = id;
         this.expandChild.emit(id);
@@ -127,6 +133,10 @@ 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);
@@ -139,18 +149,45 @@ export class DynamicPropertyComponent {
 
     onElementChanged = (event: IUiElementChangeEvent) => {
         this.property.updateValueObj(event.value, event.isValid);
+        if (this.property.hasValueObjChanged()) {
+            this.checkboxDisabled = true;
+        }
+        if (event.value === '' || event.value === null || event.value === undefined) {
+            this.checkboxDisabled = false;
+        }
         this.emitter.emit();
     };
 
     createNewChildProperty = (): void => {
 
-        let newProps: Array<DerivedFEProperty> = this.propertiesUtils.createListOrMapChildren(this.property, "", null);
+        let mapKeyValue = this.property instanceof DerivedFEProperty ? this.property.mapKey : "";
+        let parentToscaFunction = null;
+        if (this.property.type == PROPERTY_TYPES.LIST && mapKeyValue === "") {
+            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";
+            }
+        }
+        if (this.property.type == PROPERTY_TYPES.MAP && this.property instanceof DerivedFEProperty && this.property.mapInlist) {
+            parentToscaFunction = this.property.toscaFunction;
+            this.property.toscaFunction = null;
+        }
+        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);
         } else {
             this.addChildPropsToParent.emit(newProps);
         }
+        this.property.toscaFunction = parentToscaFunction;
     }
 
     addChildProps = (newProps: Array<DerivedFEProperty>, childPropName: string) => {
@@ -161,7 +198,6 @@ export class DynamicPropertyComponent {
             this.expandChildById(newProps[0].propertiesName);
 
             this.updateMapKeyValueOnMainParent(newProps);
-            this.emitter.emit();
         }
     }
 
@@ -196,9 +232,20 @@ export class DynamicPropertyComponent {
 
     deleteListOrMapItem = (item: DerivedFEProperty) => {
         if (this.property instanceof PropertyFEModel) {
+            const childMapKey = item.mapKey;
             this.removeValueFromParent(item);
             this.property.flattenedChildren.splice(this.property.getIndexOfChild(item.propertiesName), this.property.getCountOfChildren(item.propertiesName));
             this.expandChildById(item.propertiesName);
+            if (this.property.type == PROPERTY_TYPES.LIST && this.property.schemaType == PROPERTY_TYPES.MAP && childMapKey != null) {
+                let valueObject = JSON.parse(this.property.value);
+                let innerObject = valueObject[item.parentMapKey];
+                delete innerObject[childMapKey];
+                this.property.valueObj = valueObject;
+                this.property.value = JSON.stringify(valueObject);
+                this.property.flattenedChildren[0].valueObj = valueObject;
+                this.property.flattenedChildren[0].value = JSON.stringify(valueObject);
+                this.property.flattenedChildren[0].valueObjIsChanged = true;
+            }
         }
     }
 
@@ -209,9 +256,24 @@ export class DynamicPropertyComponent {
             if (!itemParent) {
                 return;
             }
-
+            let oldKey = item.getActualMapKey();
+            let keyIndex : number = 0;
+                if(item.parentMapKey != null && oldKey != null) {
+                    keyIndex = 1;
+                }
+                if(item.parentMapKey != null && oldKey == null) {
+                    oldKey = item.parentMapKey;
+                }
+            if (this.property.subPropertyToscaFunctions !== null) {
+                let tempSubToscaFunction: SubPropertyToscaFunction[] = [];
+                this.property.subPropertyToscaFunctions.forEach((subToscaItem : SubPropertyToscaFunction) => {
+                    if(subToscaItem.subPropertyPath[keyIndex] != oldKey){
+                        tempSubToscaFunction.push(subToscaItem);
+                    }
+                });
+                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];
@@ -236,13 +298,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;