route.config.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import navConfig from './nav.config';
  2. import langs from './i18n/route';
  3. const LOAD_MAP = {
  4. 'zh-CN': name => {
  5. return r => require.ensure([], () =>
  6. r(require(`./pages/zh-CN/${name}.vue`)),
  7. 'zh-CN');
  8. },
  9. 'en-US': name => {
  10. return r => require.ensure([], () =>
  11. r(require(`./pages/en-US/${name}.vue`)),
  12. 'en-US');
  13. },
  14. 'es': name => {
  15. return r => require.ensure([], () =>
  16. r(require(`./pages/es/${name}.vue`)),
  17. 'es');
  18. },
  19. 'fr-FR': name => {
  20. return r => require.ensure([], () =>
  21. r(require(`./pages/fr-FR/${name}.vue`)),
  22. 'fr-FR');
  23. }
  24. };
  25. const load = function(lang, path) {
  26. return LOAD_MAP[lang](path);
  27. };
  28. const LOAD_DOCS_MAP = {
  29. 'zh-CN': path => {
  30. return r => require.ensure([], () =>
  31. r(require(`./docs/zh-CN${path}.md`)),
  32. 'zh-CN');
  33. },
  34. 'en-US': path => {
  35. return r => require.ensure([], () =>
  36. r(require(`./docs/en-US${path}.md`)),
  37. 'en-US');
  38. },
  39. 'es': path => {
  40. return r => require.ensure([], () =>
  41. r(require(`./docs/es${path}.md`)),
  42. 'es');
  43. },
  44. 'fr-FR': path => {
  45. return r => require.ensure([], () =>
  46. r(require(`./docs/fr-FR${path}.md`)),
  47. 'fr-FR');
  48. }
  49. };
  50. const loadDocs = function(lang, path) {
  51. return LOAD_DOCS_MAP[lang](path);
  52. };
  53. const registerRoute = (navConfig) => {
  54. let route = [];
  55. Object.keys(navConfig).forEach((lang, index) => {
  56. let navs = navConfig[lang];
  57. route.push({
  58. path: `/${ lang }/component`,
  59. redirect: `/${ lang }/component/installation`,
  60. component: load(lang, 'component'),
  61. children: []
  62. });
  63. navs.forEach(nav => {
  64. if (nav.href) return;
  65. if (nav.groups) {
  66. nav.groups.forEach(group => {
  67. group.list.forEach(nav => {
  68. addRoute(nav, lang, index);
  69. });
  70. });
  71. } else if (nav.children) {
  72. nav.children.forEach(nav => {
  73. addRoute(nav, lang, index);
  74. });
  75. } else {
  76. addRoute(nav, lang, index);
  77. }
  78. });
  79. });
  80. function addRoute(page, lang, index) {
  81. const component = page.path === '/changelog'
  82. ? load(lang, 'changelog')
  83. : loadDocs(lang, page.path);
  84. let child = {
  85. path: page.path.slice(1),
  86. meta: {
  87. title: page.title || page.name,
  88. description: page.description,
  89. lang
  90. },
  91. name: 'component-' + lang + (page.title || page.name),
  92. component: component.default || component
  93. };
  94. route[index].children.push(child);
  95. }
  96. return route;
  97. };
  98. let route = registerRoute(navConfig);
  99. const generateMiscRoutes = function(lang) {
  100. let guideRoute = {
  101. path: `/${ lang }/guide`, // 指南
  102. redirect: `/${ lang }/guide/design`,
  103. component: load(lang, 'guide'),
  104. children: [{
  105. path: 'design', // 设计原则
  106. name: 'guide-design' + lang,
  107. meta: { lang },
  108. component: load(lang, 'design')
  109. }, {
  110. path: 'nav', // 导航
  111. name: 'guide-nav' + lang,
  112. meta: { lang },
  113. component: load(lang, 'nav')
  114. }]
  115. };
  116. let themeRoute = {
  117. path: `/${ lang }/theme`,
  118. component: load(lang, 'theme-nav'),
  119. children: [
  120. {
  121. path: '/', // 主题管理
  122. name: 'theme' + lang,
  123. meta: { lang },
  124. component: load(lang, 'theme')
  125. },
  126. {
  127. path: 'preview', // 主题预览编辑
  128. name: 'theme-preview-' + lang,
  129. meta: { lang },
  130. component: load(lang, 'theme-preview')
  131. }]
  132. };
  133. let resourceRoute = {
  134. path: `/${ lang }/resource`, // 资源
  135. meta: { lang },
  136. name: 'resource' + lang,
  137. component: load(lang, 'resource')
  138. };
  139. let indexRoute = {
  140. path: `/${ lang }`, // 首页
  141. meta: { lang },
  142. name: 'home' + lang,
  143. component: load(lang, 'index')
  144. };
  145. return [guideRoute, resourceRoute, themeRoute, indexRoute];
  146. };
  147. langs.forEach(lang => {
  148. route = route.concat(generateMiscRoutes(lang.lang));
  149. });
  150. route.push({
  151. path: '/play',
  152. name: 'play',
  153. component: require('./play/index.vue')
  154. });
  155. let userLanguage = localStorage.getItem('ELEMENT_LANGUAGE') || window.navigator.language || 'en-US';
  156. let defaultPath = '/en-US';
  157. if (userLanguage.indexOf('zh-') !== -1) {
  158. defaultPath = '/zh-CN';
  159. } else if (userLanguage.indexOf('es') !== -1) {
  160. defaultPath = '/es';
  161. } else if (userLanguage.indexOf('fr') !== -1) {
  162. defaultPath = '/fr-FR';
  163. }
  164. route = route.concat([{
  165. path: '/',
  166. redirect: defaultPath
  167. }, {
  168. path: '*',
  169. redirect: defaultPath
  170. }]);
  171. export default route;