2018-04-04 14:22:55 +02:00
|
|
|
import {
|
|
|
|
Directive,
|
|
|
|
ElementRef,
|
|
|
|
Input,
|
2021-10-21 00:13:37 +02:00
|
|
|
NgZone,
|
2018-04-04 14:22:55 +02:00
|
|
|
} from '@angular/core';
|
|
|
|
|
2021-10-21 00:13:37 +02:00
|
|
|
import { take } from 'rxjs/operators';
|
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { Utils } from 'jslib-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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|