diff options
Diffstat (limited to 'src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation')
6 files changed, 334 insertions, 0 deletions
diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-control.directive.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-control.directive.spec.ts new file mode 100644 index 000000000..dd588ae7b --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-control.directive.spec.ts @@ -0,0 +1,37 @@ +/** + * MIT License + * + * Copyright (c) 2017 Kevin Kipp + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * + * Based on https://github.com/third774/ng-bootstrap-form-validation + */ + +import { NgForm } from '@angular/forms'; + +import { CdFormControlDirective } from './cd-form-control.directive'; + +describe('CdFormControlDirective', () => { + it('should create an instance', () => { + const directive = new CdFormControlDirective(new NgForm([], [])); + expect(directive).toBeTruthy(); + }); +}); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-control.directive.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-control.directive.ts new file mode 100644 index 000000000..8e37f7ddd --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-control.directive.ts @@ -0,0 +1,82 @@ +/** + * MIT License + * + * Copyright (c) 2017 Kevin Kipp + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * + * Based on https://github.com/third774/ng-bootstrap-form-validation + */ + +import { Directive, Host, HostBinding, Input, Optional, SkipSelf } from '@angular/core'; +import { ControlContainer, UntypedFormControl } from '@angular/forms'; + +export function controlPath(name: string, parent: ControlContainer): string[] { + // tslint:disable-next-line:no-non-null-assertion + return [...parent.path!, name]; +} + +@Directive({ + // eslint-disable-next-line + selector: '.form-control,.form-check-input,.custom-control-input' +}) +export class CdFormControlDirective { + @Input() + formControlName: string; + @Input() + formControl: string; + + @HostBinding('class.is-valid') + get validClass() { + if (!this.control) { + return false; + } + return this.control.valid && (this.control.touched || this.control.dirty); + } + + @HostBinding('class.is-invalid') + get invalidClass() { + if (!this.control) { + return false; + } + return this.control.invalid && this.control.touched && this.control.dirty; + } + + get path() { + return controlPath(this.formControlName, this.parent); + } + + get control(): UntypedFormControl { + return this.formDirective && this.formDirective.getControl(this); + } + + get formDirective(): any { + return this.parent ? this.parent.formDirective : null; + } + + constructor( + // this value might be null, but we union type it as such until + // this issue is resolved: https://github.com/angular/angular/issues/25544 + @Optional() + @Host() + @SkipSelf() + private parent: ControlContainer + ) {} +} diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-group.directive.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-group.directive.spec.ts new file mode 100644 index 000000000..40aa251cd --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-group.directive.spec.ts @@ -0,0 +1,37 @@ +/** + * MIT License + * + * Copyright (c) 2017 Kevin Kipp + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * + * Based on https://github.com/third774/ng-bootstrap-form-validation + */ + +import { ElementRef } from '@angular/core'; + +import { CdFormGroupDirective } from './cd-form-group.directive'; + +describe('CdFormGroupDirective', () => { + it('should create an instance', () => { + const directive = new CdFormGroupDirective(new ElementRef(null)); + expect(directive).toBeTruthy(); + }); +}); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-group.directive.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-group.directive.ts new file mode 100644 index 000000000..adac343d0 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-group.directive.ts @@ -0,0 +1,76 @@ +/** + * MIT License + * + * Copyright (c) 2017 Kevin Kipp + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * + * Based on https://github.com/third774/ng-bootstrap-form-validation + */ + +import { + ContentChildren, + Directive, + ElementRef, + HostBinding, + Input, + QueryList +} from '@angular/core'; +import { FormControlName } from '@angular/forms'; + +@Directive({ + // eslint-disable-next-line + selector: '.form-group' +}) +export class CdFormGroupDirective { + @ContentChildren(FormControlName) + formControlNames: QueryList<FormControlName>; + + @Input() + validationDisabled = false; + + @HostBinding('class.has-error') + get hasErrors() { + return ( + this.formControlNames.some((c) => !c.valid && c.dirty && c.touched) && + !this.validationDisabled + ); + } + + @HostBinding('class.has-success') + get hasSuccess() { + return ( + !this.formControlNames.some((c) => !c.valid) && + this.formControlNames.some((c) => c.dirty && c.touched) && + !this.validationDisabled + ); + } + + constructor(private elRef: ElementRef) {} + + get label() { + const label = this.elRef.nativeElement.querySelector('label'); + return label && label.textContent ? label.textContent.trim() : 'This field'; + } + + get isDirtyAndTouched() { + return this.formControlNames.some((c) => c.dirty && c.touched); + } +} diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-validation.directive.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-validation.directive.spec.ts new file mode 100644 index 000000000..c4b0f424b --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-validation.directive.spec.ts @@ -0,0 +1,35 @@ +/** + * MIT License + * + * Copyright (c) 2017 Kevin Kipp + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * + * Based on https://github.com/third774/ng-bootstrap-form-validation + */ + +import { CdFormValidationDirective } from './cd-form-validation.directive'; + +describe('CdFormValidationDirective', () => { + it('should create an instance', () => { + const directive = new CdFormValidationDirective(); + expect(directive).toBeTruthy(); + }); +}); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-validation.directive.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-validation.directive.ts new file mode 100644 index 000000000..5f37f8583 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/ng-bootstrap-form-validation/cd-form-validation.directive.ts @@ -0,0 +1,67 @@ +/** + * MIT License + * + * Copyright (c) 2017 Kevin Kipp + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * + * Based on https://github.com/third774/ng-bootstrap-form-validation + */ + +import { Directive, EventEmitter, HostListener, Input, Output } from '@angular/core'; +import { + AbstractControl, + UntypedFormArray, + UntypedFormControl, + UntypedFormGroup +} from '@angular/forms'; + +@Directive({ + // eslint-disable-next-line + selector: '[formGroup]' +}) +export class CdFormValidationDirective { + @Input() + formGroup: UntypedFormGroup; + @Output() + validSubmit = new EventEmitter<any>(); + + @HostListener('submit') + onSubmit() { + this.markAsTouchedAndDirty(this.formGroup); + if (this.formGroup.valid) { + this.validSubmit.emit(this.formGroup.value); + } + } + + markAsTouchedAndDirty(control: AbstractControl) { + if (control instanceof UntypedFormGroup) { + Object.keys(control.controls).forEach((key) => + this.markAsTouchedAndDirty(control.controls[key]) + ); + } else if (control instanceof UntypedFormArray) { + control.controls.forEach((c) => this.markAsTouchedAndDirty(c)); + } else if (control instanceof UntypedFormControl && control.enabled) { + control.markAsDirty(); + control.markAsTouched(); + control.updateValueAndValidity(); + } + } +} |