BasicLayout.js 4.74 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';
jiang's avatar
jiang committed
9
import { enquireScreen } from 'enquire-js';
偏右's avatar
偏右 committed
10
import GlobalHeader from '../components/GlobalHeader';
11
import GlobalFooter from '../components/GlobalFooter';
偏右's avatar
偏右 committed
12
import SiderMenu from '../components/SiderMenu';
afc163's avatar
afc163 committed
13
import NotFound from '../routes/Exception/404';
ddcat1115's avatar
ddcat1115 committed
14
import { getRoutes } from '../utils/utils';
陈帅's avatar
陈帅 committed
15
import { getMenuData } from '../common/menu';
16 17


陈帅's avatar
陈帅 committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/**
 * 根据菜单取得重定向地址.
 */
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
38 39 40 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,
  },
};

jiang's avatar
jiang committed
59 60 61 62 63
let isMobile;
enquireScreen((b) => {
  isMobile = b;
});

64 65
class BasicLayout extends React.PureComponent {
  static childContextTypes = {
ddcat1115's avatar
ddcat1115 committed
66 67
    location: PropTypes.object,
    breadcrumbNameMap: PropTypes.object,
68
  }
jiang's avatar
jiang committed
69 70 71
  state = {
    isMobile,
  };
72
  getChildContext() {
ddcat1115's avatar
ddcat1115 committed
73 74 75 76 77
    const { location, routerData } = this.props;
    return {
      location,
      breadcrumbNameMap: routerData,
    };
78
  }
jiang's avatar
jiang committed
79
  componentDidMount() {
afc163's avatar
afc163 committed
80
    enquireScreen((mobile) => {
jiang's avatar
jiang committed
81
      this.setState({
afc163's avatar
afc163 committed
82
        isMobile: mobile,
jiang's avatar
jiang committed
83 84 85
      });
    });
  }
86
  getPageTitle() {
ddcat1115's avatar
ddcat1115 committed
87
    const { routerData, location } = this.props;
ddcat1115's avatar
ddcat1115 committed
88 89
    const { pathname } = location;
    let title = 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
90 91 92
    if (routerData[pathname] && routerData[pathname].name) {
      title = `${routerData[pathname].name} - Ant Design Pro`;
    }
ddcat1115's avatar
ddcat1115 committed
93
    return title;
94 95
  }
  render() {
偏右's avatar
偏右 committed
96
    const {
ddcat1115's avatar
ddcat1115 committed
97
      currentUser, collapsed, fetchingNotices, notices, routerData, match, location, dispatch,
偏右's avatar
偏右 committed
98
    } = this.props;
afc163's avatar
afc163 committed
99 100
    const layout = (
      <Layout>
偏右's avatar
偏右 committed
101
        <SiderMenu
afc163's avatar
afc163 committed
102
          collapsed={collapsed}
偏右's avatar
偏右 committed
103 104
          location={location}
          dispatch={dispatch}
jiang's avatar
jiang committed
105
          isMobile={this.state.isMobile}
偏右's avatar
偏右 committed
106
        />
afc163's avatar
afc163 committed
107
        <Layout>
偏右's avatar
偏右 committed
108 109 110 111 112 113
          <GlobalHeader
            currentUser={currentUser}
            fetchingNotices={fetchingNotices}
            notices={notices}
            collapsed={collapsed}
            dispatch={dispatch}
jiang's avatar
jiang committed
114
            isMobile={this.state.isMobile}
偏右's avatar
偏右 committed
115
          />
afc163's avatar
afc163 committed
116
          <Content style={{ margin: '24px 24px 0', height: '100%' }}>
afc163's avatar
afc163 committed
117 118 119
            <div style={{ minHeight: 'calc(100vh - 260px)' }}>
              <Switch>
                {
陈帅's avatar
陈帅 committed
120 121
                  redirectData.map(item =>
                    <Redirect key={item.from} exact from={item.from} to={item.to} />
ddcat1115's avatar
ddcat1115 committed
122
                  )
afc163's avatar
afc163 committed
123
                }
陈帅's avatar
陈帅 committed
124 125 126 127 128 129 130 131 132 133
                {
                  getRoutes(match.path, routerData).map(item => (
                    <Route
                      key={item.key}
                      path={item.path}
                      component={item.component}
                      exact={item.exact}
                    />
                  ))
                }
afc163's avatar
afc163 committed
134
                <Redirect exact from="/" to="/dashboard/analysis" />
ddcat1115's avatar
ddcat1115 committed
135
                <Route render={NotFound} />
afc163's avatar
afc163 committed
136 137
              </Switch>
            </div>
afc163's avatar
afc163 committed
138 139
            <GlobalFooter
              links={[{
afc163's avatar
afc163 committed
140
                title: 'Pro 首页',
afc163's avatar
afc163 committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
                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>
159
        </Layout>
afc163's avatar
afc163 committed
160 161 162 163 164 165 166 167
      </Layout>
    );

    return (
      <DocumentTitle title={this.getPageTitle()}>
        <ContainerQuery query={query}>
          {params => <div className={classNames(params)}>{layout}</div>}
        </ContainerQuery>
168 169 170 171 172 173 174 175 176 177 178
      </DocumentTitle>
    );
  }
}

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