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.
ant-design-vue/site/components/layout.vue

211 lines
6.6 KiB

7 years ago
<script>
import * as AllDemo from '../demo'
import Header from './header'
import zhCN from 'antd/locale-provider/zh_CN'
import enUS from 'antd/locale-provider/default'
import _ from 'lodash'
import { isZhCN } from '../util'
import { Provider, create } from '../../components/_util/store'
const docsList = [
{ key: 'introduce', enTitle: 'Ant Design of Vue', title: 'Ant Design of Vue' },
{ key: 'getting-started', enTitle: 'Getting Started', title: '快速上手' },
{ key: 'use-with-vue-cli', enTitle: 'Use in vue-cli', title: '在 vue-cli 中使用' },
{ key: 'customize-theme', enTitle: 'Customize Theme', title: '定制主题' },
{ key: 'changelog', enTitle: 'Change Log', title: '更新日志' },
{ key: 'i18n', enTitle: 'Internationalization', title: '国际化' },
]
7 years ago
export default {
props: {
name: String,
},
data () {
this.store = create({
currentSubMenu: [],
})
this.subscribe()
return {
currentSubMenu: [],
}
},
beforeDestroy () {
if (this.unsubscribe) {
this.unsubscribe()
}
},
7 years ago
mounted () {
this.addSubMenu()
},
7 years ago
watch: {
7 years ago
'$route.path': function () {
7 years ago
this.store.setState({ currentSubMenu: [] })
7 years ago
this.addSubMenu()
7 years ago
},
},
methods: {
7 years ago
addSubMenu () {
if (this.$route.path.indexOf('/docs/vue/') !== -1) {
this.$nextTick(() => {
const menus = []
this.$refs.doc.querySelectorAll(['h2', 'h3']).forEach(dom => {
const id = dom.id
if (id) {
const title = dom.textContent.split('#')[0].trim()
menus.push({ cnTitle: title, usTitle: title, id })
}
})
this.currentSubMenu = menus
})
}
},
7 years ago
subscribe () {
const { store } = this
this.unsubscribe = store.subscribe(() => {
this.currentSubMenu = this.store.getState().currentSubMenu
})
},
getSubMenu (isCN) {
const currentSubMenu = this.currentSubMenu
const lis = []
currentSubMenu.forEach(({ cnTitle, usTitle, id }) => {
const title = isCN ? cnTitle : usTitle
7 years ago
const className = decodeURIComponent(window.location.hash) === `#${id}` ? 'current' : ''
7 years ago
lis.push(<li title={title}><a href={`#${id}`} class={className}>{title}</a></li>)
})
7 years ago
const showApi = this.$route.path.indexOf('/components/') !== -1
7 years ago
return (
<a-affix>
<ul id='demo-toc' class='toc'>
{lis}
7 years ago
{showApi ? <li title='API' key='API'>
7 years ago
<a
7 years ago
href='#API'
7 years ago
class={{
7 years ago
current: window.location.hash === '#API',
7 years ago
}}
>API</a>
7 years ago
</li> : ''}
7 years ago
</ul>
</a-affix>
)
},
getDocsMenu (isCN) {
const docsMenu = []
docsList.forEach(({ key, enTitle, title }) => {
7 years ago
const k = isCN ? `${key}-cn` : key
docsMenu.push(<a-menu-item key={k}>
7 years ago
<router-link to={`/ant-design/docs/vue/${k}/`}>{isCN ? title : enTitle }</router-link>
7 years ago
</a-menu-item>)
})
return docsMenu
},
resetDocumentTitle (component, name, isCN) {
let titleStr = 'Vue Antd'
if (component) {
const { subtitle, title } = component
const componentName = isCN ? subtitle + ' ' + title : title
titleStr = componentName + ' - ' + titleStr
} else {
const currentKey = docsList.filter((item) => {
return item.key === name
})
if (currentKey.length) {
titleStr = (isCN ? currentKey[0]['title'] : currentKey[0]['enTitle']) + ' - ' + titleStr
}
}
document.title = titleStr
},
7 years ago
},
render () {
const name = this.name
const isCN = isZhCN(name)
// name = name.replace(/-cn\/?$/, '')
const titleMap = {}
const menuConfig = {
General: [],
Layout: [],
Navigation: [],
'Data Entry': [],
'Data Display': [],
Feedback: [],
Other: [],
}
7 years ago
const searchData = []
7 years ago
for (const [title, d] of Object.entries(AllDemo)) {
const type = d.type || 'Other'
const key = `${title.replace(/(\B[A-Z])/g, '-$1').toLowerCase()}`
titleMap[key] = title
menuConfig[type] = menuConfig[type] || []
menuConfig[type].push(d)
}
const reName = name.replace(/-cn\/?$/, '')
const Demo = AllDemo[titleMap[reName]]
7 years ago
const MenuGroup = []
for (const [type, menus] of Object.entries(menuConfig)) {
const MenuItems = []
_.sortBy(menus, ['title']).forEach(({ title, subtitle }) => {
const linkValue = isCN
? [<span>{title}</span>, <span class='chinese'>{subtitle}</span>]
: [<span>{title}</span>]
let key = `${title.replace(/(\B[A-Z])/g, '-$1').toLowerCase()}`
if (isCN) {
key = `${key}-cn`
}
7 years ago
searchData.push({
title,
subtitle,
url: `/ant-design/components/${key}/`,
})
7 years ago
MenuItems.push(<a-menu-item key={key}>
<router-link to={`/ant-design/components/${key}/`}>{linkValue}</router-link>
</a-menu-item>)
})
MenuGroup.push(<a-menu-item-group title={type}>{MenuItems}</a-menu-item-group>)
}
let locale = zhCN
if (!isCN) {
locale = enUS
}
this.resetDocumentTitle(Demo, reName, isCN)
7 years ago
return (
<div class='page-wrapper'>
7 years ago
<Header searchData={searchData} name={name}/>
7 years ago
<a-locale-provider locale={locale}>
<div class='main-wrapper'>
<a-row>
<a-col span={6} style={{ maxWidth: '260px' }}>
7 years ago
<a-menu
class='aside-container menu-site'
selectedKeys={[name]}
defaultOpenKeys={['Components']}
inlineIndent={40}
mode='inline'>
{this.getDocsMenu(isCN)}
7 years ago
<a-sub-menu title={`Components(${searchData.length})`} key='Components'>
7 years ago
{MenuGroup}
</a-sub-menu>
</a-menu>
</a-col>
<a-col span={18}>
7 years ago
<div class='content main-container'>
<div class='toc-affix' style='width: 110px;'>
7 years ago
{this.getSubMenu(isCN)}
7 years ago
</div>
{Demo ? <Provider store={this.store}>
<Demo key={isCN ? 'cn' : 'en'}/>
</Provider> : ''}
7 years ago
<div class='markdown api-container' ref='doc'>
7 years ago
<router-view></router-view>
</div>
</div>
</a-col>
</a-row>
</div>
</a-locale-provider>
</div>
)
},
}
</script>