BasicLayout.js 6.22 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';
陈帅's avatar
陈帅 committed
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';
陈帅's avatar
陈帅 committed
17
import Exception403 from '../pages/Exception/403';
陈帅's avatar
陈帅 committed
18 19 20 21 22
import PageLoading from '@/components/PageLoading';
import SiderMenu from '@/components/SiderMenu';

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

jim's avatar
jim committed
24
const { Content } = Layout;
ddcat1115's avatar
ddcat1115 committed
25

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

51
class BasicLayout extends React.PureComponent {
陈帅's avatar
陈帅 committed
52 53
  constructor(props) {
    super(props);
陈帅's avatar
陈帅 committed
54
    this.getPageTitle = memoizeOne(this.getPageTitle);
55 56
    this.getBreadcrumbNameMap = memoizeOne(this.getBreadcrumbNameMap, isEqual);
    this.breadcrumbNameMap = this.getBreadcrumbNameMap();
陈帅's avatar
陈帅 committed
57
    this.matchParamsPath = memoizeOne(this.matchParamsPath, isEqual);
陈帅's avatar
陈帅 committed
58
  }
59 60

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

77 78 79
  componentDidUpdate(preProps) {
    // After changing to phone mode,
    // if collapsed is true, you need to click twice to display
80
    this.breadcrumbNameMap = this.getBreadcrumbNameMap();
陈帅's avatar
陈帅 committed
81
    const { collapsed, isMobile } = this.props;
82 83 84
    if (isMobile && !preProps.isMobile && !collapsed) {
      this.handleMenuCollapse(false);
    }
85 86
  }

jim's avatar
jim committed
87
  getContext() {
陈帅's avatar
陈帅 committed
88
    const { location } = this.props;
ddcat1115's avatar
ddcat1115 committed
89 90
    return {
      location,
陈帅's avatar
陈帅 committed
91
      breadcrumbNameMap: this.breadcrumbNameMap,
ddcat1115's avatar
ddcat1115 committed
92
    };
93
  }
94

95 96 97 98 99 100
  /**
   * 获取面包屑映射
   * @param {Object} menuData 菜单配置
   */
  getBreadcrumbNameMap() {
    const routerMap = {};
陈小聪's avatar
陈小聪 committed
101
    const { menuData } = this.props;
102
    const flattenMenuData = data => {
103 104
      data.forEach(menuItem => {
        if (menuItem.children) {
105
          flattenMenuData(menuItem.children);
106 107 108 109 110
        }
        // Reduce memory usage
        routerMap[menuItem.path] = menuItem;
      });
    };
陈小聪's avatar
陈小聪 committed
111
    flattenMenuData(menuData);
112 113 114
    return routerMap;
  }

陈帅's avatar
陈帅 committed
115 116 117 118
  matchParamsPath = pathname => {
    const pathKey = Object.keys(this.breadcrumbNameMap).find(key =>
      pathToRegexp(key).test(pathname)
    );
陈帅's avatar
陈帅 committed
119
    return this.breadcrumbNameMap[pathKey];
陈帅's avatar
陈帅 committed
120 121
  };

陈帅's avatar
陈帅 committed
122
  getPageTitle = pathname => {
陈帅's avatar
陈帅 committed
123 124
    const currRouterData = this.matchParamsPath(pathname);

陈帅's avatar
陈帅 committed
125 126
    if (!currRouterData) {
      return 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
127
    }
xiaoiver's avatar
xiaoiver committed
128
    const pageName = formatMessage({
陈帅's avatar
陈帅 committed
129 130 131
      id: currRouterData.locale || currRouterData.name,
      defaultMessage: currRouterData.name,
    });
xiaoiver's avatar
xiaoiver committed
132
    return `${pageName} - Ant Design Pro`;
陈帅's avatar
陈帅 committed
133
  };
陈帅's avatar
陈帅 committed
134

jim's avatar
jim committed
135
  getLayoutStyle = () => {
陈帅's avatar
陈帅 committed
136
    const { fixSiderbar, isMobile, collapsed, layout } = this.props;
137
    if (fixSiderbar && layout !== 'topmenu' && !isMobile) {
jim's avatar
jim committed
138
      return {
139
        paddingLeft: collapsed ? '80px' : '256px',
jim's avatar
jim committed
140 141 142 143
      };
    }
    return null;
  };
陈帅's avatar
陈帅 committed
144

jim's avatar
jim committed
145 146 147 148 149 150 151
  getContentStyle = () => {
    const { fixedHeader } = this.props;
    return {
      margin: '24px 24px 0',
      paddingTop: fixedHeader ? 64 : 0,
    };
  };
陈帅's avatar
陈帅 committed
152

jim's avatar
jim committed
153
  handleMenuCollapse = collapsed => {
陈帅's avatar
陈帅 committed
154 155
    const { dispatch } = this.props;
    dispatch({
156 157 158
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
159
  };
160

陈帅's avatar
陈帅 committed
161
  renderSettingDrawer = () => {
kennylbj's avatar
kennylbj committed
162 163
    // Do not render SettingDrawer in production
    // unless it is deployed in preview.pro.ant.design as demo
陈帅's avatar
陈帅 committed
164
    if (process.env.NODE_ENV === 'production' && APP_TYPE !== 'site') {
165 166 167
      return null;
    }
    return <SettingDrawer />;
陈帅's avatar
陈帅 committed
168
  };
169

170
  render() {
陈帅's avatar
陈帅 committed
171
    const {
afc163's avatar
afc163 committed
172
      navTheme,
陈帅's avatar
陈帅 committed
173
      layout: PropsLayout,
愚道's avatar
愚道 committed
174
      children,
陈帅's avatar
陈帅 committed
175
      location: { pathname },
陈帅's avatar
陈帅 committed
176
      isMobile,
陈小聪's avatar
陈小聪 committed
177
      menuData,
陈帅's avatar
陈帅 committed
178 179
    } = this.props;
    const isTop = PropsLayout === 'topmenu';
陈帅's avatar
陈帅 committed
180
    const routerConfig = this.matchParamsPath(pathname);
afc163's avatar
afc163 committed
181 182
    const layout = (
      <Layout>
jim's avatar
jim committed
183
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
184 185
          <SiderMenu
            logo={logo}
afc163's avatar
afc163 committed
186
            theme={navTheme}
jim's avatar
jim committed
187
            onCollapse={this.handleMenuCollapse}
188
            menuData={menuData}
189
            isMobile={isMobile}
jim's avatar
jim committed
190
            {...this.props}
jim's avatar
jim committed
191 192
          />
        )}
陈帅's avatar
陈帅 committed
193 194 195 196 197 198
        <Layout
          style={{
            ...this.getLayoutStyle(),
            minHeight: '100vh',
          }}
        >
199 200 201 202
          <Header
            menuData={menuData}
            handleMenuCollapse={this.handleMenuCollapse}
            logo={logo}
203
            isMobile={isMobile}
204 205
            {...this.props}
          />
陈帅's avatar
陈帅 committed
206
          <Content style={this.getContentStyle()}>
陈帅's avatar
陈帅 committed
207 208 209 210
            <Authorized
              authority={routerConfig && routerConfig.authority}
              noMatch={<Exception403 />}
            >
陈帅's avatar
陈帅 committed
211 212 213
              {children}
            </Authorized>
          </Content>
jim's avatar
jim committed
214
          <Footer />
215
        </Layout>
afc163's avatar
afc163 committed
216 217 218
      </Layout>
    );
    return (
陈帅's avatar
陈帅 committed
219 220 221 222 223 224 225 226 227 228
      <React.Fragment>
        <DocumentTitle title={this.getPageTitle(pathname)}>
          <ContainerQuery query={query}>
            {params => (
              <Context.Provider value={this.getContext()}>
                <div className={classNames(params)}>{layout}</div>
              </Context.Provider>
            )}
          </ContainerQuery>
        </DocumentTitle>
陈帅's avatar
陈帅 committed
229
        <Suspense fallback={<PageLoading />}>{this.renderSettingDrawer()}</Suspense>
陈帅's avatar
陈帅 committed
230
      </React.Fragment>
231 232 233 234
    );
  }
}

陈小聪's avatar
陈小聪 committed
235
export default connect(({ global, setting, menu }) => ({
Andreas Cederström's avatar
Andreas Cederström committed
236
  collapsed: global.collapsed,
jim's avatar
jim committed
237
  layout: setting.layout,
陈小聪's avatar
陈小聪 committed
238
  menuData: menu.menuData,
jim's avatar
jim committed
239
  ...setting,
陈帅's avatar
陈帅 committed
240 241 242 243 244
}))(props => (
  <Media query="(max-width: 599px)">
    {isMobile => <BasicLayout {...props} isMobile={isMobile} />}
  </Media>
));