All files / src/app/core/directives click-outside.directive.ts

28.57% Statements 2/7
60% Branches 3/5
0% Functions 0/2
28.57% Lines 2/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 282x               2x                                      
import { Directive, ElementRef, EventEmitter, HostListener, Output } from '@angular/core';
 
/**
 * This directive can be set on a html tag to check click outside of the tag.
 */
@Directive({
  selector: '[ishClickOutside]',
})
export class ClickOutsideDirective {
  constructor(private elementRef: ElementRef) {}
 
  /**
   * Event to tell the listener, when there was an outside click
   */
  @Output() isClickedOutside = new EventEmitter<boolean>();
 
  /**
   * Method to check click outside of the targetElement. Emits true, when a click outside was checked.
   */
  @HostListener('document:click', ['$event.target'])
  onClick(targetElement: ElementRef): void {
    const clickedInside = this.elementRef.nativeElement.contains(targetElement);
    Iif (!clickedInside) {
      this.isClickedOutside.emit(true);
    }
  }
}