From c02fb51d046e7a32866129fbb5b9ec710bd9a2c0 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Mon, 15 Feb 2016 19:42:11 -0600 Subject: [PATCH] feat(menu): close any opened menu --- ionic/components/menu/menu-controller.ts | 36 ++++++++++++++++------ ionic/components/menu/test/basic/index.ts | 8 +++-- ionic/components/menu/test/basic/main.html | 2 +- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/ionic/components/menu/menu-controller.ts b/ionic/components/menu/menu-controller.ts index 67298f04da9..25f1df0f90f 100644 --- a/ionic/components/menu/menu-controller.ts +++ b/ionic/components/menu/menu-controller.ts @@ -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 { 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 { + 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 { let menu = this.get(menuId); if (menu) { return menu.toggle(); } + return Promise.resolve(false); } /** @@ -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); @@ -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); diff --git a/ionic/components/menu/test/basic/index.ts b/ionic/components/menu/test/basic/index.ts index bbe12a8b567..3cc926fbbab 100644 --- a/ionic/components/menu/test/basic/index.ts +++ b/ionic/components/menu/test/basic/index.ts @@ -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 }, @@ -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(); }); } diff --git a/ionic/components/menu/test/basic/main.html b/ionic/components/menu/test/basic/main.html index 0052e0b3b95..d6f560da6e8 100644 --- a/ionic/components/menu/test/basic/main.html +++ b/ionic/components/menu/test/basic/main.html @@ -138,6 +138,6 @@ - +
\ No newline at end of file