BasicLayout.js 5.68 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 {
67 68
  constructor(props) {
    super(props);
69
    const { menuData } = this.props;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
70
    this.getPageTitle = memoizeOne(this.getPageTitle);
71
    // Because there are many places to be. So put it here
72
    this.breadcrumbNameMap = getBreadcrumbNameMap(menuData);
73
  }
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

  state = {
    rendering: true,
  };

  componentDidMount() {
    this.renderRef = requestAnimationFrame(() => {
      this.setState({
        rendering: false,
      });
    });
  }

  componentDidUpdate() {
    const { menuData } = this.props;
    this.breadcrumbNameMap = getBreadcrumbNameMap(menuData);
  }

  componentWillUnmount() {
    cancelAnimationFrame(this.renderRef);
  }

jim's avatar
jim committed
96
  getContext() {
97
    const { location } = this.props;
ddcat1115's avatar
ddcat1115 committed
98 99
    return {
      location,
100
      breadcrumbNameMap: this.breadcrumbNameMap,
ddcat1115's avatar
ddcat1115 committed
101
    };
102
  }
103

104
  getPageTitle = pathname => {
jim's avatar
jim committed
105 106
    let currRouterData = null;
    // match params path
107
    Object.keys(this.breadcrumbNameMap).forEach(key => {
jim's avatar
jim committed
108
      if (pathToRegexp(key).test(pathname)) {
109
        currRouterData = this.breadcrumbNameMap[key];
jim's avatar
jim committed
110 111
      }
    });
112 113
    if (!currRouterData) {
      return 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
114
    }
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
115
    const message = formatMessage({
116 117 118 119 120
      id: currRouterData.locale || currRouterData.name,
      defaultMessage: currRouterData.name,
    });
    return `${message} - Ant Design Pro`;
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
121

jim's avatar
jim committed
122
  getLayoutStyle = () => {
123 124
    const { fixSiderbar, collapsed, layout } = this.props;
    if (fixSiderbar && layout !== 'topmenu') {
jim's avatar
jim committed
125
      return {
126
        paddingLeft: collapsed ? '80px' : '256px',
jim's avatar
jim committed
127 128 129 130
      };
    }
    return null;
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
131

jim's avatar
jim committed
132 133 134 135 136 137 138
  getContentStyle = () => {
    const { fixedHeader } = this.props;
    return {
      margin: '24px 24px 0',
      paddingTop: fixedHeader ? 64 : 0,
    };
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
139

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

    const redirect = urlParams.searchParams.get('redirect');
146
    // Remove the parameters in the url
jim's avatar
jim committed
147 148 149 150
    if (redirect) {
      urlParams.searchParams.delete('redirect');
      window.history.replaceState(null, 'redirect', urlParams.href);
    } else {
ddcat1115's avatar
ddcat1115 committed
151 152
      const { routerData } = this.props;
      // get the first authorized route path in routerData
jim's avatar
jim committed
153 154 155
      const authorizedPath = Object.keys(routerData).find(
        item => check(routerData[item].authority, item) && item !== '/'
      );
ddcat1115's avatar
ddcat1115 committed
156
      return authorizedPath;
jim's avatar
jim committed
157
    }
158
    return redirect;
jim's avatar
jim committed
159
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
160

jim's avatar
jim committed
161
  handleMenuCollapse = collapsed => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
162 163
    const { dispatch } = this.props;
    dispatch({
164 165 166
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
167
  };
168

169
  render() {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
170 171 172 173
    const {
      isMobile,
      silderTheme,
      layout: PropsLayout,
ๆ„š้“'s avatar
ๆ„š้“ committed
174
      children,
175
      location: { pathname },
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
176 177
    } = this.props;
    const isTop = PropsLayout === 'topmenu';
afc163's avatar
afc163 committed
178 179
    const layout = (
      <Layout>
jim's avatar
jim committed
180
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
181 182 183
          <SiderMenu
            logo={logo}
            Authorized={Authorized}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
184
            theme={silderTheme}
jim's avatar
jim committed
185
            onCollapse={this.handleMenuCollapse}
jim's avatar
jim committed
186
            {...this.props}
jim's avatar
jim committed
187 188
          />
        )}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
189 190 191 192 193 194
        <Layout
          style={{
            ...this.getLayoutStyle(),
            minHeight: '100vh',
          }}
        >
jim's avatar
jim committed
195
          <Header handleMenuCollapse={this.handleMenuCollapse} logo={logo} {...this.props} />
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
196
          <Content style={this.getContentStyle()}>{children}</Content>
jim's avatar
jim committed
197
          <Footer />
198
        </Layout>
afc163's avatar
afc163 committed
199 200 201
      </Layout>
    );
    return (
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
202 203 204 205 206 207 208 209 210 211
      <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
212 213 214
        {this.state.rendering && process.env.NODE_ENV === 'production' ? null : ( // Do show SettingDrawer in production
          <SettingDrawer />
        )}
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
215
      </React.Fragment>
216 217 218 219
    );
  }
}

jim's avatar
jim committed
220
export default connect(({ global, setting }) => ({
221
  collapsed: global.collapsed,
jim's avatar
jim committed
222 223
  layout: setting.layout,
  ...setting,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
224
}))(BasicLayout);