BasicLayout.js 5.67 KB
Newer Older
jim's avatar
jim committed
1 2
import React from 'react';
import { Layout } from 'antd';
3
import DocumentTitle from 'react-document-title';
4
import deepEqual from 'lodash.isequal';
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 { formatMessage } from 'umi/locale';
11 12
import SiderMenu from '@/components/SiderMenu';
import Authorized from '@/utils/Authorized';
13
import SettingDrawer from '@/components/SettingDrawer';
14
import logo from '../assets/logo.svg';
jim's avatar
jim committed
15 16
import Footer from './Footer';
import Header from './Header';
17
import Context from './MenuContext';
18

jim's avatar
jim committed
19
const { Content } = Layout;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
20
const { check } = Authorized;
ddcat1115's avatar
ddcat1115 committed
21

22 23 24 25
/**
 * ่Žทๅ–้ขๅŒ…ๅฑ‘ๆ˜ ๅฐ„
 * @param {Object} menuData ่œๅ•้…็ฝฎ
 */
26
const getBreadcrumbNameMap = memoizeOne(meun => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
27 28 29 30 31 32
  const routerMap = {};
  const mergeMeunAndRouter = meunData => {
    meunData.forEach(meunItem => {
      if (meunItem.children) {
        mergeMeunAndRouter(meunItem.children);
      }
33 34
      // Reduce memory usage
      routerMap[meunItem.path] = meunItem;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
35 36 37 38
    });
  };
  mergeMeunAndRouter(meun);
  return routerMap;
39
}, deepEqual);
40

afc163's avatar
afc163 committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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
59 60 61 62
    maxWidth: 1599,
  },
  'screen-xxl': {
    minWidth: 1600,
afc163's avatar
afc163 committed
63 64 65
  },
};

66
class BasicLayout extends React.PureComponent {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
67 68 69
  state = {
    rendering: true,
  };
70 71
  constructor(props) {
    super(props);
72
    const { menuData } = this.props;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
73
    this.getPageTitle = memoizeOne(this.getPageTitle);
74
    // Because there are many places to be. So put it here
75
    this.breadcrumbNameMap = getBreadcrumbNameMap(menuData);
76
  }
jim's avatar
jim committed
77
  getContext() {
78
    const { location } = this.props;
ddcat1115's avatar
ddcat1115 committed
79 80
    return {
      location,
81
      breadcrumbNameMap: this.breadcrumbNameMap,
ddcat1115's avatar
ddcat1115 committed
82
    };
83
  }
84 85 86 87
  componentDidUpdate() {
    const { menuData } = this.props;
    this.breadcrumbNameMap = getBreadcrumbNameMap(menuData);
  }
88
  getPageTitle = pathname => {
jim's avatar
jim committed
89 90
    let currRouterData = null;
    // match params path
91
    Object.keys(this.breadcrumbNameMap).forEach(key => {
jim's avatar
jim committed
92
      if (pathToRegexp(key).test(pathname)) {
93
        currRouterData = this.breadcrumbNameMap[key];
jim's avatar
jim committed
94 95
      }
    });
96 97
    if (!currRouterData) {
      return 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
98
    }
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
99
    const message = formatMessage({
100 101 102 103 104
      id: currRouterData.locale || currRouterData.name,
      defaultMessage: currRouterData.name,
    });
    return `${message} - Ant Design Pro`;
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
105

jim's avatar
jim committed
106
  getLayoutStyle = () => {
107 108
    const { fixSiderbar, collapsed, layout } = this.props;
    if (fixSiderbar && layout !== 'topmenu') {
jim's avatar
jim committed
109
      return {
110
        paddingLeft: collapsed ? '80px' : '256px',
jim's avatar
jim committed
111 112 113 114
      };
    }
    return null;
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
115

jim's avatar
jim committed
116 117 118 119 120 121 122
  getContentStyle = () => {
    const { fixedHeader } = this.props;
    return {
      margin: '24px 24px 0',
      paddingTop: fixedHeader ? 64 : 0,
    };
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
123

124 125 126 127
  getBashRedirect = () => {
    // According to the url parameter to redirect
    // ่ฟ™้‡Œๆ˜ฏ้‡ๅฎšๅ‘็š„,้‡ๅฎšๅ‘ๅˆฐ url ็š„ redirect ๅ‚ๆ•ฐๆ‰€็คบๅœฐๅ€
    const urlParams = new URL(window.location.href);
jim's avatar
jim committed
128 129

    const redirect = urlParams.searchParams.get('redirect');
130
    // Remove the parameters in the url
jim's avatar
jim committed
131 132 133 134
    if (redirect) {
      urlParams.searchParams.delete('redirect');
      window.history.replaceState(null, 'redirect', urlParams.href);
    } else {
ddcat1115's avatar
ddcat1115 committed
135 136
      const { routerData } = this.props;
      // get the first authorized route path in routerData
jim's avatar
jim committed
137 138 139
      const authorizedPath = Object.keys(routerData).find(
        item => check(routerData[item].authority, item) && item !== '/'
      );
ddcat1115's avatar
ddcat1115 committed
140
      return authorizedPath;
jim's avatar
jim committed
141
    }
142
    return redirect;
jim's avatar
jim committed
143
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
144

jim's avatar
jim committed
145
  handleMenuCollapse = collapsed => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
146 147
    const { dispatch } = this.props;
    dispatch({
148 149 150
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
151
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
152 153 154 155 156 157 158 159 160 161
  componentDidMount() {
    this.renderRef = requestAnimationFrame(() => {
      this.setState({
        rendering: false,
      });
    });
  }
  componentWillUnmount() {
    cancelAnimationFrame(this.renderRef);
  }
162
  render() {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
163 164 165 166
    const {
      isMobile,
      silderTheme,
      layout: PropsLayout,
ๆ„š้“'s avatar
ๆ„š้“ committed
167
      children,
168
      location: { pathname },
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
169 170
    } = this.props;
    const isTop = PropsLayout === 'topmenu';
afc163's avatar
afc163 committed
171 172
    const layout = (
      <Layout>
jim's avatar
jim committed
173
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
174 175 176
          <SiderMenu
            logo={logo}
            Authorized={Authorized}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
177
            theme={silderTheme}
jim's avatar
jim committed
178
            onCollapse={this.handleMenuCollapse}
jim's avatar
jim committed
179
            {...this.props}
jim's avatar
jim committed
180 181
          />
        )}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
182 183 184 185 186 187
        <Layout
          style={{
            ...this.getLayoutStyle(),
            minHeight: '100vh',
          }}
        >
jim's avatar
jim committed
188
          <Header handleMenuCollapse={this.handleMenuCollapse} logo={logo} {...this.props} />
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
189
          <Content style={this.getContentStyle()}>{children}</Content>
jim's avatar
jim committed
190
          <Footer />
191
        </Layout>
afc163's avatar
afc163 committed
192 193 194
      </Layout>
    );
    return (
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
195 196 197 198 199 200 201 202 203 204
      <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>
afc163's avatar
afc163 committed
205 206 207
        {this.state.rendering && process.env.NODE_ENV === 'production' ? null : ( // Do show SettingDrawer in production
          <SettingDrawer />
        )}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
208
      </React.Fragment>
209 210 211 212
    );
  }
}

jim's avatar
jim committed
213
export default connect(({ global, setting }) => ({
214
  collapsed: global.collapsed,
jim's avatar
jim committed
215 216
  layout: setting.layout,
  ...setting,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
217
}))(BasicLayout);