UI Support for operation milestones
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / interface-operatons / operation-creator / input-list / input-list-item / input-list-item.component.ts
index 88ff8de..80dd252 100644 (file)
 
 import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
 import {DataTypeModel} from '../../../../../../../models/data-types';
-import {SchemaPropertyGroupModel} from '../../../../../../../models/schema-property';
-import {DerivedPropertyType, PropertyBEModel} from '../../../../../../../models/properties-inputs/property-be-model';
+import {SchemaProperty, SchemaPropertyGroupModel} from '../../../../../../../models/schema-property';
+import {PropertyBEModel} from '../../../../../../../models/properties-inputs/property-be-model';
 import {PROPERTY_DATA, PROPERTY_TYPES} from '../../../../../../../utils/constants';
 import {ToscaFunction} from '../../../../../../../models/tosca-function';
-import {ToscaFunctionValidationEvent} from "../../../../../../../ng2/pages/properties-assignment/tosca-function/tosca-function.component";
+import {ToscaFunctionType} from "../../../../../../../models/tosca-function-type.enum";
+import {ToscaFunctionValidationEvent} from "../../../../../properties-assignment/tosca-function/tosca-function.component";
 import {InstanceFeDetails} from "../../../../../../../models/instance-fe-details";
+import {ToscaTypeHelper} from "app/utils/tosca-type-helper";
+import {CustomToscaFunction} from "../../../../../../../models/default-custom-functions";
+import {SubPropertyToscaFunction} from "../../../../../../../models/sub-property-tosca-function";
 
 @Component({
   selector: 'app-input-list-item',
@@ -41,24 +45,26 @@ export class InputListItemComponent implements OnInit {
   @Input() type: DataTypeModel;
   @Input() schema: SchemaPropertyGroupModel;
   @Input() nestingLevel: number;
+  @Input() isExpanded: boolean = false;
   @Input() isListChild: boolean = false;
   @Input() isMapChild: boolean = false;
   @Input() showToscaFunctionOption: boolean = false;
   @Input() listIndex: number;
+  @Input() subPropertyToscaFunctions: SubPropertyToscaFunction[];
   @Input() isViewOnly: boolean;
   @Input() allowDeletion: boolean = false;
   @Input() toscaFunction: ToscaFunction;
   @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map();
+  @Input() customToscaFunctions: Array<CustomToscaFunction> = [];
   @Output('onValueChange') onValueChangeEvent: EventEmitter<any> = new EventEmitter<any>();
   @Output('onDelete') onDeleteEvent: EventEmitter<string> = new EventEmitter<string>();
   @Output('onChildListItemDelete') onChildListItemDeleteEvent: EventEmitter<number> = new EventEmitter<number>();
 
-  isExpanded: boolean = false;
   mapEntryName: string;
   isToscaFunction: boolean = false;
   property: PropertyBEModel;
 
-  ngOnInit() {
+  ngOnInit(): void {
     if (!this.nestingLevel) {
       this.nestingLevel = 0;
     }
@@ -79,6 +85,19 @@ export class InputListItemComponent implements OnInit {
       this.valueObjRef = this.toscaFunction.value;
       this.isToscaFunction = true;
     }
+    if (this.property.type == PROPERTY_TYPES.JSON) {
+      this.valueObjRef = JSON.stringify(this.valueObjRef);
+    }
+  }
+
+  ngOnChanges(): void {
+    if (this.isToscaFunction) {
+      this.property.toscaFunction = this.toscaFunction;
+      this.valueObjRef = this.toscaFunction.value;
+    } else {
+      this.property = this.property ? this.property : new PropertyBEModel();
+      this.property.toscaFunction = undefined;
+    }
   }
 
   private initEmptyPropertyInValueObjRef(property: PropertyBEModel) {
@@ -93,26 +112,32 @@ export class InputListItemComponent implements OnInit {
     }
   }
 
-  getType(typeName: string): DerivedPropertyType {
-    if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(typeName) > -1) {
-      return DerivedPropertyType.SIMPLE;
-    } else if (typeName === PROPERTY_TYPES.LIST) {
-      return DerivedPropertyType.LIST;
-    } else if (typeName === PROPERTY_TYPES.MAP) {
-      return DerivedPropertyType.MAP;
-    } else if (typeName === PROPERTY_TYPES.RANGE) {
-      return DerivedPropertyType.RANGE;
-    } else {
-      return DerivedPropertyType.COMPLEX;
+  getToscaFunction(key: any): any {
+    if (this.subPropertyToscaFunctions) {
+      for (let subPropertyToscaFunction of this.subPropertyToscaFunctions) {
+        let found = subPropertyToscaFunction.subPropertyPath ? subPropertyToscaFunction.subPropertyPath.find(value => value === key) : false;
+        if (found) {
+          return subPropertyToscaFunction.toscaFunction;
+        }
+      }
     }
+    return undefined;
+  }
+
+  isTypeSimple(typeName: string): boolean {
+    return ToscaTypeHelper.isTypeSimple(typeName) || this.isTypeDerivedFromSimple(typeName) && (this.isTypeWithoutProperties(typeName));
+  }
+
+  isTypeRange(typeName: string): boolean {
+    return ToscaTypeHelper.isTypeRange(typeName);
   }
 
   isTypeWithoutProperties(typeName: string): boolean {
-      if (this.dataTypeMap.get(typeName) === undefined) {
-          return true;
-      }
-      return this.dataTypeMap.get(typeName).properties === undefined ||
-          this.dataTypeMap.get(typeName).properties.length == 0;
+    if (this.dataTypeMap.get(typeName) === undefined) {
+      return true;
+    }
+    return this.dataTypeMap.get(typeName).properties === undefined ||
+        this.dataTypeMap.get(typeName).properties.length == 0;
   }
 
   isTypeDerivedFromSimple(typeName: string): boolean {
@@ -134,27 +159,28 @@ export class InputListItemComponent implements OnInit {
     return true;
   }
 
-  isTypeSimple(typeName: string): boolean {
-    if (this.getType(typeName) == DerivedPropertyType.SIMPLE) {
-      return true;
-    }
-    return this.isTypeDerivedFromSimple(typeName) && (this.isTypeWithoutProperties(typeName));
+  isTypeList(typeName: string): boolean {
+    return ToscaTypeHelper.isTypeList(typeName);
   }
 
-  isTypeRange(typeName: string): boolean {
-    return this.getType(typeName) == DerivedPropertyType.RANGE;
+  isTypeMap(typeName: string): boolean {
+    return ToscaTypeHelper.isTypeMap(typeName);
   }
 
-  isTypeList(typeName: string): boolean {
-    return this.getType(typeName) == DerivedPropertyType.LIST;
+  isTypeComplex(typeName: string): boolean {
+    return ToscaTypeHelper.isTypeComplex(typeName);
   }
 
-  isTypeMap(typeName: string): boolean {
-    return this.getType(typeName) == DerivedPropertyType.MAP;
+  isTypeNumber(type: string): boolean {
+    return ToscaTypeHelper.isTypeNumber(type);
   }
 
-  isTypeComplex(typeName: string): boolean {
-    return !this.isTypeSimple(typeName) && !this.isTypeList(typeName) && !this.isTypeMap(typeName) && !this.isTypeRange(typeName);
+  isTypeBoolean(type: string): boolean {
+    return ToscaTypeHelper.isTypeBoolean(type);
+  }
+
+  isTypeLiteral(type: string): boolean {
+    return ToscaTypeHelper.isTypeLiteral(type);
   }
 
   expandAndCollapse() {
@@ -180,7 +206,7 @@ export class InputListItemComponent implements OnInit {
   }
 
   onValueChange(value: any): void {
-    if (this.isNumber(this.type.name)) {
+    if (this.isTypeNumber(this.type.name)) {
       this.emitValueChangeEvent(this.parseNumber(value));
       return;
     }
@@ -297,14 +323,20 @@ export class InputListItemComponent implements OnInit {
   }
 
   getSimpleValueInputType() {
-    if (this.isNumber(this.type.name)){
+    if (this.isTypeNumber(this.type.name)){
       return 'number';
     }
     return 'text';
   }
 
-  isNumber(type: string): boolean {
-    return type === PROPERTY_TYPES.INTEGER || type === PROPERTY_TYPES.FLOAT;
+  buildSchemaGroupProperty(): SchemaPropertyGroupModel {
+    const schemaProperty = new SchemaProperty();
+    if (this.schema.property.type === PROPERTY_TYPES.MAP || this.schema.property.type === PROPERTY_TYPES.LIST) {
+      schemaProperty.type = PROPERTY_TYPES.STRING;
+    } else {
+      schemaProperty.type = this.schema.property.type
+    }
+    return new SchemaPropertyGroupModel(schemaProperty);
   }
 
   private parseBoolean(value: any) {