1 import { Component, ContentChild, Input, OnInit, TemplateRef } from '@angular/core';
2 import { FormArray, FormControl, NgForm, Validators } from '@angular/forms';
4 import _ from 'lodash';
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';
11 selector: 'cd-nfs-form-client',
12 templateUrl: './nfs-form-client.component.html',
13 styleUrls: ['./nfs-form-client.component.scss']
15 export class NfsFormClientComponent implements OnInit {
22 @ContentChild('squashHelper', { static: true }) squashHelperTpl: TemplateRef<any>;
24 nfsSquash: any[] = Object.keys(this.nfsService.nfsSquash);
25 nfsAccessType: any[] = this.nfsService.nfsAccessType;
27 clientsFormArray: FormArray;
29 constructor(private nfsService: NfsService) {}
32 _.forEach(this.clients, (client) => {
33 const fg = this.addClient();
34 fg.patchValue(client);
36 this.clientsFormArray = this.form.get('clients') as FormArray;
39 getNoAccessTypeDescr() {
40 if (this.form.getValue('access_type')) {
41 return `${this.form.getValue('access_type')} ${$localize`(inherited from global config)`}`;
43 return $localize`-- Select the access type --`;
46 getAccessTypeHelp(index: number) {
47 const accessTypeItem = this.nfsAccessType.find((currentAccessTypeItem) => {
48 return this.getValue(index, 'access_type') === currentAccessTypeItem.value;
50 return _.isObjectLike(accessTypeItem) ? accessTypeItem.help : '';
54 if (this.form.getValue('squash')) {
55 return `${this.form.getValue('squash')} (${$localize`inherited from global config`})`;
57 return $localize`-- Select what kind of user id squashing is performed --`;
61 this.clientsFormArray = this.form.get('clients') as FormArray;
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)]
69 access_type: new FormControl(''),
70 squash: new FormControl('')
73 this.clientsFormArray.push(fg);
77 removeClient(index: number) {
78 this.clientsFormArray = this.form.get('clients') as FormArray;
79 this.clientsFormArray.removeAt(index);
82 showError(index: number, control: string, formDir: NgForm, x: string) {
83 return (<any>this.form.controls.clients).controls[index].showError(control, formDir, x);
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);
92 trackByFn(index: number) {