summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/autofocus.directive.ts
blob: dc34b9f3c30bc21288826fb7c8b8491ee2f8a7d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { AfterViewInit, Directive, ElementRef, Input } from '@angular/core';

import _ from 'lodash';

@Directive({
  selector: '[autofocus]' // tslint:disable-line
})
export class AutofocusDirective implements AfterViewInit {
  private focus = true;

  constructor(private elementRef: ElementRef) {}

  ngAfterViewInit() {
    const el: HTMLInputElement = this.elementRef.nativeElement;
    if (this.focus && _.isFunction(el.focus)) {
      el.focus();
    }
  }

  @Input()
  public set autofocus(condition: any) {
    if (_.isBoolean(condition)) {
      this.focus = condition;
    } else if (_.isFunction(condition)) {
      this.focus = condition();
    }
  }
}