BasicLayout.js 4.97 KB
Newer Older
jim's avatar
jim committed
1 2
import React from 'react';
import { Layout } from 'antd';
3
import DocumentTitle from 'react-document-title';
陈帅's avatar
陈帅 committed
4
import memoizeOne from 'memoize-one';
5
import { connect } from 'dva';
afc163's avatar
afc163 committed
6 7
import { ContainerQuery } from 'react-container-query';
import classNames from 'classnames';
jim's avatar
jim committed
8
import pathToRegexp from 'path-to-regexp';
陈帅's avatar
陈帅 committed
9
import { formatMessage } from 'umi/locale';
愚道's avatar
愚道 committed
10 11 12 13
import SiderMenu from '../../components/SiderMenu';
import Authorized from '../../utils/Authorized';
import SettingDarwer from '../../components/SettingDarwer';
import logo from '../../assets/logo.svg';
jim's avatar
jim committed
14 15
import Footer from './Footer';
import Header from './Header';
16
import Context from './MenuContext';
17

jim's avatar
jim committed
18
const { Content } = Layout;
陈帅's avatar
陈帅 committed
19
const { check } = Authorized;
ddcat1115's avatar
ddcat1115 committed
20

21 22 23 24
/**
 * 获取面包屑映射
 * @param {Object} menuData 菜单配置
 */
25
const getBreadcrumbNameMap = memoizeOne(meun => {
陈帅's avatar
陈帅 committed
26 27 28 29 30 31
  const routerMap = {};
  const mergeMeunAndRouter = meunData => {
    meunData.forEach(meunItem => {
      if (meunItem.children) {
        mergeMeunAndRouter(meunItem.children);
      }
32 33
      // Reduce memory usage
      routerMap[meunItem.path] = meunItem;
陈帅's avatar
陈帅 committed
34 35 36 37
    });
  };
  mergeMeunAndRouter(meun);
  return routerMap;
陈帅's avatar
陈帅 committed
38
});
39

afc163's avatar
afc163 committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
const query = {
  'screen-xs': {
    maxWidth: 575,
  },
  'screen-sm': {
    minWidth: 576,
    maxWidth: 767,
  },
  'screen-md': {
    minWidth: 768,
    maxWidth: 991,
  },
  'screen-lg': {
    minWidth: 992,
    maxWidth: 1199,
  },
  'screen-xl': {
    minWidth: 1200,
yoyo837's avatar
yoyo837 committed
58 59 60 61
    maxWidth: 1599,
  },
  'screen-xxl': {
    minWidth: 1600,
afc163's avatar
afc163 committed
62 63 64
  },
};

