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