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

285 lines
9.6 KiB

7 years ago
<script>
import AllDemo from '../demo'
7 years ago
import Header from './header'
import Footer from './footer'
import Sponsors from './sponsors'
7 years ago
import zhCN from 'antd/locale-provider/zh_CN'
import enUS from 'antd/locale-provider/default'
import sortBy from 'lodash/sortBy'
7 years ago
import { isZhCN } from '../util'
import { Provider, create } from '../../components/_util/store'
import NProgress from 'nprogress'
7 years ago
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: '国际化' },
{ key: 'faq', enTitle: 'FAQ', title: '常见问题' },
{ key: 'sponsor', enTitle: 'Sponsor', title: '赞助我们' },
]
7 years ago
export default {
props: {
name: String,
showDemo: Boolean,
showApi: Boolean,
7 years ago
},
data () {
this.store = create({
currentSubMenu: [],
})
this.subscribe()
return {
showSideBars: true,
7 years ago
currentSubMenu: [],
sidebarHeight: document.documentElement.offsetHeight,
7 years ago
}
},
provide () {
return {
demoContext: this,
}
},
7 years ago
beforeDestroy () {
if (this.unsubscribe) {
this.unsubscribe()
}
clearTimeout(this.timer)
if (this.resizeEvent) {
this.resizeEvent.remove()
}
if (this.debouncedResize && this.debouncedResize.cancel) {
this.debouncedResize.cancel()
}
7 years ago
},
7 years ago
mounted () {
this.$nextTick(() => {
this.addSubMenu()
const nprogressHiddenStyle = document.getElementById('nprogress-style')
if (nprogressHiddenStyle) {
this.timer = setTimeout(() => {
nprogressHiddenStyle.parentNode.removeChild(nprogressHiddenStyle)
}, 0)
}
})
7 years ago
},
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 = []
const doms = [...this.$refs.doc.querySelectorAll(['h2', 'h3'])]
doms.forEach(dom => {
7 years ago
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
lis.push(<a-anchor-link key={id} href={`#${id}`} title={title} />)
7 years ago
})
7 years ago
const showApi = this.$route.path.indexOf('/components/') !== -1
7 years ago
return (
<a-anchor offsetTop={100} class='demo-anchor'>
{lis}
{showApi ? <a-anchor-link key='API' title='API' href='#API' /> : ''}
</a-anchor>
7 years ago
)
},
getDocsMenu (isCN, pagesKey) {
7 years ago
const docsMenu = []
docsList.forEach(({ key, enTitle, title }, index) => {
7 years ago
const k = isCN ? `${key}-cn` : key
pagesKey.push({ name: k,
url: `/ant-design-vue/docs/vue/${k}/`,
title: isCN ? title : enTitle,
})
7 years ago
docsMenu.push(<a-menu-item key={k}>
<router-link to={`/ant-design-vue/docs/vue/${k}/`}>{isCN ? title : enTitle }</router-link>
7 years ago
</a-menu-item>)
})
return docsMenu
},
resetDocumentTitle (component, name, isCN) {
let titleStr = 'Ant Design Vue'
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
},
mountedCallback () {
NProgress.done()
document.documentElement.scrollTop = 0
},
7 years ago
},
7 years ago
render () {
const name = this.name
const isCN = isZhCN(name)
const titleMap = {}
const menuConfig = {
General: [],
Layout: [],
Navigation: [],
'Data Entry': [],
'Data Display': [],
Feedback: [],
Other: [],
}
const pagesKey = []
let prevPage = null
let nextPage = null
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
AllDemo[title].key = key
7 years ago
menuConfig[type] = menuConfig[type] || []
menuConfig[type].push(d)
}
const docsMenu = this.getDocsMenu(isCN, pagesKey)
const reName = name.replace(/-cn\/?$/, '')
7 years ago
const MenuGroup = []
for (const [type, menus] of Object.entries(menuConfig)) {
const MenuItems = []
sortBy(menus, ['title']).forEach(({ title, subtitle }) => {
7 years ago
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`
}
pagesKey.push({
name: key,
url: `/ant-design-vue/components/${key}/`,
title: isCN ? `${title} ${subtitle}` : title,
})
7 years ago
searchData.push({
title,
subtitle,
url: `/ant-design-vue/components/${key}/`,
7 years ago
})
7 years ago
MenuItems.push(<a-menu-item key={key}>
<router-link to={`/ant-design-vue/components/${key}/`}>{linkValue}</router-link>
7 years ago
</a-menu-item>)
})
MenuGroup.push(<a-menu-item-group title={type}>{MenuItems}</a-menu-item-group>)
}
pagesKey.forEach((item, index) => {
if (item.name === name) {
prevPage = pagesKey[index - 1]
nextPage = pagesKey[index + 1]
}
})
7 years ago
let locale = zhCN
if (!isCN) {
locale = enUS
}
const config = AllDemo[titleMap[reName]]
this.resetDocumentTitle(config, reName, isCN)
const { showSideBars } = this
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 v-show={showSideBars} ref='sidebar' class='site-sidebar' xxl={4} xl={5} lg={5} md={6} sm={8} xs={12}>
<div class='drawer-mask' onClick={() => { this.showSideBars = false }}></div>
<Sponsors title={isCN ? '赞助商' : 'Sponsors'}/>
7 years ago
<a-menu
class='aside-container menu-site'
selectedKeys={[name]}
defaultOpenKeys={['Components']}
inlineIndent={40}
mode='inline'>
{docsMenu}
7 years ago
<a-sub-menu title={`Components(${searchData.length})`} key='Components'>
7 years ago
{MenuGroup}
</a-sub-menu>
</a-menu>
<div class='close-drawer' onClick={() => { this.showSideBars = false }}>
<a-icon type='close'/>
</div>
7 years ago
</a-col>
<div v-show={!showSideBars} class='open-drawer' onClick={() => { this.showSideBars = true }}>
<a-icon type='bars'/>
</div>
<a-col class='main-container' xxl={20} xl={19} lg={19} md={18} sm={24} xs={24}>
6 years ago
<div class='content'>
<div class='toc-affix' style='width: 120px;'>
7 years ago
{this.getSubMenu(isCN)}
7 years ago
</div>
{this.showDemo ? <Provider store={this.store} key={isCN ? 'cn' : 'en'}>
<router-view
class={`demo-cols-${config.cols || 2}`}
{...{ directives: [
{
name: 'mountedCallback',
value: this.mountedCallback,
},
] }}
></router-view>
7 years ago
</Provider> : ''}
{this.showApi ? <div class='markdown api-container' ref='doc'>
<router-view
{...{ directives: [
{
name: 'mountedCallback',
value: this.mountedCallback,
},
] }}
></router-view>
</div> : ''}
7 years ago
</div>
<section class='prev-next-nav'>
{prevPage ? <router-link class='prev-page' to={`${prevPage.url}`}><a-icon type='left' />&nbsp;&nbsp;{prevPage.title}</router-link> : ''}
{nextPage ? <router-link class='next-page' to={`${nextPage.url}`}>{nextPage.title}&nbsp;&nbsp;<a-icon type='right' /></router-link> : ''}
</section>
7 years ago
</a-col>
</a-row>
</div>
</a-locale-provider>
<Footer ref='footer' isCN={isCN}/>
{ name.indexOf('back-top') === -1 ? <a-back-top /> : null }
7 years ago
</div>
)
},
}
</script>