BasicLayout.js 4.41 KB
Newer Older
1 2
import React from 'react';
import PropTypes from 'prop-types';
偏右's avatar
偏右 committed
3
import { Layout, Icon } from 'antd';
4 5
import DocumentTitle from 'react-document-title';
import { connect } from 'dva';
偏右's avatar
偏右 committed
6
import { Route, Redirect, Switch } from 'dva/router';
afc163's avatar
afc163 committed
7 8
import { ContainerQuery } from 'react-container-query';
import classNames from 'classnames';
偏右's avatar
偏右 committed
9
import GlobalHeader from '../components/GlobalHeader';
10
import GlobalFooter from '../components/GlobalFooter';
偏右's avatar
偏右 committed
11
import SiderMenu from '../components/SiderMenu';
afc163's avatar
afc163 committed
12
import NotFound from '../routes/Exception/404';
ddcat1115's avatar
ddcat1115 committed
13
import { getRoutes } from '../utils/utils';
陈帅's avatar
陈帅 committed
14
import { getMenuData } from '../common/menu';
15 16


陈帅's avatar
陈帅 committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/**
 * 根据菜单取得重定向地址.
 */
const redirectData = [];
const getRedirect = (item) => {
  if (item && item.children) {
    if (item.children[0] && item.children[0].path) {
      redirectData.push({
        from: `/${item.path}`,
        to: `/${item.children[0].path}`,
      });
      item.children.forEach((children) => {
        getRedirect(children);
      });
    }
  }
};
getMenuData().forEach(getRedirect);

const { Content } = Layout;
afc163's avatar
afc163 committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
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,
  },
};

58 59
class BasicLayout extends React.PureComponent {
  static childContextTypes = {
ddcat1115's avatar
ddcat1115 committed
60 61
    location: PropTypes.object,
    breadcrumbNameMap: PropTypes.object,
62 63
  }
  getChildContext() {
ddcat1115's avatar
ddcat1115 committed
64 65 66 67 68
    const { location, routerData } = this.props;
    return {
      location,
      breadcrumbNameMap: routerData,
    };
69 70
  }
  getPageTitle() {
ddcat1115's avatar
ddcat1115 committed
71
    const { routerData, location } = this.props;
ddcat1115's avatar
ddcat1115 committed
72 73
    const { pathname } = location;
    let title = 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
74 75 76
    if (routerData[pathname] && routerData[pathname].name) {
      title = `${routerData[pathname].name} - Ant Design Pro`;
    }
ddcat1115's avatar
ddcat1115 committed
77
    return title;
78 79
  }
  render() {
偏右's avatar
偏右 committed
80
    const {
ddcat1115's avatar
ddcat1115 committed
81
      currentUser, collapsed, fetchingNotices, notices, routerData, match, location, dispatch,
偏右's avatar
偏右 committed
82
    } = this.props;
afc163's avatar
afc163 committed
83 84
    const layout = (
      <Layout>
偏右's avatar
偏右 committed
85
        <SiderMenu
afc163's avatar
afc163 committed
86
          collapsed={collapsed}
偏右's avatar
偏右 committed
87 88 89
          location={location}
          dispatch={dispatch}
        />
afc163's avatar
afc163 committed
90
        <Layout>
偏右's avatar
偏右 committed
91 92 93 94 95 96 97
          <GlobalHeader
            currentUser={currentUser}
            fetchingNotices={fetchingNotices}
            notices={notices}
            collapsed={collapsed}
            dispatch={dispatch}
          />
afc163's avatar
afc163 committed
98
          <Content style={{ margin: '24px 24px 0', height: '100%' }}>
afc163's avatar
afc163 committed
99 100 101
            <div style={{ minHeight: 'calc(100vh - 260px)' }}>
              <Switch>
                {
陈帅's avatar
陈帅 committed
102 103
                  redirectData.map(item =>
                    <Redirect key={item.from} exact from={item.from} to={item.to} />
ddcat1115's avatar
ddcat1115 committed
104
                  )
afc163's avatar
afc163 committed
105
                }
陈帅's avatar
陈帅 committed
106 107 108 109 110 111 112 113 114 115
                {
                  getRoutes(match.path, routerData).map(item => (
                    <Route
                      key={item.key}
                      path={item.path}
                      component={item.component}
                      exact={item.exact}
                    />
                  ))
                }
afc163's avatar
afc163 committed
116
                <Redirect exact from="/" to="/dashboard/analysis" />
ddcat1115's avatar
ddcat1115 committed
117
                <Route render={NotFound} />
afc163's avatar
afc163 committed
118 119
              </Switch>
            </div>
afc163's avatar
afc163 committed
120 121
            <GlobalFooter
              links={[{
afc163's avatar
afc163 committed
122
                title: 'Pro 首页',
afc163's avatar
afc163 committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
                href: 'http://pro.ant.design',
                blankTarget: true,
              }, {
                title: 'GitHub',
                href: 'https://github.com/ant-design/ant-design-pro',
                blankTarget: true,
              }, {
                title: 'Ant Design',
                href: 'http://ant.design',
                blankTarget: true,
              }]}
              copyright={
                <div>
                  Copyright <Icon type="copyright" /> 2017 蚂蚁金服体验技术部出品
                </div>
              }
            />
          </Content>
141
        </Layout>
afc163's avatar
afc163 committed
142 143 144 145 146 147 148 149
      </Layout>
    );

    return (
      <DocumentTitle title={this.getPageTitle()}>
        <ContainerQuery query={query}>
          {params => <div className={classNames(params)}>{layout}</div>}
        </ContainerQuery>
150 151 152 153 154 155 156 157 158 159 160
      </DocumentTitle>
    );
  }
}

export default connect(state => ({
  currentUser: state.user.currentUser,
  collapsed: state.global.collapsed,
  fetchingNotices: state.global.fetchingNotices,
  notices: state.global.notices,
}))(BasicLayout);