BasicLayout.js 5.31 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';
陈帅's avatar
陈帅 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 13
import SiderMenu from '@/components/SiderMenu';
import Authorized from '@/utils/Authorized';
import SettingDarwer from '@/components/SettingDarwer';
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 69
  state = {
    rendering: true,
  };
陈帅's avatar
陈帅 committed
70 71
  constructor(props) {
    super(props);
72
    const { menuData } = this.props;
陈帅's avatar
陈帅 committed
73
    this.getPageTitle = memoizeOne(this.getPageTitle);
74
    this.breadcrumbNameMap = getBreadcrumbNameMap(menuData);
陈帅's avatar
陈帅 committed
75
  }
jim's avatar
jim committed
76
  getContext() {
陈帅's avatar
陈帅 committed
77
    const { location } = this.props;
ddcat1115's avatar
ddcat1115 committed
78 79
    return {
      location,
陈帅's avatar
陈帅 committed
80
      breadcrumbNameMap: this.breadcrumbNameMap,
ddcat1115's avatar
ddcat1115 committed
81
    };
82
  }
陈帅's avatar
陈帅 committed
83
  getPageTitle = pathname => {
jim's avatar
jim committed
84 85
    let currRouterData = null;
    // match params path
陈帅's avatar
陈帅 committed
86
    Object.keys(this.breadcrumbNameMap).forEach(key => {
jim's avatar
jim committed
87
      if (pathToRegexp(key).test(pathname)) {
陈帅's avatar
陈帅 committed
88
        currRouterData = this.breadcrumbNameMap[key];
jim's avatar
jim committed
89 90
      }
    });
陈帅's avatar
陈帅 committed
91 92
    if (!currRouterData) {
      return 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
93
    }
陈帅's avatar
陈帅 committed
94
    const message = formatMessage({
陈帅's avatar
陈帅 committed
95 96 97 98 99
      id: currRouterData.locale || currRouterData.name,
      defaultMessage: currRouterData.name,
    });
    return `${message} - Ant Design Pro`;
  };
陈帅's avatar
陈帅 committed
100

jim's avatar
jim committed
101
  getLayoutStyle = () => {
陈帅's avatar
陈帅 committed
102 103
    const { fixSiderbar, collapsed, layout } = this.props;
    if (fixSiderbar && layout !== 'topmenu') {
jim's avatar
jim committed
104
      return {
105
        paddingLeft: collapsed ? '80px' : '256px',
jim's avatar
jim committed
106 107 108 109
      };
    }
    return null;
  };
陈帅's avatar
陈帅 committed
110

jim's avatar
jim committed
111 112 113 114 115 116 117
  getContentStyle = () => {
    const { fixedHeader } = this.props;
    return {
      margin: '24px 24px 0',
      paddingTop: fixedHeader ? 64 : 0,
    };
  };
陈帅's avatar
陈帅 committed
118

119 120 121 122
  getBashRedirect = () => {
    // According to the url parameter to redirect
    // 这里是重定向的,重定向到 url 的 redirect 参数所示地址
    const urlParams = new URL(window.location.href);
jim's avatar
jim committed
123 124

    const redirect = urlParams.searchParams.get('redirect');
125
    // Remove the parameters in the url
jim's avatar
jim committed
126 127 128 129
    if (redirect) {
      urlParams.searchParams.delete('redirect');
      window.history.replaceState(null, 'redirect', urlParams.href);
    } else {
ddcat1115's avatar
ddcat1115 committed
130 131
      const { routerData } = this.props;
      // get the first authorized route path in routerData
jim's avatar
jim committed
132 133 134
      const authorizedPath = Object.keys(routerData).find(
        item => check(routerData[item].authority, item) && item !== '/'
      );
ddcat1115's avatar
ddcat1115 committed
135
      return authorizedPath;
jim's avatar
jim committed
136
    }
137
    return redirect;
jim's avatar
jim committed
138
  };
陈帅's avatar
陈帅 committed
139

jim's avatar
jim committed
140
  handleMenuCollapse = collapsed => {
陈帅's avatar
陈帅 committed
141 142
    const { dispatch } = this.props;
    dispatch({
143 144 145
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
146
  };
陈帅's avatar
陈帅 committed
147 148 149 150 151 152 153 154 155 156
  componentDidMount() {
    this.renderRef = requestAnimationFrame(() => {
      this.setState({
        rendering: false,
      });
    });
  }
  componentWillUnmount() {
    cancelAnimationFrame(this.renderRef);
  }
157
  render() {
陈帅's avatar
陈帅 committed
158 159 160 161
    const {
      isMobile,
      silderTheme,
      layout: PropsLayout,
愚道's avatar
愚道 committed
162
      children,
陈帅's avatar
陈帅 committed
163
      location: { pathname },
陈帅's avatar
陈帅 committed
164 165
    } = this.props;
    const isTop = PropsLayout === 'topmenu';
afc163's avatar
afc163 committed
166 167
    const layout = (
      <Layout>
jim's avatar
jim committed
168
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
169 170 171
          <SiderMenu
            logo={logo}
            Authorized={Authorized}
陈帅's avatar
陈帅 committed
172
            theme={silderTheme}
jim's avatar
jim committed
173
            onCollapse={this.handleMenuCollapse}
jim's avatar
jim committed
174
            {...this.props}
jim's avatar
jim committed
175 176
          />
        )}
jim's avatar
jim committed
177
        <Layout style={this.getLayoutStyle()}>
jim's avatar
jim committed
178
          <Header handleMenuCollapse={this.handleMenuCollapse} logo={logo} {...this.props} />
陈帅's avatar
陈帅 committed
179
          <Content style={this.getContentStyle()}>{children}</Content>
jim's avatar
jim committed
180
          <Footer />
181
        </Layout>
afc163's avatar
afc163 committed
182 183 184
      </Layout>
    );
    return (
陈帅's avatar
陈帅 committed
185 186 187 188 189 190 191 192 193 194 195 196
      <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>
        {this.state.rendering ? null : <SettingDarwer />}
      </React.Fragment>
197 198 199 200
    );
  }
}

jim's avatar
jim committed
201
export default connect(({ global, setting }) => ({
Andreas Cederström's avatar
Andreas Cederström committed
202
  collapsed: global.collapsed,
jim's avatar
jim committed
203 204
  layout: setting.layout,
  ...setting,
陈帅's avatar
陈帅 committed
205
}))(BasicLayout);