BaseMenu.js 4.53 KB
Newer Older
jim's avatar
jim committed
1 2 3 4
import React, { PureComponent } from 'react';
import { Menu, Icon } from 'antd';
import { Link } from 'dva/router';
import pathToRegexp from 'path-to-regexp';
jim's avatar
jim committed
5
import { urlToList } from '../_utils/pathTools';
jim's avatar
jim committed
6 7 8 9 10 11 12 13
import styles from './index.less';

const { SubMenu } = Menu;

// Allow menu.js config icon as string or ReactNode
//   icon: 'setting',
//   icon: 'http://demo.com/icon.png',
//   icon: <Icon type="setting" />,
jim's avatar
jim committed
14
const getIcon = icon => {
jim's avatar
jim committed
15 16 17 18 19 20 21 22 23
  if (typeof icon === 'string' && icon.indexOf('http') === 0) {
    return <img src={icon} alt="icon" className={styles.icon} />;
  }
  if (typeof icon === 'string') {
    return <Icon type={icon} />;
  }
  return icon;
};

Erwin Zhang's avatar
Erwin Zhang committed
24
export const getMenuMatches = (flatMenuKeys, path) => {
jim's avatar
jim committed
25
  return flatMenuKeys.filter(item => {
jim's avatar
jim committed
26 27 28 29
    return pathToRegexp(item).test(path);
  });
};

Erwin Zhang's avatar
Erwin Zhang committed
30
export default class BaseMenu extends PureComponent {
jim's avatar
jim committed
31 32 33 34
  constructor(props) {
    super(props);
    this.flatMenuKeys = this.getFlatMenuKeys(props.menuData);
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
35

jim's avatar
jim committed
36 37 38 39 40 41 42
  /**
   * Recursively flatten the data
   * [{path:string},{path:string}] => {path,path2}
   * @param  menus
   */
  getFlatMenuKeys(menus) {
    let keys = [];
jim's avatar
jim committed
43
    menus.forEach(item => {
jim's avatar
jim committed
44 45 46 47 48 49 50
      if (item.children) {
        keys = keys.concat(this.getFlatMenuKeys(item.children));
      }
      keys.push(item.path);
    });
    return keys;
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
51

jim's avatar
jim committed
52 53 54 55
  /**
   * θŽ·εΎ—θœε•ε­θŠ‚η‚Ή
   * @memberof SiderMenu
   */
jim's avatar
jim committed
56
  getNavMenuItems = menusData => {
jim's avatar
jim committed
57 58 59 60 61
    if (!menusData) {
      return [];
    }
    return menusData
      .filter(item => item.name && !item.hideInMenu)
jim's avatar
jim committed
62
      .map(item => {
jim's avatar
jim committed
63 64 65 66 67 68
        // make dom
        const ItemDom = this.getSubMenuOrItem(item);
        return this.checkPermissionItem(item.authority, ItemDom);
      })
      .filter(item => item);
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
69

jim's avatar
jim committed
70 71
  // Get the currently selected menu
  getSelectedMenuKeys = () => {
jim's avatar
jim committed
72 73 74
    const {
      location: { pathname },
    } = this.props;
Erwin Zhang's avatar
Erwin Zhang committed
75
    return urlToList(pathname).map(itemPath => getMenuMatches(this.flatMenuKeys, itemPath).pop());
jim's avatar
jim committed
76
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
77

jim's avatar
jim committed
78 79 80
  /**
   * get SubMenu or Item
   */
jim's avatar
jim committed
81
  getSubMenuOrItem = item => {
jim's avatar
jim committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    if (item.children && item.children.some(child => child.name)) {
      return (
        <SubMenu
          title={
            item.icon ? (
              <span>
                {getIcon(item.icon)}
                <span>{item.name}</span>
              </span>
            ) : (
              item.name
            )
          }
          key={item.path}
        >
          {this.getNavMenuItems(item.children)}
        </SubMenu>
      );
    } else {
jim's avatar
jim committed
101
      return <Menu.Item key={item.path}>{this.getMenuItemPath(item)}</Menu.Item>;
jim's avatar
jim committed
102 103
    }
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
104

jim's avatar
jim committed
105 106 107 108 109
  /**
   * εˆ€ζ–­ζ˜―ε¦ζ˜―httpι“ΎζŽ₯.θΏ”ε›ž Link ζˆ– a
   * Judge whether it is http link.return a or Link
   * @memberof SiderMenu
   */
jim's avatar
jim committed
110
  getMenuItemPath = item => {
jim's avatar
jim committed
111 112 113 114 115 116 117 118 119 120 121 122
    const itemPath = this.conversionPath(item.path);
    const icon = getIcon(item.icon);
    const { target, name } = item;
    // Is it a http link
    if (/^https?:\/\//.test(itemPath)) {
      return (
        <a href={itemPath} target={target}>
          {icon}
          <span>{name}</span>
        </a>
      );
    }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
123
    const { location, isMobile, onCollapse } = this.props;
jim's avatar
jim committed
124 125 126 127
    return (
      <Link
        to={itemPath}
        target={target}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
128
        replace={itemPath === location.pathname}
jim's avatar
jim committed
129
        onClick={
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
130
          isMobile
jim's avatar
jim committed
131
            ? () => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
132
                onCollapse(true);
jim's avatar
jim committed
133 134 135 136 137 138 139 140 141
              }
            : undefined
        }
      >
        {icon}
        <span>{name}</span>
      </Link>
    );
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
142

jim's avatar
jim committed
143 144
  // permission to check
  checkPermissionItem = (authority, ItemDom) => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
145 146 147
    const { Authorized } = this.props;
    if (Authorized && Authorized.check) {
      const { check } = Authorized;
jim's avatar
jim committed
148 149 150 151
      return check(authority, ItemDom);
    }
    return ItemDom;
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
152

jim's avatar
jim committed
153
  conversionPath = path => {
jim's avatar
jim committed
154 155 156 157 158 159
    if (path && path.indexOf('http') === 0) {
      return path;
    } else {
      return `/${path || ''}`.replace(/\/+/g, '/');
    }
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
160

jim's avatar
jim committed
161
  render() {
jim's avatar
jim committed
162
    const { openKeys, theme, mode } = this.props;
jim's avatar
jim committed
163 164 165 166 167
    // if pathname can't match, use the nearest parent's key
    let selectedKeys = this.getSelectedMenuKeys();
    if (!selectedKeys.length && openKeys) {
      selectedKeys = [openKeys[openKeys.length - 1]];
    }
jim's avatar
jim committed
168 169 170 171 172 173
    let props = {};
    if (openKeys) {
      props = {
        openKeys,
      };
    }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
174
    const { handleOpenChange, style, menuData } = this.props;
jim's avatar
jim committed
175 176 177
    return (
      <Menu
        key="Menu"
jim's avatar
jim committed
178 179
        mode={mode}
        theme={theme}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
180
        onOpenChange={handleOpenChange}
jim's avatar
jim committed
181
        selectedKeys={selectedKeys}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
182
        style={style}
jim's avatar
jim committed
183
        {...props}
jim's avatar
jim committed
184
      >
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
185
        {this.getNavMenuItems(menuData)}
jim's avatar
jim committed
186 187 188 189
      </Menu>
    );
  }
}