Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / workspace / attributes / attribute-modal.component.ts
1 import { Component, OnInit, ViewChild } from '@angular/core';
2 import { IDropDownOption } from 'onap-ui-angular/dist/form-elements/dropdown/dropdown-models';
3 import { InputComponent } from 'onap-ui-angular/dist/form-elements/text-elements/input/input.component';
4 import { Subject } from 'rxjs/Subject';
5 import { AttributeModel } from '../../../../models/attributes';
6 import { ValidationUtils } from '../../../../utils/validation-utils';
7 import { CacheService } from '../../../services/cache.service';
8 import { TranslateService } from '../../../shared/translator/translate.service';
9 import { AttributeOptions } from './attributes-options';
10
11 @Component({
12     selector: 'attribute-modal',
13     templateUrl: './attribute-modal.component.html',
14     styleUrls: ['./attributes.component.less']
15 })
16 export class AttributeModalComponent implements OnInit {
17
18     @ViewChild('defaultValue') validatedInput: InputComponent;
19
20     public readonly types = AttributeOptions.types;                         // integer, string, boolean etc.
21
22     public readonly booleanValues = AttributeOptions.booleanValues;         // true / false
23
24     public readonly entrySchemaValues = AttributeOptions.entrySchemaValues; // integer, string, boolean, float
25
26     public onValidationChange: Subject<boolean> = new Subject();
27
28     public validationPatterns: any;
29     public readonly listPattern = ValidationUtils.getPropertyListPatterns();
30     public readonly mapPattern = ValidationUtils.getPropertyMapPatterns();
31
32     // The current effective default value pattern
33     public defaultValuePattern: string;
34     public defaultValueErrorMessage: string;
35
36     // Attribute being Edited
37     public attributeToEdit: AttributeModel;
38
39     constructor(private translateService: TranslateService, private cacheService: CacheService) {
40         this.validationPatterns = this.cacheService.get('validation').validationPatterns;
41     }
42
43     ngOnInit() {
44         this.revalidateDefaultValue();
45     }
46
47     onHiddenCheckboxClicked(event: boolean) {
48         this.attributeToEdit.hidden = event;
49     }
50
51     onTypeSelected(selectedElement: IDropDownOption) {
52         if (this.attributeToEdit.type !== selectedElement.value && selectedElement.value === 'boolean') {
53             this.attributeToEdit.defaultValue = ''; // Clean old value in case we choose change type to boolean
54         }
55         this.attributeToEdit.type = selectedElement.value;
56         this.revalidateDefaultValue();
57     }
58
59     onBooleanDefaultValueSelected(selectedElement: IDropDownOption) {
60         if (this.attributeToEdit.type === 'boolean') {
61             this.attributeToEdit.defaultValue = selectedElement.value;
62         }
63     }
64
65     onEntrySchemaTypeSelected(selectedElement: IDropDownOption) {
66         this.attributeToEdit.schema.property.type = selectedElement.value;
67         this.revalidateDefaultValue();
68     }
69
70     onValidityChange(isValid: boolean, field: string) {
71         const typeIsValid = this.attributeToEdit.type && this.attributeToEdit.type.length > 0; // Make sure type is defined
72
73         // Make sure name is defined when other fields are changed
74         let nameIsValid = true;
75         if (field !== 'name') {
76             nameIsValid = this.attributeToEdit.name && this.attributeToEdit.name.length > 0;
77         }
78         this.onValidationChange.next(isValid && nameIsValid && typeIsValid);
79     }
80
81     defaultValueChanged() {
82         this.revalidateDefaultValue();
83     }
84
85     /**
86      * Utility function for UI that converts a simple value to IDropDownOption
87      * @param val
88      * @returns {{value: any; label: any}}
89      */
90     toDropDownOption(val: string) {
91         return { value : val, label: val };
92     }
93
94     public isMapUnique = () => {
95         if (this.attributeToEdit && this.attributeToEdit.type === 'map' && this.attributeToEdit.defaultValue) {
96             return ValidationUtils.validateUniqueKeys(this.attributeToEdit.defaultValue);
97         }
98         return true;
99     }
100
101     private revalidateDefaultValue() {
102         this.setDefaultValuePattern(this.attributeToEdit.type);
103         setTimeout(() => {
104             if (this.validatedInput) {
105                 this.validatedInput.onKeyPress(this.attributeToEdit.defaultValue);
106             } }, 250);
107     }
108
109     private setDefaultValuePattern(valueType: string) {
110         const selectedSchemaType = this.attributeToEdit.schema.property.type;
111         this.defaultValuePattern = '.*';
112         switch (valueType) {
113             case 'float':
114                 this.defaultValuePattern = this.validationPatterns.number;
115                 this.defaultValueErrorMessage = this.translateService.translate('VALIDATION_ERROR_TYPE', { type : 'float' });
116                 break;
117             case 'integer':
118                 this.defaultValuePattern = this.validationPatterns.integerNoLeadingZero;
119                 this.defaultValueErrorMessage = this.translateService.translate('VALIDATION_ERROR_TYPE', { type : 'integer' });
120                 break;
121             case 'list':
122                 if (selectedSchemaType != undefined) {
123                     this.defaultValuePattern = this.listPattern[selectedSchemaType];
124                     const listTypeStr = `list of ${selectedSchemaType}s (v1, v2, ...) `;
125                     this.defaultValueErrorMessage = this.translateService.translate('VALIDATION_ERROR_TYPE', { type : listTypeStr });
126                 }
127                 break;
128             case 'map':
129                 if (selectedSchemaType != undefined) {
130                     this.defaultValuePattern = this.mapPattern[selectedSchemaType];
131                     const mapTypeStr = `map of ${selectedSchemaType}s (k1:v1, k2:v2, ...)`;
132                     this.defaultValueErrorMessage = this.translateService.translate('VALIDATION_ERROR_TYPE', { type : mapTypeStr });
133                 }
134                 break;
135         }
136     }
137
138 }