| /** |
| ****************************************************************************** |
| * Copyright © 2017-2018 PTA GmbH. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| ****************************************************************************** |
| */ |
| import {Directive, ElementRef, Output, EventEmitter, HostListener} from '@angular/core'; |
| |
| @Directive({ |
| selector: '[clickOutside]' |
| }) |
| export class ClickOutsideDirective { |
| constructor(private _elementRef: ElementRef) { |
| } |
| |
| @Output() |
| public clickOutside = new EventEmitter<MouseEvent>(); |
| |
| @HostListener('document:click', ['$event', '$event.target']) |
| public onClick(event: MouseEvent, targetElement: HTMLElement): void { |
| if (!targetElement) { |
| return; |
| } |
| |
| const clickedInside = this._elementRef.nativeElement.contains(targetElement); |
| if (!clickedInside) { |
| this.clickOutside.emit(event); |
| } |
| } |
| } |