refactor(all): strict boolean conditions

This commit is contained in:
Manu Mtz.-Almeida
2018-08-31 18:59:09 +02:00
parent f383ebdf13
commit ba2230510e
96 changed files with 990 additions and 962 deletions

View File

@@ -1,6 +1,6 @@
import { Build, Component, Method, Prop } from '@stencil/core';
import { Animation, AnimationBuilder, MenuI } from '../../interface';
import { Animation, AnimationBuilder, MenuControllerI, MenuI } from '../../interface';
import { menuOverlayAnimation } from './animations/overlay';
import { menuPushAnimation } from './animations/push';
@@ -10,7 +10,7 @@ import { menuRevealAnimation } from './animations/reveal';
tag: 'ion-menu-controller',
styleUrl: 'menu-controller.scss'
})
export class MenuController {
export class MenuController implements MenuControllerI {
private menus: MenuI[] = [];
private menuAnimations = new Map<string, AnimationBuilder>();
@@ -40,8 +40,8 @@ export class MenuController {
*/
@Method()
async close(menuId?: string): Promise<boolean> {
const menu = await (menuId ? this.get(menuId) : this.getOpen());
if (menu) {
const menu = await (menuId !== undefined ? this.get(menuId) : this.getOpen());
if (menu !== undefined) {
return menu.close();
}
return false;
@@ -67,7 +67,7 @@ export class MenuController {
* will also automatically disable all the others that are on the same side.
*/
@Method()
async enable(shouldEnable: boolean, menuId?: string): Promise<HTMLIonMenuElement | null> {
async enable(shouldEnable: boolean, menuId?: string): Promise<HTMLIonMenuElement | undefined> {
const menu = await this.get(menuId);
if (menu) {
menu.disabled = !shouldEnable;
@@ -79,7 +79,7 @@ export class MenuController {
* Used to enable or disable the ability to swipe open the menu.
*/
@Method()
async swipeGesture(shouldEnable: boolean, menuId?: string): Promise<HTMLIonMenuElement | null> {
async swipeGesture(shouldEnable: boolean, menuId?: string): Promise<HTMLIonMenuElement | undefined> {
const menu = await this.get(menuId);
if (menu) {
menu.swipeGesture = shouldEnable;
@@ -93,11 +93,13 @@ export class MenuController {
*/
@Method()
async isOpen(menuId?: string): Promise<boolean> {
if (menuId) {
if (menuId !== undefined) {
const menu = await this.get(menuId);
return (menu && menu.isOpen()) || false;
return (menu !== undefined && menu.isOpen());
} else {
const menu = await this.getOpen();
return menu !== undefined;
}
return !!this.getOpen();
}
/**
@@ -120,15 +122,15 @@ export class MenuController {
* return `null`.
*/
@Method()
async get(menuId?: string): Promise<HTMLIonMenuElement | null> {
async get(menuId?: string): Promise<HTMLIonMenuElement | undefined> {
if (Build.isDev) {
if (menuId === 'left') {
console.error('menu.side=left is deprecated, use "start" instead');
return null;
return undefined;
}
if (menuId === 'right') {
console.error('menu.side=right is deprecated, use "end" instead');
return null;
return undefined;
}
}
if (menuId === 'start' || menuId === 'end') {
@@ -141,11 +143,12 @@ export class MenuController {
// didn't find a menu side that is enabled
// so try to get the first menu side found
return this.find(m => m.side === menuId) || null;
} else if (menuId) {
return this.find(m => m.side === menuId);
} else if (menuId !== undefined) {
// the menuId was not left or right
// so try to get the menu by its "id"
return this.find(m => m.menuId === menuId) || null;
return this.find(m => m.menuId === menuId);
}
// return the first enabled menu
@@ -155,23 +158,23 @@ export class MenuController {
}
// get the first menu in the array, if one exists
return this.menus.length > 0 ? this.menus[0].el : null;
return this.menus.length > 0 ? this.menus[0].el : undefined;
}
/**
* Returns the instance of the menu already opened, otherwise `null`.
*/
@Method()
async getOpen(): Promise<HTMLIonMenuElement | null> {
return this.find(m => m._isOpen);
getOpen(): Promise<HTMLIonMenuElement | undefined> {
return Promise.resolve(this.getOpenSync());
}
/**
* Returns an array of all menu instances.
*/
@Method()
async getMenus(): Promise<HTMLIonMenuElement[]> {
return this.menus.map(menu => menu.el);
getMenus(): Promise<HTMLIonMenuElement[]> {
return Promise.resolve(this.getMenusSync());
}
/**
@@ -216,14 +219,19 @@ export class MenuController {
if (shouldOpen) {
const openedMenu = await this.getOpen();
if (openedMenu && menu.el !== openedMenu) {
openedMenu.setOpen(false, false);
return openedMenu.setOpen(false, false);
}
}
return menu._setOpen(shouldOpen, animated);
}
@Method()
createAnimation(type: string, menuCmp: MenuI): Promise<Animation> {
_getInstance(): Promise<MenuControllerI> {
return Promise.resolve(this);
}
@Method()
_createAnimation(type: string, menuCmp: MenuI): Promise<Animation> {
const animationBuilder = this.menuAnimations.get(type);
if (!animationBuilder) {
return Promise.reject('animation not registered');
@@ -231,16 +239,23 @@ export class MenuController {
return this.animationCtrl.create(animationBuilder, null, menuCmp);
}
@Method()
registerAnimation(name: string, animation: AnimationBuilder) {
getOpenSync(): HTMLIonMenuElement | undefined {
return this.find(m => m._isOpen);
}
getMenusSync(): HTMLIonMenuElement[] {
return this.menus.map(menu => menu.el);
}
private registerAnimation(name: string, animation: AnimationBuilder) {
this.menuAnimations.set(name, animation);
}
private find(predicate: (menu: MenuI) => boolean): HTMLIonMenuElement | null {
private find(predicate: (menu: MenuI) => boolean): HTMLIonMenuElement | undefined {
const instance = this.menus.find(predicate);
if (instance) {
if (instance !== undefined) {
return instance.el;
}
return null;
return undefined;
}
}

View File

@@ -8,25 +8,25 @@ The MenuController makes it easy to control a Menu. Its methods can be used to d
## Methods
| Method | Description |
| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `_register` | |
| `_setActiveMenu` | |
| `_setOpen` | |
| `_unregister` | |
| `close` | Close the menu. If no menu is specified, then it will close any menu that is open. If a menu is specified, it will close that menu. |
| `createAnimation` | |
| `enable` | 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 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. |
| `get` | Used to get a menu instance. If a menu is not provided then it will return the first menu found. If the specified menu is `left` or `right`, then it will return the enabled menu on that side. Otherwise, it will try to find the menu using the menu's `id` property. If a menu is not found then it will return `null`. |
| `getMenus` | Returns an array of all menu instances. |
| `getOpen` | Returns the instance of the menu already opened, otherwise `null`. |
| `isAnimating` | Returns true if any menu is currently animating. |
| `isEnabled` | Returns true if the specified menu is enabled. |
| `isOpen` | Returns true if the specified menu is open. If the menu is not specified, it will return true if any menu is currently open. |
| `open` | Open the menu. |
| `registerAnimation` | |
| `swipeGesture` | Used to enable or disable the ability to swipe open the menu. |
| `toggle` | Toggle the menu. If it's closed, it will open, and if opened, it will close. |
| Method | Description |
| ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `_createAnimation` | |
| `_getInstance` | |
| `_register` | |
| `_setActiveMenu` | |
| `_setOpen` | |
| `_unregister` | |
| `close` | Close the menu. If no menu is specified, then it will close any menu that is open. If a menu is specified, it will close that menu. |
| `enable` | 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 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. |
| `get` | Used to get a menu instance. If a menu is not provided then it will return the first menu found. If the specified menu is `left` or `right`, then it will return the enabled menu on that side. Otherwise, it will try to find the menu using the menu's `id` property. If a menu is not found then it will return `null`. |
| `getMenus` | Returns an array of all menu instances. |
| `getOpen` | Returns the instance of the menu already opened, otherwise `null`. |
| `isAnimating` | Returns true if any menu is currently animating. |
| `isEnabled` | Returns true if the specified menu is enabled. |
| `isOpen` | Returns true if the specified menu is open. If the menu is not specified, it will return true if any menu is currently open. |
| `open` | Open the menu. |
| `swipeGesture` | Used to enable or disable the ability to swipe open the menu. |
| `toggle` | Toggle the menu. If it's closed, it will open, and if opened, it will close. |
----------------------------------------------