2017-05-09 13:40:33 +08:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-01-17 15:24:13 +08:00
|
|
|
import { Link } from 'bisheng/router';
|
2016-09-08 16:53:50 +08:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2016-04-28 15:42:02 +08:00
|
|
|
import classNames from 'classnames';
|
2017-12-29 12:02:22 +08:00
|
|
|
import { Select, Menu, Row, Col, Icon, Popover, Input, Badge, Button } from 'antd';
|
2016-12-09 13:02:16 +08:00
|
|
|
import * as utils from '../utils';
|
2017-02-09 19:46:23 +08:00
|
|
|
import { version as antdVersion } from '../../../../package.json';
|
2016-08-23 21:00:35 +08:00
|
|
|
|
2017-12-29 12:02:22 +08:00
|
|
|
const { Option } = Select;
|
|
|
|
|
|
|
|
let docsearch;
|
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
docsearch = require('docsearch.js'); // eslint-disable-line
|
|
|
|
}
|
|
|
|
|
|
|
|
function initDocSearch(locale) {
|
|
|
|
if (!docsearch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const lang = locale === 'zh-CN' ? 'cn' : 'en';
|
|
|
|
docsearch({
|
|
|
|
apiKey: '60ac2c1a7d26ab713757e4a081e133d0',
|
|
|
|
indexName: 'ant_design',
|
|
|
|
inputSelector: '#search-box input',
|
|
|
|
algoliaOptions: { facetFilters: [`tags:${lang}`] },
|
|
|
|
transformData(hits) {
|
|
|
|
if (lang === 'cn') {
|
|
|
|
const categories = {
|
|
|
|
Components: '组件',
|
|
|
|
Documentation: '文档',
|
|
|
|
};
|
|
|
|
return hits.map((hit) => {
|
|
|
|
const category = categories[hit.hierarchy.lvl0];
|
|
|
|
hit.hierarchy.lvl0 = category;
|
|
|
|
hit._highlightResult.hierarchy.lvl0.value = category; // eslint-disable-line
|
|
|
|
hit._highlightResult.hierarchy_camel[0].lvl0.value = category; // eslint-disable-line
|
|
|
|
return hit;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return hits;
|
|
|
|
},
|
|
|
|
debug: false, // Set debug to true if you want to inspect the dropdown
|
|
|
|
});
|
|
|
|
}
|
2016-02-29 14:08:40 +08:00
|
|
|
|
2016-03-01 16:20:32 +08:00
|
|
|
export default class Header extends React.Component {
|
2016-03-30 17:41:17 +08:00
|
|
|
static contextTypes = {
|
2016-11-21 14:39:15 +08:00
|
|
|
router: PropTypes.object.isRequired,
|
|
|
|
intl: PropTypes.object.isRequired,
|
2018-01-05 17:23:59 +08:00
|
|
|
isMobile: PropTypes.bool.isRequired,
|
2016-03-30 17:41:17 +08:00
|
|
|
}
|
|
|
|
|
2016-11-21 14:39:15 +08:00
|
|
|
state = {
|
2017-01-08 19:16:13 +08:00
|
|
|
menuVisible: false,
|
2016-11-21 14:39:15 +08:00
|
|
|
};
|
2016-05-09 11:34:45 +08:00
|
|
|
|
|
|
|
componentDidMount() {
|
2017-12-29 12:02:22 +08:00
|
|
|
const { intl, router } = this.context;
|
|
|
|
router.listen(this.handleHideMenu);
|
2017-10-09 13:23:20 +08:00
|
|
|
const { searchInput } = this;
|
2016-11-21 14:39:15 +08:00
|
|
|
/* eslint-disable global-require */
|
2017-08-31 11:47:53 +08:00
|
|
|
document.addEventListener('keyup', (event) => {
|
|
|
|
if (event.keyCode === 83 && event.target === document.body) {
|
|
|
|
searchInput.focus();
|
|
|
|
}
|
|
|
|
});
|
2017-12-29 12:02:22 +08:00
|
|
|
initDocSearch(intl.locale);
|
2016-11-21 14:39:15 +08:00
|
|
|
/* eslint-enable global-require */
|
2016-03-24 17:13:19 +08:00
|
|
|
}
|
|
|
|
|
2017-01-08 19:16:13 +08:00
|
|
|
handleShowMenu = () => {
|
|
|
|
this.setState({
|
|
|
|
menuVisible: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleHideMenu = () => {
|
|
|
|
this.setState({
|
|
|
|
menuVisible: false,
|
|
|
|
});
|
2016-02-29 14:08:40 +08:00
|
|
|
}
|
|
|
|
|
2017-02-21 13:36:17 +08:00
|
|
|
onMenuVisibleChange = (visible) => {
|
|
|
|
this.setState({
|
|
|
|
menuVisible: visible,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-09 19:46:23 +08:00
|
|
|
handleVersionChange = (url) => {
|
|
|
|
const currentUrl = window.location.href;
|
|
|
|
const currentPathname = window.location.pathname;
|
|
|
|
window.location.href = currentUrl.replace(window.location.origin, url)
|
|
|
|
.replace(currentPathname, utils.getLocalizedPathname(currentPathname));
|
|
|
|
}
|
|
|
|
|
2017-12-01 20:37:14 +08:00
|
|
|
handleLangChange = () => {
|
|
|
|
const { pathname } = this.props.location;
|
|
|
|
const currentProtocol = `${window.location.protocol}//`;
|
|
|
|
const currentHref = window.location.href.substr(currentProtocol.length);
|
|
|
|
|
|
|
|
if (utils.isLocalStorageNameSupported()) {
|
|
|
|
localStorage.setItem('locale', utils.isZhCN(pathname) ? 'en-US' : 'zh-CN');
|
|
|
|
}
|
|
|
|
|
|
|
|
window.location.href = currentProtocol + currentHref.replace(
|
|
|
|
window.location.pathname,
|
|
|
|
utils.getLocalizedPathname(pathname, !utils.isZhCN(pathname)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-01 16:20:32 +08:00
|
|
|
render() {
|
2018-01-04 20:00:38 +08:00
|
|
|
const { menuVisible } = this.state;
|
2018-01-05 17:23:59 +08:00
|
|
|
const { isMobile } = this.context;
|
|
|
|
const menuMode = isMobile ? 'inline' : 'horizontal';
|
2017-10-09 13:23:20 +08:00
|
|
|
const {
|
2017-12-29 12:02:22 +08:00
|
|
|
location, themeConfig,
|
2017-10-09 13:23:20 +08:00
|
|
|
} = this.props;
|
2017-02-09 19:46:23 +08:00
|
|
|
const docVersions = { ...themeConfig.docVersions, [antdVersion]: antdVersion };
|
|
|
|
const versionOptions = Object.keys(docVersions)
|
2017-08-31 11:47:53 +08:00
|
|
|
.map(version => <Option value={docVersions[version]} key={version}>{version}</Option>);
|
2016-11-21 14:39:15 +08:00
|
|
|
const module = location.pathname.replace(/(^\/|\/$)/g, '').split('/').slice(0, -1).join('/');
|
2016-07-26 15:24:48 +08:00
|
|
|
let activeMenuItem = module || 'home';
|
|
|
|
if (activeMenuItem === 'components' || location.pathname === 'changelog') {
|
2016-06-15 15:55:39 +08:00
|
|
|
activeMenuItem = 'docs/react';
|
|
|
|
}
|
2017-10-09 13:23:20 +08:00
|
|
|
const { locale } = this.context.intl;
|
2016-12-09 13:02:16 +08:00
|
|
|
const isZhCN = locale === 'zh-CN';
|
2017-08-31 11:47:53 +08:00
|
|
|
|
2016-04-28 15:42:02 +08:00
|
|
|
const headerClassName = classNames({
|
|
|
|
clearfix: true,
|
|
|
|
});
|
|
|
|
|
2016-09-26 12:12:44 +08:00
|
|
|
const menu = [
|
2017-12-01 20:27:44 +08:00
|
|
|
<Button ghost size="small" onClick={this.handleLangChange} className="header-lang-button" key="lang-button">
|
|
|
|
<FormattedMessage id="app.header.lang" />
|
|
|
|
</Button>,
|
2017-02-09 19:46:23 +08:00
|
|
|
<Select
|
|
|
|
key="version"
|
|
|
|
className="version"
|
|
|
|
size="small"
|
|
|
|
dropdownMatchSelectWidth={false}
|
|
|
|
defaultValue={antdVersion}
|
|
|
|
onChange={this.handleVersionChange}
|
2017-08-14 22:57:31 +08:00
|
|
|
getPopupContainer={trigger => trigger.parentNode}
|
2017-02-09 19:46:23 +08:00
|
|
|
>
|
|
|
|
{versionOptions}
|
|
|
|
</Select>,
|
2017-11-29 11:22:15 +08:00
|
|
|
<Menu className="menu-site" mode={menuMode} selectedKeys={[activeMenuItem]} id="nav" key="nav">
|
2016-09-26 12:12:44 +08:00
|
|
|
<Menu.Item key="home">
|
2016-12-09 13:02:16 +08:00
|
|
|
<Link to={utils.getLocalizedPathname('/', isZhCN)}>
|
2016-09-26 12:12:44 +08:00
|
|
|
<FormattedMessage id="app.header.menu.home" />
|
|
|
|
</Link>
|
|
|
|
</Menu.Item>
|
2016-10-07 14:33:08 +08:00
|
|
|
<Menu.Item key="docs/spec">
|
2016-12-09 13:02:16 +08:00
|
|
|
<Link to={utils.getLocalizedPathname('/docs/spec/introduce', isZhCN)}>
|
2016-10-07 14:33:08 +08:00
|
|
|
<FormattedMessage id="app.header.menu.spec" />
|
2016-09-26 12:12:44 +08:00
|
|
|
</Link>
|
|
|
|
</Menu.Item>
|
|
|
|
<Menu.Item key="docs/react">
|
2016-12-09 13:02:16 +08:00
|
|
|
<Link to={utils.getLocalizedPathname('/docs/react/introduce', isZhCN)}>
|
2016-09-26 12:12:44 +08:00
|
|
|
<FormattedMessage id="app.header.menu.components" />
|
|
|
|
</Link>
|
|
|
|
</Menu.Item>
|
2017-10-31 16:32:09 +08:00
|
|
|
<Menu.Item key="pro">
|
2017-10-31 21:58:41 +08:00
|
|
|
<a
|
|
|
|
href="http://pro.ant.design"
|
|
|
|
className="header-link"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
>
|
2017-10-31 18:07:37 +08:00
|
|
|
<FormattedMessage id="app.header.menu.pro" />
|
2017-11-12 14:10:41 +08:00
|
|
|
<span style={{ display: 'inline-block', position: 'relative', top: -2, width: 6, marginLeft: 4 }}>
|
2017-10-31 18:07:37 +08:00
|
|
|
<Badge dot />
|
|
|
|
</span>
|
|
|
|
</a>
|
2017-06-11 22:44:55 +08:00
|
|
|
</Menu.Item>
|
2016-09-26 12:12:44 +08:00
|
|
|
</Menu>,
|
|
|
|
];
|
|
|
|
|
2016-10-07 14:33:08 +08:00
|
|
|
const searchPlaceholder = locale === 'zh-CN' ? '搜索组件...' : 'Search Components...';
|
2016-03-01 16:20:32 +08:00
|
|
|
return (
|
2016-04-28 15:42:02 +08:00
|
|
|
<header id="header" className={headerClassName}>
|
2018-01-05 17:23:59 +08:00
|
|
|
{isMobile && (
|
2017-10-09 13:23:20 +08:00
|
|
|
<Popover
|
|
|
|
overlayClassName="popover-menu"
|
|
|
|
placement="bottomRight"
|
|
|
|
content={menu}
|
|
|
|
trigger="click"
|
|
|
|
visible={menuVisible}
|
|
|
|
arrowPointAtCenter
|
|
|
|
onVisibleChange={this.onMenuVisibleChange}
|
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
className="nav-phone-icon"
|
|
|
|
type="menu"
|
|
|
|
onClick={this.handleShowMenu}
|
|
|
|
/>
|
|
|
|
</Popover>
|
2018-01-04 20:00:38 +08:00
|
|
|
)}
|
2016-03-01 16:33:19 +08:00
|
|
|
<Row>
|
2018-01-04 20:00:38 +08:00
|
|
|
<Col xxl={4} xl={5} lg={5} md={6} sm={24} xs={24}>
|
2016-12-09 13:02:16 +08:00
|
|
|
<Link to={utils.getLocalizedPathname('/', isZhCN)} id="logo">
|
2017-11-28 17:01:41 +08:00
|
|
|
<img alt="logo" src="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg" />
|
2017-12-29 15:24:55 +08:00
|
|
|
<img alt="Ant Design" src="https://gw.alipayobjects.com/zos/rmsportal/DkKNubTaaVsKURhcVGkh.svg" />
|
2016-03-01 16:20:32 +08:00
|
|
|
</Link>
|
2016-03-01 16:33:19 +08:00
|
|
|
</Col>
|
2018-01-04 20:00:38 +08:00
|
|
|
<Col xxl={20} xl={19} lg={19} md={18} sm={0} xs={0}>
|
2016-03-01 16:33:19 +08:00
|
|
|
<div id="search-box">
|
2017-11-27 15:36:58 +08:00
|
|
|
<Icon type="search" />
|
2017-12-29 12:02:22 +08:00
|
|
|
<Input ref={ref => this.searchInput = ref} placeholder={searchPlaceholder} />
|
2016-03-01 16:33:19 +08:00
|
|
|
</div>
|
2018-01-06 12:38:28 +08:00
|
|
|
{!isMobile && (
|
|
|
|
<div id="header-menu">
|
|
|
|
{menu}
|
|
|
|
</div>
|
|
|
|
)}
|
2016-03-01 16:33:19 +08:00
|
|
|
</Col>
|
|
|
|
</Row>
|
2016-03-01 16:20:32 +08:00
|
|
|
</header>
|
|
|
|
);
|
|
|
|
}
|
2016-02-29 14:08:40 +08:00
|
|
|
}
|