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 72

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

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

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