Change to enable SDC list type input
[sdc.git] / catalog-ui / src / app / ng2 / pages / properties-assignment / declare-list / declare-list.component.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Fujitsu Limited. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import * as _ from "lodash";
22 import {Component} from '@angular/core';
23 import {DropdownValue} from "app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component";
24 import { DataTypeService } from "app/ng2/services/data-type.service";
25 import {PropertyBEModel, DataTypesMap} from "app/models";
26 import {PROPERTY_DATA} from "app/utils";
27 import {PROPERTY_TYPES} from "../../../../utils";
28 import { ModalService } from "app/ng2/services/modal.service";
29 import { InstancePropertiesAPIMap } from "app/models/properties-inputs/property-fe-map";
30 import { ModalModel } from "app/models/modal";
31 import { DataTypeModel } from "app/models/data-types";
32
33
34
35 @Component({
36     selector: 'declare-list',
37     templateUrl: './declare-list.component.html',
38     styleUrls:['./declare-list.component.less'],
39 })
40
41 export class DeclareListComponent {
42
43     typesProperties: Array<DropdownValue>;
44     typesSchemaProperties: Array<DropdownValue>;
45     propertyModel: PropertyBEModel;
46     //propertyNameValidationPattern:RegExp = /^[a-zA-Z0-9_:-]{1,50}$/;
47     //commentValidationPattern:RegExp = /^[\u0000-\u00BF]*$/;
48     //types:Array<string>;
49     dataTypes:DataTypesMap;
50     isLoading:boolean;
51     inputsToCreate:InstancePropertiesAPIMap;
52     propertiesListString:string;
53     privateDataType: DataTypeModel;
54
55     constructor(protected dataTypeService:DataTypeService, private modalService:ModalService) {}
56
57     ngOnInit() {
58         console.log('DeclareListComponent.ngOnInit() - enter');
59         this.propertyModel = new PropertyBEModel();
60         this.propertyModel.type = '';
61         this.propertyModel.schema.property.type = '';
62         const types: Array<string> =  PROPERTY_DATA.TYPES; //All types - simple type + map + list
63         this.dataTypes = this.dataTypeService.getAllDataTypes(); //Get all data types in service
64         const nonPrimitiveTypes :Array<string> = _.filter(Object.keys(this.dataTypes), (type:string)=> {
65             return types.indexOf(type) == -1;
66         });
67
68         this.typesProperties = _.map(PROPERTY_DATA.TYPES,
69             (type: string) => new DropdownValue(type, type)
70         );
71         let typesSimpleProperties = _.map(PROPERTY_DATA.SIMPLE_TYPES,
72             (type: string) => new DropdownValue(type, type)
73         );
74         let nonPrimitiveTypesValues = _.map(nonPrimitiveTypes,
75             (type: string) => new DropdownValue(type,
76                     type.replace("org.openecomp.datatypes.heat.",""))
77         );
78         this.typesProperties = _.concat(this.typesProperties,nonPrimitiveTypesValues);
79         this.typesSchemaProperties = _.concat(typesSimpleProperties,nonPrimitiveTypesValues);
80         this.typesProperties.unshift(new DropdownValue('','Select Type...'));
81         this.typesSchemaProperties.unshift(new DropdownValue('','Select Schema Type...'));
82
83         this.inputsToCreate = this.modalService.currentModal.instance.dynamicContent.instance.input.properties;
84
85         this.propertiesListString = this.modalService.currentModal.instance.dynamicContent.instance.input.propertyNameList.join(", ");
86
87         this.privateDataType = new DataTypeModel(null);
88         this.privateDataType.name = "datatype";
89
90         console.log('DeclareListComponent.ngOnInit() - leave');
91     }
92
93     checkFormValidForSubmit(){
94         const showSchema:boolean = this.showSchema();
95         let isSchemaValid: boolean = (showSchema && !this.propertyModel.schema.property.type)? false : true;
96         if (!showSchema){
97             this.propertyModel.schema.property.type = '';
98         }
99         return this.propertyModel.name && this.propertyModel.type && isSchemaValid;
100     }
101
102     showSchema():boolean {
103         return [PROPERTY_TYPES.LIST, PROPERTY_TYPES.MAP].indexOf(this.propertyModel.type) > -1;
104     };
105
106     onSchemaTypeChange():void {
107         if (this.propertyModel.type == PROPERTY_TYPES.MAP) {
108             this.propertyModel.value = JSON.stringify({'': null});
109         } else if (this.propertyModel.type == PROPERTY_TYPES.LIST) {
110             this.propertyModel.value = JSON.stringify([]);
111         }
112     };
113
114 }