BasicLayout.js 4.08 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
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
89
    if (process.env.NODE_ENV === 'production' && APP_TYPE !== 'site') {
90 91 92
      return null;
    }
    return <SettingDrawer />;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
93
  };
94

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

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

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