BasicLayout.js 6.17 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';
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';
17
import Exception403 from '../pages/Exception/403';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
18 19 20 21 22
import PageLoading from '@/components/PageLoading';
import SiderMenu from '@/components/SiderMenu';

// lazy load SettingDrawer
const SettingDrawer = React.lazy(() => import('@/components/SettingDrawer'));
23

jim's avatar
jim committed
24
const { Content } = Layout;
ddcat1115's avatar
ddcat1115 committed
25

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

51
class BasicLayout extends React.PureComponent {
52 53
  constructor(props) {
    super(props);
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
54
    this.getPageTitle = memoizeOne(this.getPageTitle);
55
    this.matchParamsPath = memoizeOne(this.matchParamsPath, isEqual);
56
  }
57 58

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

75 76 77
  componentDidUpdate(preProps) {
    // After changing to phone mode,
    // if collapsed is true, you need to click twice to display
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
78
    const { collapsed, isMobile } = this.props;
79 80 81
    if (isMobile && !preProps.isMobile && !collapsed) {
      this.handleMenuCollapse(false);
    }
82 83
  }

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

92 93 94 95 96 97
  /**
   * ่Žทๅ–้ขๅŒ…ๅฑ‘ๆ˜ ๅฐ„
   * @param {Object} menuData ่œๅ•้…็ฝฎ
   */
  getBreadcrumbNameMap() {
    const routerMap = {};
98
    const { menuData } = this.props;
99
    const flattenMenuData = data => {
100 101
      data.forEach(menuItem => {
        if (menuItem.children) {
102
          flattenMenuData(menuItem.children);
103 104 105 106 107
        }
        // Reduce memory usage
        routerMap[menuItem.path] = menuItem;
      });
    };
108
    flattenMenuData(menuData);
109 110 111
    return routerMap;
  }

112 113 114
  matchParamsPath = (pathname, breadcrumbNameMap) => {
    const pathKey = Object.keys(breadcrumbNameMap).find(key => pathToRegexp(key).test(pathname));
    return breadcrumbNameMap[pathKey];
115 116
  };

117 118
  getPageTitle = (pathname, breadcrumbNameMap) => {
    const currRouterData = this.matchParamsPath(pathname, breadcrumbNameMap);
119

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

xiaoiver's avatar
xiaoiver committed
128
    return `${pageName} - Ant Design Pro`;
129
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
130

jim's avatar
jim committed
131
  getLayoutStyle = () => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
132
    const { fixSiderbar, isMobile, collapsed, layout } = this.props;
133
    if (fixSiderbar && layout !== 'topmenu' && !isMobile) {
jim's avatar
jim committed
134
      return {
135
        paddingLeft: collapsed ? '80px' : '256px',
jim's avatar
jim committed
136 137 138 139
      };
    }
    return null;
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
140

jim's avatar
jim committed
141 142 143 144 145 146 147
  getContentStyle = () => {
    const { fixedHeader } = this.props;
    return {
      margin: '24px 24px 0',
      paddingTop: fixedHeader ? 64 : 0,
    };
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
148

jim's avatar
jim committed
149
  handleMenuCollapse = collapsed => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
150 151
    const { dispatch } = this.props;
    dispatch({
152 153 154
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
155
  };
156

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
157
  renderSettingDrawer = () => {
kennylbj's avatar
kennylbj committed
158 159
    // Do not render SettingDrawer in production
    // unless it is deployed in preview.pro.ant.design as demo
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
160
    if (process.env.NODE_ENV === 'production' && APP_TYPE !== 'site') {
161 162 163
      return null;
    }
    return <SettingDrawer />;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
164
  };
165

166
  render() {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
167
    const {
afc163's avatar
afc163 committed
168
      navTheme,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
169
      layout: PropsLayout,
ๆ„š้“'s avatar
ๆ„š้“ committed
170
      children,
171
      location: { pathname },
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
172
      isMobile,
173
      menuData,
174
      breadcrumbNameMap,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
175
    } = this.props;
176

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

233
export default connect(({ global, setting, menu }) => ({
234
  collapsed: global.collapsed,
jim's avatar
jim committed
235
  layout: setting.layout,
236
  menuData: menu.menuData,
237
  breadcrumbNameMap: menu.breadcrumbNameMap,
jim's avatar
jim committed
238
  ...setting,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
239 240 241 242 243
}))(props => (
  <Media query="(max-width: 599px)">
    {isMobile => <BasicLayout {...props} isMobile={isMobile} />}
  </Media>
));