BasicLayout.js 6.18 KB
Newer Older
愚道's avatar
愚道 committed
1 2
/* eslint-disable no-unused-vars */
// TODO remove eslint-disable
jim's avatar
jim committed
3 4
import React from 'react';
import { Layout } from 'antd';
5
import DocumentTitle from 'react-document-title';
陈帅's avatar
陈帅 committed
6 7
import { injectIntl } from 'react-intl';
import memoizeOne from 'memoize-one';
8
import { connect } from 'dva';
愚道's avatar
愚道 committed
9
// import { Route, Redirect, Switch } from 'dva/router';
afc163's avatar
afc163 committed
10 11
import { ContainerQuery } from 'react-container-query';
import classNames from 'classnames';
jim's avatar
jim committed
12
import pathToRegexp from 'path-to-regexp';
愚道's avatar
愚道 committed
13 14 15 16 17 18
import SiderMenu from '../../components/SiderMenu';
// import NotFound from '../Exception/404';
// import { getRoutes } from '../utils/utils';
import Authorized from '../../utils/Authorized';
import SettingDarwer from '../../components/SettingDarwer';
import logo from '../../assets/logo.svg';
jim's avatar
jim committed
19 20
import Footer from './Footer';
import Header from './Header';
21
import Context from './MenuContext';
22

jim's avatar
jim committed
23
const { Content } = Layout;
ddcat1115's avatar
ddcat1115 committed
24
const { AuthorizedRoute, check } = Authorized;
ddcat1115's avatar
ddcat1115 committed
25

26 27 28 29 30
/**
 * 获取面包屑映射
 * @param {Object} menuData 菜单配置
 * @param {Object} routerData 路由配置
 */
陈帅's avatar
陈帅 committed
31
const getBreadcrumbNameMap = memoizeOne((meun, router) => {
陈帅's avatar
陈帅 committed
32 33 34 35 36 37 38 39 40 41 42
  const routerMap = {};
  const mergeMeunAndRouter = meunData => {
    meunData.forEach(meunItem => {
      routerMap[meunItem.path] = Object.assign(meunItem, router);
      if (meunItem.children) {
        mergeMeunAndRouter(meunItem.children);
      }
    });
  };
  mergeMeunAndRouter(meun);
  return routerMap;
陈帅's avatar
陈帅 committed
43
});
44

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

