BasicLayout.js 5.7 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';
afc163's avatar
afc163 committed
4
import deepEqual 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 { 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;
陈帅's avatar
陈帅 committed
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
  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);
陈帅's avatar
陈帅 committed
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() {
陈帅's avatar
陈帅 committed
97
    const { location } = this.props;
ddcat1115's avatar
ddcat1115 committed
98 99
    return {
      location,
陈帅's avatar
陈帅 committed
100
      breadcrumbNameMap: this.breadcrumbNameMap,
ddcat1115's avatar
ddcat1115 committed
101
    };
102
  }
103

陈帅's avatar
陈帅 committed
104
  getPageTitle = pathname => {
jim's avatar
jim committed
105 106
    let currRouterData = null;
    // match params path
陈帅's avatar
陈帅 committed
107
    Object.keys(this.breadcrumbNameMap).forEach(key => {
jim's avatar
jim committed
108
      if (pathToRegexp(key).test(pathname)) {
陈帅's avatar
陈帅 committed
109
        currRouterData = this.breadcrumbNameMap[key];
jim's avatar
jim committed
110 111
      }
    });
陈帅's avatar
陈帅 committed
112 113
    if (!currRouterData) {
      return 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
114
    }
陈帅's avatar
陈帅 committed
115
    const message = formatMessage({
陈帅's avatar
陈帅 committed
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 = () => {
陈帅's avatar
陈帅 committed
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,
陈帅's avatar
陈帅 committed
175
      location: { pathname },
陈帅's avatar
陈帅 committed
176
    } = this.props;
177
    const { rendering } = this.state;
陈帅's avatar
陈帅 committed
178
    const isTop = PropsLayout === 'topmenu';
afc163's avatar
afc163 committed
179 180
    const layout = (
      <Layout>
jim's avatar
jim committed
181
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
182 183 184
          <SiderMenu
            logo={logo}
            Authorized={Authorized}
陈帅's avatar
陈帅 committed
185
            theme={silderTheme}
jim's avatar
jim committed
186
            onCollapse={this.handleMenuCollapse}
jim's avatar
jim committed
187
            {...this.props}
jim's avatar
jim committed
188 189
          />
        )}
陈帅's avatar
陈帅 committed
190 191 192 193 194 195
        <Layout
          style={{
            ...this.getLayoutStyle(),
            minHeight: '100vh',
          }}
        >
jim's avatar
jim committed
196
          <Header handleMenuCollapse={this.handleMenuCollapse} logo={logo} {...this.props} />
陈帅's avatar
陈帅 committed
197
          <Content style={this.getContentStyle()}>{children}</Content>
jim's avatar
jim committed
198
          <Footer />
199
        </Layout>
afc163's avatar
afc163 committed
200 201 202
      </Layout>
    );
    return (
陈帅's avatar
陈帅 committed
203 204 205 206 207 208 209 210 211 212
      <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>
213
        {rendering && process.env.NODE_ENV === 'production' ? null : ( // Do show SettingDrawer in production
afc163's avatar
afc163 committed
214 215
          <SettingDrawer />
        )}
陈帅's avatar
陈帅 committed
216
      </React.Fragment>
217 218 219 220
    );
  }
}

jim's avatar
jim committed
221
export default connect(({ global, setting }) => ({
Andreas Cederström's avatar
Andreas Cederström committed
222
  collapsed: global.collapsed,
jim's avatar
jim committed
223 224
  layout: setting.layout,
  ...setting,
陈帅's avatar
陈帅 committed
225
}))(BasicLayout);