BasicLayout.js 6.15 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import React, { Suspense } from 'react';
jim's avatar
jim committed
2
import { Layout } from 'antd';
3
import DocumentTitle from 'react-document-title';
afc163's avatar
afc163 committed
4
import isEqual 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 Media from 'react-media';
陈帅's avatar
陈帅 committed
11
import { formatMessage } from 'umi/locale';
12
import Authorized from '@/utils/Authorized';
13
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';
陈帅's avatar
陈帅 committed
17
import Exception403 from '../pages/Exception/403';
陈帅's avatar
陈帅 committed
18 19 20
import PageLoading from '@/components/PageLoading';
import SiderMenu from '@/components/SiderMenu';

21 22
import styles from './BasicLayout.less';

陈帅's avatar
陈帅 committed
23 24
// lazy load SettingDrawer
const SettingDrawer = React.lazy(() => import('@/components/SettingDrawer'));
25

jim's avatar
jim committed
26
const { Content } = Layout;
ddcat1115's avatar
ddcat1115 committed
27

afc163's avatar
afc163 committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
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
46 47 48 49
    maxWidth: 1599,
  },
  'screen-xxl': {
    minWidth: 1600,
afc163's avatar
afc163 committed
50 51 52
  },
};

53
class BasicLayout extends React.PureComponent {
陈帅's avatar
陈帅 committed
54 55
  constructor(props) {
    super(props);
陈帅's avatar
陈帅 committed
56
    this.getPageTitle = memoizeOne(this.getPageTitle);
陈帅's avatar
陈帅 committed
57
    this.matchParamsPath = memoizeOne(this.matchParamsPath, isEqual);
陈帅's avatar
陈帅 committed
58
  }
59 60

