Skip to content

Commit 9f293e8

Browse files
committed
feat(navPop): add nav pop method on the app instance
1 parent 84f37cf commit 9f293e8

File tree

6 files changed

+473
-39
lines changed

6 files changed

+473
-39
lines changed

src/components/app/app.ts

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {Injectable, Injector} from '@angular/core';
22
import {Title} from '@angular/platform-browser';
33

4-
import {Config} from '../../config/config';
54
import {ClickBlock} from '../../util/click-block';
5+
import {Config} from '../../config/config';
6+
import {NavController} from '../nav/nav-controller';
67
import {Platform} from '../../platform/platform';
8+
import {Tabs} from '../tabs/tabs';
79

810

911
/**
@@ -15,24 +17,17 @@ export class App {
1517
private _scrollTime: number = 0;
1618
private _title: string = '';
1719
private _titleSrv: Title = new Title();
18-
private _rootNav: any = null;
20+
private _rootNav: NavController = null;
1921
private _appInjector: Injector;
2022

2123
constructor(
2224
private _config: Config,
2325
private _clickBlock: ClickBlock,
24-
platform: Platform
26+
private _platform: Platform
2527
) {
26-
platform.backButton.subscribe(() => {
27-
let activeNav = this.getActiveNav();
28-
if (activeNav) {
29-
if (activeNav.length() === 1) {
30-
platform.exitApp();
31-
} else {
32-
activeNav.pop();
33-
}
34-
}
35-
});
28+
// listen for hardware back button events
29+
// register this back button action with a default priority
30+
_platform.registerBackButtonAction(this.navPop.bind(this));
3631
}
3732

3833
/**
@@ -100,7 +95,7 @@ export class App {
10095
/**
10196
* @private
10297
*/
103-
getActiveNav(): any {
98+
getActiveNav(): NavController {
10499
var nav = this._rootNav || null;
105100
var activeChildNav: any;
106101

@@ -118,7 +113,7 @@ export class App {
118113
/**
119114
* @private
120115
*/
121-
getRootNav(): any {
116+
getRootNav(): NavController {
122117
return this._rootNav;
123118
}
124119

@@ -129,6 +124,72 @@ export class App {
129124
this._rootNav = nav;
130125
}
131126

127+
/**
128+
* @private
129+
*/
130+
navPop(): Promise<any> {
131+
// function used to climb up all parent nav controllers
132+
function navPop(nav: any): Promise<any> {
133+
if (nav) {
134+
if (nav.length && nav.length() > 1) {
135+
// this nav controller has more than one view
136+
// pop the current view on this nav and we're done here
137+
console.debug('app, goBack pop nav');
138+
return nav.pop();
139+
140+
} else if (nav.previousTab) {
141+
// FYI, using "nav instanceof Tabs" throws a Promise runtime error for whatever reason, idk
142+
// this is a Tabs container
143+
// see if there is a valid previous tab to go to
144+
let prevTab = nav.previousTab(true);
145+
if (prevTab) {
146+
console.debug('app, goBack previous tab');
147+
nav.select(prevTab);
148+
return Promise.resolve();
149+
}
150+
}
151+
152+
// try again using the parent nav (if there is one)
153+
return navPop(nav.parent);
154+
}
155+
156+
// nerp, never found nav that could pop off a view
157+
return null;
158+
}
159+
160+
// app must be enabled and there must be a
161+
// root nav controller for go back to work
162+
if (this._rootNav && this.isEnabled()) {
163+
164+
// first check if the root navigation has any overlays
165+
// opened in it's portal, like alert/actionsheet/popup
166+
let portal = this._rootNav.getPortal && this._rootNav.getPortal();
167+
if (portal && portal.length() > 0) {
168+
// there is an overlay view in the portal
169+
// let's pop this one off to go back
170+
console.debug('app, goBack pop overlay');
171+
return portal.pop();
172+
}
173+
174+
// next get the active nav, check itself and climb up all
175+
// of its parent navs until it finds a nav that can pop
176+
let navPromise = navPop(this.getActiveNav());
177+
if (navPromise === null) {
178+
// no views to go back to
179+
// let's exit the app
180+
if (this._config.getBoolean('navExitApp', true)) {
181+
console.debug('app, goBack exitApp');
182+
this._platform.exitApp();
183+
}
184+
185+
} else {
186+
return navPromise;
187+
}
188+
}
189+
190+
return Promise.resolve();
191+
}
192+
132193
/**
133194
* @private
134195
*/

0 commit comments

Comments
 (0)