70
class BasicLayout extends React.PureComponent {
陈帅's avatar
陈帅 committed
71 72 73 74 75 76
  constructor(props) {
    super(props);
    const { routerData, menuData } = this.props;
    this.breadcrumbNameMap = getBreadcrumbNameMap(menuData, routerData);
  }

jim's avatar
jim committed
77
  getContext() {
陈帅's avatar
陈帅 committed
78
    const { location } = this.props;
ddcat1115's avatar
ddcat1115 committed
79 80
    return {
      location,
陈帅's avatar
陈帅 committed
81
      breadcrumbNameMap: this.breadcrumbNameMap,
ddcat1115's avatar
ddcat1115 committed
82
    };
83
  }
陈帅's avatar
陈帅 committed
84

陈帅's avatar
陈帅 committed
85
  getPageTitle = pathname => {
jim's avatar
jim committed
86
    let currRouterData = null;
陈帅's avatar
陈帅 committed
87
    const { intl } = this.props;
jim's avatar
jim committed
88
    // match params path
陈帅's avatar
陈帅 committed
89
    Object.keys(this.breadcrumbNameMap).forEach(key => {
jim's avatar
jim committed
90
      if (pathToRegexp(key).test(pathname)) {
陈帅's avatar
陈帅 committed
91
        currRouterData = this.breadcrumbNameMap[key];
jim's avatar
jim committed
92 93
      }
    });
陈帅's avatar
陈帅 committed
94 95
    if (!currRouterData) {
      return 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
96
    }
陈帅's avatar
陈帅 committed
97 98 99 100 101 102
    const message = intl.formatMessage({
      id: currRouterData.locale || currRouterData.name,
      defaultMessage: currRouterData.name,
    });
    return `${message} - Ant Design Pro`;
  };
陈帅's avatar
陈帅 committed
103

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

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

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

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

jim's avatar
jim committed
143
  handleMenuCollapse = collapsed => {
陈帅's avatar
陈帅 committed
144 145
    const { dispatch } = this.props;
    dispatch({
146 147 148
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
149
  };
jim's avatar
jim committed
150

151
  render() {
愚道's avatar
愚道 committed
152
    // TODO remove old router code
陈帅's avatar
陈帅 committed
153 154
    const {
      isMobile,
愚道's avatar
愚道 committed
155 156
      // redirectData,
      // routerData,
陈帅's avatar
陈帅 committed
157 158
      silderTheme,
      layout: PropsLayout,
愚道's avatar
愚道 committed
159
      children,
陈帅's avatar
陈帅 committed
160
      location: { pathname },
陈帅's avatar
陈帅 committed
161 162
    } = this.props;
    const isTop = PropsLayout === 'topmenu';
愚道's avatar
愚道 committed
163 164
    // const bashRedirect = this.getBashRedirect();
    // const myRedirectData = redirectData || [];
afc163's avatar
afc163 committed
165 166
    const layout = (
      <Layout>
jim's avatar
jim committed
167
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
168 169 170
          <SiderMenu
            logo={logo}
            Authorized={Authorized}
陈帅's avatar
陈帅 committed
171
            theme={silderTheme}
jim's avatar
jim committed
172
            onCollapse={this.handleMenuCollapse}
jim's avatar
jim committed
173
            {...this.props}
jim's avatar
jim committed
174 175
          />
        )}
jim's avatar
jim committed
176
        <Layout style={this.getLayoutStyle()}>
jim's avatar
jim committed
177
          <Header handleMenuCollapse={this.handleMenuCollapse} logo={logo} {...this.props} />
jim's avatar
jim committed
178
          <Content style={this.getContentStyle()}>
愚道's avatar
愚道 committed
179 180
            {children}
            {/* <Switch> TODO remove
jim's avatar
jim committed
181 182 183 184 185 186 187 188 189 190 191 192 193
              {myRedirectData.map(item => (
                <Redirect key={item.from} exact from={item.from} to={item.to} />
              ))}
              {getRoutes(match.path, routerData).map(item => (
                <AuthorizedRoute
                  key={item.key}
                  path={item.path}
                  component={item.component}
                  exact={item.exact}
                  authority={item.authority}
                  redirectPath="/exception/403"
                />
              ))}
194 195
              <Redirect exact from="/" to={bashRedirect} />
              <Route render={NotFound} />
愚道's avatar
愚道 committed
196
            </Switch> */}
afc163's avatar
afc163 committed
197
          </Content>
jim's avatar
jim committed
198
          <Footer />
199
        </Layout>
afc163's avatar
afc163 committed
200 201
      </Layout>
    );
陈帅's avatar
陈帅 committed
202
    const getPageTitle = memoizeOne(this.getPageTitle);
afc163's avatar
afc163 committed
203
    return (
陈帅's avatar
陈帅 committed
204
      <DocumentTitle title={getPageTitle(pathname)}>
afc163's avatar
afc163 committed
205
        <ContainerQuery query={query}>
jim's avatar
jim committed
206
          {params => (
jim's avatar
jim committed
207 208 209
            <Context.Provider value={this.getContext()}>
              <div className={classNames(params)}>
                {layout}
jim's avatar
jim committed
210
                <SettingDarwer />
jim's avatar
jim committed
211 212
              </div>
            </Context.Provider>
jim's avatar
jim committed
213
          )}
afc163's avatar
afc163 committed
214
        </ContainerQuery>
215 216 217 218 219
      </DocumentTitle>
    );
  }
}

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