forked from 17thshard/roshar-map
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes.js
94 lines (79 loc) · 2.05 KB
/
routes.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
import Vue from 'vue'
import VueRouter from 'vue-router'
import { i18n, loadLanguageAsync } from '@/i18n'
import store from '@/store'
import Map from '@/components/map/Map.vue'
Vue.use(VueRouter)
const detailRoutes = [
{
name: 'events',
specialAction: event => store.commit('selectEvent', event)
},
{
name: 'locations',
specialAction: () => store.commit('unselectEvent')
},
{
name: 'characters',
specialAction: () => store.commit('unselectEvent')
},
{
name: 'misc',
specialAction: () => store.commit('unselectEvent')
}
]
const router = new VueRouter({
routes: [
{
name: 'root',
path: '/:locale',
component: Map,
children: detailRoutes.map(({ name, specialAction }) => ({
name,
path: `${name}/:id`,
meta: {
details: true
},
beforeEnter (to, from, next) {
const entry = store.state.mappings[name][to.params.id]
if (to.params.id === undefined || entry === undefined) {
next(false)
return
}
specialAction(entry)
next()
}
}))
}
]
})
router.beforeEach(async (to, from, next) => {
const locale = to.params.locale
if (locale === undefined) {
const userLocale = navigator.languages !== undefined ? navigator.languages[0] : navigator.language
next(`/${userLocale}`)
return
}
try {
await loadLanguageAsync(locale, locale)
} catch {
next({ name: to.name, params: { ...to.params, locale: 'en-US' }, replace: true })
return
}
next()
})
router.afterEach((to, from) => {
const oldLocale = from.params.locale
const newLocale = to.params.locale
if (oldLocale !== undefined && i18n.t('textureLocale', newLocale) !== i18n.t('textureLocale', oldLocale)) {
location.reload()
}
})
router.afterEach((to) => {
let pageName = i18n.t('name')
if (to.name !== 'root') {
pageName = i18n.t(`${to.name}.${to.params.id}.name`)
}
document.querySelector('title').innerHTML = i18n.t('title', { page: pageName })
})
export { router }