FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

refactor(menu): improve menu get lookup · Java1024/ionic@004e635 · GitHub

Repository navigation

Commit 004e635

Browse files
committed
refactor(menu): improve menu get lookup
If a `menuId` is not provided then it'll return the first menu found. If a `menuId` is `left` or `right`, then it'll return the enabled menu on that side. Otherwise, if a `menuId` is provided, then it'll try to find the menu using the menu's `id` property. If a menu is not found then it'll return `null`. If a menu id was provided, but was not found, it will not fallback to finding any menu. Closes ionic-team#5535
1 parent 8564d79 commit 004e635

3 files changed

Lines changed: 239 additions & 87 deletions

File tree

‎ionic/components/menu/menu-controller.ts‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,9 @@ export class MenuController {
207207

208208
/**
209209
* Used to enable or disable a menu. For example, there could be multiple
210-
* left menus, but only one of them should be able to be dragged open.
211-
* @param {boolean} shouldEnable True if it should be enabled, false if not.
210+
* left menus, but only one of them should be able to be opened at the same
211+
* time. If there are multiple menus on the same side, then enabling one menu
212+
* will also automatically disable all the others that are on the same side.
212213
* @param {string} [menuId] Optionally get the menu by its id, or side.
213214
* @return {Menu} Returns the instance of the menu, which is useful for chaining.
214215
*/
@@ -249,24 +250,37 @@ export class MenuController {
249250
}
250251

251252
/**
252-
* Used to get a menu instance. If a `menuId` is not provided then it'll return
253-
* the first menu found. If a `menuId` is provided, then it'll first try to find
254-
* the menu using the menu's `id` attribute. If a menu is not found using the `id`
255-
* attribute, then it'll try to find the menu by its `side` name.
253+
* Used to get a menu instance. If a `menuId` is not provided then it'll
254+
* return the first menu found. If a `menuId` is `left` or `right`, then
255+
* it'll return the enabled menu on that side. Otherwise, if a `menuId` is
256+
* provided, then it'll try to find the menu using the menu's `id`
257+
* property. If a menu is not found then it'll return `null`.
256258
* @param {string} [menuId] Optionally get the menu by its id, or side.
257259
* @return {Menu} Returns the instance of the menu if found, otherwise `null`.
258260
*/
259261
get(menuId?: string): Menu {
260-
if (menuId) {
261-
// first try by "id"
262-
let menu = this._menus.find(m => m.id === menuId);
263-
if (menu) return menu;
262+
var menu: Menu;
264263

265-
// not found by "id", next try by "side"
264+
if (menuId === 'left' || menuId === 'right') {
265+
// there could be more than one menu on the same side
266+
// so first try to get the enabled one
266267
menu = this._menus.find(m => m.side === menuId && m.enabled);
267268
if (menu) return menu;
269+
270+
// didn't find a menu side that is enabled
271+
// so try to get the first menu side found
272+
return this._menus.find(m => m.side === menuId) || null;
273+
274+
} else if (menuId) {
275+
// the menuId was not left or right
276+
// so try to get the menu by its "id"
277+
return this._menus.find(m => m.id === menuId) || null;
268278
}
269279

280+
// return the first enabled menu
281+
menu = this._menus.find(m => m.enabled);
282+
if (menu) return menu;
283+
270284
// get the first menu in the array, if one exists
271285
return (this._menus.length ? this._menus[0] : null);
272286
}

‎ionic/components/menu/menu.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,29 @@ export class Menu extends Ion {
370370

371371
/**
372372
* Used to enable or disable a menu. For example, there could be multiple
373-
* left menus, but only one of them should be able to be dragged open.
373+
* left menus, but only one of them should be able to be opened at the same
374+
* time. If there are multiple menus on the same side, then enabling one menu
375+
* will also automatically disable all the others that are on the same side.
374376
* @param {boolean} shouldEnable True if it should be enabled, false if not.
375377
* @return {Menu} Returns the instance of the menu, which is useful for chaining.
376378
*/
377379
enable(shouldEnable: boolean): Menu {
378380
this.enabled = shouldEnable;
379381
if (!shouldEnable && this.isOpen) {
382+
// close if this menu is open, and should not be enabled
380383
this.close();
381384
}
385+
386+
if (shouldEnable) {
387+
// if this menu should be enabled
388+
// then find all the other menus on this same side
389+
// and automatically disable other same side menus
390+
let sameSideMenus = this._menuCtrl
391+
.getMenus()
392+
.filter(m => m.side === this.side && m !== this)
393+
.map(m => m.enabled = false);
394+
}
395+
382396
return this;
383397
}
384398

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL