123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- import navConfig from './nav.config';
- import langs from './i18n/route';
- const LOAD_MAP = {
- 'zh-CN': name => {
- return r => require.ensure([], () =>
- r(require(`./pages/zh-CN/${name}.vue`)),
- 'zh-CN');
- },
- 'en-US': name => {
- return r => require.ensure([], () =>
- r(require(`./pages/en-US/${name}.vue`)),
- 'en-US');
- },
- 'es': name => {
- return r => require.ensure([], () =>
- r(require(`./pages/es/${name}.vue`)),
- 'es');
- },
- 'fr-FR': name => {
- return r => require.ensure([], () =>
- r(require(`./pages/fr-FR/${name}.vue`)),
- 'fr-FR');
- }
- };
- const load = function(lang, path) {
- return LOAD_MAP[lang](path);
- };
- const LOAD_DOCS_MAP = {
- 'zh-CN': path => {
- return r => require.ensure([], () =>
- r(require(`./docs/zh-CN${path}.md`)),
- 'zh-CN');
- },
- 'en-US': path => {
- return r => require.ensure([], () =>
- r(require(`./docs/en-US${path}.md`)),
- 'en-US');
- },
- 'es': path => {
- return r => require.ensure([], () =>
- r(require(`./docs/es${path}.md`)),
- 'es');
- },
- 'fr-FR': path => {
- return r => require.ensure([], () =>
- r(require(`./docs/fr-FR${path}.md`)),
- 'fr-FR');
- }
- };
- 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 themeRoute = {
- path: `/${ lang }/theme`,
- component: load(lang, 'theme-nav'),
- children: [
- {
- path: '/', // 主题管理
- name: 'theme' + lang,
- meta: { lang },
- component: load(lang, 'theme')
- },
- {
- path: 'preview', // 主题预览编辑
- name: 'theme-preview-' + lang,
- meta: { lang },
- component: load(lang, 'theme-preview')
- }]
- };
- 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, themeRoute, indexRoute];
- };
- langs.forEach(lang => {
- route = route.concat(generateMiscRoutes(lang.lang));
- });
- route.push({
- path: '/play',
- name: 'play',
- component: require('./play/index.vue')
- });
- let userLanguage = localStorage.getItem('ELEMENT_LANGUAGE') || window.navigator.language || 'en-US';
- let defaultPath = '/en-US';
- if (userLanguage.indexOf('zh-') !== -1) {
- defaultPath = '/zh-CN';
- } else if (userLanguage.indexOf('es') !== -1) {
- defaultPath = '/es';
- } else if (userLanguage.indexOf('fr') !== -1) {
- defaultPath = '/fr-FR';
- }
- route = route.concat([{
- path: '/',
- redirect: defaultPath
- }, {
- path: '*',
- redirect: defaultPath
- }]);
- export default route;
|