BaseMenu.js 4.79 KB
Newer Older
jim's avatar
jim committed
1 2 3
import React, { PureComponent } from 'react';
import { Menu, Icon } from 'antd';
import { Link } from 'dva/router';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
4
import { FormattedMessage } 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 = <FormattedMessage defaultMessage={item.name} 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 114
  getMenuItemPath = item => {
    const name = <FormattedMessage defaultMessage={item.name} 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
              }
            : undefined
        }
      >
        {icon}
        <span>{name}</span>
      </Link>
    );
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
146

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

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

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