1 import { Component, OnInit } from '@angular/core';
2 import { ValidatorFn, Validators } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
5 import * as _ from 'lodash';
6 import { forkJoin as observableForkJoin } from 'rxjs';
8 import { MgrModuleService } from '../../../../shared/api/mgr-module.service';
9 import { NotificationType } from '../../../../shared/enum/notification-type.enum';
10 import { CdForm } from '../../../../shared/forms/cd-form';
11 import { CdFormBuilder } from '../../../../shared/forms/cd-form-builder';
12 import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
13 import { CdValidators } from '../../../../shared/forms/cd-validators';
14 import { NotificationService } from '../../../../shared/services/notification.service';
17 selector: 'cd-mgr-module-form',
18 templateUrl: './mgr-module-form.component.html',
19 styleUrls: ['./mgr-module-form.component.scss']
21 export class MgrModuleFormComponent extends CdForm implements OnInit {
22 mgrModuleForm: CdFormGroup;
24 moduleOptions: any[] = [];
27 private route: ActivatedRoute,
28 private router: Router,
29 private formBuilder: CdFormBuilder,
30 private mgrModuleService: MgrModuleService,
31 private notificationService: NotificationService
37 this.route.params.subscribe((params: { name: string }) => {
38 this.moduleName = decodeURIComponent(params.name);
40 this.mgrModuleService.getOptions(this.moduleName),
41 this.mgrModuleService.getConfig(this.moduleName)
43 observableForkJoin(observables).subscribe(
45 this.moduleOptions = resp[0];
46 // Create the form dynamically.
48 // Set the form field values.
49 this.mgrModuleForm.setValue(resp[1]);
59 getValidators(moduleOption: any): ValidatorFn[] {
61 switch (moduleOption.type) {
63 result.push(CdValidators.ip());
69 result.push(CdValidators.number());
70 result.push(Validators.required);
71 if (_.isNumber(moduleOption.min)) {
72 result.push(Validators.min(moduleOption.min));
74 if (_.isNumber(moduleOption.max)) {
75 result.push(Validators.max(moduleOption.max));
79 if (_.isNumber(moduleOption.min)) {
80 result.push(Validators.minLength(moduleOption.min));
82 if (_.isNumber(moduleOption.max)) {
83 result.push(Validators.maxLength(moduleOption.max));
87 result.push(Validators.required);
88 result.push(CdValidators.decimalNumber());
91 result.push(CdValidators.uuid());
98 const controlsConfig = {};
99 _.forEach(this.moduleOptions, (moduleOption) => {
100 controlsConfig[moduleOption.name] = [
101 moduleOption.default_value,
102 this.getValidators(moduleOption)
105 this.mgrModuleForm = this.formBuilder.group(controlsConfig);
109 this.router.navigate(['/mgr-modules']);
113 // Exit immediately if the form isn't dirty.
114 if (this.mgrModuleForm.pristine) {
119 _.forEach(this.moduleOptions, (moduleOption) => {
120 const control = this.mgrModuleForm.get(moduleOption.name);
121 // Append the option only if the value has been modified.
122 if (control.dirty && control.valid) {
123 config[moduleOption.name] = control.value;
126 this.mgrModuleService.updateConfig(this.moduleName, config).subscribe(
128 this.notificationService.show(
129 NotificationType.success,
130 $localize`Updated options for module '${this.moduleName}'.`
135 // Reset the 'Submit' button.
136 this.mgrModuleForm.setErrors({ cdSubmitButton: true });