-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ng-click-outside is deprecated and not maintained
- Loading branch information
Showing
8 changed files
with
78 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { | ||
Directive, | ||
ElementRef, | ||
EventEmitter, | ||
Input, | ||
NgZone, | ||
OnDestroy, | ||
OnInit, | ||
Output | ||
} from '@angular/core'; | ||
|
||
// eslint-disable-next-line @angular-eslint/directive-selector | ||
@Directive({ selector: '[clickOutside]' }) | ||
export class ClickOutsideDirective implements OnInit, OnDestroy { | ||
@Input() exclude = ''; | ||
@Output() clickOutside: EventEmitter<Event> = new EventEmitter<Event>(); | ||
|
||
private excludeElements: HTMLElement[] = []; | ||
private events: string[] = ['click']; | ||
|
||
constructor(private elementRef: ElementRef, private zone: NgZone) { | ||
this.eventListener = this.eventListener.bind(this); | ||
} | ||
|
||
ngOnInit() { | ||
if (this.exclude) { | ||
try { | ||
const elements = Array.from( | ||
document.querySelectorAll(this.exclude) | ||
) as HTMLElement[]; | ||
if (elements) { | ||
this.excludeElements = elements; | ||
} | ||
} catch (err) { | ||
console.error('Check the exclude selector syntax!', err); | ||
} | ||
} | ||
this.initEventListener(); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.zone.runOutsideAngular(() => { | ||
this.events.forEach((e) => | ||
document.removeEventListener(e, this.eventListener) | ||
); | ||
}); | ||
} | ||
|
||
private initEventListener() { | ||
this.zone.runOutsideAngular(() => { | ||
this.events.forEach((e) => | ||
document.addEventListener(e, this.eventListener) | ||
); | ||
}); | ||
} | ||
|
||
private eventListener(e: Event) { | ||
if (!e || !e.target) return; | ||
if ( | ||
!this.elementRef.nativeElement.contains(e.target) && | ||
!this.excludeElements.some((el) => el.contains(e.target as Node)) | ||
) { | ||
this.emit(e); | ||
} | ||
} | ||
|
||
private emit(e: Event) { | ||
this.zone.run(() => this.clickOutside.emit(e)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters