forked from element-plus/element-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.config.js
175 lines (159 loc) · 4.8 KB
/
route.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { defineAsyncComponent } from 'vue'
import langs from './i18n/route'
import navConfig from './nav.config'
import { Language } from './enums/language'
const LoadingComponent = {
template: `<div v-loading="true" style="min-height: 500px; width: 100%;"></div>`,
}
const ErrorComponent = {
template: `
<div style="text-align: center;padding: 100px 0;">Loading error. Please refresh the page and try again</div>`,
}
const getAsyncComponent = func => {
return defineAsyncComponent({
loader: func,
delay: 0,
timeout: 30000,
errorComponent: ErrorComponent,
loadingComponent: LoadingComponent,
})
}
const LOAD_MAP = {
[Language.CN]: name => {
return getAsyncComponent(() => import(/* webpackChunkName: "zh-CN" */ `./pages/${name}.vue`))
},
[Language.EN]: name => {
return getAsyncComponent(() => import(/* webpackChunkName: "en-US" */ `./pages/${name}.vue`))
},
[Language.ES]: name => {
return getAsyncComponent(() => import(/* webpackChunkName: "es" */ `./pages/${name}.vue`))
},
[Language.FR]: name => {
return getAsyncComponent(() => import(/* webpackChunkName: "fr-FR" */ `./pages/${name}.vue`))
},
[Language.JP]: name => {
return getAsyncComponent(() => import(/* webpackChunkName: "jp" */ `./pages/${name}.vue`))
},
}
const load = function(lang, path) {
return LOAD_MAP[lang](path)
}
const LOAD_DOCS_MAP = {
[Language.CN]: path => {
return getAsyncComponent(() => import(/* webpackChunkName: "DOCS zh-CN" */ `./docs/zh-CN${path}.md`))
},
[Language.EN]: path => {
return getAsyncComponent(() => import(/* webpackChunkName: "DOCS en-US" */ `./docs/en-US${path}.md`))
},
[Language.ES]: path => {
return getAsyncComponent(() => import(/* webpackChunkName: "DOCS es" */ `./docs/es${path}.md`))
},
[Language.FR]: path => {
return getAsyncComponent(() => import(/* webpackChunkName: "DOCS fr-FR" */ `./docs/fr-FR${path}.md`))
},
[Language.JP]: path => {
return getAsyncComponent(() => import(/* webpackChunkName: "DOCS fr-FR" */ `./docs/jp${path}.md`))
},
}
const loadDocs = function(lang, path) {
return LOAD_DOCS_MAP[lang](path)
}
const registerRoute = navConfig => {
let route = []
Object.keys(navConfig).forEach((lang, index) => {
let navs = navConfig[lang]
route.push({
path: `/${ lang }/component`,
redirect: `/${ lang }/component/installation`,
component: load(lang, 'component'),
children: [],
})
navs.forEach(nav => {
if (nav.href) return
if (nav.groups) {
nav.groups.forEach(group => {
group.list.forEach(nav => {
addRoute(nav, lang, index)
})
})
} else if (nav.children) {
nav.children.forEach(nav => {
addRoute(nav, lang, index)
})
} else {
addRoute(nav, lang, index)
}
})
})
function addRoute(page, lang, index) {
const component = page.path === '/changelog'
? load(lang, 'changelog')
: loadDocs(lang, page.path)
let child = {
path: page.path.slice(1),
meta: {
title: page.title || page.name,
description: page.description,
lang,
},
name: 'component-' + lang + (page.title || page.name),
component: component.default || component,
}
route[index].children.push(child)
}
return route
}
let route = registerRoute(navConfig)
const generateMiscRoutes = function(lang) {
let guideRoute = {
path: `/${ lang }/guide`, // 指南
redirect: `/${ lang }/guide/design`,
component: load(lang, 'guide'),
children: [{
path: 'design', // 设计原则
name: 'guide-design' + lang,
meta: { lang },
component: load(lang, 'design'),
}, {
path: 'nav', // 导航
name: 'guide-nav' + lang,
meta: { lang },
component: load(lang, 'nav'),
}],
}
let resourceRoute = {
path: `/${ lang }/resource`, // 资源
meta: { lang },
name: 'resource' + lang,
component: load(lang, 'resource'),
}
let indexRoute = {
path: `/${ lang }`, // 首页
meta: { lang },
name: 'home' + lang,
component: load(lang, 'index'),
}
return [guideRoute, resourceRoute, indexRoute]
}
langs.forEach(lang => {
route = route.concat(generateMiscRoutes(lang.lang))
})
let userLanguage = localStorage.getItem('ELEMENT_LANGUAGE') || window.navigator.language || Language.EN
let defaultPath = Language.EN
if (userLanguage.indexOf('zh-') !== -1) {
defaultPath = Language.CN
} else if (userLanguage.indexOf('es') !== -1) {
defaultPath = Language.ES
} else if (userLanguage.indexOf('fr') !== -1) {
defaultPath = Language.FR
} else if (userLanguage.indexOf('jp') !== -1) {
defaultPath = Language.JP
}
route = route.concat([{
path: '/',
redirect: { path: `/${defaultPath}` },
}, {
path: '/*',
redirect: { path: `/${defaultPath}` },
}])
export default route