Skip to content

Commit

Permalink
perf: skip unnecessary change detection
Browse files Browse the repository at this point in the history
  • Loading branch information
isoden authored and arkon committed Dec 31, 2017
1 parent 871db61 commit cbda570
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/click-outside.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
OnInit,
Output,
PLATFORM_ID,
SimpleChanges
SimpleChanges,
NgZone,
} from '@angular/core';
import { isPlatformBrowser } from '@angular/common';

Expand All @@ -31,6 +32,7 @@ export class ClickOutsideDirective implements OnInit, OnChanges, OnDestroy {
private _events: Array<string> = ['click'];

constructor(private _el: ElementRef,
private _ngZone: NgZone,
@Inject(PLATFORM_ID) private platformId: Object) {
this._initOnClickBody = this._initOnClickBody.bind(this);
this._onClickBody = this._onClickBody.bind(this);
Expand Down Expand Up @@ -68,7 +70,9 @@ export class ClickOutsideDirective implements OnInit, OnChanges, OnDestroy {
this._excludeCheck();

if (this.attachOutsideOnClick) {
this._events.forEach(e => this._el.nativeElement.addEventListener(e, this._initOnClickBody));
this._ngZone.runOutsideAngular(() => {
this._events.forEach(e => this._el.nativeElement.addEventListener(e, this._initOnClickBody));
});
} else {
this._initOnClickBody();
}
Expand All @@ -83,7 +87,9 @@ export class ClickOutsideDirective implements OnInit, OnChanges, OnDestroy {
}

private _initClickListeners() {
this._events.forEach(e => document.body.addEventListener(e, this._onClickBody));
this._ngZone.runOutsideAngular(() => {
this._events.forEach(e => document.body.addEventListener(e, this._onClickBody));
});
}

private _excludeCheck() {
Expand All @@ -109,7 +115,7 @@ export class ClickOutsideDirective implements OnInit, OnChanges, OnDestroy {
}

if (!this._el.nativeElement.contains(ev.target) && !this._shouldExclude(ev.target)) {
this.clickOutside.emit(ev);
this._ngZone.run(() => this.clickOutside.emit(ev));

if (this.attachOutsideOnClick) {
this._events.forEach(e => document.body.removeEventListener(e, this._onClickBody));
Expand Down

0 comments on commit cbda570

Please sign in to comment.