Skip to content

Commit 8de253a

Browse files
committed
fix(app): go back navigation can close menus
1 parent 5567191 commit 8de253a

File tree

6 files changed

+44
-30
lines changed

6 files changed

+44
-30
lines changed

src/components/app/app-root.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ export class IonicApp extends Ion implements OnInit {
118118
_getActivePortal(): OverlayPortal {
119119
const defaultPortal = this._overlayPortal;
120120
const modalPortal = this._modalPortal;
121-
122-
assert(defaultPortal, 'default must be valid');
123-
assert(modalPortal, 'modal must be valid');
124-
125121
const hasModal = modalPortal.length() > 0;
126122
const hasDefault = defaultPortal.length() > 0;
127123

src/components/app/app.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EventEmitter, Injectable } from '@angular/core';
1+
import { EventEmitter, Injectable, Optional } from '@angular/core';
22
import { Title } from '@angular/platform-browser';
33

44
import { AppPortal, IonicApp } from './app-root';
@@ -9,7 +9,7 @@ import { isNav, isTabs, NavOptions, DIRECTION_FORWARD, DIRECTION_BACK } from '..
99
import { NavController } from '../../navigation/nav-controller';
1010
import { Platform } from '../../platform/platform';
1111
import { ViewController } from '../../navigation/view-controller';
12-
12+
import { MenuController } from '../menu/menu-controller';
1313

1414
/**
1515
* @name App
@@ -67,18 +67,19 @@ export class App {
6767

6868
constructor(
6969
private _config: Config,
70-
private _platform: Platform
70+
private _platform: Platform,
71+
@Optional() private _menuCtrl?: MenuController
7172
) {
7273
// listen for hardware back button events
7374
// register this back button action with a default priority
74-
_platform.registerBackButtonAction(this.navPop.bind(this));
75+
_platform.registerBackButtonAction(this.goBack.bind(this));
7576
this._disableScrollAssist = _config.getBoolean('disableScrollAssist', false);
7677

7778
runInDev(() => {
7879
// During developement, navPop can be triggered by calling
7980
// window.ClickBackButton();
8081
if (!window['HWBackButton']) {
81-
window['HWBackButton'] = this.navPop.bind(this);
82+
window['HWBackButton'] = this.goBack.bind(this);
8283
}
8384
});
8485
}
@@ -233,6 +234,23 @@ export class App {
233234
return portal.insertPages(-1, [enteringView], opts);
234235
}
235236

237+
goBack(): Promise<any> {
238+
if (this._menuCtrl && this._menuCtrl.isOpen()) {
239+
return this._menuCtrl.close();
240+
}
241+
242+
let navPromise = this.navPop();
243+
if (navPromise === null) {
244+
// no views to go back to
245+
// let's exit the app
246+
if (this._config.getBoolean('navExitApp', true)) {
247+
console.debug('app, goBack exitApp');
248+
this._platform.exitApp();
249+
}
250+
}
251+
return navPromise;
252+
}
253+
236254
/**
237255
* @private
238256
*/
@@ -275,7 +293,7 @@ export class App {
275293
const portal = this._appRoot._getActivePortal();
276294

277295
// first check if the root navigation has any overlays
278-
// opened in it's portal, like alert/actionsheet/popup
296+
// opened in it's portal, like alert/actionsheet/popup/modals
279297
if (portal) {
280298
// there is an overlay view in the portal
281299
// let's pop this one off to go back
@@ -285,17 +303,7 @@ export class App {
285303

286304
// next get the active nav, check itself and climb up all
287305
// of its parent navs until it finds a nav that can pop
288-
let navPromise = navPop(this.getActiveNav());
289-
if (navPromise === null) {
290-
// no views to go back to
291-
// let's exit the app
292-
if (this._config.getBoolean('navExitApp', true)) {
293-
console.debug('app, goBack exitApp');
294-
this._platform.exitApp();
295-
}
296-
}
297-
298-
return navPromise;
306+
return navPop(this.getActiveNav());
299307
}
300308

301309
}

src/components/menu/menu-controller.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,17 @@ export class MenuController {
211211
}
212212

213213
/**
214-
* @param {string} [menuId] Optionally get the menu by its id, or side.
215-
* @return {boolean} Returns true if the menu is currently open, otherwise false.
214+
* @param {string} [menuId] Optionally get the menu by its id, or side.
215+
* @return {boolean} Returns true if the specified menu is currently open, otherwise false.
216+
* If the menuId is not specified, it returns true if ANY menu is currenly open.
216217
*/
217218
isOpen(menuId?: string): boolean {
218-
let menu = this.get(menuId);
219-
return menu && menu.isOpen || false;
219+
if (menuId) {
220+
var menu = this.get(menuId);
221+
return menu && menu.isOpen || false;
222+
} else {
223+
return !!this.getOpen();
224+
}
220225
}
221226

222227
/**

src/components/nav/nav.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { DeepLinker } from '../../navigation/deep-linker';
66
import { GestureController } from '../../gestures/gesture-controller';
77
import { isTrueProperty } from '../../util/util';
88
import { Keyboard } from '../../util/keyboard';
9+
import { NavController } from '../../navigation/nav-controller';
910
import { NavControllerBase } from '../../navigation/nav-controller-base';
1011
import { NavOptions } from '../../navigation/nav-util';
1112
import { TransitionController } from '../../transitions/transition-controller';
@@ -57,7 +58,7 @@ export class Nav extends NavControllerBase implements AfterViewInit {
5758

5859
constructor(
5960
@Optional() viewCtrl: ViewController,
60-
@Optional() parent: NavControllerBase,
61+
@Optional() parent: NavController,
6162
app: App,
6263
config: Config,
6364
keyboard: Keyboard,

src/navigation/nav-controller-base.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { DomController } from '../util/dom-controller';
2525
*/
2626
export class NavControllerBase extends Ion implements NavController {
2727

28-
_children: any[] = [];
28+
_children: NavController[] = [];
2929
_ids: number = -1;
3030
_init = false;
3131
_isPortal: boolean;
@@ -883,15 +883,15 @@ export class NavControllerBase extends Ion implements NavController {
883883
this._app.viewWillUnload.emit(view);
884884
}
885885

886-
getActiveChildNav(): any {
886+
getActiveChildNav(): NavController {
887887
return this._children[this._children.length - 1];
888888
}
889889

890-
registerChildNav(nav: any) {
890+
registerChildNav(nav: NavController) {
891891
this._children.push(nav);
892892
}
893893

894-
unregisterChildNav(nav: any) {
894+
unregisterChildNav(nav: NavController) {
895895
removeArrayItem(this._children, nav);
896896
}
897897

src/navigation/nav-controller.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,4 +597,8 @@ export abstract class NavController {
597597
*/
598598
abstract canGoBack(): boolean;
599599

600+
/**
601+
* @private
602+
*/
603+
abstract registerChildNav(nav: NavController);
600604
}

0 commit comments

Comments
 (0)