BaseMenu.js 4.73 KB
Newer Older
jim's avatar
jim committed
1 2
import React, { PureComponent } from 'react';
import { Menu, Icon } from 'antd';
zinkey's avatar
zinkey committed
3
import Link from 'umi/link';
陈帅's avatar
陈帅 committed
4
import { formatMessage } from 'umi/locale';
jim's avatar
jim committed
5
import pathToRegexp from 'path-to-regexp';
jim's avatar
jim committed
6
import { urlToList } from '../_utils/pathTools';
jim's avatar
jim committed
7 8 9 10 11 12 13 14
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
15
const getIcon = icon => {
jim's avatar
jim committed
16 17 18 19 20 21 22 23 24
  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
25
export const getMenuMatches = (flatMenuKeys, path) => {
jim's avatar
jim committed
26
  return flatMenuKeys.filter(item => {
jim's avatar
jim committed
27 28 29 30
    return pathToRegexp(item).test(path);
  });
};

Erwin Zhang's avatar
Erwin Zhang committed
31
export default class BaseMenu extends PureComponent {
jim's avatar
jim committed
32 33 34 35
  constructor(props) {
    super(props);
    this.flatMenuKeys = this.getFlatMenuKeys(props.menuData);
  }
陈帅's avatar
陈帅 committed
36

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

jim's avatar
jim committed
53 54 55 56
  /**
   * 获得菜单子节点
   * @memberof SiderMenu
   */
陈帅's avatar
陈帅 committed
57
  getNavMenuItems = (menusData, parent) => {
jim's avatar
jim committed
58 59 60 61 62
    if (!menusData) {
      return [];
    }
    return menusData
      .filter(item => item.name && !item.hideInMenu)
jim's avatar
jim committed
63
      .map(item => {
jim's avatar
jim committed
64
        // make dom
陈帅's avatar
陈帅 committed
65
        const ItemDom = this.getSubMenuOrItem(item, parent);
jim's avatar
jim committed
66 67 68 69
        return this.checkPermissionItem(item.authority, ItemDom);
      })
      .filter(item => item);
  };
陈帅's avatar
陈帅 committed
70

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

jim's avatar
jim committed
79 80 81
  /**
   * get SubMenu or Item
   */
陈帅's avatar
陈帅 committed
82
  getSubMenuOrItem = item => {
83 84
    // doc: add hideChildren
    if (item.children && !item.hideChildren && item.children.some(child => child.name)) {
陈帅's avatar
陈帅 committed
85
      const name = formatMessage({ id: item.locale });
jim's avatar
jim committed
86 87 88 89 90 91
      return (
        <SubMenu
          title={
            item.icon ? (
              <span>
                {getIcon(item.icon)}
陈帅's avatar
陈帅 committed
92
                <span>{name}</span>
jim's avatar
jim committed
93 94
              </span>
            ) : (
陈帅's avatar
陈帅 committed
95
              name
jim's avatar
jim committed
96 97 98 99
            )
          }
          key={item.path}
        >
陈帅's avatar
陈帅 committed
100
          {this.getNavMenuItems(item.children)}
jim's avatar
jim committed
101 102 103
        </SubMenu>
      );
    } else {
陈帅's avatar
陈帅 committed
104
      return <Menu.Item key={item.path}>{this.getMenuItemPath(item)}</Menu.Item>;
jim's avatar
jim committed
105 106
    }
  };
陈帅's avatar
陈帅 committed
107

jim's avatar
jim committed
108 109 110 111 112
  /**
   * 判断是否是http链接.返回 Link 或 a
   * Judge whether it is http link.return a or Link
   * @memberof SiderMenu
   */
陈帅's avatar
陈帅 committed
113
  getMenuItemPath = item => {
陈帅's avatar
陈帅 committed
114
    const name = formatMessage({ id: item.locale });
jim's avatar
jim committed
115 116
    const itemPath = this.conversionPath(item.path);
    const icon = getIcon(item.icon);
陈帅's avatar
陈帅 committed
117
    const { target } = item;
jim's avatar
jim committed
118 119 120 121 122 123 124 125 126
    // Is it a http link
    if (/^https?:\/\//.test(itemPath)) {
      return (
        <a href={itemPath} target={target}>
          {icon}
          <span>{name}</span>
        </a>
      );
    }
陈帅's avatar
陈帅 committed
127
    const { location, isMobile, onCollapse } = this.props;
jim's avatar
jim committed
128 129 130 131
    return (
      <Link
        to={itemPath}
        target={target}
陈帅's avatar
陈帅 committed
132
        replace={itemPath === location.pathname}
jim's avatar
jim committed
133
        onClick={
陈帅's avatar
陈帅 committed
134
          isMobile
jim's avatar
jim committed
135
            ? () => {
陈帅's avatar
陈帅 committed
136
                onCollapse(true);
jim's avatar
jim committed
137 138 139 140 141 142 143 144 145 146 147
              }
            : undefined
        }
      >
        {icon}
        <span>{name}</span>
      </Link>
    );
  };
  // permission to check
  checkPermissionItem = (authority, ItemDom) => {
陈帅's avatar
陈帅 committed
148 149 150
    const { Authorized } = this.props;
    if (Authorized && Authorized.check) {
      const { check } = Authorized;
jim's avatar
jim committed
151 152 153 154
      return check(authority, ItemDom);
    }
    return ItemDom;
  };
陈帅's avatar
陈帅 committed
155

jim's avatar
jim committed
156
  conversionPath = path => {
jim's avatar
jim committed
157 158 159 160 161 162
    if (path && path.indexOf('http') === 0) {
      return path;
    } else {
      return `/${path || ''}`.replace(/\/+/g, '/');
    }
  };
陈帅's avatar
陈帅 committed
163

jim's avatar
jim committed
164
  render() {
jim's avatar
jim committed
165
    const { openKeys, theme, mode } = this.props;
jim's avatar
jim committed
166 167 168 169 170
    // 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
171 172 173 174 175 176
    let props = {};
    if (openKeys) {
      props = {
        openKeys,
      };
    }
陈帅's avatar
陈帅 committed
177
    const { handleOpenChange, style, menuData } = this.props;
jim's avatar
jim committed
178 179 180
    return (
      <Menu
        key="Menu"
jim's avatar
jim committed
181 182
        mode={mode}
        theme={theme}
陈帅's avatar
陈帅 committed
183
        onOpenChange={handleOpenChange}
jim's avatar
jim committed
184
        selectedKeys={selectedKeys}
陈帅's avatar
陈帅 committed
185
        style={style}
jim's avatar
jim committed
186
        {...props}
jim's avatar
jim committed
187
      >
陈帅's avatar
陈帅 committed
188
        {this.getNavMenuItems(menuData)}
jim's avatar
jim committed
189 190 191 192
      </Menu>
    );
  }
}