]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.ts
dc34b9f3c30bc21288826fb7c8b8491ee2f8a7d4
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / directives / autofocus.directive.ts
1 import { AfterViewInit, Directive, ElementRef, Input } from '@angular/core';
2
3 import _ from 'lodash';
4
5 @Directive({
6 selector: '[autofocus]' // tslint:disable-line
7 })
8 export class AutofocusDirective implements AfterViewInit {
9 private focus = true;
10
11 constructor(private elementRef: ElementRef) {}
12
13 ngAfterViewInit() {
14 const el: HTMLInputElement = this.elementRef.nativeElement;
15 if (this.focus && _.isFunction(el.focus)) {
16 el.focus();
17 }
18 }
19
20 @Input()
21 public set autofocus(condition: any) {
22 if (_.isBoolean(condition)) {
23 this.focus = condition;
24 } else if (_.isFunction(condition)) {
25 this.focus = condition();
26 }
27 }
28 }