1 |
57336
|
k.triantaf
|
import {Directive, OnInit, OnDestroy, Output, EventEmitter, ElementRef, Input} from '@angular/core';
|
2 |
57261
|
k.triantaf
|
import {Observable} from 'rxjs/Observable';
|
3 |
|
|
import 'rxjs/add/observable/fromEvent';
|
4 |
|
|
import 'rxjs/add/operator/delay';
|
5 |
|
|
import 'rxjs/add/operator/do';
|
6 |
57325
|
k.triantaf
|
import {Subscriber} from "rxjs";
|
7 |
57261
|
k.triantaf
|
|
8 |
|
|
@Directive({
|
9 |
|
|
selector: '[click-outside-or-esc]'
|
10 |
|
|
})
|
11 |
|
|
|
12 |
|
|
export class ClickOutsideOrEsc implements OnInit, OnDestroy {
|
13 |
|
|
private listening: boolean;
|
14 |
57325
|
k.triantaf
|
private subscriptions: any[] = [];
|
15 |
57336
|
k.triantaf
|
@Input()
|
16 |
|
|
public targetId = null;
|
17 |
57665
|
k.triantaf
|
@Input()
|
18 |
|
|
public escClose = true;
|
19 |
57261
|
k.triantaf
|
|
20 |
|
|
@Output('clickOutside') clickOutside: EventEmitter<Object>;
|
21 |
|
|
|
22 |
|
|
constructor(private _elRef: ElementRef) {
|
23 |
|
|
this.listening = false;
|
24 |
|
|
this.clickOutside = new EventEmitter();
|
25 |
|
|
}
|
26 |
|
|
|
27 |
|
|
ngOnInit() {
|
28 |
57325
|
k.triantaf
|
if(typeof document !== 'undefined') {
|
29 |
|
|
this.subscriptions.push(Observable
|
30 |
|
|
.fromEvent(document, 'click')
|
31 |
|
|
.delay(1)
|
32 |
|
|
.do(() => {
|
33 |
|
|
this.listening = true;
|
34 |
|
|
}).subscribe((event: MouseEvent) => {
|
35 |
|
|
this.onGlobalClick(event);
|
36 |
|
|
}));
|
37 |
|
|
this.subscriptions.push(Observable
|
38 |
|
|
.fromEvent(document, 'keydown')
|
39 |
|
|
.delay(1)
|
40 |
|
|
.do(() => {
|
41 |
|
|
this.listening = true;
|
42 |
|
|
}).subscribe((event: KeyboardEvent) => {
|
43 |
57665
|
k.triantaf
|
if (event.keyCode === 27 && this.escClose) {
|
44 |
57325
|
k.triantaf
|
this.clickOutside.emit({
|
45 |
|
|
target: (event.target || null),
|
46 |
|
|
value: true
|
47 |
|
|
});
|
48 |
|
|
}
|
49 |
|
|
}));
|
50 |
|
|
}
|
51 |
57261
|
k.triantaf
|
}
|
52 |
|
|
|
53 |
|
|
ngOnDestroy() {
|
54 |
57325
|
k.triantaf
|
if (this.subscriptions) {
|
55 |
|
|
this.subscriptions.forEach((subscription: Subscriber<any>) => {
|
56 |
|
|
subscription.unsubscribe();
|
57 |
|
|
});
|
58 |
|
|
}
|
59 |
|
|
this.subscriptions = [];
|
60 |
57261
|
k.triantaf
|
}
|
61 |
|
|
|
62 |
|
|
onGlobalClick(event: MouseEvent) {
|
63 |
|
|
if (event instanceof MouseEvent && this.listening === true) {
|
64 |
58598
|
k.triantaf
|
let element: HTMLElement = <HTMLElement>event.target;
|
65 |
|
|
while (element) {
|
66 |
|
|
if(element.id === this.targetId) {
|
67 |
|
|
this.clickOutside.emit({
|
68 |
|
|
target: (event.target || null),
|
69 |
|
|
value: false
|
70 |
|
|
});
|
71 |
|
|
return;
|
72 |
|
|
}
|
73 |
|
|
element = element.parentElement;
|
74 |
57261
|
k.triantaf
|
}
|
75 |
58598
|
k.triantaf
|
this.clickOutside.emit({
|
76 |
|
|
target: (event.target || null),
|
77 |
|
|
value: true
|
78 |
|
|
});
|
79 |
57261
|
k.triantaf
|
}
|
80 |
|
|
}
|
81 |
|
|
}
|