BasicLayout.js 5.87 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 6
import DocumentTitle from 'react-document-title';
import { connect } from 'dva';
愚道's avatar
愚道 committed
7
// import { Route, Redirect, Switch } from 'dva/router';
afc163's avatar
afc163 committed
8 9
import { ContainerQuery } from 'react-container-query';
import classNames from 'classnames';
jim's avatar
jim committed
10
import pathToRegexp from 'path-to-regexp';
愚道's avatar
愚道 committed
11 12 13 14 15 16
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
17 18
import Footer from './Footer';
import Header from './Header';
19
import Context from './MenuContext';
20

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

24 25 26 27 28
/**
 * 获取面包屑映射
 * @param {Object} menuData 菜单配置
 * @param {Object} routerData 路由配置
 */
陈帅's avatar
陈帅 committed
29 30 31 32 33 34 35 36 37 38 39 40
const getBreadcrumbNameMap = (meun, router) => {
  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;
41 42
};

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

68
class BasicLayout extends React.PureComponent {
jim's avatar
jim committed
69
  getContext() {
ddcat1115's avatar
ddcat1115 committed
70
    const { location, routerData, menuData } = this.props;
陈帅's avatar
陈帅 committed
71
    console.log(getBreadcrumbNameMap(menuData, routerData));
ddcat1115's avatar
ddcat1115 committed
72 73
    return {
      location,
ddcat1115's avatar
ddcat1115 committed
74
      breadcrumbNameMap: getBreadcrumbNameMap(menuData, routerData),
ddcat1115's avatar
ddcat1115 committed
75
    };
76
  }
陈帅's avatar
陈帅 committed
77

78
  getPageTitle() {
ddcat1115's avatar
ddcat1115 committed
79
    const { routerData, location } = this.props;
ddcat1115's avatar
ddcat1115 committed
80 81
    const { pathname } = location;
    let title = 'Ant Design Pro';
jim's avatar
jim committed
82 83 84 85 86 87 88 89 90
    let currRouterData = null;
    // match params path
    Object.keys(routerData).forEach(key => {
      if (pathToRegexp(key).test(pathname)) {
        currRouterData = routerData[key];
      }
    });
    if (currRouterData && currRouterData.name) {
      title = `${currRouterData.name} - Ant Design Pro`;
ddcat1115's avatar
ddcat1115 committed
91
    }
ddcat1115's avatar
ddcat1115 committed
92
    return title;
93
  }
陈帅's avatar
陈帅 committed
94

jim's avatar
jim committed
95
  getLayoutStyle = () => {
陈帅's avatar
陈帅 committed
96 97
    const { fixSiderbar, collapsed, layout } = this.props;
    if (fixSiderbar && layout !== 'topmenu') {
jim's avatar
jim committed
98
      return {
99
        paddingLeft: collapsed ? '80px' : '256px',
jim's avatar
jim committed
100 101 102 103
      };
    }
    return null;
  };
陈帅's avatar
陈帅 committed
104

jim's avatar
jim committed
105 106 107 108 109 110 111
  getContentStyle = () => {
    const { fixedHeader } = this.props;
    return {
      margin: '24px 24px 0',
      paddingTop: fixedHeader ? 64 : 0,
    };
  };
陈帅's avatar
陈帅 committed
112

113 114 115 116
  getBashRedirect = () => {
    // According to the url parameter to redirect
    // 这里是重定向的,重定向到 url 的 redirect 参数所示地址
    const urlParams = new URL(window.location.href);
jim's avatar
jim committed
117 118

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

jim's avatar
jim committed
134
  handleMenuCollapse = collapsed => {
陈帅's avatar
陈帅 committed
135 136
    const { dispatch } = this.props;
    dispatch({
137 138 139
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
140
  };
jim's avatar
jim committed
141

142
  render() {
愚道's avatar
愚道 committed
143
    // TODO remove old router code
陈帅's avatar
陈帅 committed
144 145
    const {
      isMobile,
愚道's avatar
愚道 committed
146 147
      // redirectData,
      // routerData,
陈帅's avatar
陈帅 committed
148 149
      silderTheme,
      layout: PropsLayout,
愚道's avatar
愚道 committed
150 151
      children,
      // match,
陈帅's avatar
陈帅 committed
152 153
    } = this.props;
    const isTop = PropsLayout === 'topmenu';
愚道's avatar
愚道 committed
154 155
    // const bashRedirect = this.getBashRedirect();
    // const myRedirectData = redirectData || [];
afc163's avatar
afc163 committed
156 157
    const layout = (
      <Layout>
jim's avatar
jim committed
158
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
159 160 161
          <SiderMenu
            logo={logo}
            Authorized={Authorized}
陈帅's avatar
陈帅 committed
162
            theme={silderTheme}
jim's avatar
jim committed
163
            onCollapse={this.handleMenuCollapse}
jim's avatar
jim committed
164
            {...this.props}
jim's avatar
jim committed
165 166
          />
        )}
jim's avatar
jim committed
167
        <Layout style={this.getLayoutStyle()}>
jim's avatar
jim committed
168
          <Header handleMenuCollapse={this.handleMenuCollapse} logo={logo} {...this.props} />
jim's avatar
jim committed
169
          <Content style={this.getContentStyle()}>
愚道's avatar
愚道 committed
170 171
            {children}
            {/* <Switch> TODO remove
jim's avatar
jim committed
172 173 174 175 176 177 178 179 180 181 182 183 184
              {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"
                />
              ))}
185 186
              <Redirect exact from="/" to={bashRedirect} />
              <Route render={NotFound} />
愚道's avatar
愚道 committed
187
            </Switch> */}
afc163's avatar
afc163 committed
188
          </Content>
jim's avatar
jim committed
189
          <Footer />
190
        </Layout>
afc163's avatar
afc163 committed
191 192 193 194 195 196
      </Layout>
    );

    return (
      <DocumentTitle title={this.getPageTitle()}>
        <ContainerQuery query={query}>
jim's avatar
jim committed
197
          {params => (
jim's avatar
jim committed
198 199 200
            <Context.Provider value={this.getContext()}>
              <div className={classNames(params)}>
                {layout}
jim's avatar
jim committed
201
                <SettingDarwer />
jim's avatar
jim committed
202 203
              </div>
            </Context.Provider>
jim's avatar
jim committed
204
          )}
afc163's avatar
afc163 committed
205
        </ContainerQuery>
206 207 208 209 210
      </DocumentTitle>
    );
  }
}

jim's avatar
jim committed
211
export default connect(({ global, setting }) => ({
Andreas Cederström's avatar
Andreas Cederström committed
212
  collapsed: global.collapsed,
jim's avatar
jim committed
213 214
  layout: setting.layout,
  ...setting,
215
}))(BasicLayout);