From 1daa8b0ab382e745d23c2140bae0749d1a1dd4b1 Mon Sep 17 00:00:00 2001 From: Volker Theile Date: Fri, 20 Apr 2018 14:33:44 +0200 Subject: [PATCH] mgr/dashboard: Add 'autofocus' directive Signed-off-by: Volker Theile --- .../directives/autofocus.directive.spec.ts | 8 ++++++++ .../shared/directives/autofocus.directive.ts | 17 +++++++++++++++++ .../frontend/src/app/shared/shared.module.ts | 4 +++- 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.spec.ts create mode 100644 src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.ts diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.spec.ts new file mode 100644 index 0000000000000..3126b4cfba76f --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.spec.ts @@ -0,0 +1,8 @@ +import { AutofocusDirective } from './autofocus.directive'; + +describe('AutofocusDirective', () => { + it('should create an instance', () => { + const directive = new AutofocusDirective(null); + expect(directive).toBeTruthy(); + }); +}); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.ts new file mode 100644 index 0000000000000..3c5eae227eed6 --- /dev/null +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.ts @@ -0,0 +1,17 @@ +import { Directive, ElementRef, OnInit } from '@angular/core'; + +@Directive({ + selector: '[autofocus]' // tslint:disable-line +}) +export class AutofocusDirective implements OnInit { + + constructor(private elementRef: ElementRef) {} + + ngOnInit() { + setTimeout(() => { + if (this.elementRef && this.elementRef.nativeElement) { + this.elementRef.nativeElement.focus(); + } + }, 0); + } +} diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/shared.module.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/shared.module.ts index 584ed09681703..1ed5da8f07864 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/shared.module.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/shared.module.ts @@ -4,6 +4,7 @@ import { NgModule } from '@angular/core'; import { ApiModule } from './api/api.module'; import { ComponentsModule } from './components/components.module'; import { DataTableModule } from './datatable/datatable.module'; +import { AutofocusDirective } from './directives/autofocus.directive'; import { DimlessBinaryDirective } from './directives/dimless-binary.directive'; import { PasswordButtonDirective } from './directives/password-button.directive'; import { PipesModule } from './pipes/pipes.module'; @@ -23,7 +24,8 @@ import { ServicesModule } from './services/services.module'; ], declarations: [ PasswordButtonDirective, - DimlessBinaryDirective + DimlessBinaryDirective, + AutofocusDirective ], exports: [ ComponentsModule, -- 2.39.5