This repository has been archived by the owner on Nov 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
89 lines (75 loc) · 2.23 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
function install (Vue, options) {
function getMatchedRoutes (routes) {
// Convert to an array if Vue 1.x
if (parseFloat(Vue.version) < 2) {
routes = (Object.keys(routes)).filter(function (key) {
return !isNaN(key)
}).map(function (key) {
return routes[key]
})
}
return routes
}
// Add the $breadcrumbs property to the Vue instance
Object.defineProperty(Vue.prototype, '$breadcrumbs', {
get: function get () {
var crumbs = []
var matched = getMatchedRoutes(this.$route.matched)
matched.forEach(function (route) {
// Backwards compatibility
var hasBreadcrumb = (parseFloat(Vue.version) < 2)
? route.handler && route.handler.breadcrumb
: route.meta && route.meta.breadcrumb
if (hasBreadcrumb) {
crumbs.push(route)
}
})
return crumbs
}
})
var defaults = {
registerComponent: true,
methods: {
// Return the correct prop data
linkProp: function (crumb) {
// If it's a named route, we'll base the route
// off of that instead
if (crumb.name || (crumb.handler && crumb.handler.name)) {
return {
name: crumb.name || crumb.handler.name,
params: this.$route.params
}
}
return {
path: (crumb.handler && crumb.handler.fullPath)
? crumb.handler.fullPath
: crumb.path,
params: this.$route.params
}
}
},
filters: {
// Display the correct breadcrumb text
// depending on the Vue version
crumbText: function (crumb) {
return (parseFloat(Vue.version) < 2) ? crumb.handler.breadcrumb : crumb.meta.breadcrumb
}
},
template: '<nav class="breadcrumbs" v-if="$breadcrumbs.length"> ' +
'<ul> ' +
'<li v-for="crumb in $breadcrumbs"> ' +
'<router-link :to="linkProp(crumb)">{{ crumb | crumbText }}</router-link> ' +
'</li> ' +
'</ul> ' +
'</nav>'
}
options = Object.assign(defaults, options)
// Add a default breadcrumbs component
if (options.registerComponent) {
Vue.component('breadcrumbs', options)
}
}
export default {
install: install,
version: '0.3.1'
}