2021-07-14 13:19:57 +02:00
|
|
|
import { Directive, ElementRef, AfterViewInit, Input, Renderer2 } from "@angular/core";
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: "[mydpfocus]"
|
|
|
|
})
|
|
|
|
|
|
|
|
export class FocusDirective implements AfterViewInit {
|
|
|
|
@Input("mydpfocus") value: string;
|
|
|
|
|
2021-07-14 13:19:57 +02:00
|
|
|
constructor(private el: ElementRef, private renderer: Renderer2) {}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
// Focus to element: if value 0 = don't set focus, 1 = set only focus, 2 = set focus and set cursor position
|
|
|
|
ngAfterViewInit() {
|
|
|
|
if (this.value === "0") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-03 15:20:36 +02:00
|
|
|
console.info("focus");
|
|
|
|
/*if (typeof document !== 'undefined') {
|
|
|
|
this.el.nativeElement.focus();
|
|
|
|
}*/
|
2021-07-14 13:19:57 +02:00
|
|
|
this.el.nativeElement.focus();
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
// Set cursor position at the end of text if input element
|
|
|
|
if (this.value === "2") {
|
|
|
|
let len = this.el.nativeElement.value.length;
|
|
|
|
this.el.nativeElement.setSelectionRange(len, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|