]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/blob
7a9cf1033d41d92a38bbb60292e967042f4839f5
[ceph.git] /
1 import { ChangeDetectorRef, Component, Inject, OnInit, Optional } from '@angular/core';
2 import { FormArray, FormControl, FormGroup, UntypedFormControl, Validators } from '@angular/forms';
3 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
4 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
5 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
6 import { CdValidators } from '~/app/shared/forms/cd-validators';
7
8 import { NotificationService } from '~/app/shared/services/notification.service';
9 import { RgwRealmService } from '~/app/shared/api/rgw-realm.service';
10 import { SmbService } from '~/app/shared/api/smb.service';
11 import { CdForm } from '~/app/shared/forms/cd-form';
12 import { DomainSettings } from '../smb.model';
13
14 @Component({
15   selector: 'cd-smb-domain-setting-modal',
16   templateUrl: './smb-domain-setting-modal.component.html',
17   styleUrls: ['./smb-domain-setting-modal.component.scss']
18 })
19 export class SmbDomainSettingModalComponent extends CdForm implements OnInit {
20   domainSettingsForm: CdFormGroup;
21   realmNames: string[];
22
23   constructor(
24     public activeModal: NgbActiveModal,
25     public actionLabels: ActionLabelsI18n,
26     public rgwRealmService: RgwRealmService,
27     public notificationService: NotificationService,
28     public smbService: SmbService,
29     private cd: ChangeDetectorRef,
30     @Optional() @Inject('action') public action: string,
31     @Optional() @Inject('resource') public resource: string,
32     @Optional()
33     @Inject('domainSettingsObject')
34     public domainSettingsObject?: DomainSettings
35   ) {
36     super();
37     this.action = this.actionLabels.UPDATE;
38     this.resource = $localize`Domain Setting`;
39   }
40
41   private createForm() {
42     this.domainSettingsForm = new CdFormGroup({
43       realm: new UntypedFormControl('', {
44         validators: [
45           Validators.required,
46           CdValidators.custom('uniqueName', (realm: string) => {
47             return this.realmNames && this.realmNames.indexOf(realm) !== -1;
48           })
49         ]
50       }),
51       join_sources: new FormArray([])
52     });
53   }
54
55   ngOnInit(): void {
56     this.createForm();
57     this.loadingReady();
58     this.domainSettingsForm.get('realm').setValue(this.domainSettingsObject?.realm);
59     const join_sources = this.domainSettingsForm.get('join_sources') as FormArray;
60
61     if (this.domainSettingsObject?.join_sources) {
62       this.domainSettingsObject.join_sources.forEach((source: { ref: string }) => {
63         join_sources.push(
64           new FormGroup({
65             ref: new FormControl(source.ref || '', Validators.required)
66           })
67         );
68       });
69     }
70
71     if (!this.domainSettingsObject) {
72       this.join_sources.push(
73         new FormGroup({
74           ref: new FormControl('', Validators.required)
75         })
76       );
77     } else {
78       this.action = this.actionLabels.EDIT;
79     }
80   }
81
82   submit() {
83     this.smbService.passData(this.domainSettingsForm.value);
84     this.closeModal();
85   }
86
87   get join_sources() {
88     return this.domainSettingsForm.get('join_sources') as FormArray;
89   }
90
91   addJoinSource() {
92     this.join_sources.push(
93       new FormGroup({
94         ref: new FormControl('', Validators.required)
95       })
96     );
97     this.cd.detectChanges();
98   }
99
100   removeJoinSource(index: number) {
101     const join_sources = this.domainSettingsForm.get('join_sources') as FormArray;
102
103     if (index >= 0 && index < join_sources.length) {
104       join_sources.removeAt(index);
105     }
106
107     this.cd.detectChanges();
108   }
109 }