2016-02-29 14:08:40 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { Link } from 'react-router';
|
2016-03-07 17:33:38 +08:00
|
|
|
import scrollIntoView from 'dom-scroll-into-view';
|
2016-04-11 10:12:48 +08:00
|
|
|
import { Row, Col, Menu } from 'antd';
|
2016-05-27 11:48:08 +08:00
|
|
|
import Article from './Article';
|
|
|
|
import ComponentDoc from './ComponentDoc';
|
|
|
|
import * as utils from '../utils';
|
|
|
|
import config from '../../';
|
2016-03-03 11:12:46 +08:00
|
|
|
const SubMenu = Menu.SubMenu;
|
2016-02-29 14:08:40 +08:00
|
|
|
|
2016-03-02 11:57:37 +08:00
|
|
|
export default class MainContent extends React.Component {
|
2016-05-27 11:48:08 +08:00
|
|
|
static contextTypes = {
|
|
|
|
intl: React.PropTypes.object.isRequired,
|
|
|
|
}
|
|
|
|
|
2016-03-07 17:33:38 +08:00
|
|
|
componentDidMount() {
|
2016-05-27 11:48:08 +08:00
|
|
|
scrollIntoView(document.body, document, { alignWithTop: true });
|
2016-03-07 17:33:38 +08:00
|
|
|
}
|
2016-03-03 11:12:46 +08:00
|
|
|
|
2016-04-19 17:06:15 +08:00
|
|
|
shouldComponentUpdate(nextProps) {
|
|
|
|
const pathname = this.props.location.pathname;
|
|
|
|
return pathname !== nextProps.location.pathname ||
|
|
|
|
/^\/components\//i.test(pathname);
|
|
|
|
}
|
|
|
|
|
2016-03-10 16:56:17 +08:00
|
|
|
getActiveMenuItem(props) {
|
2016-03-23 11:54:10 +08:00
|
|
|
return props.params.children;
|
2016-03-10 16:56:17 +08:00
|
|
|
}
|
|
|
|
|
2016-05-27 11:48:08 +08:00
|
|
|
fileNameToPath(filename) {
|
|
|
|
const snippets = filename.replace(/(\/index)?((\.zh-CN)|(\.en-US))?\.md$/i, '').split('/');
|
2016-03-10 16:56:17 +08:00
|
|
|
return snippets[snippets.length - 1];
|
2016-03-07 10:32:14 +08:00
|
|
|
}
|
|
|
|
|
2016-03-07 17:33:38 +08:00
|
|
|
generateMenuItem(isTop, item) {
|
2016-05-27 11:48:08 +08:00
|
|
|
const key = this.fileNameToPath(item.filename);
|
2016-03-07 17:33:38 +08:00
|
|
|
const text = isTop ?
|
2016-05-17 11:53:10 +08:00
|
|
|
item.title || item.chinese || item.english : [
|
2016-05-03 10:34:44 +08:00
|
|
|
<span key="english">{item.title || item.english}</span>,
|
2016-05-11 12:06:13 +08:00
|
|
|
<span className="chinese" key="chinese">{item.subtitle || item.chinese}</span>,
|
2016-04-22 14:53:33 +08:00
|
|
|
];
|
2016-03-31 15:22:38 +08:00
|
|
|
const disabled = item.disabled;
|
2016-06-03 15:51:44 +08:00
|
|
|
const url = item.filename.replace(/(\/index)?((\.zh-CN)|(\.en-US))?\.md$/i, '').toLowerCase();
|
2016-03-03 11:12:46 +08:00
|
|
|
const child = !item.link ?
|
2016-04-29 12:13:27 +08:00
|
|
|
<Link to={url} disabled={disabled}>
|
|
|
|
{text}
|
|
|
|
</Link> :
|
|
|
|
<a href={item.link} target="_blank" disabled={disabled}>
|
|
|
|
{text}
|
|
|
|
</a>;
|
2016-03-03 11:12:46 +08:00
|
|
|
|
|
|
|
return (
|
2016-05-06 11:06:26 +08:00
|
|
|
<Menu.Item key={key.toLowerCase()} disabled={disabled}>
|
2016-04-29 12:13:27 +08:00
|
|
|
{child}
|
2016-03-03 11:12:46 +08:00
|
|
|
</Menu.Item>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-07 11:35:23 +08:00
|
|
|
isNotTopLevel(level) {
|
|
|
|
return level !== 'topLevel';
|
|
|
|
}
|
|
|
|
|
|
|
|
generateSubMenuItems(obj) {
|
2016-03-07 17:33:38 +08:00
|
|
|
const topLevel = (obj.topLevel || []).map(this.generateMenuItem.bind(this, true));
|
2016-03-07 11:35:23 +08:00
|
|
|
const itemGroups = Object.keys(obj).filter(this.isNotTopLevel)
|
2016-04-22 14:53:33 +08:00
|
|
|
.sort((a, b) => {
|
|
|
|
return config.typeOrder[a] - config.typeOrder[b];
|
|
|
|
})
|
|
|
|
.map((type, index) => {
|
|
|
|
const groupItems = obj[type].sort((a, b) => {
|
|
|
|
return (a.title || a.english).charCodeAt(0) -
|
|
|
|
(b.title || b.english).charCodeAt(0);
|
|
|
|
}).map(this.generateMenuItem.bind(this, false));
|
2016-04-29 12:13:27 +08:00
|
|
|
|
2016-04-22 14:53:33 +08:00
|
|
|
return (
|
|
|
|
<Menu.ItemGroup title={type} key={index}>
|
2016-05-03 10:34:44 +08:00
|
|
|
{groupItems}
|
2016-04-22 14:53:33 +08:00
|
|
|
</Menu.ItemGroup>
|
|
|
|
);
|
|
|
|
});
|
2016-03-07 11:35:23 +08:00
|
|
|
return [...topLevel, ...itemGroups];
|
|
|
|
}
|
|
|
|
|
2016-05-27 11:48:08 +08:00
|
|
|
getModuleData() {
|
2016-05-27 11:03:49 +08:00
|
|
|
const props = this.props;
|
2016-05-27 11:48:08 +08:00
|
|
|
|
|
|
|
let moduleData;
|
2016-06-03 15:51:44 +08:00
|
|
|
if (/(docs\/react\/)|(components\/)|(changelog)/i.test(props.location.pathname)) {
|
2016-05-27 11:48:08 +08:00
|
|
|
moduleData = {
|
|
|
|
...props.data.docs.react,
|
|
|
|
...props.data.components,
|
2016-06-03 15:51:44 +08:00
|
|
|
changelog: props.data.CHANGELOG,
|
2016-05-27 11:48:08 +08:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
moduleData = props.utils.get(props.data, props.location.pathname.split('/').slice(0, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
return moduleData;
|
|
|
|
}
|
|
|
|
|
|
|
|
getMenuItems() {
|
|
|
|
const moduleData = this.getModuleData();
|
|
|
|
|
|
|
|
const menuItems = utils.getMenuItems(moduleData, this.context.intl.locale);
|
2016-03-07 11:35:23 +08:00
|
|
|
const topLevel = this.generateSubMenuItems(menuItems.topLevel);
|
|
|
|
const subMenu = Object.keys(menuItems).filter(this.isNotTopLevel)
|
|
|
|
.sort((a, b) => {
|
|
|
|
return config.categoryOrder[a] - config.categoryOrder[b];
|
|
|
|
})
|
2016-03-03 11:12:46 +08:00
|
|
|
.map((category) => {
|
2016-03-07 11:35:23 +08:00
|
|
|
const subMenuItems = this.generateSubMenuItems(menuItems[category]);
|
2016-03-03 11:12:46 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<SubMenu title={<h4>{category}</h4>} key={category}>
|
2016-04-29 12:13:27 +08:00
|
|
|
{subMenuItems}
|
2016-03-03 11:12:46 +08:00
|
|
|
</SubMenu>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return [...topLevel, ...subMenu];
|
2016-03-01 17:05:24 +08:00
|
|
|
}
|
|
|
|
|
2016-03-07 10:32:14 +08:00
|
|
|
flattenMenu(menu) {
|
|
|
|
if (menu.type === Menu.Item) {
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(menu)) {
|
2016-04-26 14:09:19 +08:00
|
|
|
return menu.reduce((acc, item) => acc.concat(this.flattenMenu(item)), []);
|
2016-03-07 10:32:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.flattenMenu(menu.props.children);
|
|
|
|
}
|
|
|
|
|
|
|
|
getFooterNav(menuItems, activeMenuItem) {
|
|
|
|
const menuItemsList = this.flattenMenu(menuItems);
|
2016-03-30 22:51:30 +08:00
|
|
|
let activeMenuItemIndex = -1;
|
|
|
|
menuItemsList.forEach((menuItem, i) => {
|
|
|
|
if (menuItem.key === activeMenuItem) {
|
|
|
|
activeMenuItemIndex = i;
|
|
|
|
}
|
2016-03-07 10:32:14 +08:00
|
|
|
});
|
|
|
|
const prev = menuItemsList[activeMenuItemIndex - 1];
|
|
|
|
const next = menuItemsList[activeMenuItemIndex + 1];
|
|
|
|
return { prev, next };
|
|
|
|
}
|
|
|
|
|
2016-02-29 14:08:40 +08:00
|
|
|
render() {
|
2016-05-27 11:48:08 +08:00
|
|
|
const props = this.props;
|
|
|
|
const activeMenuItem = this.getActiveMenuItem(props);
|
2016-03-01 17:05:24 +08:00
|
|
|
const menuItems = this.getMenuItems();
|
2016-03-07 10:32:14 +08:00
|
|
|
const { prev, next } = this.getFooterNav(menuItems, activeMenuItem);
|
2016-03-01 16:20:32 +08:00
|
|
|
|
2016-05-27 11:48:08 +08:00
|
|
|
const locale = this.context.intl.locale;
|
|
|
|
const moduleData = this.getModuleData();
|
2016-06-03 15:51:44 +08:00
|
|
|
const pageData = /changelog/i.test(props.location.pathname) ?
|
|
|
|
props.data.CHANGELOG :
|
|
|
|
(props.pageData.index || props.pageData);
|
2016-05-27 11:48:08 +08:00
|
|
|
const localizedPageData = pageData[locale] || pageData;
|
2016-02-29 14:08:40 +08:00
|
|
|
return (
|
2016-03-07 14:22:30 +08:00
|
|
|
<div className="main-wrapper">
|
|
|
|
<Row>
|
2016-03-24 17:13:19 +08:00
|
|
|
<Col lg={4} md={6} sm={24} xs={24}>
|
|
|
|
<Menu className="aside-container" mode="inline"
|
2016-05-27 11:48:08 +08:00
|
|
|
defaultOpenKeys={Object.keys(utils.getMenuItems(moduleData, locale))}
|
2016-03-07 14:22:30 +08:00
|
|
|
selectedKeys={[activeMenuItem]}>
|
2016-04-29 12:13:27 +08:00
|
|
|
{menuItems}
|
2016-03-07 14:22:30 +08:00
|
|
|
</Menu>
|
|
|
|
</Col>
|
2016-03-24 17:13:19 +08:00
|
|
|
<Col lg={20} md={18} sm={24} xs={24} className="main-container">
|
2016-05-27 11:48:08 +08:00
|
|
|
{
|
|
|
|
props.pageData.demo ?
|
|
|
|
<ComponentDoc {...props} doc={localizedPageData} demos={props.pageData.demo} /> :
|
|
|
|
<Article {...props} content={localizedPageData} />
|
|
|
|
}
|
2016-03-07 14:22:30 +08:00
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
|
|
|
|
<Row>
|
2016-03-24 17:13:19 +08:00
|
|
|
<Col lg={{ span: 20, offset: 4 }}
|
|
|
|
md={{ span: 18, offset: 6 }}
|
|
|
|
sm={24} xs={24}>
|
2016-03-07 14:22:30 +08:00
|
|
|
<section className="prev-next-nav">
|
|
|
|
{
|
|
|
|
!!prev ?
|
|
|
|
React.cloneElement(prev.props.children, { className: 'prev-page' }) :
|
|
|
|
null
|
|
|
|
}
|
|
|
|
{
|
|
|
|
!!next ?
|
|
|
|
React.cloneElement(next.props.children, { className: 'next-page' }) :
|
|
|
|
null
|
|
|
|
}
|
|
|
|
</section>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
</div>
|
2016-02-29 14:08:40 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|