Skip to content

Commit

Permalink
perf: lazily initialise event handlers
Browse files Browse the repository at this point in the history
Closes #52
  • Loading branch information
Matt Lewis committed Aug 28, 2017
1 parent 200e5eb commit 08eebab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/confirmationPopover.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ export class ConfirmationPopover implements OnDestroy, OnChanges, OnInit {
*/
popover: ComponentRef<ConfirmationPopoverWindow> = null;

/**
* @private
*/
eventListeners: Function[] = [];

/**
* @private
*/
Expand Down Expand Up @@ -226,18 +231,6 @@ export class ConfirmationPopover implements OnDestroy, OnChanges, OnInit {
this.hidePopover();
}

/**
* @private
*/
@HostListener('document:click', ['$event.target'])
@HostListener('document:touchend', ['$event.target'])
onDocumentClick(target: HTMLElement): void {

if (this.popover && !this.elm.nativeElement.contains(target) && !this.popover.location.nativeElement.contains(target)) {
this.hidePopover();
}
}

/**
* @private
*/
Expand All @@ -250,17 +243,21 @@ export class ConfirmationPopover implements OnDestroy, OnChanges, OnInit {
}
}

/**
* @private
*/
@HostListener('window:resize')
onResize(): void {
this.positionPopover();
private onDocumentClick(event: Event): void {
if (this.popover && !this.elm.nativeElement.contains(event.target) && !this.popover.location.nativeElement.contains(event.target)) {
this.hidePopover();
}
}

private showPopover(): void {
if (!this.popover && !this.isDisabled) {

this.eventListeners = [
this.renderer.listen('document', 'click', (event: Event) => this.onDocumentClick(event)),
this.renderer.listen('document', 'touchend', (event: Event) => this.onDocumentClick(event)),
this.renderer.listen('window', 'resize', () => this.positionPopover())
];

const options: ConfirmationPopoverWindowOptions = new ConfirmationPopoverWindowOptions();
Object.assign(options, this.defaultOptions, {
title: this.title,
Expand Down Expand Up @@ -330,6 +327,8 @@ export class ConfirmationPopover implements OnDestroy, OnChanges, OnInit {
this.popover.destroy();
this.popover = null;
this.isOpenChange.emit(false);
this.eventListeners.forEach(fn => fn());
this.eventListeners = [];
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/angular-confirmation-popover.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ describe('bootstrap confirm', () => {
it('should re-position the popover when the window resizes', () => {
const fixture: ComponentFixture<TestCmp> = TestBed.createComponent(TestCmp);
fixture.detectChanges();
fixture.componentInstance.focusButton = 'confirm';
fixture.detectChanges();
clickFixture(fixture);
const popover: ComponentRef<ConfirmationPopoverWindow> = fixture.componentInstance.confirm.popover;
popover.changeDetectorRef.detectChanges();
const positionPopover: sinon.SinonSpy = sinon.spy(fixture.componentInstance.confirm as any, 'positionPopover');
window.dispatchEvent(new Event('resize'));
expect(positionPopover).to.have.been.calledOnce;
Expand Down

0 comments on commit 08eebab

Please sign in to comment.