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