2022-04-13 12:00:40 +02:00
|
|
|
import {Directive, ElementRef, EventEmitter, HostListener, Output} from '@angular/core';
|
|
|
|
|
|
|
|
export interface ClickEvent {
|
|
|
|
event: any,
|
|
|
|
clicked: boolean;
|
|
|
|
}
|
2019-10-04 10:45:51 +02:00
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: '[click-outside-or-esc]'
|
|
|
|
})
|
2022-04-13 12:00:40 +02:00
|
|
|
export class ClickOutsideOrEsc {
|
|
|
|
@Output('clickOutside') clickOutside: EventEmitter<ClickEvent> = new EventEmitter<ClickEvent>();
|
2019-10-04 10:45:51 +02:00
|
|
|
|
2022-04-13 12:00:40 +02:00
|
|
|
constructor(private elementRef: ElementRef) {}
|
|
|
|
|
2022-04-17 14:30:21 +02:00
|
|
|
@HostListener('document:click', ['$event'])
|
2022-04-13 12:00:40 +02:00
|
|
|
click(event) {
|
|
|
|
if(event.isTrusted) {
|
|
|
|
this.clickOutside.emit({
|
|
|
|
event: event,
|
|
|
|
clicked: !(this.elementRef && this.elementRef.nativeElement.contains(event.target))
|
2019-10-09 11:53:59 +02:00
|
|
|
});
|
|
|
|
}
|
2019-10-04 10:45:51 +02:00
|
|
|
}
|
2022-04-13 12:00:40 +02:00
|
|
|
|
2022-04-17 14:25:01 +02:00
|
|
|
@HostListener('window:keydown.escape', ['$event'])
|
|
|
|
esc(event: KeyboardEvent) {
|
|
|
|
this.clickOutside.emit({
|
|
|
|
event: event,
|
|
|
|
clicked: true
|
|
|
|
});
|
2019-10-04 10:45:51 +02:00
|
|
|
}
|
|
|
|
}
|