import React from 'react';
import NotFound from '../../src/components/404';
import Layout from '../../src/components/Layout';
import AsideNav from '../../src/components/AsideNav';
import {
AlertComponent,
Drawer,
ToastComponent
} from '../../src/components/index';
import {mapTree} from '../../src/utils/helper';
import {Icon} from '../../src/components/icons';
import '../../src/locale/en';
import {
Router,
Route,
IndexRoute,
browserHistory,
hashHistory,
Link,
Redirect,
withRouter
} from 'react-router';
import Select from '../../src/components/Select';
import DocSearch from './DocSearch';
import Doc, {docs} from './Doc';
import Example, {examples} from './Example';
let ExamplePathPrefix = '/examples';
let DocPathPrefix = '/docs';
let ContextPath = '';
if (process.env.NODE_ENV === 'production') {
ExamplePathPrefix = '';
DocPathPrefix = '';
ContextPath = '/amis';
}
const themes = [
{
label: '默认主题',
ns: 'a-',
value: 'default'
},
{
label: '百度云舍',
ns: 'cxd-',
value: 'cxd'
},
{
label: 'Dark',
ns: 'dark-',
value: 'dark'
}
];
const locales = [
{
label: '中文',
value: 'zh-cn'
},
{
label: 'English',
value: 'en'
}
];
const viewModes = [
{
label: '桌面端',
value: 'pc'
},
{
label: '移动端',
value: 'mobile'
}
];
function getPath(path) {
return path
? path[0] === '/'
? ContextPath + path
: `${ContextPath}/${path}`
: '';
}
class BackTop extends React.PureComponent {
state = {
show: false
};
componentDidMount() {
document.addEventListener('scroll', this.handleScroll.bind(this));
}
componentWillUnmount() {
document.removeEventListener('scroll', this.handleScroll.bind(this));
}
handleScroll(e) {
this.setState({
show: e.target.scrollingElement?.scrollTop > 350
});
}
render() {
return (
scrollTo({top: 0})}
>
);
}
}
@withRouter
export class App extends React.PureComponent {
state = {
viewMode: localStorage.getItem('viewMode') || 'pc',
offScreen: false,
headerVisible: true,
themeIndex: 0,
themes: themes,
theme: themes[localStorage.getItem('themeIndex') || 0],
locale: localStorage.getItem('locale') || '',
navigations: []
};
constructor(props) {
super(props);
this.setNavigations = this.setNavigations.bind(this);
}
componentDidMount() {
if (this.state.theme.value !== 'default') {
document.querySelectorAll('link[title]').forEach(item => {
item.disabled = true;
});
document
.querySelectorAll(`link[title=${this.state.theme.value}]`)
.forEach(item => {
item.disabled = false;
});
if (this.state.theme.value === 'dark') {
document.querySelector('body').classList.add('dark');
}
}
}
componentDidUpdate(preProps, preState) {
const props = this.props;
if (preState.theme.value !== this.state.theme.value) {
document
.querySelectorAll(`link[title=${preState.theme.value}]`)
.forEach(item => {
item.disabled = true;
});
document
.querySelectorAll(`link[title=${this.state.theme.value}]`)
.forEach(item => {
item.disabled = false;
});
}
if (props.location.pathname !== preProps.location.pathname) {
this.setState(
{
offScreen: false
},
() => window.scrollTo(0, 0)
);
_hmt && _hmt.push(['_trackPageview', props.location.pathname]);
}
}
setNavigations(items) {
this.setState({
navigations: items
});
}
renderHeader() {
const location = this.props.location;
const theme = this.state.theme;
if (location.pathname === '/edit') {
return (
);
}
return (
<>
this.setState({offScreen: !this.state.offScreen})}
className={`${theme.ns}Layout-offScreen-btn pull-left visible-xs`}
>
>
);
}
renderNavigation() {
return (
({
...item,
children: item.children
? item.children.map(item => ({
...item,
className: 'is-top'
}))
: []
}))}
renderLink={({
link,
active,
toggleExpand,
classnames: cx,
depth
}: any) => {
let children = [];
if (link.children && link.children.length) {
children.push(
toggleExpand(link, e)}
>
);
}
children.push(
{link.label}
);
return link.path ? (
/^https?\:/.test(link.path) ? (
{children}
) : (
{children}
)
) : (
toggleExpand(link) : undefined}>
{children}
);
}}
isActive={(link: any) => isActive(link, location)}
/>
);
}
render() {
const theme = this.state.theme;
return (
{this.renderNavigation()}
this.setState({offScreen: false})}
show={this.state.offScreen}
position="left"
>
{this.renderNavigation()}
{React.cloneElement(this.props.children, {
key: theme.value,
...this.props.children.props,
setNavigations: this.setNavigations,
theme: theme.value,
classPrefix: theme.ns,
viewMode: this.state.viewMode,
locale: this.state.locale,
offScreen: this.state.offScreen,
ContextPath
})}
);
}
}
function isActive(link: any, location: any) {
return !!(link.path && getPath(link.path) === location.pathname);
}
function navigations2route(pathPrefix = DocPathPrefix, navigations) {
let routes = [];
navigations.forEach(root => {
root.children &&
mapTree(root.children, item => {
if (item.path && item.component) {
routes.push(
);
} else if (item.path && item.getComponent) {
routes.push(
);
}
});
});
return routes;
}
export default function entry({pathPrefix}) {
// PathPrefix = pathPrefix || DocPathPrefix;
return (
{navigations2route(DocPathPrefix, docs)}
{navigations2route(ExamplePathPrefix, examples)}
);
}