1 import { Component, Input, OnInit } from '@angular/core';
2 import { FormControl, NgForm } from '@angular/forms';
4 import _ from 'lodash';
6 import { ConfigurationService } from '~/app/shared/api/configuration.service';
7 import { Icons } from '~/app/shared/enum/icons.enum';
8 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
9 import { ConfigOptionTypes } from './config-option.types';
12 selector: 'cd-config-option',
13 templateUrl: './config-option.component.html',
14 styleUrls: ['./config-option.component.scss']
16 export class ConfigOptionComponent implements OnInit {
18 optionNames: Array<string> = [];
20 optionsForm: CdFormGroup = new CdFormGroup({});
22 optionsFormDir: NgForm = new NgForm([], []);
24 optionsFormGroupName = '';
26 optionsFormShowReset = true;
29 options: Array<any> = [];
30 optionsFormGroup: CdFormGroup = new CdFormGroup({});
32 constructor(private configService: ConfigurationService) {}
34 private static optionNameToText(optionName: string): string {
35 const sections = ['mon', 'mgr', 'osd', 'mds', 'client'];
38 .filter((c, index) => index !== 0 || !sections.includes(c))
39 .map((c) => c.charAt(0).toUpperCase() + c.substring(1))
45 this.loadStoredData();
48 private createForm() {
49 this.optionsForm.addControl(this.optionsFormGroupName, this.optionsFormGroup);
50 this.optionNames.forEach((optionName) => {
51 this.optionsFormGroup.addControl(optionName, new FormControl(null));
55 getStep(type: string, value: any): number | undefined {
56 return ConfigOptionTypes.getTypeStep(type, value);
59 private loadStoredData() {
60 this.configService.filter(this.optionNames).subscribe((data: any) => {
61 this.options = data.map((configOption: any) => {
62 const formControl = this.optionsForm.get(configOption.name);
63 const typeValidators = ConfigOptionTypes.getTypeValidators(configOption);
64 configOption.additionalTypeInfo = ConfigOptionTypes.getType(configOption.type);
66 // Set general information and value
67 configOption.text = ConfigOptionComponent.optionNameToText(configOption.name);
68 configOption.value = _.find(configOption.value, (p) => {
69 return p.section === 'osd'; // TODO: Can handle any other section
71 if (configOption.value) {
72 if (configOption.additionalTypeInfo.name === 'bool') {
73 formControl.setValue(configOption.value.value === 'true');
75 formControl.setValue(configOption.value.value);
79 // Set type information and validators
81 configOption.patternHelpText = typeValidators.patternHelpText;
82 if ('max' in typeValidators && typeValidators.max !== '') {
83 configOption.maxValue = typeValidators.max;
85 if ('min' in typeValidators && typeValidators.min !== '') {
86 configOption.minValue = typeValidators.min;
88 formControl.setValidators(typeValidators.validators);
98 this.optionNames.forEach((optionName) => {
99 const optionValue = this.optionsForm.getValue(optionName);
100 if (optionValue !== null && optionValue !== '') {
101 options[optionName] = {
102 section: 'osd', // TODO: Can handle any other section
108 return this.configService.bulkCreate({ options: options });
111 resetValue(optionName: string) {
112 this.configService.delete(optionName, 'osd').subscribe(
113 // TODO: Can handle any other section
115 const formControl = this.optionsForm.get(optionName);