1 import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
4 import { Icons } from '../../../shared/enum/icons.enum';
5 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
8 RbdConfigurationSourceField,
10 } from '../../../shared/models/configuration';
11 import { FormatterService } from '../../../shared/services/formatter.service';
12 import { RbdConfigurationService } from '../../../shared/services/rbd-configuration.service';
15 selector: 'cd-rbd-configuration-form',
16 templateUrl: './rbd-configuration-form.component.html',
17 styleUrls: ['./rbd-configuration-form.component.scss']
19 export class RbdConfigurationFormComponent implements OnInit {
23 initializeData: EventEmitter<{
24 initialData: RbdConfigurationEntry[];
25 sourceType: RbdConfigurationSourceField;
28 changes = new EventEmitter<any>();
32 ngDataReady = new EventEmitter<any>();
33 initialData: RbdConfigurationEntry[];
34 configurationType = RbdConfigurationType;
35 sectionVisibility: { [key: string]: boolean } = {};
38 public formatterService: FormatterService,
39 public rbdConfigurationService: RbdConfigurationService
43 const configFormGroup = this.createConfigurationFormGroup();
44 this.form.addControl('configuration', configFormGroup);
46 // Listen to changes and emit the values to the parent component
47 configFormGroup.valueChanges.subscribe(() => {
48 this.changes.emit(this.getDirtyValues.bind(this));
51 if (this.initializeData) {
52 this.initializeData.subscribe((data: Record<string, any>) => {
53 this.initialData = data.initialData;
54 const dataType = data.sourceType;
56 this.rbdConfigurationService.getWritableOptionFields().forEach((option) => {
57 const optionData = data.initialData
58 .filter((entry: Record<string, any>) => entry.name === option.name)
60 if (optionData && optionData['source'] === dataType) {
61 this.form.get(`configuration.${option.name}`).setValue(optionData['value']);
64 this.ngDataReady.emit();
68 this.rbdConfigurationService
69 .getWritableSections()
70 .forEach((section) => (this.sectionVisibility[section.class] = false));
73 getDirtyValues(includeLocalValues = false, localFieldType?: RbdConfigurationSourceField) {
74 if (includeLocalValues && !localFieldType) {
76 'ProgrammingError: If local values shall be included, a proper localFieldType argument has to be provided, too';
81 this.rbdConfigurationService.getWritableOptionFields().forEach((config) => {
82 const control: any = this.form.get('configuration').get(config.name);
83 const dirty = control.dirty;
85 if (this.initialData && this.initialData[config.name] === control.value) {
86 return; // Skip controls with initial data loaded
89 if (dirty || (includeLocalValues && control['source'] === localFieldType)) {
90 if (control.value === null) {
91 result[config.name] = control.value;
92 } else if (config.type === RbdConfigurationType.bps) {
93 result[config.name] = this.formatterService.toBytes(control.value);
94 } else if (config.type === RbdConfigurationType.milliseconds) {
95 result[config.name] = this.formatterService.toMilliseconds(control.value);
96 } else if (config.type === RbdConfigurationType.iops) {
97 result[config.name] = this.formatterService.toIops(control.value);
99 result[config.name] = control.value;
108 * Dynamically create form controls.
110 private createConfigurationFormGroup() {
111 const configFormGroup = new CdFormGroup({});
113 this.rbdConfigurationService.getWritableOptionFields().forEach((c) => {
114 let control: FormControl;
116 c.type === RbdConfigurationType.milliseconds ||
117 c.type === RbdConfigurationType.iops ||
118 c.type === RbdConfigurationType.bps
120 control = new FormControl(0, Validators.min(0));
123 `Type ${c.type} is unknown, you may need to add it to RbdConfiguration class`
126 configFormGroup.addControl(c.name, control);
129 return configFormGroup;
133 * Reset the value. The inherited value will be used instead.
135 reset(optionName: string) {
136 const formControl: any = this.form.get('configuration').get(optionName);
137 if (formControl.disabled) {
138 formControl.setValue(formControl['previousValue'] || 0);
139 formControl.enable();
140 if (!formControl['previousValue']) {
141 formControl.markAsPristine();
144 formControl['previousValue'] = formControl.value;
145 formControl.setValue(null);
146 formControl.markAsDirty();
147 formControl.disable();
151 isDisabled(optionName: string) {
152 return this.form.get('configuration').get(optionName).disabled;
155 toggleSectionVisibility(className: string) {
156 this.sectionVisibility[className] = !this.sectionVisibility[className];