Skip to content

Commit

Permalink
Add backButtonClicked event to page component
Browse files Browse the repository at this point in the history
  • Loading branch information
mictro committed Jun 16, 2022
1 parent e5a1c1b commit fc9fa15
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ export class PageShowcaseComponent {
description: 'Emitted when leaving the page',
signature: 'func',
},
{
name: 'backButtonClicked',
description:
'Emitted when the back-button is clicked. When bound, the default back-button click behaviour is disabled.',
signature: 'func',
},
];

layoutColumns: ApiDescriptionPropertyColumns = {
Expand Down
26 changes: 26 additions & 0 deletions libs/designsystem/src/lib/components/page/page.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,32 @@ describe('PageComponent', () => {
flush();
}));

describe('with a back-button', () => {
let ionBackButton;

beforeEach(() => {
ionBackButton = spectator.queryHost('ion-toolbar ion-buttons ion-back-button');
});

it('should call the default click handler if no back-button-click observer is provided', () => {
const defaultHandler = jasmine.createSpy();
ionBackButton.onclick = defaultHandler;

spectator.click(ionBackButton);

expect(defaultHandler).toHaveBeenCalledTimes(1);
});

it('should emit an event on click if a back-button-click observer is provided', () => {
const subscriber = jasmine.createSpy();
spectator.output('backButtonClick').subscribe(subscriber);

spectator.click(ionBackButton);

expect(subscriber).toHaveBeenCalledTimes(1);
});
});

describe('pull-to-refresh', () => {
it('should be available when "refresh" is subscribed to', () => {
spectator.output('refresh').subscribe(() => {});
Expand Down
19 changes: 18 additions & 1 deletion libs/designsystem/src/lib/components/page/page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
ViewChild,
} from '@angular/core';
import { NavigationEnd, NavigationStart, Router, RouterEvent } from '@angular/router';
import { IonContent, IonFooter, IonHeader } from '@ionic/angular';
import { IonBackButtonDelegate, IonContent, IonFooter, IonHeader } from '@ionic/angular';
import { Observable, Subject } from 'rxjs';
import { filter, takeUntil } from 'rxjs/operators';

Expand Down Expand Up @@ -161,6 +161,7 @@ export class PageComponent
@Output() enter = new EventEmitter<void>();
@Output() leave = new EventEmitter<void>();
@Output() refresh = new EventEmitter<PullToRefreshEvent>();
@Output() backButtonClick = new EventEmitter<Event>();

@ViewChild(IonContent, { static: true }) private content: IonContent;
@ViewChild(IonContent, { static: true, read: ElementRef })
Expand All @@ -170,6 +171,8 @@ export class PageComponent
@ViewChild(IonFooter, { static: true, read: ElementRef })
private ionFooterElement: ElementRef<HTMLIonFooterElement>;

@ViewChild(IonBackButtonDelegate, { static: false })
private backButtonDelegate: IonBackButtonDelegate;
@ViewChild('pageTitle', { static: false, read: ElementRef })
private pageTitle: ElementRef;

Expand Down Expand Up @@ -259,6 +262,8 @@ export class PageComponent
this.windowRef.nativeWindow.addEventListener(selectedTabClickEvent, () => {
this.content.scrollToTop(KirbyAnimation.Duration.LONG);
});

this.interceptBackButtonClicksSetup();
}

ngAfterContentChecked(): void {
Expand Down Expand Up @@ -311,6 +316,18 @@ export class PageComponent
}
}

private interceptBackButtonClicksSetup() {
// Intercept back-button click events, defaulting to the built-in click-handler.
if (this.backButtonClick.observers.length === 0) {
this.backButtonClick
.pipe(takeUntil(this.ngOnDestroy$))
.subscribe(this.backButtonDelegate.onClick.bind(this.backButtonDelegate));
}
this.backButtonDelegate.onClick = (event: Event) => {
this.backButtonClick.emit(event);
};
}

private initializeTitle() {
// Ensures initializeTitle() won't run, if already initialized
if (this.hasPageTitle) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export class MockPageComponent {
@Output() enter = new EventEmitter<void>();
@Output() leave = new EventEmitter<void>();
@Output() refresh = new EventEmitter<PullToRefreshEvent>();
@Output() backButtonClicked = new EventEmitter<Event>();
}

// #endregion

0 comments on commit fc9fa15

Please sign in to comment.