BasicLayout.js 4.22 KB
Newer Older
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';
陈帅's avatar
陈帅 committed
7
import Media from 'react-media';
8
import logo from '../assets/logo.svg';
jim's avatar
jim committed
9 10
import Footer from './Footer';
import Header from './Header';
11
import Context from './MenuContext';
12
import PageLoading from '@/components/PageLoading';
陈帅's avatar
陈帅 committed
13
import SiderMenu from '@/components/SiderMenu';
Yu's avatar
Yu committed
14
import getPageTitle from '@/utils/getPageTitle';
15 16
import styles from './BasicLayout.less';

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

jim's avatar
jim committed
20
const { Content } = Layout;
ddcat1115's avatar
ddcat1115 committed
21

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

Yu's avatar
Yu committed
47
class BasicLayout extends React.Component {
48
  componentDidMount() {
陈小聪's avatar
陈小聪 committed
49 50 51 52
    const {
      dispatch,
      route: { routes, authority },
    } = this.props;
afc163's avatar
afc163 committed
53 54 55 56 57 58
    dispatch({
      type: 'user/fetchCurrent',
    });
    dispatch({
      type: 'setting/getSetting',
    });
陈小聪's avatar
陈小聪 committed
59 60 61 62
    dispatch({
      type: 'menu/getMenuData',
      payload: { routes, authority },
    });
63 64
  }

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

jim's avatar
jim committed
73
  getLayoutStyle = () => {
陈帅's avatar
陈帅 committed
74
    const { fixSiderbar, isMobile, collapsed, layout } = this.props;
75
    if (fixSiderbar && layout !== 'topmenu' && !isMobile) {
jim's avatar
jim committed
76
      return {
77
        paddingLeft: collapsed ? '80px' : '256px',
jim's avatar
jim committed
78 79 80 81
      };
    }
    return null;
  };
陈帅's avatar
陈帅 committed
82

jim's avatar
jim committed
83
  handleMenuCollapse = collapsed => {
陈帅's avatar
陈帅 committed
84 85
    const { dispatch } = this.props;
    dispatch({
86 87 88
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
89
  };
90

91 92 93 94 95 96 97 98 99
  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 />;
  };

100
  render() {
陈帅's avatar
陈帅 committed
101
    const {
afc163's avatar
afc163 committed
102
      navTheme,
陈帅's avatar
陈帅 committed
103
      layout: PropsLayout,
愚道's avatar
愚道 committed
104
      children,
陈帅's avatar
陈帅 committed
105
      location: { pathname },
陈帅's avatar
陈帅 committed
106
      isMobile,
陈小聪's avatar
陈小聪 committed
107
      menuData,
108
      breadcrumbNameMap,
109
      fixedHeader,
陈帅's avatar
陈帅 committed
110
    } = this.props;
111

陈帅's avatar
陈帅 committed
112
    const isTop = PropsLayout === 'topmenu';
113
    const contentStyle = !fixedHeader ? { paddingTop: 0 } : {};
afc163's avatar
afc163 committed
114 115
    const layout = (
      <Layout>
jim's avatar
jim committed
116
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
117 118
          <SiderMenu
            logo={logo}
afc163's avatar
afc163 committed
119
            theme={navTheme}
jim's avatar
jim committed
120
            onCollapse={this.handleMenuCollapse}
121
            menuData={menuData}
122
            isMobile={isMobile}
jim's avatar
jim committed
123
            {...this.props}
jim's avatar
jim committed
124 125
          />
        )}
陈帅's avatar
陈帅 committed
126 127 128 129 130 131
        <Layout
          style={{
            ...this.getLayoutStyle(),
            minHeight: '100vh',
          }}
        >
132 133 134 135
          <Header
            menuData={menuData}
            handleMenuCollapse={this.handleMenuCollapse}
            logo={logo}
136
            isMobile={isMobile}
137 138
            {...this.props}
          />
139
          <Content className={styles.content} style={contentStyle}>
Yu's avatar
Yu committed
140
            {children}
陈帅's avatar
陈帅 committed
141
          </Content>
jim's avatar
jim committed
142
          <Footer />
143
        </Layout>
afc163's avatar
afc163 committed
144 145 146
      </Layout>
    );
    return (
陈帅's avatar
陈帅 committed
147
      <React.Fragment>
Yu's avatar
Yu committed
148
        <DocumentTitle title={getPageTitle(pathname, breadcrumbNameMap)}>
陈帅's avatar
陈帅 committed
149 150 151 152 153 154 155 156
          <ContainerQuery query={query}>
            {params => (
              <Context.Provider value={this.getContext()}>
                <div className={classNames(params)}>{layout}</div>
              </Context.Provider>
            )}
          </ContainerQuery>
        </DocumentTitle>
157
        <Suspense fallback={<PageLoading />}>{this.renderSettingDrawer()}</Suspense>
陈帅's avatar
陈帅 committed
158
      </React.Fragment>
159 160 161 162
    );
  }
}

Yu's avatar
Yu committed
163
export default connect(({ global, setting, menu: menuModel }) => ({
Andreas Cederström's avatar
Andreas Cederström committed
164
  collapsed: global.collapsed,
jim's avatar
jim committed
165
  layout: setting.layout,
Yu's avatar
Yu committed
166 167
  menuData: menuModel.menuData,
  breadcrumbNameMap: menuModel.breadcrumbNameMap,
jim's avatar
jim committed
168
  ...setting,
陈帅's avatar
陈帅 committed
169 170 171 172 173
}))(props => (
  <Media query="(max-width: 599px)">
    {isMobile => <BasicLayout {...props} isMobile={isMobile} />}
  </Media>
));