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) {}
|
|
|
|
|
|
|
|
@HostListener('document:click', ['$event'])
|
|
|
|
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
|
|
|
|
|
|
|
@HostListener('window:keydown', ['$event'])
|
|
|
|
keyEvent(event: KeyboardEvent) {
|
|
|
|
if(event.code === 'Escape') {
|
|
|
|
this.clickOutside.emit({
|
|
|
|
event: event,
|
|
|
|
clicked: true
|
|
|
|
});
|
2019-10-04 10:45:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|