2018-04-04 14:22:55 +02:00
|
|
|
import { Directive, ElementRef, Input, NgZone } from "@angular/core";
|
2021-10-21 00:13:37 +02:00
|
|
|
import { take } from "rxjs/operators";
|
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { Utils } from "@bitwarden/common/misc/utils";
|
2018-08-01 15:13:49 +02:00
|
|
|
|
2018-04-04 14:22:55 +02:00
|
|
|
@Directive({
|
|
|
|
selector: "[appAutofocus]",
|
|
|
|
})
|
|
|
|
export class AutofocusDirective {
|
|
|
|
@Input() set appAutofocus(condition: boolean | string) {
|
|
|
|
this.autofocus = condition === "" || condition === true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private autofocus: boolean;
|
|
|
|
|
2021-10-21 00:13:37 +02:00
|
|
|
constructor(private el: ElementRef, private ngZone: NgZone) {}
|
2018-04-04 14:22:55 +02:00
|
|
|
|
|
|
|
ngOnInit() {
|
2018-08-01 15:13:49 +02:00
|
|
|
if (!Utils.isMobileBrowser && this.autofocus) {
|
2021-10-21 00:13:37 +02:00
|
|
|
if (this.ngZone.isStable) {
|
|
|
|
this.el.nativeElement.focus();
|
|
|
|
} else {
|
|
|
|
this.ngZone.onStable.pipe(take(1)).subscribe(() => this.el.nativeElement.focus());
|
2018-04-04 14:22:55 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2018-04-04 14:22:55 +02:00
|
|
|
}
|