|
| 1 | +import { android, AndroidApplication } from 'application' |
| 2 | + |
1 | 3 | export default {
|
2 | 4 | install(Vue) {
|
3 | 5 | const patchGo = router => {
|
4 |
| - if (!router.go.__patched__) { |
5 |
| - if (router.history.index === -1) { |
6 |
| - // Todo: make sure this is indeed required |
7 |
| - // The problem: When using router.replace() to set the initial route |
8 |
| - // the history index stays -1, which then causes an issue when visiting a route, |
9 |
| - // going back, and then trying to visit again (the active route is not changed on nav back) |
10 |
| - // This fixes it, since it allows the router.go logic to run |
11 |
| - router.history.index = 0 |
12 |
| - } |
| 6 | + if (router.go.__patched__) { |
| 7 | + return |
| 8 | + } |
| 9 | + |
| 10 | + if (router.history.index === -1) { |
| 11 | + // Todo: make sure this is indeed required |
| 12 | + // The problem: When using router.replace() to set the initial route |
| 13 | + // the history index stays -1, which then causes an issue when visiting a route, |
| 14 | + // going back, and then trying to visit again (the active route is not changed on nav back) |
| 15 | + // This fixes it, since it allows the router.go logic to run |
| 16 | + router.history.index = 0 |
| 17 | + } |
13 | 18 |
|
14 |
| - const go = router.go |
| 19 | + const go = router.go |
15 | 20 |
|
16 |
| - router.go = n => { |
17 |
| - if (n < 0) { |
18 |
| - for (let i = n; i < 0; ++i) { |
19 |
| - // Todo: figure out a cleaner way to navigate back n frames |
20 |
| - Vue.navigateBack() |
21 |
| - } |
| 21 | + router.go = n => { |
| 22 | + if (router.isPageRouter && n < 0) { |
| 23 | + for (let i = n; i < 0; ++i) { |
| 24 | + // Todo: figure out a cleaner way to navigate back n frames |
| 25 | + Vue.navigateBack() |
22 | 26 | }
|
23 |
| - |
24 |
| - go.call(router, n) |
25 | 27 | }
|
26 | 28 |
|
27 |
| - router.go.__patched__ = true |
| 29 | + go.call(router, n)<
10000
/div> |
| 30 | + } |
| 31 | + |
| 32 | + router.go.__patched__ = true |
| 33 | + } |
| 34 | + |
| 35 | + const registerGuards = router => { |
| 36 | + if (router.__patched__) { |
| 37 | + return |
28 | 38 | }
|
| 39 | + |
| 40 | + router.afterEach(() => { |
| 41 | + if (!router.isPageRouter) { |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + const component = router.getMatchedComponents()[0] |
| 46 | + |
| 47 | + Vue.navigateTo(component, { |
| 48 | + context: { router } |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + router.__patched__ = true |
29 | 53 | }
|
30 | 54 |
|
31 | 55 | Vue.mixin({
|
32 | 56 | beforeCreate() {
|
33 |
| - if (this.$options.router) { |
34 |
| - this.__is_router__ = true |
35 |
| - const router = this.$options.router |
36 |
| - const self = this |
37 |
| - |
38 |
| - patchGo(router) |
39 |
| - |
40 |
| - this.$start = () => { |
41 |
| - this.__is_root__ = true |
42 |
| - this.__started__ = true |
43 |
| - |
44 |
| - const initial = router.history.current.matched[0].components.default |
45 |
| - this.$navigateTo(initial, { |
46 |
| - context: { router }, |
47 |
| - clearHistory: true |
48 |
| - }).then(() => { |
49 |
| - // Mount the root component (Should be <router-page>) to re
57AE
gister router hooks |
50 |
| - const placeholder = Vue.$document.createComment('placeholder') |
51 |
| - self.$mount(placeholder) |
52 |
| - }) |
53 |
| - } |
| 57 | + if (!this.$options.router) { |
| 58 | + // If there is no router, we don't care |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + const router = this.$options.router |
| 63 | + const self = this |
| 64 | + |
| 65 | + // Handle back button on android |
| 66 | + if (android && !android.__patched__) { |
| 67 | + android.on(AndroidApplication.activityBackPressedEvent, e => { |
| 68 | + if (router.history.index !== 0) { |
| 69 | + e.cancel = true |
| 70 | + } |
| 71 | + router.back() |
| 72 | + }) |
| 73 | + |
| 74 | + android.__patched__ = true |
| 75 | + } |
| 76 | + |
| 77 | + if (!router.hasOwnProperty('isPageRouter')) { |
| 78 | + Object.defineProperty(router, 'isPageRouter', { |
| 79 | + get() { |
| 80 | + return self.$options.pageRouting |
| 81 | + } |
| 82 | + }) |
| 83 | + } |
| 84 | + |
| 85 | + patchGo(router) |
| 86 | + registerGuards(router) |
| 87 | + |
| 88 | + // Overwrite the default $start function |
| 89 | + this.$start = () => { |
| 90 | + this.__is_root__ = true |
| 91 | + this.__started__ = true // skips the default start procedure |
| 92 | + |
| 93 | + // Mount the root component |
| 94 | + const placeholder = Vue.$document.createComment('placeholder') |
| 95 | + self.$mount(placeholder) |
| 96 | + |
| 97 | + const initial = router.getMatchedComponents()[0] |
| 98 | + |
| 99 | + this.$navigateTo(initial, { |
| 100 | + context: { router }, |
| 101 | + clearHistory: true |
| 102 | + }) |
54 | 103 | }
|
55 | 104 | }
|
56 | 105 | })
|
|
0 commit comments