]>
Commit | Line | Data |
---|---|---|
a4b75251 | 1 | import { Component, ContentChild, Input, OnInit, TemplateRef } from '@angular/core'; |
aee94f69 | 2 | import { UntypedFormArray, UntypedFormControl, NgForm, Validators } from '@angular/forms'; |
11fdf7f2 | 3 | |
f67539c2 | 4 | import _ from 'lodash'; |
11fdf7f2 | 5 | |
f67539c2 TL |
6 | import { NfsService } from '~/app/shared/api/nfs.service'; |
7 | import { Icons } from '~/app/shared/enum/icons.enum'; | |
8 | import { CdFormGroup } from '~/app/shared/forms/cd-form-group'; | |
11fdf7f2 TL |
9 | |
10 | @Component({ | |
11 | selector: 'cd-nfs-form-client', | |
12 | templateUrl: './nfs-form-client.component.html', | |
13 | styleUrls: ['./nfs-form-client.component.scss'] | |
14 | }) | |
f67539c2 | 15 | export class NfsFormClientComponent implements OnInit { |
11fdf7f2 TL |
16 | @Input() |
17 | form: CdFormGroup; | |
18 | ||
f67539c2 TL |
19 | @Input() |
20 | clients: any[]; | |
21 | ||
a4b75251 TL |
22 | @ContentChild('squashHelper', { static: true }) squashHelperTpl: TemplateRef<any>; |
23 | ||
aee94f69 TL |
24 | nfsSquash: any[] = []; |
25 | nfsAccessType: any[] = []; | |
9f95a23c | 26 | icons = Icons; |
aee94f69 | 27 | clientsFormArray: UntypedFormArray; |
11fdf7f2 | 28 | |
f67539c2 TL |
29 | constructor(private nfsService: NfsService) {} |
30 | ||
31 | ngOnInit() { | |
aee94f69 TL |
32 | this.nfsSquash = Object.keys(this.nfsService.nfsSquash); |
33 | this.nfsAccessType = this.nfsService.nfsAccessType; | |
f67539c2 TL |
34 | _.forEach(this.clients, (client) => { |
35 | const fg = this.addClient(); | |
36 | fg.patchValue(client); | |
37 | }); | |
aee94f69 | 38 | this.clientsFormArray = this.form.get('clients') as UntypedFormArray; |
f67539c2 | 39 | } |
11fdf7f2 TL |
40 | |
41 | getNoAccessTypeDescr() { | |
42 | if (this.form.getValue('access_type')) { | |
f67539c2 | 43 | return `${this.form.getValue('access_type')} ${$localize`(inherited from global config)`}`; |
11fdf7f2 | 44 | } |
f67539c2 | 45 | return $localize`-- Select the access type --`; |
11fdf7f2 TL |
46 | } |
47 | ||
9f95a23c | 48 | getAccessTypeHelp(index: number) { |
11fdf7f2 TL |
49 | const accessTypeItem = this.nfsAccessType.find((currentAccessTypeItem) => { |
50 | return this.getValue(index, 'access_type') === currentAccessTypeItem.value; | |
51 | }); | |
52 | return _.isObjectLike(accessTypeItem) ? accessTypeItem.help : ''; | |
53 | } | |
54 | ||
55 | getNoSquashDescr() { | |
56 | if (this.form.getValue('squash')) { | |
f67539c2 | 57 | return `${this.form.getValue('squash')} (${$localize`inherited from global config`})`; |
11fdf7f2 | 58 | } |
f67539c2 | 59 | return $localize`-- Select what kind of user id squashing is performed --`; |
11fdf7f2 TL |
60 | } |
61 | ||
62 | addClient() { | |
aee94f69 | 63 | this.clientsFormArray = this.form.get('clients') as UntypedFormArray; |
11fdf7f2 TL |
64 | |
65 | const REGEX_IP = `(([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\.([0-9]{1,3})([/](\\d|[1-2]\\d|3[0-2]))?)`; | |
66 | const REGEX_LIST_IP = `${REGEX_IP}([ ,]{1,2}${REGEX_IP})*`; | |
11fdf7f2 | 67 | const fg = new CdFormGroup({ |
aee94f69 | 68 | addresses: new UntypedFormControl('', { |
11fdf7f2 TL |
69 | validators: [Validators.required, Validators.pattern(REGEX_LIST_IP)] |
70 | }), | |
aee94f69 TL |
71 | access_type: new UntypedFormControl(''), |
72 | squash: new UntypedFormControl('') | |
11fdf7f2 TL |
73 | }); |
74 | ||
a4b75251 | 75 | this.clientsFormArray.push(fg); |
11fdf7f2 TL |
76 | return fg; |
77 | } | |
78 | ||
9f95a23c | 79 | removeClient(index: number) { |
aee94f69 | 80 | this.clientsFormArray = this.form.get('clients') as UntypedFormArray; |
a4b75251 | 81 | this.clientsFormArray.removeAt(index); |
11fdf7f2 TL |
82 | } |
83 | ||
9f95a23c | 84 | showError(index: number, control: string, formDir: NgForm, x: string) { |
11fdf7f2 TL |
85 | return (<any>this.form.controls.clients).controls[index].showError(control, formDir, x); |
86 | } | |
87 | ||
9f95a23c | 88 | getValue(index: number, control: string) { |
aee94f69 | 89 | this.clientsFormArray = this.form.get('clients') as UntypedFormArray; |
a4b75251 | 90 | const client = this.clientsFormArray.at(index) as CdFormGroup; |
11fdf7f2 TL |
91 | return client.getValue(control); |
92 | } | |
93 | ||
9f95a23c | 94 | trackByFn(index: number) { |
11fdf7f2 TL |
95 | return index; |
96 | } | |
97 | } |