BasicLayout.js 5.27 KB
Newer Older
jim's avatar
jim committed
1 2
import React from 'react';
import { Layout } from 'antd';
3
import DocumentTitle from 'react-document-title';
陈帅's avatar
陈帅 committed
4
import memoizeOne from 'memoize-one';
5
import { connect } from 'dva';
afc163's avatar
afc163 committed
6 7
import { ContainerQuery } from 'react-container-query';
import classNames from 'classnames';
jim's avatar
jim committed
8
import pathToRegexp from 'path-to-regexp';
陈帅's avatar
陈帅 committed
9
import { formatMessage } from 'umi/locale';
愚道's avatar
愚道 committed
10 11 12 13
import SiderMenu from '../../components/SiderMenu';
import Authorized from '../../utils/Authorized';
import SettingDarwer from '../../components/SettingDarwer';
import logo from '../../assets/logo.svg';
jim's avatar
jim committed
14 15
import Footer from './Footer';
import Header from './Header';
16
import Context from './MenuContext';
17

jim's avatar
jim committed
18
const { Content } = Layout;
陈帅's avatar
陈帅 committed
19
const { check } = Authorized;
ddcat1115's avatar
ddcat1115 committed
20

21 22 23 24
/**
 * 获取面包屑映射
 * @param {Object} menuData 菜单配置
 */
25
const getBreadcrumbNameMap = memoizeOne(meun => {
陈帅's avatar
陈帅 committed
26 27 28 29 30 31
  const routerMap = {};
  const mergeMeunAndRouter = meunData => {
    meunData.forEach(meunItem => {
      if (meunItem.children) {
        mergeMeunAndRouter(meunItem.children);
      }
32 33
      // Reduce memory usage
      routerMap[meunItem.path] = meunItem;
陈帅's avatar
陈帅 committed
34 35 36 37
    });
  };
  mergeMeunAndRouter(meun);
  return routerMap;
陈帅's avatar
陈帅 committed
38
});
39

afc163's avatar
afc163 committed
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,
yoyo837's avatar
yoyo837 committed
58 59 60 61
    maxWidth: 1599,
  },
  'screen-xxl': {
    minWidth: 1600,
afc163's avatar
afc163 committed
62 63 64
  },
};

65
class BasicLayout extends React.PureComponent {
陈帅's avatar
陈帅 committed
66 67 68
  state = {
    rendering: true,
  };
陈帅's avatar
陈帅 committed
69 70
  constructor(props) {
    super(props);
71
    const { menuData } = this.props;
陈帅's avatar
陈帅 committed
72
    this.getPageTitle = memoizeOne(this.getPageTitle);
73
    this.breadcrumbNameMap = getBreadcrumbNameMap(menuData);
陈帅's avatar
陈帅 committed
74
  }
jim's avatar
jim committed
75
  getContext() {
陈帅's avatar
陈帅 committed
76
    const { location } = this.props;
ddcat1115's avatar
ddcat1115 committed
77 78
    return {
      location,
陈帅's avatar
陈帅 committed
79
      breadcrumbNameMap: this.breadcrumbNameMap,
ddcat1115's avatar
ddcat1115 committed
80
    };
81
  }
陈帅's avatar
陈帅 committed
82
  getPageTitle = pathname => {
jim's avatar
jim committed
83 84
    let currRouterData = null;
    // match params path
陈帅's avatar
陈帅 committed
85
    Object.keys(this.breadcrumbNameMap).forEach(key => {
jim's avatar
jim committed
86
      if (pathToRegexp(key).test(pathname)) {
陈帅's avatar
陈帅 committed
87
        currRouterData = this.breadcrumbNameMap[key];
jim's avatar
jim committed
88 89
      }
    });
陈帅's avatar
陈帅 committed
90 91
    if (!currRouterData) {
      return 'Ant Design Pro';
ddcat1115's avatar
ddcat1115 committed
92
    }
陈帅's avatar
陈帅 committed
93
    const message = formatMessage({
陈帅's avatar
陈帅 committed
94 95 96 97 98
      id: currRouterData.locale || currRouterData.name,
      defaultMessage: currRouterData.name,
    });
    return `${message} - Ant Design Pro`;
  };
陈帅's avatar
陈帅 committed
99

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

jim's avatar
jim committed
110 111 112 113 114 115 116
  getContentStyle = () => {
    const { fixedHeader } = this.props;
    return {
      margin: '24px 24px 0',
      paddingTop: fixedHeader ? 64 : 0,
    };
  };
陈帅's avatar
陈帅 committed
117

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

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

jim's avatar
jim committed
139
  handleMenuCollapse = collapsed => {
陈帅's avatar
陈帅 committed
140 141
    const { dispatch } = this.props;
    dispatch({
142 143 144
      type: 'global/changeLayoutCollapsed',
      payload: collapsed,
    });
jim's avatar
jim committed
145
  };
陈帅's avatar
陈帅 committed
146 147 148 149 150 151 152 153 154 155
  componentDidMount() {
    this.renderRef = requestAnimationFrame(() => {
      this.setState({
        rendering: false,
      });
    });
  }
  componentWillUnmount() {
    cancelAnimationFrame(this.renderRef);
  }
156
  render() {
陈帅's avatar
陈帅 committed
157 158 159 160
    const {
      isMobile,
      silderTheme,
      layout: PropsLayout,
愚道's avatar
愚道 committed
161
      children,
陈帅's avatar
陈帅 committed
162
      location: { pathname },
陈帅's avatar
陈帅 committed
163 164
    } = this.props;
    const isTop = PropsLayout === 'topmenu';
afc163's avatar
afc163 committed
165 166
    const layout = (
      <Layout>
jim's avatar
jim committed
167
        {isTop && !isMobile ? null : (
jim's avatar
jim committed
168 169 170
          <SiderMenu
            logo={logo}
            Authorized={Authorized}
陈帅's avatar
陈帅 committed
171
            theme={silderTheme}
jim's avatar
jim committed
172
            onCollapse={this.handleMenuCollapse}
jim's avatar
jim committed
173
            {...this.props}
jim's avatar
jim committed
174 175
          />
        )}
jim's avatar
jim committed
176
        <Layout style={this.getLayoutStyle()}>
jim's avatar
jim committed
177
          <Header handleMenuCollapse={this.handleMenuCollapse} logo={logo} {...this.props} />
陈帅's avatar
陈帅 committed
178
          <Content style={this.getContentStyle()}>{children}</Content>
jim's avatar
jim committed
179
          <Footer />
180
        </Layout>
afc163's avatar
afc163 committed
181 182 183
      </Layout>
    );
    return (
陈帅's avatar
陈帅 committed
184 185 186 187 188 189 190 191 192 193 194 195
      <React.Fragment>
        <DocumentTitle title={this.getPageTitle(pathname)}>
          <ContainerQuery query={query}>
            {params => (
              <Context.Provider value={this.getContext()}>
                <div className={classNames(params)}>{layout}</div>
              </Context.Provider>
            )}
          </ContainerQuery>
        </DocumentTitle>
        {this.state.rendering ? null : <SettingDarwer />}
      </React.Fragment>
196 197 198 199
    );
  }
}

jim's avatar
jim committed
200
export default connect(({ global, setting }) => ({
Andreas Cederström's avatar
Andreas Cederström committed
201
  collapsed: global.collapsed,
jim's avatar
jim committed
202 203
  layout: setting.layout,
  ...setting,
陈帅's avatar
陈帅 committed
204
}))(BasicLayout);