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-02-29 14:08:40 +08:00
|
|
|
import { Row, Col, Menu } from '../../../';
|
2016-03-07 11:35:23 +08:00
|
|
|
import config from '../../website.config';
|
2016-03-03 11:12:46 +08:00
|
|
|
const SubMenu = Menu.SubMenu;
|
2016-02-29 14:08:40 +08:00
|
|
|
|
2016-03-02 17:12:43 +08:00
|
|
|
function dashed(name) {
|
|
|
|
return name.toLowerCase().trim().replace(/\s+/g, '-');
|
|
|
|
}
|
|
|
|
|
2016-03-02 11:57:37 +08:00
|
|
|
export default class MainContent extends React.Component {
|
2016-03-07 17:33:38 +08:00
|
|
|
componentDidMount() {
|
|
|
|
this.componentDidUpdate();
|
|
|
|
}
|
2016-03-03 11:12:46 +08:00
|
|
|
|
2016-03-07 17:33:38 +08:00
|
|
|
componentDidUpdate() {
|
|
|
|
const scrollTo = this.props.location.query.scrollTo;
|
|
|
|
if (scrollTo !== undefined) {
|
|
|
|
const target = document.getElementById(scrollTo);
|
|
|
|
|
|
|
|
if (target !== null) {
|
|
|
|
scrollIntoView(
|
|
|
|
target,
|
|
|
|
document,
|
|
|
|
{ alignWithTop: true, onlyScrollIfNeeded: false }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-03-03 11:12:46 +08:00
|
|
|
}
|
|
|
|
|
2016-03-07 10:32:14 +08:00
|
|
|
getActiveMenuItem(props, index) {
|
|
|
|
const routes = props.routes;
|
|
|
|
return routes[routes.length - 1].path || index;
|
|
|
|
}
|
|
|
|
|
2016-03-07 17:33:38 +08:00
|
|
|
generateMenuItem(isTop, item) {
|
2016-03-03 11:12:46 +08:00
|
|
|
const key = dashed(item.english);
|
2016-03-07 17:33:38 +08:00
|
|
|
const text = isTop ?
|
|
|
|
item.chinese || item.english :
|
|
|
|
[
|
|
|
|
<span key="english">{ item.english }</span>,
|
|
|
|
<span className="chinese" key="chinese">{ item.chinese }</span>
|
|
|
|
];
|
2016-03-03 11:12:46 +08:00
|
|
|
const disabled = item.disabled === 'true';
|
|
|
|
|
|
|
|
const child = !item.link ?
|
|
|
|
<Link to={`/${this.props.category}/${key}`} disabled={disabled}>
|
|
|
|
{ text }
|
|
|
|
</Link> :
|
|
|
|
<a href={item.link} target="_blank" disabled={disabled}>
|
|
|
|
{ text }
|
|
|
|
</a>;
|
|
|
|
|
|
|
|
return (
|
2016-03-07 10:32:14 +08:00
|
|
|
<Menu.Item key={key} disabled={disabled}>
|
2016-03-03 11:12:46 +08:00
|
|
|
{ child }
|
|
|
|
</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)
|
|
|
|
.sort((a, b) => {
|
|
|
|
return config.typeOrder[a] - config.typeOrder[b];
|
|
|
|
})
|
|
|
|
.map((type, index) => {
|
2016-03-09 15:09:10 +08:00
|
|
|
const groupItems = obj[type].sort((a, b) => {
|
|
|
|
return a.english.charCodeAt(0) - b.english.charCodeAt(0);
|
|
|
|
}).map(this.generateMenuItem.bind(this, false));
|
2016-03-07 11:35:23 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Menu.ItemGroup title={type} key={index}>
|
|
|
|
{ groupItems }
|
|
|
|
</Menu.ItemGroup>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
return [...topLevel, ...itemGroups];
|
|
|
|
}
|
|
|
|
|
2016-03-01 17:05:24 +08:00
|
|
|
getMenuItems() {
|
2016-03-02 11:57:37 +08:00
|
|
|
const props = this.props;
|
2016-03-03 11:12:46 +08:00
|
|
|
const menuItems = props.menuItems;
|
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}>
|
|
|
|
{ subMenuItems }
|
|
|
|
</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)) {
|
|
|
|
return menu.reduce((acc, item) => {
|
|
|
|
return acc.concat(this.flattenMenu(item));
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.flattenMenu(menu.props.children);
|
|
|
|
}
|
|
|
|
|
|
|
|
getFooterNav(menuItems, activeMenuItem) {
|
|
|
|
const menuItemsList = this.flattenMenu(menuItems);
|
|
|
|
const activeMenuItemIndex = menuItemsList.findIndex((menuItem) => {
|
|
|
|
return menuItem.key === activeMenuItem;
|
|
|
|
});
|
|
|
|
const prev = menuItemsList[activeMenuItemIndex - 1];
|
|
|
|
const next = menuItemsList[activeMenuItemIndex + 1];
|
|
|
|
return { prev, next };
|
|
|
|
}
|
|
|
|
|
2016-02-29 14:08:40 +08:00
|
|
|
render() {
|
2016-03-07 10:32:14 +08:00
|
|
|
const activeMenuItem = this.getActiveMenuItem(this.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-02-29 14:08:40 +08:00
|
|
|
return (
|
2016-03-07 14:22:30 +08:00
|
|
|
<div className="main-wrapper">
|
|
|
|
<Row>
|
|
|
|
<Col span="4">
|
|
|
|
<Menu className="sidebar" mode="inline"
|
2016-03-07 17:33:38 +08:00
|
|
|
defaultOpenKeys={Object.keys(this.props.menuItems)}
|
2016-03-07 14:22:30 +08:00
|
|
|
selectedKeys={[activeMenuItem]}>
|
|
|
|
{ menuItems }
|
|
|
|
</Menu>
|
|
|
|
</Col>
|
|
|
|
<Col span="20" className="main-container">
|
|
|
|
{ this.props.children }
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
|
|
|
|
<Row>
|
|
|
|
<Col span="20" offset="4">
|
|
|
|
<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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|