BasicLayout.js 6.1 KB
Newer Older
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 PageLoading from '@/components/PageLoading';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
18
import SiderMenu from '@/components/SiderMenu';
19
import { title } from '../defaultSettings';
20 21
import styles from './BasicLayout.less';

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

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

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

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

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

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

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

93 94 95
  matchParamsPath = (pathname, breadcrumbNameMap) => {
    const pathKey = Object.keys(breadcrumbNameMap).find(key => pathToRegexp(key).test(pathname));
    return breadcrumbNameMap[pathKey];
96 97
  };

98 99 100
  getRouterAuthority = (pathname, routeData) => {
    let routeAuthority = ['noAuthority'];
    const getAuthority = (key, routes) => {
101
      routes.forEach(route => {
102
        if (route.path && pathToRegexp(route.path).test(key)) {
103 104 105 106 107 108 109 110 111 112 113
          routeAuthority = route.authority;
        } else if (route.routes) {
          routeAuthority = getAuthority(key, route.routes);
        }
        return route;
      });
      return routeAuthority;
    };
    return getAuthority(pathname, routeData);
  };

114 115
  getPageTitle = (pathname, breadcrumbNameMap) => {
    const currRouterData = this.matchParamsPath(pathname, breadcrumbNameMap);
116

117
    if (!currRouterData) {
118
      return title;
ddcat1115's avatar
ddcat1115 committed
119
    }
xiaoiver's avatar
xiaoiver committed
120
    const pageName = formatMessage({
121 122 123
      id: currRouterData.locale || currRouterData.name,
      defaultMessage: currRouterData.name,
    });
124

125
    return `${pageName} - ${title}`;
126
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
127

jim's avatar
jim committed
128
  getLayoutStyle = () => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
129
    const { fixSiderbar, isMobile, collapsed, layout } = this.props;
130
    if (fixSiderbar && layout !== 'topmenu' && !isMobile) {
jim's avatar
jim committed
131
      return {
132
        paddingLeft: collapsed ? '80px' : '256px',
jim's avatar
jim committed
133 134 135 136
      };
    }
    return null;
  };
้™ˆๅธ…'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
  };
145

146 147 148 149 150 151 152 153 154
  renderSettingDrawer = () => {
    // Do not render SettingDrawer in production
    // unless it is deployed in preview.pro.ant.design as demo
    if (process.env.NODE_ENV === 'production' && APP_TYPE !== 'site') {
      return null;
    }
    return <SettingDrawer />;
  };

155
  render() {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
156
    const {
afc163's avatar
afc163 committed
157
      navTheme,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
158
      layout: PropsLayout,
ๆ„š้“'s avatar
ๆ„š้“ committed
159
      children,
160
      location: { pathname },
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
161
      isMobile,
162
      menuData,
163
      breadcrumbNameMap,
164
      route: { routes },
165
      fixedHeader,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
166
    } = this.props;
167

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
168
    const isTop = PropsLayout === 'topmenu';
169
    const routerConfig = this.getRouterAuthority(pathname, routes);
170
    const contentStyle = !fixedHeader ? { paddingTop: 0 } : {};
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
          <SiderMenu
            logo={logo}
afc163's avatar
afc163 committed
176
            theme={navTheme}
jim's avatar
jim committed
177
            onCollapse={this.handleMenuCollapse}
178
            menuData={menuData}
179
            isMobile={isMobile}
jim's avatar
jim committed
180
            {...this.props}
jim's avatar
jim committed
181 182
          />
        )}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
183 184 185 186 187 188
        <Layout
          style={{
            ...this.getLayoutStyle(),
            minHeight: '100vh',
          }}
        >
189 190 191 192
          <Header
            menuData={menuData}
            handleMenuCollapse={this.handleMenuCollapse}
            logo={logo}
193
            isMobile={isMobile}
194 195
            {...this.props}
          />
196
          <Content className={styles.content} style={contentStyle}>
ๆ„š้“'s avatar
ๆ„š้“ committed
197
            <Authorized authority={routerConfig} noMatch={<p>Exception403</p>}>
198 199 200
              {children}
            </Authorized>
          </Content>
jim's avatar
jim committed
201
          <Footer />
202
        </Layout>
afc163's avatar
afc163 committed
203 204 205
      </Layout>
    );
    return (
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
206
      <React.Fragment>
207
        <DocumentTitle title={this.getPageTitle(pathname, breadcrumbNameMap)}>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
208 209 210 211 212 213 214 215
          <ContainerQuery query={query}>
            {params => (
              <Context.Provider value={this.getContext()}>
                <div className={classNames(params)}>{layout}</div>
              </Context.Provider>
            )}
          </ContainerQuery>
        </DocumentTitle>
216
        <Suspense fallback={<PageLoading />}>{this.renderSettingDrawer()}</Suspense>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
217
      </React.Fragment>
218 219 220 221
    );
  }
}

222
export default connect(({ global, setting, menu }) => ({
223
  collapsed: global.collapsed,
jim's avatar
jim committed
224
  layout: setting.layout,
225
  menuData: menu.menuData,
226
  breadcrumbNameMap: menu.breadcrumbNameMap,
jim's avatar
jim committed
227
  ...setting,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
228 229 230 231 232
}))(props => (
  <Media query="(max-width: 599px)">
    {isMobile => <BasicLayout {...props} isMobile={isMobile} />}
  </Media>
));