  componentDidMount() {
陈小聪's avatar
陈小聪 committed
61 62 63 64
    const {
      dispatch,
      route: { routes, authority },
    } = this.props;
afc163's avatar
afc163 committed
65 66 67 68 69 70
    dispatch({
      type: 'user/fetchCurrent',
    });
    dispatch({
      type: 'setting/getSetting',
    });
陈小聪's avatar
陈小聪 committed
71 72 73 74
    dispatch({
      type: 'menu/getMenuData',
      payload: { routes, authority },
    });
75 76
  }

77 78 79
  componentDidUpdate(preProps) {
    // After changing to phone mode,
    // if collapsed is true, you need to click twice to display
陈帅's avatar
陈帅 committed
80
    const { collapsed, isMobile } = this.props;
81 82 83
    if (isMobile && !preProps.isMobile && !collapsed) {
      this.handleMenuCollapse(false);
    }
84 85
  }

jim's avatar
jim committed
86
  getContext() {
87
    const { location, breadcrumbNameMap } = this.props;
ddcat1115's avatar
ddcat1115 committed
88 89
    return {
      location,
90
      breadcrumbNameMap,
ddcat1115's avatar
ddcat1115 committed
91
    };
92
  }
93

94 95 96 97 98 99
  /**
   * 获取面包屑映射
   * @param {Object} menuData 菜单配置
   */
  getBreadcrumbNameMap() {
    const routerMap = {};
陈小聪's avatar
陈小聪 committed
100
    const { menuData } = this.props;
101
    const flattenMenuData = data => {
102 103
      data.forEach(menuItem => {
        if (menuItem.children) {
104
          flattenMenuData(menuItem.children);
105 106 107 108 109
        }
        // Reduce memory usage
        routerMap[menuItem.path] = menuItem;
      });
    };
陈小聪's avatar
陈小聪 committed
110
    flattenMenuData(menuData);
111 112 113
    return routerMap;
  }

114 115 116
  matchParamsPath = (pathname, breadcrumbNameMap) => {
    const pathKey = Object.keys(breadcrumbNameMap).find(key => pathToRegexp(key).test(pathname));
    return breadcrumbNameMap[pathKey];
陈帅's avatar
陈帅 committed
117 118
  };

119 120
  getPageTitle = (pathname, breadcrumbNameMap) => {
    const currRouterData = this.matchParamsPath(pathname, breadcrumbNameMap);
陈帅's avatar
陈帅 committed
121

陈帅's avatar
陈帅 committed
122 123
    if (!currRouterData) {
      return 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
124
    }
xiaoiver's avatar
xiaoiver committed
125
    const pageName = formatMessage({
陈帅's avatar
陈帅 committed
126 127 128
      id: currRouterData.locale || currRouterData.name,
      defaultMessage: currRouterData.name,
    });
129

xiaoiver's avatar
xiaoiver committed
130
    return `${pageName} - Ant Design Pro`;
陈帅's avatar
陈帅 committed
131
  };
陈帅's avatar
陈帅 committed
132

jim's avatar
jim committed
133
  getLayoutStyle = () => {
陈帅's avatar
陈帅 committed
134
    const { fixSiderbar, isMobile, collapsed, layout } = this.props;
135
    if (fixSiderbar && layout !== 'topmenu' && !isMobile) {
jim's avatar
jim committed
136
      return {
137
        paddingLeft: collapsed ? '80px' : '256px',
jim's avatar
jim committed
138 139 140 141
      };
    }
    return null;
  };
陈帅's avatar
陈帅 committed
142

jim's avatar
jim committed
143
  handleMenuCollapse = collapsed => {
陈帅's avatar
陈帅 committed
144 145
    const { dispatch } = this.props;
    dispatch({
146 147 148
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
149
  };
150

陈帅's avatar
陈帅 committed
151
  renderSettingDrawer = () => {
kennylbj's avatar
kennylbj committed
152 153
    // Do not render SettingDrawer in production
    // unless it is deployed in preview.pro.ant.design as demo
陈帅's avatar
陈帅 committed
154
    if (process.env.NODE_ENV === 'production' && APP_TYPE !== 'site') {
155 156 157
      return null;
    }
    return <SettingDrawer />;
陈帅's avatar
陈帅 committed
158
  };
159

160
  render() {
陈帅's avatar
陈帅 committed
161
    const {
afc163's avatar
afc163 committed
162
      navTheme,
陈帅's avatar
陈帅 committed
163
      layout: PropsLayout,
愚道's avatar
愚道 committed
164
      children,
陈帅's avatar
陈帅 committed
165
      location: { pathname },
陈帅's avatar
陈帅 committed
166
      isMobile,
陈小聪's avatar
陈小聪 committed
167
      menuData,
168
      breadcrumbNameMap,
169
      fixedHeader,
陈帅's avatar
陈帅 committed
170
    } = this.props;
171

陈帅's avatar
陈帅 committed
172
    const isTop = PropsLayout === 'topmenu';
173
    const routerConfig = this.matchParamsPath(pathname, breadcrumbNameMap);
174
    const contentStyle = !fixedHeader ? { paddingTop: 0 } : {};
afc163's avatar
afc163 committed
175 176
    const layout = (
      <Layout>
jim's avatar
jim committed
177
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
178 179
          <SiderMenu
            logo={logo}
afc163's avatar
afc163 committed
180
            theme={navTheme}
jim's avatar
jim committed
181
            onCollapse={this.handleMenuCollapse}
182
            menuData={menuData}
183
            isMobile={isMobile}
jim's avatar
jim committed
184
            {...this.props}
jim's avatar
jim committed
185 186
          />
        )}
陈帅's avatar
陈帅 committed
187 188 189 190 191 192
        <Layout
          style={{
            ...this.getLayoutStyle(),
            minHeight: '100vh',
          }}
        >
193 194 195 196
          <Header
            menuData={menuData}
            handleMenuCollapse={this.handleMenuCollapse}
            logo={logo}
197
            isMobile={isMobile}
198 199
            {...this.props}
          />
200
          <Content className={styles.content} style={contentStyle}>
陈帅's avatar
陈帅 committed
201 202 203 204
            <Authorized
              authority={routerConfig && routerConfig.authority}
              noMatch={<Exception403 />}
            >
陈帅's avatar
陈帅 committed
205 206 207
              {children}
            </Authorized>
          </Content>
jim's avatar
jim committed
208
          <Footer />
209
        </Layout>
afc163's avatar
afc163 committed
210 211 212
      </Layout>
    );
    return (
陈帅's avatar
陈帅 committed
213
      <React.Fragment>
214
        <DocumentTitle title={this.getPageTitle(pathname, breadcrumbNameMap)}>
陈帅's avatar
陈帅 committed
215 216 217 218 219 220 221 222
          <ContainerQuery query={query}>
            {params => (
              <Context.Provider value={this.getContext()}>
                <div className={classNames(params)}>{layout}</div>
              </Context.Provider>
            )}
          </ContainerQuery>
        </DocumentTitle>
陈帅's avatar
陈帅 committed
223
        <Suspense fallback={<PageLoading />}>{this.renderSettingDrawer()}</Suspense>
陈帅's avatar
陈帅 committed
224
      </React.Fragment>
225 226 227 228
    );
  }
}

陈小聪's avatar
陈小聪 committed
229
export default connect(({ global, setting, menu }) => ({
Andreas Cederström's avatar
Andreas Cederström committed
230
  collapsed: global.collapsed,
jim's avatar
jim committed
231
  layout: setting.layout,
陈小聪's avatar
陈小聪 committed
232
  menuData: menu.menuData,
233
  breadcrumbNameMap: menu.breadcrumbNameMap,
jim's avatar
jim committed
234
  ...setting,
陈帅's avatar
陈帅 committed
235 236 237 238 239
}))(props => (
  <Media query="(max-width: 599px)">
    {isMobile => <BasicLayout {...props} isMobile={isMobile} />}
  </Media>
));