]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/forms/cd-form-group.ts
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / forms / cd-form-group.ts
1 import {
2 AbstractControl,
3 AbstractControlOptions,
4 AsyncValidatorFn,
5 FormGroup,
6 NgForm,
7 ValidatorFn
8 } from '@angular/forms';
9
10 /**
11 * CdFormGroup extends FormGroup with a few new methods that will help form development.
12 */
13 export class CdFormGroup extends FormGroup {
14 constructor(
15 public controls: { [key: string]: AbstractControl },
16 validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null,
17 asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null
18 ) {
19 super(controls, validatorOrOpts, asyncValidator);
20 }
21
22 /**
23 * Get a control out of any control even if its nested in other CdFormGroups or a FormGroup
24 */
25 get(controlName: string): AbstractControl {
26 const control = this._get(controlName);
27 if (!control) {
28 throw new Error(`Control '${controlName}' could not be found!`);
29 }
30 return control;
31 }
32
33 _get(controlName): AbstractControl {
34 return (
35 super.get(controlName) ||
36 Object.values(this.controls)
37 .filter((c) => c.get)
38 .map((form) => {
39 if (form instanceof CdFormGroup) {
40 return form._get(controlName);
41 }
42 return form.get(controlName);
43 })
44 .find((c) => Boolean(c))
45 );
46 }
47
48 /**
49 * Get the value of a control
50 */
51 getValue(controlName: string): any {
52 return this.get(controlName).value;
53 }
54
55 /**
56 * Sets a control without triggering a value changes event
57 *
58 * Very useful if a function is called through a value changes event but the value
59 * should be changed within the call.
60 */
61 silentSet(controlName: string, value: any) {
62 this.get(controlName).setValue(value, { emitEvent: false });
63 }
64
65 /**
66 * Indicates errors of the control in templates
67 */
68 showError(controlName: string, form: NgForm, errorName?: string): boolean {
69 const control = this.get(controlName);
70 return (
71 (form.submitted || control.dirty) &&
72 (errorName ? control.hasError(errorName) : control.invalid)
73 );
74 }
75 }