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';
12 selector: 'attribute-modal',
13 templateUrl: './attribute-modal.component.html',
14 styleUrls: ['./attributes.component.less']
16 export class AttributeModalComponent implements OnInit {
18 @ViewChild('defaultValue') validatedInput: InputComponent;
20 public readonly types = AttributeOptions.types; // integer, string, boolean etc.
22 public readonly booleanValues = AttributeOptions.booleanValues; // true / false
24 public readonly entrySchemaValues = AttributeOptions.entrySchemaValues; // integer, string, boolean, float
26 public onValidationChange: Subject<boolean> = new Subject();
28 public validationPatterns: any;
29 public readonly listPattern = ValidationUtils.getPropertyListPatterns();
30 public readonly mapPattern = ValidationUtils.getPropertyMapPatterns();
32 // The current effective default value pattern
33 public defaultValuePattern: string;
34 public defaultValueErrorMessage: string;
36 // Attribute being Edited
37 public attributeToEdit: AttributeModel;
39 constructor(private translateService: TranslateService, private cacheService: CacheService) {
40 this.validationPatterns = this.cacheService.get('validation').validationPatterns;
44 this.revalidateDefaultValue();
47 onHiddenCheckboxClicked(event: boolean) {
48 this.attributeToEdit.hidden = event;
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
55 this.attributeToEdit.type = selectedElement.value;
56 this.revalidateDefaultValue();
59 onBooleanDefaultValueSelected(selectedElement: IDropDownOption) {
60 if (this.attributeToEdit.type === 'boolean') {
61 this.attributeToEdit.defaultValue = selectedElement.value;
65 onEntrySchemaTypeSelected(selectedElement: IDropDownOption) {
66 this.attributeToEdit.schema.property.type = selectedElement.value;
67 this.revalidateDefaultValue();
70 onValidityChange(isValid: boolean, field: string) {
71 const typeIsValid = this.attributeToEdit.type && this.attributeToEdit.type.length > 0; // Make sure type is defined
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;
78 this.onValidationChange.next(isValid && nameIsValid && typeIsValid);
81 defaultValueChanged() {
82 this.revalidateDefaultValue();
86 * Utility function for UI that converts a simple value to IDropDownOption
88 * @returns {{value: any; label: any}}
90 toDropDownOption(val: string) {
91 return { value : val, label: val };
94 public isMapUnique = () => {
95 if (this.attributeToEdit && this.attributeToEdit.type === 'map' && this.attributeToEdit.defaultValue) {
96 return ValidationUtils.validateUniqueKeys(this.attributeToEdit.defaultValue);
101 private revalidateDefaultValue() {
102 this.setDefaultValuePattern(this.attributeToEdit.type);
104 if (this.validatedInput) {
105 this.validatedInput.onKeyPress(this.attributeToEdit.defaultValue);
109 private setDefaultValuePattern(valueType: string) {
110 const selectedSchemaType = this.attributeToEdit.schema.property.type;
111 this.defaultValuePattern = '.*';
114 this.defaultValuePattern = this.validationPatterns.number;
115 this.defaultValueErrorMessage = this.translateService.translate('VALIDATION_ERROR_TYPE', { type : 'float' });
118 this.defaultValuePattern = this.validationPatterns.integerNoLeadingZero;
119 this.defaultValueErrorMessage = this.translateService.translate('VALIDATION_ERROR_TYPE', { type : 'integer' });
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 });
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 });