ant-design/site/theme/template/Layout/index.jsx

94 lines
2.1 KiB
React
Raw Normal View History

2017-11-30 21:38:48 +08:00
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
2018-01-04 20:00:38 +08:00
import { enquireScreen } from 'enquire-js';
import { addLocaleData, IntlProvider } from 'react-intl';
import Header from './Header';
import Footer from './Footer';
import enLocale from '../../en-US';
import cnLocale from '../../zh-CN';
import * as utils from '../utils';
if (typeof window !== 'undefined') {
/* eslint-disable global-require */
require('../../static/style');
// Expose to iframe
window.react = React;
window['react-dom'] = ReactDOM;
window.antd = require('antd');
/* eslint-enable global-require */
}
2018-01-04 20:00:38 +08:00
let isMobile = false;
enquireScreen((b) => {
isMobile = b;
});
2016-06-03 15:26:25 +08:00
export default class Layout extends React.Component {
static contextTypes = {
router: PropTypes.object.isRequired,
2016-06-03 15:26:25 +08:00
}
2018-01-04 20:00:38 +08:00
static childContextTypes = {
2018-01-05 17:23:59 +08:00
isMobile: PropTypes.bool,
2018-01-04 20:00:38 +08:00
};
getChildContext() {
return {
2018-01-05 17:23:59 +08:00
isMobile: this.state.isMobile,
2018-01-04 20:00:38 +08:00
};
}
2016-06-03 15:26:25 +08:00
constructor(props) {
super(props);
2017-10-09 13:23:20 +08:00
const { pathname } = props.location;
const appLocale = utils.isZhCN(pathname) ? cnLocale : enLocale;
addLocaleData(appLocale.data);
2018-04-22 18:58:40 +08:00
this.state = {
appLocale,
2018-01-04 20:00:38 +08:00
isMobile,
};
}
2016-06-03 15:26:25 +08:00
componentDidMount() {
2017-01-07 23:30:33 +08:00
if (typeof window.ga !== 'undefined') {
2016-06-03 15:26:25 +08:00
this.context.router.listen((loc) => {
window.ga('send', 'pageview', loc.pathname + loc.search);
});
}
2016-07-26 17:40:08 +08:00
2016-12-09 14:24:38 +08:00
const nprogressHiddenStyle = document.getElementById('nprogress-style');
if (nprogressHiddenStyle) {
2016-07-26 17:40:08 +08:00
this.timer = setTimeout(() => {
2016-12-09 14:24:38 +08:00
nprogressHiddenStyle.parentNode.removeChild(nprogressHiddenStyle);
}, 0);
2016-06-23 21:10:02 +08:00
}
2018-01-04 20:00:38 +08:00
enquireScreen((b) => {
this.setState({
isMobile: !!b,
});
});
2016-06-23 21:10:02 +08:00
}
componentWillUnmount() {
clearTimeout(this.timer);
2016-06-03 15:26:25 +08:00
}
render() {
2016-07-26 17:40:08 +08:00
const { children, ...restProps } = this.props;
2017-11-30 21:38:48 +08:00
const { appLocale } = this.state;
2018-04-22 18:58:40 +08:00
2016-06-03 15:26:25 +08:00
return (
<IntlProvider locale={appLocale.locale} messages={appLocale.messages}>
<div className="page-wrapper">
2017-11-30 21:38:48 +08:00
<Header {...restProps} />
{children}
<Footer {...restProps} />
</div>
2016-06-03 15:26:25 +08:00
</IntlProvider>
);
}
}