Skip to content

Commit

Permalink
refactor(menu): improve menu get lookup
Browse files Browse the repository at this point in the history
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 #5535
  • Loading branch information
adamdbradley committed Feb 20, 2016
1 parent 8564d79 commit 004e635
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 87 deletions.
36 changes: 25 additions & 11 deletions ionic/components/menu/menu-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ export class MenuController {

/**
* Used to enable or disable a menu. For example, there could be multiple
* left menus, but only one of them should be able to be dragged open.
* @param {boolean} shouldEnable True if it should be enabled, false if not.
* left menus, but only one of them should be able to be opened at the same
* time. If there are multiple menus on the same side, then enabling one menu
* will also automatically disable all the others that are on the same side.
* @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.
*/
Expand Down Expand Up @@ -249,24 +250,37 @@ export class MenuController {
}

/**
* Used to get a menu instance. If a `menuId` is not provided then it'll return
* the first menu found. If a `menuId` is provided, then it'll first try to find
* the menu using the menu's `id` attribute. If a menu is not found using the `id`
* attribute, then it'll try to find the menu by its `side` name.
* Used to get a menu instance. 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`.
* @param {string} [menuId] Optionally get the menu by its id, or side.
* @return {Menu} Returns the instance of the menu if found, otherwise `null`.
*/
get(menuId?: string): Menu {
if (menuId) {
// first try by "id"
let menu = this._menus.find(m => m.id === menuId);
if (menu) return menu;
var menu: Menu;

// not found by "id", next try by "side"
if (menuId === 'left' || menuId === 'right') {
// there could be more than one menu on the same side
// so first try to get the enabled one
menu = this._menus.find(m => m.side === menuId && m.enabled);
if (menu) return menu;

// didn't find a menu side that is enabled
// so try to get the first menu side found
return this._menus.find(m => m.side === menuId) || null;

} else if (menuId) {
// the menuId was not left or right
// so try to get the menu by its "id"
return this._menus.find(m => m.id === menuId) || null;
}

// return the first enabled menu
menu = this._menus.find(m => m.enabled);
if (menu) return menu;

// get the first menu in the array, if one exists
return (this._menus.length ? this._menus[0] : null);
}
Expand Down
16 changes: 15 additions & 1 deletion ionic/components/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,29 @@ export class Menu extends Ion {

/**
* Used to enable or disable a menu. For example, there could be multiple
* left menus, but only one of them should be able to be dragged open.
* left menus, but only one of them should be able to be opened at the same
* time. If there are multiple menus on the same side, then enabling one menu
* will also automatically disable all the others that are on the same side.
* @param {boolean} shouldEnable True if it should be enabled, false if not.
* @return {Menu} Returns the instance of the menu, which is useful for chaining.
*/
enable(shouldEnable: boolean): Menu {
this.enabled = shouldEnable;
if (!shouldEnable && this.isOpen) {
// close if this menu is open, and should not be enabled
this.close();
}

if (shouldEnable) {
// if this menu should be enabled
// then find all the other menus on this same side
// and automatically disable other same side menus
let sameSideMenus = this._menuCtrl
.getMenus()
.filter(m => m.side === this.side && m !== this)
.map(m => m.enabled = false);
}

return this;
}

Expand Down
Loading

0 comments on commit 004e635

Please sign in to comment.