BasicLayout.js 4.28 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
    const { dispatch } = this.props;
afc163's avatar
afc163 committed
49 50 51 52 53 54
    dispatch({
      type: 'user/fetchCurrent',
    });
    dispatch({
      type: 'setting/getSetting',
    });
55 56 57
    dispatch({
      type: 'menu/getMenuData',
    });
58 59
  }

jim's avatar
jim committed
60
  getContext() {
61
    const { location, breadcrumbNameMap } = this.props;
ddcat1115's avatar
ddcat1115 committed
62 63
    return {
      location,
64
      breadcrumbNameMap,
ddcat1115's avatar
ddcat1115 committed
65
    };
66
  }
67

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

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

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
86
  renderSettingDrawer = () => {
kennylbj's avatar
kennylbj committed
87 88
    // Do not render SettingDrawer in production
    // unless it is deployed in preview.pro.ant.design as demo
89 90 91 92 93
    // preview.pro.ant.design only do not use in your production ; preview.pro.ant.design ไธ“็”จ็Žฏๅขƒๅ˜้‡๏ผŒ่ฏทไธ่ฆๅœจไฝ ็š„้กน็›ฎไธญไฝฟ็”จๅฎƒใ€‚
    if (
      process.env.NODE_ENV === 'production' &&
      ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION !== '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>
));