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'; 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]; } generateMenuItem(isTop, item) { const key = this.fileNameToPath(item.filename); const text = isTop ? item.title || item.chinese || item.english : [ {item.title || item.english}, {item.subtitle || item.chinese}, ]; const disabled = item.disabled; const url = item.filename.replace(/(\/index)?((\.zh-CN)|(\.en-US))?\.md$/i, '').toLowerCase(); const child = !item.link ? {text} : {text} ; return ( {child} ); } isNotTopLevel(level) { return level !== 'topLevel'; } generateSubMenuItems(obj) { const topLevel = (obj.topLevel || []).map(this.generateMenuItem.bind(this, true)); const itemGroups = Object.keys(obj).filter(this.isNotTopLevel) .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)); return ( {groupItems} ); }); return [...topLevel, ...itemGroups]; } getModuleData() { const props = this.props; let moduleData; if (/(docs\/react\/)|(components\/)|(changelog)/i.test(props.location.pathname)) { moduleData = { ...props.data.docs.react, ...props.data.components, changelog: props.data.CHANGELOG, }; } 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); 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) => { const subMenuItems = this.generateSubMenuItems(menuItems[category]); return ( {category}} key={category}> {subMenuItems} ); }); return [...topLevel, ...subMenu]; } flattenMenu(menu) { if (menu.type === Menu.Item) { return menu; } if (Array.isArray(menu)) { return menu.reduce((acc, item) => acc.concat(this.flattenMenu(item)), []); } return this.flattenMenu(menu.props.children); } getFooterNav(menuItems, activeMenuItem) { const menuItemsList = this.flattenMenu(menuItems); let activeMenuItemIndex = -1; menuItemsList.forEach((menuItem, i) => { if (menuItem.key === activeMenuItem) { activeMenuItemIndex = i; } }); 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(); const { prev, next } = this.getFooterNav(menuItems, activeMenuItem); const locale = this.context.intl.locale; const moduleData = this.getModuleData(); const pageData = /changelog/i.test(props.location.pathname) ? props.data.CHANGELOG : (props.pageData.index || props.pageData); const localizedPageData = pageData[locale] || pageData; return (
{menuItems} { props.pageData.demo ? :
}
{ !!prev ? React.cloneElement(prev.props.children, { className: 'prev-page' }) : null } { !!next ? React.cloneElement(next.props.children, { className: 'next-page' }) : null }
); } }