Skip to content

Commit

Permalink
fix(app): go back navigation can close menus
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Dec 2, 2016
1 parent 5567191 commit 8de253a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 30 deletions.
4 changes: 0 additions & 4 deletions src/components/app/app-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ export class IonicApp extends Ion implements OnInit {
_getActivePortal(): OverlayPortal {
const defaultPortal = this._overlayPortal;
const modalPortal = this._modalPortal;

assert(defaultPortal, 'default must be valid');
assert(modalPortal, 'modal must be valid');

const hasModal = modalPortal.length() > 0;
const hasDefault = defaultPortal.length() > 0;

Expand Down
42 changes: 25 additions & 17 deletions src/components/app/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEmitter, Injectable } from '@angular/core';
import { EventEmitter, Injectable, Optional } from '@angular/core';
import { Title } from '@angular/platform-browser';

import { AppPortal, IonicApp } from './app-root';
Expand All @@ -9,7 +9,7 @@ import { isNav, isTabs, NavOptions, DIRECTION_FORWARD, DIRECTION_BACK } from '..
import { NavController } from '../../navigation/nav-controller';
import { Platform } from '../../platform/platform';
import { ViewController } from '../../navigation/view-controller';

import { MenuController } from '../menu/menu-controller';

/**
* @name App
Expand Down Expand Up @@ -67,18 +67,19 @@ export class App {

constructor(
private _config: Config,
private _platform: Platform
private _platform: Platform,
@Optional() private _menuCtrl?: MenuController
) {
// listen for hardware back button events
// register this back button action with a default priority
_platform.registerBackButtonAction(this.navPop.bind(this));
_platform.registerBackButtonAction(this.goBack.bind(this));
this._disableScrollAssist = _config.getBoolean('disableScrollAssist', false);

runInDev(() => {
// During developement, navPop can be triggered by calling
// window.ClickBackButton();
if (!window['HWBackButton']) {
window['HWBackButton'] = this.navPop.bind(this);
window['HWBackButton'] = this.goBack.bind(this);
}
});
}
Expand Down Expand Up @@ -233,6 +234,23 @@ export class App {
return portal.insertPages(-1, [enteringView], opts);
}

goBack(): Promise<any> {
if (this._menuCtrl && this._menuCtrl.isOpen()) {
return this._menuCtrl.close();
}

let navPromise = this.navPop();
if (navPromise === null) {
// no views to go back to
// let's exit the app
if (this._config.getBoolean('navExitApp', true)) {
console.debug('app, goBack exitApp');
this._platform.exitApp();
}
}
return navPromise;
}

/**
* @private
*/
Expand Down Expand Up @@ -275,7 +293,7 @@ export class App {
const portal = this._appRoot._getActivePortal();

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

// next get the active nav, check itself and climb up all
// of its parent navs until it finds a nav that can pop
let navPromise = navPop(this.getActiveNav());
if (navPromise === null) {
// no views to go back to
// let's exit the app
if (this._config.getBoolean('navExitApp', true)) {
console.debug('app, goBack exitApp');
this._platform.exitApp();
}
}

return navPromise;
return navPop(this.getActiveNav());
}

}
Expand Down
13 changes: 9 additions & 4 deletions src/components/menu/menu-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,17 @@ export class MenuController {
}

/**
* @param {string} [menuId] Optionally get the menu by its id, or side.
* @return {boolean} Returns true if the menu is currently open, otherwise false.
* @param {string} [menuId] Optionally get the menu by its id, or side.
* @return {boolean} Returns true if the specified menu is currently open, otherwise false.
* If the menuId is not specified, it returns true if ANY menu is currenly open.
*/
isOpen(menuId?: string): boolean {
let menu = this.get(menuId);
return menu && menu.isOpen || false;
if (menuId) {
var menu = this.get(menuId);
return menu && menu.isOpen || false;
} else {
return !!this.getOpen();
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/components/nav/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DeepLinker } from '../../navigation/deep-linker';
import { GestureController } from '../../gestures/gesture-controller';
import { isTrueProperty } from '../../util/util';
import { Keyboard } from '../../util/keyboard';
import { NavController } from '../../navigation/nav-controller';
import { NavControllerBase } from '../../navigation/nav-controller-base';
import { NavOptions } from '../../navigation/nav-util';
import { TransitionController } from '../../transitions/transition-controller';
Expand Down Expand Up @@ -57,7 +58,7 @@ export class Nav extends NavControllerBase implements AfterViewInit {

constructor(
@Optional() viewCtrl: ViewController,
@Optional() parent: NavControllerBase,
@Optional() parent: NavController,
app: App,
config: Config,
keyboard: Keyboard,
Expand Down
8 changes: 4 additions & 4 deletions src/navigation/nav-controller-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { DomController } from '../util/dom-controller';
*/
export class NavControllerBase extends Ion implements NavController {

_children: any[] = [];
_children: NavController[] = [];
_ids: number = -1;
_init = false;
_isPortal: boolean;
Expand Down Expand Up @@ -883,15 +883,15 @@ export class NavControllerBase extends Ion implements NavController {
this._app.viewWillUnload.emit(view);
}

getActiveChildNav(): any {
getActiveChildNav(): NavController {
return this._children[this._children.length - 1];
}

registerChildNav(nav: any) {
registerChildNav(nav: NavController) {
this._children.push(nav);
}

unregisterChildNav(nav: any) {
unregisterChildNav(nav: NavController) {
removeArrayItem(this._children, nav);
}

Expand Down
4 changes: 4 additions & 0 deletions src/navigation/nav-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,4 +597,8 @@ export abstract class NavController {
*/
abstract canGoBack(): boolean;

/**
* @private
*/
abstract registerChildNav(nav: NavController);
}

0 comments on commit 8de253a

Please sign in to comment.