BasicLayout.js 6.1 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
import PageLoading from '@/components/PageLoading';
import SiderMenu from '@/components/SiderMenu';
20 21
import { menu, title } from '../defaultSettings';

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

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

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

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

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

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

jim's avatar
jim committed
78
  getContext() {
79
    const { location, breadcrumbNameMap } = this.props;
ddcat1115's avatar
ddcat1115 committed
80 81
    return {
      location,
82
      breadcrumbNameMap,
ddcat1115's avatar
ddcat1115 committed
83
    };
84
  }
85

86 87 88
  matchParamsPath = (pathname, breadcrumbNameMap) => {
    const pathKey = Object.keys(breadcrumbNameMap).find(key => pathToRegexp(key).test(pathname));
    return breadcrumbNameMap[pathKey];
89 90
  };

91 92 93
  getRouteAuthority = (pathname, routeData) => {
    const routes = routeData.slice(); // clone

94 95 96 97 98 99 100 101 102 103 104 105
    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);
          }
106
        }
107 108 109
      });
      return authorities;
    };
110

111
    return getAuthority(routes, pathname);
112 113
  };

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

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

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

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

jim's avatar
jim committed
140
  handleMenuCollapse = collapsed => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
141 142
    const { dispatch } = this.props;
    dispatch({
143 144 145
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
146
  };
147

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
148
  renderSettingDrawer = () => {
kennylbj's avatar
kennylbj committed
149 150
    // Do not render SettingDrawer in production
    // unless it is deployed in preview.pro.ant.design as demo
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
151
    if (process.env.NODE_ENV === 'production' && APP_TYPE !== 'site') {
152 153 154
      return null;
    }
    return <SettingDrawer />;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
155
  };
156

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

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

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