2021-07-14 13:19:57 +02:00
|
|
|
import {Directive, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output} from '@angular/core';
|
|
|
|
import {fromEvent, Subscriber} from 'rxjs';
|
|
|
|
import {delay, tap} from 'rxjs/operators';
|
2019-10-04 10:45:51 +02:00
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: '[click-outside-or-esc]'
|
|
|
|
})
|
|
|
|
|
|
|
|
export class ClickOutsideOrEsc implements OnInit, OnDestroy {
|
|
|
|
private listening: boolean;
|
2019-10-09 11:53:59 +02:00
|
|
|
private subscriptions: any[] = [];
|
2019-10-10 14:13:01 +02:00
|
|
|
@Input()
|
|
|
|
public targetId = null;
|
2019-11-22 09:51:17 +01:00
|
|
|
@Input()
|
|
|
|
public escClose = true;
|
2020-08-04 09:25:32 +02:00
|
|
|
@Input()
|
|
|
|
public clickClose = true;
|
2019-10-04 10:45:51 +02:00
|
|
|
@Output('clickOutside') clickOutside: EventEmitter<Object>;
|
|
|
|
|
|
|
|
constructor(private _elRef: ElementRef) {
|
|
|
|
this.listening = false;
|
|
|
|
this.clickOutside = new EventEmitter();
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2019-10-09 11:53:59 +02:00
|
|
|
if(typeof document !== 'undefined') {
|
2021-07-14 13:19:57 +02:00
|
|
|
this.subscriptions.push(fromEvent(document, 'click').pipe(
|
|
|
|
delay(1),
|
|
|
|
tap(() => {
|
2019-10-09 11:53:59 +02:00
|
|
|
this.listening = true;
|
2021-07-14 13:19:57 +02:00
|
|
|
})).subscribe((event: MouseEvent) => {
|
2019-10-09 11:53:59 +02:00
|
|
|
this.onGlobalClick(event);
|
|
|
|
}));
|
2021-07-14 13:19:57 +02:00
|
|
|
this.subscriptions.push(fromEvent(document, 'click').pipe(
|
|
|
|
delay(1),
|
|
|
|
tap(() => {
|
2019-10-09 11:53:59 +02:00
|
|
|
this.listening = true;
|
2021-07-14 13:19:57 +02:00
|
|
|
})).subscribe((event: KeyboardEvent) => {
|
2019-11-22 09:51:17 +01:00
|
|
|
if (event.keyCode === 27 && this.escClose) {
|
2019-10-09 11:53:59 +02:00
|
|
|
this.clickOutside.emit({
|
|
|
|
target: (event.target || null),
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2019-10-04 10:45:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
2019-10-09 11:53:59 +02:00
|
|
|
if (this.subscriptions) {
|
|
|
|
this.subscriptions.forEach((subscription: Subscriber<any>) => {
|
|
|
|
subscription.unsubscribe();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.subscriptions = [];
|
2019-10-04 10:45:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onGlobalClick(event: MouseEvent) {
|
|
|
|
if (event instanceof MouseEvent && this.listening === true) {
|
2020-05-05 12:37:36 +02:00
|
|
|
let element: HTMLElement = <HTMLElement>event.target;
|
|
|
|
while (element) {
|
|
|
|
if(element.id === this.targetId) {
|
|
|
|
this.clickOutside.emit({
|
|
|
|
target: (event.target || null),
|
|
|
|
value: false
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
element = element.parentElement;
|
2019-10-04 10:45:51 +02:00
|
|
|
}
|
2020-08-04 09:25:32 +02:00
|
|
|
if(this.clickClose) {
|
|
|
|
this.clickOutside.emit({
|
|
|
|
target: (event.target || null),
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
}
|
2019-10-04 10:45:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|