-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
index.ts
159 lines (135 loc) Β· 3.88 KB
/
index.ts
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import Vue, { VNodeData, PropType } from 'vue'
// Directives
import Ripple, { RippleOptions } from '../../directives/ripple'
// Utilities
import { getObjectValueByPath } from '../../util/helpers'
export default Vue.extend({
name: 'routable',
directives: {
Ripple,
},
props: {
activeClass: String,
append: Boolean,
disabled: Boolean,
exact: {
type: Boolean as PropType<boolean | undefined>,
default: undefined,
},
exactPath: Boolean,
exactActiveClass: String,
link: Boolean,
href: [String, Object],
to: [String, Object],
nuxt: Boolean,
replace: Boolean,
ripple: {
type: [Boolean, Object],
default: null,
},
tag: String,
target: String,
},
data: () => ({
isActive: false,
proxyClass: '',
}),
computed: {
classes (): object {
const classes: Record<string, boolean> = {}
if (this.to) return classes
if (this.activeClass) classes[this.activeClass] = this.isActive
if (this.proxyClass) classes[this.proxyClass] = this.isActive
return classes
},
computedRipple (): RippleOptions | boolean {
return this.ripple ?? (!this.disabled && this.isClickable)
},
isClickable (): boolean {
if (this.disabled) return false
return Boolean(
this.isLink ||
this.$listeners.click ||
this.$listeners['!click'] ||
this.$attrs.tabindex
)
},
isLink (): boolean {
return this.to || this.href || this.link
},
styles: () => ({}),
},
watch: {
$route: 'onRouteChange',
},
mounted () {
this.onRouteChange()
},
methods: {
generateRouteLink () {
let exact = this.exact
let tag
const data: VNodeData = {
attrs: {
tabindex: 'tabindex' in this.$attrs ? this.$attrs.tabindex : undefined,
},
class: this.classes,
style: this.styles,
props: {},
directives: [{
name: 'ripple',
value: this.computedRipple,
}],
[this.to ? 'nativeOn' : 'on']: {
...this.$listeners,
...('click' in this ? { click: (this as any).click } : undefined), // #14447
},
ref: 'link',
}
if (typeof this.exact === 'undefined') {
exact = this.to === '/' ||
(this.to === Object(this.to) && this.to.path === '/')
}
if (this.to) {
// Add a special activeClass hook
// for component level styles
let activeClass = this.activeClass
let exactActiveClass = this.exactActiveClass || activeClass
if (this.proxyClass) {
activeClass = `${activeClass} ${this.proxyClass}`.trim()
exactActiveClass = `${exactActiveClass} ${this.proxyClass}`.trim()
}
tag = this.nuxt ? 'nuxt-link' : 'router-link'
Object.assign(data.props, {
to: this.to,
exact,
exactPath: this.exactPath,
activeClass,
exactActiveClass,
append: this.append,
replace: this.replace,
})
} else {
tag = (this.href && 'a') || this.tag || 'div'
if (tag === 'a' && this.href) data.attrs!.href = this.href
}
if (this.target) data.attrs!.target = this.target
return { tag, data }
},
onRouteChange () {
if (!this.to || !this.$refs.link || !this.$route) return
const activeClass = `${this.activeClass || ''} ${this.proxyClass || ''}`.trim()
const exactActiveClass = `${this.exactActiveClass || ''} ${this.proxyClass || ''}`.trim() || activeClass
const path = '_vnode.data.class.' + (this.exact ? exactActiveClass : activeClass)
this.$nextTick(() => {
/* istanbul ignore else */
if (!getObjectValueByPath(this.$refs.link, path) === this.isActive) {
this.toggle()
}
})
},
toggle () {
this.isActive = !this.isActive
},
},
})