ant-design-vue/site/components/demo.vue

141 lines
4.2 KiB
Vue
Raw Normal View History

2018-04-04 10:39:21 +00:00
<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'
2018-04-04 15:59:38 +00:00
import { Provider, create } from '../../components/_util/store'
2018-04-04 10:39:21 +00:00
export default {
2018-04-04 15:59:38 +00:00
data () {
this.store = create({
currentSubMenu: [],
})
this.subscribe()
return {
currentSubMenu: [],
}
},
beforeDestroy () {
if (this.unsubscribe) {
this.unsubscribe()
}
},
watch: {
'$route.params.name': function () {
this.store.setState({ currentSubMenu: [] })
},
},
methods: {
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
const className = window.location.hash === `#${id}` ? 'current' : ''
lis.push(<li title={title} key={id}><a href={`#${id}`} class={className}>{title}</a></li>)
})
return (
<a-affix>
<ul id='demo-toc' class='toc'>
{lis}
2018-04-04 16:07:04 +00:00
<li title='API' key='API'>
<a
href='#component-api'
class={{
current: window.location.hash === '#component-api',
}}
>API</a>
</li>
2018-04-04 15:59:38 +00:00
</ul>
</a-affix>
)
},
},
2018-04-04 10:39:21 +00:00
render () {
2018-04-04 10:51:50 +00:00
const { name } = this.$route.params
2018-04-04 15:59:38 +00:00
const isCN = isZhCN(name)
const lang = isCN ? 'cn' : 'us'
2018-04-04 10:51:50 +00:00
// name = name.replace('-cn', '')
2018-04-04 10:39:21 +00:00
const titleMap = {}
const menuConfig = {
General: [],
Layout: [],
Navigation: [],
'Data Entry': [],
'Data Display': [],
Feedback: [],
Other: [],
}
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)
}
2018-04-04 10:51:50 +00:00
const Demo = AllDemo[titleMap[name.replace('-cn', '')]]
2018-04-04 10:39:21 +00:00
const MenuGroup = []
for (const [type, menus] of Object.entries(menuConfig)) {
const MenuItems = []
_.sortBy(menus, ['title']).forEach(({ title, subtitle }) => {
const linkValue = lang === 'cn'
? [<span>{title}</span>, <span class='chinese'>{subtitle}</span>]
: [<span>{title}</span>]
let key = `${title.replace(/(\B[A-Z])/g, '-$1').toLowerCase()}`
if (lang === 'cn') {
key = `${key}-cn`
}
MenuItems.push(<a-menu-item key={key}>
2018-04-04 15:59:38 +00:00
<router-link to={{ path: `/ant-design/components/${key}/` }}>{linkValue}</router-link>
2018-04-04 10:39:21 +00:00
</a-menu-item>)
})
MenuGroup.push(<a-menu-item-group title={type}>{MenuItems}</a-menu-item-group>)
}
let locale = zhCN
if (lang !== 'cn') {
locale = enUS
}
return (
<div class='page-wrapper'>
<Header num={Object.keys(AllDemo).length}/>
2018-04-04 15:59:38 +00:00
2018-04-04 10:39:21 +00:00
<a-locale-provider locale={locale}>
<div class='main-wrapper'>
<a-row>
<a-col xxl={4} xl={5} lg={5} md={6} sm={24} xs={24}>
<a-menu
class='aside-container menu-site'
selectedKeys={[name]}
defaultOpenKeys={['Components']}
inlineIndent={40}
mode='inline'>
<a-sub-menu title='Components' key='Components'>
{MenuGroup}
</a-sub-menu>
</a-menu>
</a-col>
<a-col xxl={20} xl={19} lg={19} md={18} sm={0} xs={0}>
<div class='content main-container'>
2018-04-04 15:59:38 +00:00
<div class='toc-affix' style='width: 110px;'>
{this.getSubMenu(isCN)}
</div>
{Demo ? <Provider store={this.store}><Demo key={lang}/></Provider> : '正在紧急开发中...'}
2018-04-04 10:39:21 +00:00
</div>
</a-col>
</a-row>
</div>
</a-locale-provider>
</div>
)
},
}
</script>