1 import { Component, OnInit } from '@angular/core';
2 import { ValidatorFn, Validators } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
5 import { I18n } from '@ngx-translate/i18n-polyfill';
6 import * as _ from 'lodash';
7 import { forkJoin as observableForkJoin } from 'rxjs';
9 import { MgrModuleService } from '../../../../shared/api/mgr-module.service';
10 import { NotificationType } from '../../../../shared/enum/notification-type.enum';
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 implements OnInit {
22 mgrModuleForm: CdFormGroup;
26 moduleOptions: any[] = [];
29 private route: ActivatedRoute,
30 private router: Router,
31 private formBuilder: CdFormBuilder,
32 private mgrModuleService: MgrModuleService,
33 private notificationService: NotificationService,
38 this.route.params.subscribe((params: { name: string }) => {
39 this.moduleName = decodeURIComponent(params.name);
42 this.mgrModuleService.getOptions(this.moduleName),
43 this.mgrModuleService.getConfig(this.moduleName)
45 observableForkJoin(observables).subscribe(
48 this.moduleOptions = resp[0];
49 // Create the form dynamically.
51 // Set the form field values.
52 this.mgrModuleForm.setValue(resp[1]);
61 getValidators(moduleOption: any): ValidatorFn[] {
63 switch (moduleOption.type) {
65 result.push(CdValidators.ip());
71 result.push(CdValidators.number());
72 result.push(Validators.required);
73 if (_.isNumber(moduleOption.min)) {
74 result.push(Validators.min(moduleOption.min));
76 if (_.isNumber(moduleOption.max)) {
77 result.push(Validators.max(moduleOption.max));
81 if (_.isNumber(moduleOption.min)) {
82 result.push(Validators.minLength(moduleOption.min));
84 if (_.isNumber(moduleOption.max)) {
85 result.push(Validators.maxLength(moduleOption.max));
89 result.push(Validators.required);
90 result.push(CdValidators.decimalNumber());
93 result.push(CdValidators.uuid());
100 const controlsConfig = {};
101 _.forEach(this.moduleOptions, (moduleOption) => {
102 controlsConfig[moduleOption.name] = [
103 moduleOption.default_value,
104 this.getValidators(moduleOption)
107 this.mgrModuleForm = this.formBuilder.group(controlsConfig);
111 this.router.navigate(['/mgr-modules']);
115 // Exit immediately if the form isn't dirty.
116 if (this.mgrModuleForm.pristine) {
121 _.forEach(this.moduleOptions, (moduleOption) => {
122 const control = this.mgrModuleForm.get(moduleOption.name);
123 // Append the option only if the value has been modified.
124 if (control.dirty && control.valid) {
125 config[moduleOption.name] = control.value;
128 this.mgrModuleService.updateConfig(this.moduleName, config).subscribe(
130 this.notificationService.show(
131 NotificationType.success,
132 this.i18n('Updated options for module "{{name}}".', { name: this.moduleName })
137 // Reset the 'Submit' button.
138 this.mgrModuleForm.setErrors({ cdSubmitButton: true });