-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
93 lines (84 loc) · 2.46 KB
/
index.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
import Navigator from './components/Navigator'
export default function install(Vue, {routes}) {
let appRoot;
const start = Vue.prototype.$start
Vue.prototype.$start = function () {
appRoot = this
start.call(this)
}
Vue.component('Navigator', Navigator)
Object.keys(routes).map(path => {
routes[path].component.__path = path
// this is required to attach the path to vue-class-components. See #31
if(routes[path].component.options) {
routes[path].component.options.__path = path;
}
})
Vue.mixin({
mounted() {
// attach the current path if set to the root element
if (this.$options.__path) {
this.$el.setAttribute('__path', this.$options.__path)
}
},
})
Vue.prototype.$navigator = new Vue({
data: {
path: false,
paths: {},
defaultPaths: {},
},
computed: {
route() {
return this.routes('navigator')
},
routes() {
return id => routes[this.paths[id] || this.defaultPaths[id]]
},
},
methods: {
_resolveComponent(defaultPath, id) {
if (defaultPath) {
this.$set(this.defaultPaths, id, defaultPath)
}
if (this.routes(id)) {
return this.routes(id).component
}
return false
},
_updatePath(path, id = 'navigator') {
if (id === 'navigator') {
this.path = path
}
this.$set(this.paths, id, path)
},
navigate(to, options) {
const matchedRoute = routes[to]
if (!matchedRoute) {
if (TNS_ENV === 'development') {
throw new Error(`[Navigator] Navigating to a route that does not exist: ${to}`)
}
return false
}
options = Object.assign({frame: 'navigator'}, options)
return this.$navigateTo(matchedRoute.component, options)
.catch(err => console.log(`[Navigator] Failed to navigate: ${err}`))
},
back(options, ...args) {
options = Object.assign({frame: 'navigator'}, options)
return this.$navigateBack.call(this, options, ...args)
},
modal(to, options) {
return appRoot.$showModal({
render: h => h(Navigator, {
props: {
id: options.id || 'navigatorModal',
defaultRoute: to,
defaultRouteProps: options.props
}
})
}, options).catch(err => console.log(`[Navigator] Failed to show modal: ${err}`))
}
},
})
}