forked from jebaGem/AngularRedux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrop-down-directive.ts
44 lines (41 loc) · 1.39 KB
/
drop-down-directive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Directive, ElementRef, Output, EventEmitter, HostListener } from '@angular/core';
import { isNil } from 'lodash';
@Directive({
selector: '[appClickOutside]',
})
export class ClickOutsideDirective {
@Output() public clickOutside = new EventEmitter();
constructor(private _elementRef: ElementRef) {
}
@HostListener('document:keypress', ['$event.target'])
public onTypes(targetElement) {
const isClickedInside = this._elementRef.nativeElement.contains(targetElement);
if (!isClickedInside) {
this.clickOutside.emit(null);
}
}
@HostListener('document:keydown', ['$event'])
public onTab(targetElementCode) {
const isTabClicked = targetElementCode.key === 'Tab';
if (isTabClicked) {
this.clickOutside.emit(null);
}
}
@HostListener('document:click', ['$event.target'])
public onClick(targetElement) {
const isClickedInside = this._elementRef.nativeElement.contains(targetElement);
let isClickInsideList = false;
const isCLickInTheList = targetElement.parentElement.parentElement;
if (!isNil(isCLickInTheList)) {
if (targetElement.parentElement.parentElement.className.indexOf('width-half') >= 0) {
isClickInsideList = true;
}
}
if (!isClickInsideList) {
this.clickOutside.emit(null);
}
if (!isClickedInside) {
this.clickOutside.emit(null);
}
}
}