BasicLayout.js 4.58 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';
13

偏右's avatar
偏右 committed
14
const { Content } = Layout;
15

afc163's avatar
afc163 committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
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,
  },
};

37 38
class BasicLayout extends React.PureComponent {
  static childContextTypes = {
ddcat1115's avatar
ddcat1115 committed
39 40
    location: PropTypes.object,
    breadcrumbNameMap: PropTypes.object,
偏右's avatar
偏右 committed
41
    routeData: PropTypes.array,
42 43
  }
  getChildContext() {
44
    const { location, navData, getRouteData } = this.props;
ddcat1115's avatar
ddcat1115 committed
45
    const routeData = getRouteData('BasicLayout');
46
    const firstMenuData = navData.reduce((arr, current) => arr.concat(current.children), []);
ddcat1115's avatar
ddcat1115 committed
47
    const menuData = this.getMenuData(firstMenuData, '');
ddcat1115's avatar
ddcat1115 committed
48
    const breadcrumbNameMap = {};
ddcat1115's avatar
ddcat1115 committed
49

ddcat1115's avatar
ddcat1115 committed
50
    routeData.concat(menuData).forEach((item) => {
afc163's avatar
afc163 committed
51 52 53 54
      breadcrumbNameMap[item.path] = {
        name: item.name,
        component: item.component,
      };
ddcat1115's avatar
ddcat1115 committed
55
    });
偏右's avatar
偏右 committed
56
    return { location, breadcrumbNameMap, routeData };
57 58
  }
  getPageTitle() {
59
    const { location, getRouteData } = this.props;
ddcat1115's avatar
ddcat1115 committed
60 61
    const { pathname } = location;
    let title = 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
62
    getRouteData('BasicLayout').forEach((item) => {
ddcat1115's avatar
ddcat1115 committed
63 64
      if (item.path === pathname) {
        title = `${item.name} - Ant Design Pro`;
65
      }
ddcat1115's avatar
ddcat1115 committed
66 67
    });
    return title;
68
  }
偏右's avatar
偏右 committed
69 70 71
  getMenuData = (data, parentPath) => {
    let arr = [];
    data.forEach((item) => {
偏右's avatar
偏右 committed
72
      if (item.name) {
偏右's avatar
偏右 committed
73
        arr.push({ path: `${parentPath}/${item.path}`, name: item.name });
偏右's avatar
偏右 committed
74 75
      }
      if (item.children) {
偏右's avatar
偏右 committed
76
        arr = arr.concat(this.getMenuData(item.children, `${parentPath}/${item.path}`));
77 78
      }
    });
偏右's avatar
偏右 committed
79
    return arr;
80 81
  }
  render() {
偏右's avatar
偏右 committed
82 83 84
    const {
      currentUser, collapsed, fetchingNotices, notices, getRouteData, navData, location, dispatch,
    } = this.props;
afc163's avatar
afc163 committed
85

afc163's avatar
afc163 committed
86 87
    const layout = (
      <Layout>
偏右's avatar
偏右 committed
88
        <SiderMenu
afc163's avatar
afc163 committed
89
          collapsed={collapsed}
偏右's avatar
偏右 committed
90 91 92 93
          navData={navData}
          location={location}
          dispatch={dispatch}
        />
afc163's avatar
afc163 committed
94
        <Layout>
偏右's avatar
偏右 committed
95 96 97 98 99 100 101
          <GlobalHeader
            currentUser={currentUser}
            fetchingNotices={fetchingNotices}
            notices={notices}
            collapsed={collapsed}
            dispatch={dispatch}
          />
afc163's avatar
afc163 committed
102
          <Content style={{ margin: '24px 24px 0', height: '100%' }}>
afc163's avatar
afc163 committed
103 104 105 106 107 108 109 110 111 112 113 114
            <div style={{ minHeight: 'calc(100vh - 260px)' }}>
              <Switch>
                {
                  getRouteData('BasicLayout').map(item =>
                    (
                      <Route
                        exact={item.exact}
                        key={item.path}
                        path={item.path}
                        component={item.component}
                      />
                    )
ddcat1115's avatar
ddcat1115 committed
115
                  )
afc163's avatar
afc163 committed
116 117 118 119 120
                }
                <Redirect exact from="/" to="/dashboard/analysis" />
                <Route component={NotFound} />
              </Switch>
            </div>
afc163's avatar
afc163 committed
121 122
            <GlobalFooter
              links={[{
afc163's avatar
afc163 committed
123
                title: 'Pro 首页',
afc163's avatar
afc163 committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
                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>
142
        </Layout>
afc163's avatar
afc163 committed
143 144 145 146 147 148 149 150
      </Layout>
    );

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

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