65
class BasicLayout extends React.PureComponent {
陈帅's avatar
陈帅 committed
66 67
  constructor(props) {
    super(props);
68
    const { menuData } = this.props;
陈帅's avatar
陈帅 committed
69
    this.getPageTitle = memoizeOne(this.getPageTitle);
70
    this.breadcrumbNameMap = getBreadcrumbNameMap(menuData);
陈帅's avatar
陈帅 committed
71 72
  }

jim's avatar
jim committed
73
  getContext() {
陈帅's avatar
陈帅 committed
74
    const { location } = this.props;
ddcat1115's avatar
ddcat1115 committed
75 76
    return {
      location,
陈帅's avatar
陈帅 committed
77
      breadcrumbNameMap: this.breadcrumbNameMap,
ddcat1115's avatar
ddcat1115 committed
78
    };
79
  }
陈帅's avatar
陈帅 committed
80

陈帅's avatar
陈帅 committed
81
  getPageTitle = pathname => {
jim's avatar
jim committed
82 83
    let currRouterData = null;
    // match params path
陈帅's avatar
陈帅 committed
84
    Object.keys(this.breadcrumbNameMap).forEach(key => {
jim's avatar
jim committed
85
      if (pathToRegexp(key).test(pathname)) {
陈帅's avatar
陈帅 committed
86
        currRouterData = this.breadcrumbNameMap[key];
jim's avatar
jim committed
87 88
      }
    });
陈帅's avatar
陈帅 committed
89 90
    if (!currRouterData) {
      return 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
91
    }
陈帅's avatar
陈帅 committed
92
    const message = formatMessage({
陈帅's avatar
陈帅 committed
93 94 95 96 97
      id: currRouterData.locale || currRouterData.name,
      defaultMessage: currRouterData.name,
    });
    return `${message} - Ant Design Pro`;
  };
陈帅's avatar
陈帅 committed
98

jim's avatar
jim committed
99
  getLayoutStyle = () => {
陈帅's avatar
陈帅 committed
100 101
    const { fixSiderbar, collapsed, layout } = this.props;
    if (fixSiderbar && layout !== 'topmenu') {
jim's avatar
jim committed
102
      return {
103
        paddingLeft: collapsed ? '80px' : '256px',
jim's avatar
jim committed
104 105 106 107
      };
    }
    return null;
  };
陈帅's avatar
陈帅 committed
108

jim's avatar
jim committed
109 110 111 112 113 114 115
  getContentStyle = () => {
    const { fixedHeader } = this.props;
    return {
      margin: '24px 24px 0',
      paddingTop: fixedHeader ? 64 : 0,
    };
  };
陈帅's avatar
陈帅 committed
116

117 118 119 120
  getBashRedirect = () => {
    // According to the url parameter to redirect
    // 这里是重定向的,重定向到 url 的 redirect 参数所示地址
    const urlParams = new URL(window.location.href);
jim's avatar
jim committed
121 122

    const redirect = urlParams.searchParams.get('redirect');
123
    // Remove the parameters in the url
jim's avatar
jim committed
124 125 126 127
    if (redirect) {
      urlParams.searchParams.delete('redirect');
      window.history.replaceState(null, 'redirect', urlParams.href);
    } else {
ddcat1115's avatar
ddcat1115 committed
128 129
      const { routerData } = this.props;
      // get the first authorized route path in routerData
jim's avatar
jim committed
130 131 132
      const authorizedPath = Object.keys(routerData).find(
        item => check(routerData[item].authority, item) && item !== '/'
      );
ddcat1115's avatar
ddcat1115 committed
133
      return authorizedPath;
jim's avatar
jim committed
134
    }
135
    return redirect;
jim's avatar
jim committed
136
  };
陈帅's avatar
陈帅 committed
137

jim's avatar
jim committed
138
  handleMenuCollapse = collapsed => {
陈帅's avatar
陈帅 committed
139 140
    const { dispatch } = this.props;
    dispatch({
141 142 143
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
144
  };
jim's avatar
jim committed
145

146
  render() {
陈帅's avatar
陈帅 committed
147 148 149 150
    const {
      isMobile,
      silderTheme,
      layout: PropsLayout,
愚道's avatar
愚道 committed
151
      children,
陈帅's avatar
陈帅 committed
152
      location: { pathname },
陈帅's avatar
陈帅 committed
153 154
    } = this.props;
    const isTop = PropsLayout === 'topmenu';
afc163's avatar
afc163 committed
155 156
    const layout = (
      <Layout>
jim's avatar
jim committed
157
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
158 159 160
          <SiderMenu
            logo={logo}
            Authorized={Authorized}
陈帅's avatar
陈帅 committed
161
            theme={silderTheme}
jim's avatar
jim committed
162
            onCollapse={this.handleMenuCollapse}
jim's avatar
jim committed
163
            {...this.props}
jim's avatar
jim committed
164 165
          />
        )}
jim's avatar
jim committed
166
        <Layout style={this.getLayoutStyle()}>
jim's avatar
jim committed
167
          <Header handleMenuCollapse={this.handleMenuCollapse} logo={logo} {...this.props} />
陈帅's avatar
陈帅 committed
168
          <Content style={this.getContentStyle()}>{children}</Content>
jim's avatar
jim committed
169
          <Footer />
170
        </Layout>
afc163's avatar
afc163 committed
171 172
      </Layout>
    );
陈帅's avatar
陈帅 committed
173

afc163's avatar
afc163 committed
174
    return (
陈帅's avatar
陈帅 committed
175
      <DocumentTitle title={this.getPageTitle(pathname)}>
afc163's avatar
afc163 committed
176
        <ContainerQuery query={query}>
jim's avatar
jim committed
177
          {params => (
jim's avatar
jim committed
178 179 180
            <Context.Provider value={this.getContext()}>
              <div className={classNames(params)}>
                {layout}
jim's avatar
jim committed
181
                <SettingDarwer />
jim's avatar
jim committed
182 183
              </div>
            </Context.Provider>
jim's avatar
jim committed
184
          )}
afc163's avatar
afc163 committed
185
        </ContainerQuery>
186 187 188 189 190
      </DocumentTitle>
    );
  }
}

jim's avatar
jim committed
191
export default connect(({ global, setting }) => ({
Andreas Cederström's avatar
Andreas Cederström committed
192
  collapsed: global.collapsed,
jim's avatar
jim committed
193 194
  layout: setting.layout,
  ...setting,
陈帅's avatar
陈帅 committed
195
}))(BasicLayout);