Skip to content

Commit

Permalink
feat(menu): close any opened menu
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Feb 16, 2016
1 parent 84cf9ce commit c02fb51
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
36 changes: 27 additions & 9 deletions ionic/components/menu/menu-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,36 +137,54 @@ export class MenuController {
* Progamatically open the Menu.
* @return {Promise} returns a promise when the menu is fully opened
*/
open(menuId?: string) {
open(menuId?: string): Promise<boolean> {
let menu = this.get(menuId);
if (menu) {
return menu.open();
}

return Promise.resolve(false);
}

/**
* Progamatically close the Menu.
* Progamatically close the Menu. If no `menuId` is given as the first
* argument then it'll close any menu which is open. If a `menuId`
* is given then it'll close that exact menu.
* @param {string} [menuId] Optionally get the menu by its id, or side.
* @return {Promise} returns a promise when the menu is fully closed
*/
close(menuId?: string) {
let menu = this.get(menuId);
close(menuId?: string): Promise<boolean> {
let menu: Menu;

if (menuId) {
// find the menu by its id
menu = this.get(menuId);

} else {
// find the menu that is open
menu = this._menus.find(m => m.isOpen);
}

if (menu) {
// close the menu
return menu.close();
}

return Promise.resolve(false);
}

/**
* Toggle the menu. If it's closed, it will open, and if opened, it will
* close.
* Toggle the menu. If it's closed, it will open, and if opened, it
* will close.
* @param {string} [menuId] Optionally get the menu by its id, or side.
* @return {Promise} returns a promise when the menu has been toggled
*/
toggle(menuId?: string) {
toggle(menuId?: string): Promise<boolean> {
let menu = this.get(menuId);
if (menu) {
return menu.toggle();
}
return Promise.resolve(false);
}

/**
Expand All @@ -176,7 +194,7 @@ export class MenuController {
* @param {string} [menuId] Optionally get the menu by its id, or side.
* @return {Menu} Returns the instance of the menu, which is useful for chaining.
*/
enable(shouldEnable: boolean, menuId?: string) {
enable(shouldEnable: boolean, menuId?: string): Menu {
let menu = this.get(menuId);
if (menu) {
return menu.enable(shouldEnable);
Expand All @@ -189,7 +207,7 @@ export class MenuController {
* @param {string} [menuId] Optionally get the menu by its id, or side.
* @return {Menu} Returns the instance of the menu, which is useful for chaining.
*/
swipeEnable(shouldEnable: boolean, menuId?: string) {
swipeEnable(shouldEnable: boolean, menuId?: string): Menu {
let menu = this.get(menuId);
if (menu) {
return menu.swipeEnable(shouldEnable);
Expand Down
8 changes: 5 additions & 3 deletions ionic/components/menu/test/basic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ class Page2 {
templateUrl: 'main.html'
})
class E2EApp {
rootPage;
changeDetectionCount: number = 0;
pages: Array<{title: string, component: any}>;

constructor(private app: IonicApp, private menu: MenuController) {
this.rootView = Page1;
this.changeDetectionCount = 0;
this.rootPage = Page1;

this.pages = [
{ title: 'Page 1', component: Page1 },
Expand All @@ -56,7 +58,7 @@ class E2EApp {
nav.setRoot(page.component).then(() => {
// wait for the root page to be completely loaded
// then close the menu
this.menu.close('left');
this.menu.close();
});
}

Expand Down
2 changes: 1 addition & 1 deletion ionic/components/menu/test/basic/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,6 @@

</ion-menu>

<ion-nav id="nav" [root]="rootView" #content swipe-back-enabled="false"></ion-nav>
<ion-nav id="nav" [root]="rootPage" #content swipe-back-enabled="false"></ion-nav>

<div [hidden]="isChangeDetecting()"></div>

0 comments on commit c02fb51

Please sign in to comment.