BaseMenu.js 4.7 KB
Newer Older
J
jim 已提交
1 2
import React, { PureComponent } from 'react';
import { Menu, Icon } from 'antd';
Z
zinkey 已提交
3
import Link from 'umi/link';
陈帅 已提交
4
import { formatMessage } from 'umi/locale';
J
jim 已提交
5
import pathToRegexp from 'path-to-regexp';
J
jim 已提交
6
import { urlToList } from '../_utils/pathTools';
J
jim 已提交
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" />,
J
jim 已提交
15
const getIcon = icon => {
J
jim 已提交
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;
};

E
Erwin Zhang 已提交
25
export const getMenuMatches = (flatMenuKeys, path) => {
J
jim 已提交
26
  return flatMenuKeys.filter(item => {
J
jim 已提交
27 28 29 30
    return pathToRegexp(item).test(path);
  });
};

E
Erwin Zhang 已提交
31
export default class BaseMenu extends PureComponent {
J
jim 已提交
32 33 34 35
  constructor(props) {
    super(props);
    this.flatMenuKeys = this.getFlatMenuKeys(props.menuData);
  }
陈帅 已提交
36

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

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

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

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

J
jim 已提交
108 109 110 111 112
  /**
   * 判断是否是http链接.返回 Link 或 a
   * Judge whether it is http link.return a or Link
   * @memberof SiderMenu
   */
陈帅 已提交
113
  getMenuItemPath = item => {
陈帅 已提交
114
    const name = formatMessage({ id: item.locale });
J
jim 已提交
115 116
    const itemPath = this.conversionPath(item.path);
    const icon = getIcon(item.icon);
陈帅 已提交
117
    const { target } = item;
J
jim 已提交
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>
      );
    }
陈帅 已提交
127
    const { location, isMobile, onCollapse } = this.props;
J
jim 已提交
128 129 130 131
    return (
      <Link
        to={itemPath}
        target={target}
陈帅 已提交
132
        replace={itemPath === location.pathname}
J
jim 已提交
133
        onClick={
陈帅 已提交
134
          isMobile
J
jim 已提交
135
            ? () => {
陈帅 已提交
136
                onCollapse(true);
J
jim 已提交
137 138 139 140 141 142 143 144 145 146 147
              }
            : undefined
        }
      >
        {icon}
        <span>{name}</span>
      </Link>
    );
  };
  // permission to check
  checkPermissionItem = (authority, ItemDom) => {
陈帅 已提交
148 149 150
    const { Authorized } = this.props;
    if (Authorized && Authorized.check) {
      const { check } = Authorized;
J
jim 已提交
151 152 153 154
      return check(authority, ItemDom);
    }
    return ItemDom;
  };
陈帅 已提交
155

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

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