You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
6.1 KiB

import React from 'react';
import { Link } from 'react-router';
import scrollIntoView from 'dom-scroll-into-view';
import { Row, Col, Menu } from 'antd';
import Article from './Article';
9 years ago
import ComponentDoc from './ComponentDoc';
import * as utils from '../utils';
import config from '../../';
const SubMenu = Menu.SubMenu;
export default class MainContent extends React.Component {
static contextTypes = {
intl: React.PropTypes.object.isRequired,
}
componentDidMount() {
scrollIntoView(document.body, document, { alignWithTop: true });
}
shouldComponentUpdate(nextProps) {
const pathname = this.props.location.pathname;
return pathname !== nextProps.location.pathname ||
/^\/components\//i.test(pathname);
}
getActiveMenuItem(props) {
return props.params.children;
}
fileNameToPath(filename) {
const snippets = filename.replace(/(\/index)?((\.zh-CN)|(\.en-US))?\.md$/i, '').split('/');
return snippets[snippets.length - 1];
9 years ago
}
generateMenuItem(isTop, item) {
const key = this.fileNameToPath(item.filename);
const text = isTop ?
item.title || item.chinese || item.english : [
<span key="english">{item.title || item.english}</span>,
<span className="chinese" key="chinese">{item.subtitle || item.chinese}</span>,
9 years ago
];
const disabled = item.disabled;
const url = item.filename.replace(/(\/index)?((\.zh-CN)|(\.en-US))?\.md$/i, '');
const child = !item.link ?
<Link to={url} disabled={disabled}>
{text}
</Link> :
<a href={item.link} target="_blank" disabled={disabled}>
{text}
</a>;
return (
<Menu.Item key={key.toLowerCase()} disabled={disabled}>
{child}
</Menu.Item>
);
}
9 years ago
isNotTopLevel(level) {
return level !== 'topLevel';
}
generateSubMenuItems(obj) {
const topLevel = (obj.topLevel || []).map(this.generateMenuItem.bind(this, true));
9 years ago
const itemGroups = Object.keys(obj).filter(this.isNotTopLevel)
9 years ago
.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));
9 years ago
return (
<Menu.ItemGroup title={type} key={index}>
{groupItems}
9 years ago
</Menu.ItemGroup>
);
});
9 years ago
return [...topLevel, ...itemGroups];
}
getModuleData() {
const props = this.props;
9 years ago
let moduleData;
if (/(docs\/react\/)|(components\/)|(CHANGELOG)/i.test(props.location.pathname)) {
9 years ago
moduleData = {
...props.data.docs.react,
...props.data.components,
CHANGELOG: props.data.CHANGELOG,
9 years ago
};
} 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);
9 years ago
const topLevel = this.generateSubMenuItems(menuItems.topLevel);
const subMenu = Object.keys(menuItems).filter(this.isNotTopLevel)
.sort((a, b) => {
return config.categoryOrder[a] - config.categoryOrder[b];
})
.map((category) => {
9 years ago
const subMenuItems = this.generateSubMenuItems(menuItems[category]);
return (
<SubMenu title={<h4>{category}</h4>} key={category}>
{subMenuItems}
</SubMenu>
);
});
return [...topLevel, ...subMenu];
}
9 years ago
flattenMenu(menu) {
if (menu.type === Menu.Item) {
return menu;
}
if (Array.isArray(menu)) {
return menu.reduce((acc, item) => acc.concat(this.flattenMenu(item)), []);
9 years ago
}
return this.flattenMenu(menu.props.children);
}
getFooterNav(menuItems, activeMenuItem) {
const menuItemsList = this.flattenMenu(menuItems);
9 years ago
let activeMenuItemIndex = -1;
menuItemsList.forEach((menuItem, i) => {
if (menuItem.key === activeMenuItem) {
activeMenuItemIndex = i;
}
9 years ago
});
const prev = menuItemsList[activeMenuItemIndex - 1];
const next = menuItemsList[activeMenuItemIndex + 1];
return { prev, next };
}
render() {
const props = this.props;
const activeMenuItem = this.getActiveMenuItem(props);
const menuItems = this.getMenuItems();
9 years ago
const { prev, next } = this.getFooterNav(menuItems, activeMenuItem);
const locale = this.context.intl.locale;
const moduleData = this.getModuleData();
9 years ago
const pageData = props.pageData.index || props.pageData;
const localizedPageData = pageData[locale] || pageData;
return (
<div className="main-wrapper">
<Row>
<Col lg={4} md={6} sm={24} xs={24}>
<Menu className="aside-container" mode="inline"
defaultOpenKeys={Object.keys(utils.getMenuItems(moduleData, locale))}
selectedKeys={[activeMenuItem]}>
{menuItems}
</Menu>
</Col>
<Col lg={20} md={18} sm={24} xs={24} className="main-container">
9 years ago
{
props.pageData.demo ?
<ComponentDoc {...props} doc={localizedPageData} demos={props.pageData.demo} /> :
<Article {...props} content={localizedPageData} />
}
</Col>
</Row>
<Row>
<Col lg={{ span: 20, offset: 4 }}
md={{ span: 18, offset: 6 }}
sm={24} xs={24}>
<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>
);
}
}