mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-08 02:24:11 +00:00
29 lines
777 B
TypeScript
29 lines
777 B
TypeScript
import { Directive, ElementRef, Output, EventEmitter, HostListener } from '@angular/core';
|
|
|
|
@Directive({
|
|
selector: '[dsClickOutside]'
|
|
})
|
|
/**
|
|
* Directive to detect when the users clicks outside of the element the directive was put on
|
|
*/
|
|
export class ClickOutsideDirective {
|
|
/**
|
|
* Emits null when the user clicks outside of the element
|
|
*/
|
|
@Output()
|
|
public dsClickOutside = new EventEmitter();
|
|
|
|
constructor(private _elementRef: ElementRef) {
|
|
}
|
|
|
|
@HostListener('document:click')
|
|
public onClick() {
|
|
const hostElement = this._elementRef.nativeElement;
|
|
const focusElement = hostElement.ownerDocument.activeElement;
|
|
const clickedInside = hostElement.contains(focusElement);
|
|
if (!clickedInside) {
|
|
this.dsClickOutside.emit(null);
|
|
}
|
|
}
|
|
}
|