BasicLayout.js 5.19 KB
Newer Older
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
1
import React, { Suspense } from 'react';
jim's avatar
jim committed
2
import { Layout } from 'antd';
3 4
import DocumentTitle from 'react-document-title';
import { connect } from 'dva';
afc163's avatar
afc163 committed
5 6
import { ContainerQuery } from 'react-container-query';
import classNames from 'classnames';
jim's avatar
jim committed
7
import pathToRegexp from 'path-to-regexp';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
8
import Media from 'react-media';
9
import Authorized from '@/utils/Authorized';
10
import logo from '../assets/logo.svg';
jim's avatar
jim committed
11 12
import Footer from './Footer';
import Header from './Header';
13
import Context from './MenuContext';
14
import Exception403 from '../pages/Exception/403';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
15 16
import PageLoading from '@/components/PageLoading';
import SiderMenu from '@/components/SiderMenu';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
17
import getPageTitle from '@/utils/getPageTitle';
18 19
import styles from './BasicLayout.less';

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
20 21
// lazy load SettingDrawer
const SettingDrawer = React.lazy(() => import('@/components/SettingDrawer'));
22

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

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

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
50
class BasicLayout extends React.Component {
51
  componentDidMount() {
52 53 54 55
    const {
      dispatch,
      route: { routes, authority },
    } = this.props;
afc163's avatar
afc163 committed
56 57 58 59 60 61
    dispatch({
      type: 'user/fetchCurrent',
    });
    dispatch({
      type: 'setting/getSetting',
    });
62 63 64 65
    dispatch({
      type: 'menu/getMenuData',
      payload: { routes, authority },
    });
66 67
  }

jim's avatar
jim committed
68
  getContext() {
69
    const { location, breadcrumbNameMap } = this.props;
ddcat1115's avatar
ddcat1115 committed
70 71
    return {
      location,
72
      breadcrumbNameMap,
ddcat1115's avatar
ddcat1115 committed
73
    };
74
  }
75

76 77 78
  getRouteAuthority = (pathname, routeData) => {
    const routes = routeData.slice(); // clone

79 80 81 82 83 84 85 86 87 88 89 90
    const getAuthority = (routeDatas, path) => {
      let authorities;
      routeDatas.forEach(route => {
        // check partial route
        if (pathToRegexp(`${route.path}(.*)`).test(path)) {
          if (route.authority) {
            authorities = route.authority;
          }
          // is exact route?
          if (!pathToRegexp(route.path).test(path) && route.routes) {
            authorities = getAuthority(route.routes, path);
          }
91
        }
92 93 94
      });
      return authorities;
    };
95

96
    return getAuthority(routes, pathname);
97 98
  };

jim's avatar
jim committed
99
  getLayoutStyle = () => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
100
    const { fixSiderbar, isMobile, collapsed, layout } = this.props;
101
    if (fixSiderbar && layout !== 'topmenu' && !isMobile) {
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
  handleMenuCollapse = collapsed => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
110 111
    const { dispatch } = this.props;
    dispatch({
112 113 114
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
115
  };
116

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
117
  renderSettingDrawer = () => {
kennylbj's avatar
kennylbj committed
118 119
    // Do not render SettingDrawer in production
    // unless it is deployed in preview.pro.ant.design as demo
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
120
    if (process.env.NODE_ENV === 'production' && APP_TYPE !== 'site') {
121 122 123
      return null;
    }
    return <SettingDrawer />;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
124
  };
125

126
  render() {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
127
    const {
afc163's avatar
afc163 committed
128
      navTheme,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
129
      layout: PropsLayout,
ๆ„š้“'s avatar
ๆ„š้“ committed
130
      children,
131
      location: { pathname },
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
132
      isMobile,
133
      menuData,
134
      breadcrumbNameMap,
135
      route: { routes },
136
      fixedHeader,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
137
    } = this.props;
138

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
139
    const isTop = PropsLayout === 'topmenu';
140
    const routerConfig = this.getRouteAuthority(pathname, routes);
141
    const contentStyle = !fixedHeader ? { paddingTop: 0 } : {};
afc163's avatar
afc163 committed
142 143
    const layout = (
      <Layout>
jim's avatar
jim committed
144
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
145 146
          <SiderMenu
            logo={logo}
afc163's avatar
afc163 committed
147
            theme={navTheme}
jim's avatar
jim committed
148
            onCollapse={this.handleMenuCollapse}
149
            menuData={menuData}
150
            isMobile={isMobile}
jim's avatar
jim committed
151
            {...this.props}
jim's avatar
jim committed
152 153
          />
        )}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
154 155 156 157 158 159
        <Layout
          style={{
            ...this.getLayoutStyle(),
            minHeight: '100vh',
          }}
        >
160 161 162 163
          <Header
            menuData={menuData}
            handleMenuCollapse={this.handleMenuCollapse}
            logo={logo}
164
            isMobile={isMobile}
165 166
            {...this.props}
          />
167
          <Content className={styles.content} style={contentStyle}>
168
            <Authorized authority={routerConfig} noMatch={<Exception403 />}>
169 170 171
              {children}
            </Authorized>
          </Content>
jim's avatar
jim committed
172
          <Footer />
173
        </Layout>
afc163's avatar
afc163 committed
174 175 176
      </Layout>
    );
    return (
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
177
      <React.Fragment>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
178
        <DocumentTitle title={getPageTitle(pathname, breadcrumbNameMap)}>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
179 180 181 182 183 184 185 186
          <ContainerQuery query={query}>
            {params => (
              <Context.Provider value={this.getContext()}>
                <div className={classNames(params)}>{layout}</div>
              </Context.Provider>
            )}
          </ContainerQuery>
        </DocumentTitle>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
187
        <Suspense fallback={<PageLoading />}>{this.renderSettingDrawer()}</Suspense>
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
188
      </React.Fragment>
189 190 191 192
    );
  }
}

193
export default connect(({ global, setting, menu: menuModel }) => ({
194
  collapsed: global.collapsed,
jim's avatar
jim committed
195
  layout: setting.layout,
196 197
  menuData: menuModel.menuData,
  breadcrumbNameMap: menuModel.breadcrumbNameMap,
jim's avatar
jim committed
198
  ...setting,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
199 200 201 202 203
}))(props => (
  <Media query="(max-width: 599px)">
    {isMobile => <BasicLayout {...props} isMobile={isMobile} />}
  </Media>
));