BasicLayout.js 5.67 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 deepEqual from 'lodash.isequal';
陈帅's avatar
陈帅 committed
5
import memoizeOne from 'memoize-one';
6
import { connect } from 'dva';
afc163's avatar
afc163 committed
7 8
import { ContainerQuery } from 'react-container-query';
import classNames from 'classnames';
jim's avatar
jim committed
9
import pathToRegexp from 'path-to-regexp';
陈帅's avatar
陈帅 committed
10
import { formatMessage } from 'umi/locale';
11 12
import SiderMenu from '@/components/SiderMenu';
import Authorized from '@/utils/Authorized';
13
import SettingDrawer from '@/components/SettingDrawer';
14
import logo from '../assets/logo.svg';
jim's avatar
jim committed
15 16
import Footer from './Footer';
import Header from './Header';
17
import Context from './MenuContext';
18

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

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

afc163's avatar
afc163 committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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
59 60 61 62
    maxWidth: 1599,
  },
  'screen-xxl': {
    minWidth: 1600,
afc163's avatar
afc163 committed
63 64 65
  },
};

66
class BasicLayout extends React.PureComponent {
陈帅's avatar
陈帅 committed
67 68 69
  state = {
    rendering: true,
  };
陈帅's avatar
陈帅 committed
70 71
  constructor(props) {
    super(props);
72
    const { menuData } = this.props;
陈帅's avatar
陈帅 committed
73
    this.getPageTitle = memoizeOne(this.getPageTitle);
74
    // Because there are many places to be. So put it here
75
    this.breadcrumbNameMap = getBreadcrumbNameMap(menuData);
陈帅's avatar
陈帅 committed
76
  }
jim's avatar
jim committed
77
  getContext() {
陈帅's avatar
陈帅 committed
78
    const { location } = this.props;
ddcat1115's avatar
ddcat1115 committed
79 80
    return {
      location,
陈帅's avatar
陈帅 committed
81
      breadcrumbNameMap: this.breadcrumbNameMap,
ddcat1115's avatar
ddcat1115 committed
82
    };
83
  }
84 85 86 87
  componentDidUpdate() {
    const { menuData } = this.props;
    this.breadcrumbNameMap = getBreadcrumbNameMap(menuData);
  }
陈帅's avatar
陈帅 committed
88
  getPageTitle = pathname => {
jim's avatar
jim committed
89 90
    let currRouterData = null;
    // match params path
陈帅's avatar
陈帅 committed
91
    Object.keys(this.breadcrumbNameMap).forEach(key => {
jim's avatar
jim committed
92
      if (pathToRegexp(key).test(pathname)) {
陈帅's avatar
陈帅 committed
93
        currRouterData = this.breadcrumbNameMap[key];
jim's avatar
jim committed
94 95
      }
    });
陈帅's avatar
陈帅 committed
96 97
    if (!currRouterData) {
      return 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
98
    }
陈帅's avatar
陈帅 committed
99
    const message = formatMessage({
陈帅's avatar
陈帅 committed
100 101 102 103 104
      id: currRouterData.locale || currRouterData.name,
      defaultMessage: currRouterData.name,
    });
    return `${message} - Ant Design Pro`;
  };
陈帅's avatar
陈帅 committed
105

jim's avatar
jim committed
106
  getLayoutStyle = () => {
陈帅's avatar
陈帅 committed
107 108
    const { fixSiderbar, collapsed, layout } = this.props;
    if (fixSiderbar && layout !== 'topmenu') {
jim's avatar
jim committed
109
      return {
110
        paddingLeft: collapsed ? '80px' : '256px',
jim's avatar
jim committed
111 112 113 114
      };
    }
    return null;
  };
陈帅's avatar
陈帅 committed
115

jim's avatar
jim committed
116 117 118 119 120 121 122
  getContentStyle = () => {
    const { fixedHeader } = this.props;
    return {
      margin: '24px 24px 0',
      paddingTop: fixedHeader ? 64 : 0,
    };
  };
陈帅's avatar
陈帅 committed
123

124 125 126 127
  getBashRedirect = () => {
    // According to the url parameter to redirect
    // 这里是重定向的,重定向到 url 的 redirect 参数所示地址
    const urlParams = new URL(window.location.href);
jim's avatar
jim committed
128 129

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

jim's avatar
jim committed
145
  handleMenuCollapse = collapsed => {
陈帅's avatar
陈帅 committed
146 147
    const { dispatch } = this.props;
    dispatch({
148 149 150
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
151
  };
陈帅's avatar
陈帅 committed
152 153 154 155 156 157 158 159 160 161
  componentDidMount() {
    this.renderRef = requestAnimationFrame(() => {
      this.setState({
        rendering: false,
      });
    });
  }
  componentWillUnmount() {
    cancelAnimationFrame(this.renderRef);
  }
162
  render() {
陈帅's avatar
陈帅 committed
163 164 165 166
    const {
      isMobile,
      silderTheme,
      layout: PropsLayout,
愚道's avatar
愚道 committed
167
      children,
陈帅's avatar
陈帅 committed
168
      location: { pathname },
陈帅's avatar
陈帅 committed
169 170
    } = this.props;
    const isTop = PropsLayout === 'topmenu';
afc163's avatar
afc163 committed
171 172
    const layout = (
      <Layout>
jim's avatar
jim committed
173
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
174 175 176
          <SiderMenu
            logo={logo}
            Authorized={Authorized}
陈帅's avatar
陈帅 committed
177
            theme={silderTheme}
jim's avatar
jim committed
178
            onCollapse={this.handleMenuCollapse}
jim's avatar
jim committed
179
            {...this.props}
jim's avatar
jim committed
180 181
          />
        )}
陈帅's avatar
陈帅 committed
182 183 184 185 186 187
        <Layout
          style={{
            ...this.getLayoutStyle(),
            minHeight: '100vh',
          }}
        >
jim's avatar
jim committed
188
          <Header handleMenuCollapse={this.handleMenuCollapse} logo={logo} {...this.props} />
陈帅's avatar
陈帅 committed
189
          <Content style={this.getContentStyle()}>{children}</Content>
jim's avatar
jim committed
190
          <Footer />
191
        </Layout>
afc163's avatar
afc163 committed
192 193 194
      </Layout>
    );
    return (
陈帅's avatar
陈帅 committed
195 196 197 198 199 200 201 202 203 204
      <React.Fragment>
        <DocumentTitle title={this.getPageTitle(pathname)}>
          <ContainerQuery query={query}>
            {params => (
              <Context.Provider value={this.getContext()}>
                <div className={classNames(params)}>{layout}</div>
              </Context.Provider>
            )}
          </ContainerQuery>
        </DocumentTitle>
afc163's avatar
afc163 committed
205 206 207
        {this.state.rendering && process.env.NODE_ENV === 'production' ? null : ( // Do show SettingDrawer in production
          <SettingDrawer />
        )}
陈帅's avatar
陈帅 committed
208
      </React.Fragment>
209 210 211 212
    );
  }
}

jim's avatar
jim committed
213
export default connect(({ global, setting }) => ({
Andreas Cederström's avatar
Andreas Cederström committed
214
  collapsed: global.collapsed,
jim's avatar
jim committed
215 216
  layout: setting.layout,
  ...setting,
陈帅's avatar
陈帅 committed
217
}))(BasicLayout);