BasicLayout.js 4.17 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';
้™ˆๅธ…'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';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
12
import SiderMenu from '@/components/SiderMenu';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
13
import getPageTitle from '@/utils/getPageTitle';
14 15
import styles from './BasicLayout.less';

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

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

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

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

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

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

jim's avatar
jim committed
82
  handleMenuCollapse = collapsed => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
83 84
    const { dispatch } = this.props;
    dispatch({
85 86 87
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
88
  };
89

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
90
  renderSettingDrawer = () => {
kennylbj's avatar
kennylbj committed
91 92
    // Do not render SettingDrawer in production
    // unless it is deployed in preview.pro.ant.design as demo
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
93
    if (process.env.NODE_ENV === 'production' && APP_TYPE !== 'site') {
94 95 96
      return null;
    }
    return <SettingDrawer />;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
97
  };
98

99
  render() {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
100
    const {
afc163's avatar
afc163 committed
101
      navTheme,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
102
      layout: PropsLayout,
ๆ„š้“'s avatar
ๆ„š้“ committed
103
      children,
104
      location: { pathname },
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
105
      isMobile,
106
      menuData,
107
      breadcrumbNameMap,
108
      fixedHeader,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
109
    } = this.props;
110

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

162
export default connect(({ global, setting, menu: menuModel }) => ({
163
  collapsed: global.collapsed,
jim's avatar
jim committed
164
  layout: setting.layout,
165 166
  menuData: menuModel.menuData,
  breadcrumbNameMap: menuModel.breadcrumbNameMap,
jim's avatar
jim committed
167
  ...setting,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
168 169 170 171 172
}))(props => (
  <Media query="(max-width: 599px)">
    {isMobile => <BasicLayout {...props} isMobile={isMobile} />}
  </Media>
));