[Library]: Fix directive click outside for server mode.

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@57325 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
k.triantafyllou 2019-10-09 09:53:59 +00:00
parent 8e7a67932c
commit f07c3b6f1f
1 changed files with 31 additions and 25 deletions

View File

@ -3,6 +3,7 @@ import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/fromEvent'; import 'rxjs/add/observable/fromEvent';
import 'rxjs/add/operator/delay'; import 'rxjs/add/operator/delay';
import 'rxjs/add/operator/do'; import 'rxjs/add/operator/do';
import {Subscriber} from "rxjs";
@Directive({ @Directive({
selector: '[click-outside-or-esc]' selector: '[click-outside-or-esc]'
@ -10,8 +11,7 @@ import 'rxjs/add/operator/do';
export class ClickOutsideOrEsc implements OnInit, OnDestroy { export class ClickOutsideOrEsc implements OnInit, OnDestroy {
private listening: boolean; private listening: boolean;
private globalClick: any; private subscriptions: any[] = [];
private escClick: any;
@Output('clickOutside') clickOutside: EventEmitter<Object>; @Output('clickOutside') clickOutside: EventEmitter<Object>;
@ -21,32 +21,38 @@ export class ClickOutsideOrEsc implements OnInit, OnDestroy {
} }
ngOnInit() { ngOnInit() {
this.globalClick = Observable if(typeof document !== 'undefined') {
this.subscriptions.push(Observable
.fromEvent(document, 'click') .fromEvent(document, 'click')
.delay(1) .delay(1)
.do(() => { .do(() => {
this.listening = true; this.listening = true;
}).subscribe((event: MouseEvent) => { }).subscribe((event: MouseEvent) => {
this.onGlobalClick(event); this.onGlobalClick(event);
}); }));
this.escClick = Observable this.subscriptions.push(Observable
.fromEvent(document, 'keydown') .fromEvent(document, 'keydown')
.delay(1) .delay(1)
.do(() => { .do(() => {
this.listening = true; this.listening = true;
}).subscribe((event: KeyboardEvent) => { }).subscribe((event: KeyboardEvent) => {
if(event.keyCode === 27) { if (event.keyCode === 27) {
this.clickOutside.emit({ this.clickOutside.emit({
target: (event.target || null), target: (event.target || null),
value: true value: true
}); });
} }
}); }));
}
} }
ngOnDestroy() { ngOnDestroy() {
this.globalClick.unsubscribe(); if (this.subscriptions) {
this.escClick.unsubscribe(); this.subscriptions.forEach((subscription: Subscriber<any>) => {
subscription.unsubscribe();
});
}
this.subscriptions = [];
} }
onGlobalClick(event: MouseEvent) { onGlobalClick(event: MouseEvent) {