Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / properties-assignment / property-creator / property-creator.component.ts
1
2 import { Component } from '@angular/core';
3 import { DataTypesMap, PropertyBEModel } from 'app/models';
4 import { DropdownValue } from 'app/ng2/components/ui/form-components/dropdown/ui-element-dropdown.component';
5 import { DataTypeService } from 'app/ng2/services/data-type.service';
6 import { PROPERTY_DATA } from 'app/utils';
7 import * as _ from 'lodash';
8 import { PROPERTY_TYPES } from '../../../../utils';
9
10 @Component({
11     selector: 'property-creator',
12     templateUrl: './property-creator.component.html',
13     styleUrls: ['./property-creator.component.less'],
14 })
15
16 export class PropertyCreatorComponent {
17
18     typesProperties: DropdownValue[];
19     typesSchemaProperties: DropdownValue[];
20     propertyModel: PropertyBEModel;
21     // propertyNameValidationPattern:RegExp = /^[a-zA-Z0-9_:-]{1,50}$/;
22     // commentValidationPattern:RegExp = /^[\u0000-\u00BF]*$/;
23     // types:Array<string>;
24     dataTypes: DataTypesMap;
25     isLoading: boolean;
26
27     constructor(protected dataTypeService: DataTypeService) {}
28
29     ngOnInit() {
30         this.propertyModel = new PropertyBEModel();
31         this.propertyModel.type = '';
32         this.propertyModel.schema.property.type = '';
33         const types: string[] =  PROPERTY_DATA.TYPES; // All types - simple type + map + list
34         this.dataTypes = this.dataTypeService.getAllDataTypes(); // Get all data types in service
35         const nonPrimitiveTypes: string[] = _.filter(Object.keys(this.dataTypes), (type: string) => {
36             return types.indexOf(type) === -1;
37         });
38
39         this.typesProperties = _.map(PROPERTY_DATA.TYPES,
40             (type: string) => new DropdownValue(type, type)
41         );
42         const typesSimpleProperties = _.map(PROPERTY_DATA.SIMPLE_TYPES,
43             (type: string) => new DropdownValue(type, type)
44         );
45         const nonPrimitiveTypesValues = _.map(nonPrimitiveTypes,
46             (type: string) => new DropdownValue(type,
47                     type.replace('org.openecomp.datatypes.heat.', ''))
48         )
49         .sort((a, b) => a.label.localeCompare(b.label));
50         this.typesProperties = _.concat(this.typesProperties, nonPrimitiveTypesValues);
51         this.typesSchemaProperties = _.concat(typesSimpleProperties, nonPrimitiveTypesValues);
52         this.typesProperties.unshift(new DropdownValue('', 'Select Type...'));
53         this.typesSchemaProperties.unshift(new DropdownValue('', 'Select Schema Type...'));
54
55     }
56
57     checkFormValidForSubmit() {
58         const showSchema: boolean = this.showSchema();
59         const isSchemaValid: boolean = (showSchema && !this.propertyModel.schema.property.type) ? false : true;
60         if (!showSchema) {
61             this.propertyModel.schema.property.type = '';
62         }
63         return this.propertyModel.name && this.propertyModel.type && isSchemaValid;
64     }
65
66     showSchema(): boolean {
67         return [PROPERTY_TYPES.LIST, PROPERTY_TYPES.MAP].indexOf(this.propertyModel.type) > -1;
68     }
69
70     onSchemaTypeChange(): void {
71         if (this.propertyModel.type === PROPERTY_TYPES.MAP) {
72             this.propertyModel.value = JSON.stringify({'': null});
73         } else if (this.propertyModel.type === PROPERTY_TYPES.LIST) {
74             this.propertyModel.value = JSON.stringify([]);
75         }
76     }
77
78 }