Skip to content

Commit c02fb51

Browse files
committed
feat(menu): close any opened menu
1 parent 84cf9ce commit c02fb51

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

ionic/components/menu/menu-controller.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,36 +137,54 @@ export class MenuController {
137137
* Progamatically open the Menu.
138138
* @return {Promise} returns a promise when the menu is fully opened
139139
*/
140-
open(menuId?: string) {
140+
open(menuId?: string): Promise<boolean> {
141141
let menu = this.get(menuId);
142142
if (menu) {
143143
return menu.open();
144144
}
145+
146+
return Promise.resolve(false);
145147
}
146148

147149
/**
148-
* Progamatically close the Menu.
150+
* Progamatically close the Menu. If no `menuId` is given as the first
151+
* argument then it'll close any menu which is open. If a `menuId`
152+
* is given then it'll close that exact menu.
149153
* @param {string} [menuId] Optionally get the menu by its id, or side.
150154
* @return {Promise} returns a promise when the menu is fully closed
151155
*/
152-
close(menuId?: string) {
153-
let menu = this.get(menuId);
156+
close(menuId?: string): Promise<boolean> {
157+
let menu: Menu;
158+
159+
if (menuId) {
160+
// find the menu by its id
161+
menu = this.get(menuId);
162+
163+
} else {
164+
// find the menu that is open
165+
menu = this._menus.find(m => m.isOpen);
166+
}
167+
154168
if (menu) {
169+
// close the menu
155170
return menu.close();
156171
}
172+
173+
return Promise.resolve(false);
157174
}
158175

159176
/**
160-
* Toggle the menu. If it's closed, it will open, and if opened, it will
161-
* close.
177+
* Toggle the menu. If it's closed, it will open, and if opened, it
178+
* will close.
162179
* @param {string} [menuId] Optionally get the menu by its id, or side.
163180
* @return {Promise} returns a promise when the menu has been toggled
164181
*/
165-
toggle(menuId?: string) {
182+
toggle(menuId?: string): Promise<boolean> {
166183
let menu = this.get(menuId);
167184
if (menu) {
168185
return menu.toggle();
169186
}
187+
return Promise.resolve(false);
170188
}
171189

172190
/**
@@ -176,7 +194,7 @@ export class MenuController {
176194
* @param {string} [menuId] Optionally get the menu by its id, or side.
177195
* @return {Menu} Returns the instance of the menu, which is useful for chaining.
178196
*/
179-
enable(shouldEnable: boolean, menuId?: string) {
197+
enable(shouldEnable: boolean, menuId?: string): Menu {
180198
let menu = this.get(menuId);
181199
if (menu) {
182200
return menu.enable(shouldEnable);
@@ -189,7 +207,7 @@ export class MenuController {
189207
* @param {string} [menuId] Optionally get the menu by its id, or side.
190208
* @return {Menu} Returns the instance of the menu, which is useful for chaining.
191209
*/
192-
swipeEnable(shouldEnable: boolean, menuId?: string) {
210+
swipeEnable(shouldEnable: boolean, menuId?: string): Menu {
193211
let menu = this.get(menuId);
194212
if (menu) {
195213
return menu.swipeEnable(shouldEnable);

ionic/components/menu/test/basic/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ class Page2 {
3737
templateUrl: 'main.html'
3838
})
3939
class E2EApp {
40+
rootPage;
41+
changeDetectionCount: number = 0;
42+
pages: Array<{title: string, component: any}>;
4043

4144
constructor(private app: IonicApp, private menu: MenuController) {
42-
this.rootView = Page1;
43-
this.changeDetectionCount = 0;
45+
this.rootPage = Page1;
4446

4547
this.pages = [
4648
{ title: 'Page 1', component: Page1 },
@@ -56,7 +58,7 @@ class E2EApp {
5658
nav.setRoot(page.component).then(() => {
5759
// wait for the root page to be completely loaded
5860
// then close the menu
59-
this.menu.close('left');
61+
this.menu.close();
6062
});
6163
}
6264

ionic/components/menu/test/basic/main.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,6 @@
138138

139139
</ion-menu>
140140

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

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

0 commit comments

Comments
 (0)