1
1
import { keys } from '../../utils/object'
2
2
import { arrayIncludes , concat } from '../../utils/array'
3
+ import { isRouterLink , computeTag , computeRel , computeHref } from '../../utils/router'
3
4
import { mergeData } from 'vue-functional-data-merge'
4
5
5
6
/**
@@ -77,6 +78,7 @@ export function propsFactory() {
77
78
78
79
export const props = propsFactory ( )
79
80
81
+ // Return a fresh copy of BLink props, containing only the specifeid prop(s)
80
82
export function pickLinkProps ( propsToPick ) {
81
83
const freshLinkProps = propsFactory ( )
82
84
// Normalize everything to array.
@@ -91,6 +93,7 @@ export function pickLinkProps(propsToPick) {
91
93
} , { } )
92
94
}
93
95
96
+ // Return a fresh copy of BLink props, keeping all but the specified omitting prop(s)
94
97
export function omitLinkProps ( propsToOmit ) {
95
98
const freshLinkProps = propsFactory ( )
96
99
// Normalize everything to array.
@@ -105,90 +108,29 @@ export function omitLinkProps(propsToOmit) {
105
108
} , { } )
106
109
}
107
110
108
- export const computed = {
109
- linkProps ( ) {
110
- let linkProps = { }
111
- let propKeys = keys ( props )
112
-
113
- for ( let i = 0 ; i < propKeys . length ; i ++ ) {
114
- const prop = propKeys [ i ]
115
- // Computed Vue getters are bound to the instance.
116
- linkProps [ prop ] = this [ prop ]
117
- }
118
-
119
- return linkProps
120
- }
121
- }
122
-
123
- function computeTag ( props , parent ) {
124
- return parent . $router && props . to && ! props . disabled
125
- ? parent . $nuxt
126
- ? 'nuxt-link'
127
- : 'router-link'
128
- : 'a'
129
- }
130
-
131
- function isRouterLink ( tag ) {
132
- return tag !== 'a'
133
- }
134
-
135
- function computeHref ( { disabled, href, to } , tag ) {
136
- // We've already checked the parent.$router in computeTag,
137
- // so isRouterLink(tag) indicates a live router.
138
- // When deferring to Vue Router's router-link, don't use the href attr at all.
139
- // We return null, and then remove href from the attributes passed to router-link
140
- if ( isRouterLink ( tag ) ) {
141
- return null
142
- }
143
-
144
- // If href explicitly provided
145
- if ( href ) {
146
- return href
147
- }
148
-
149
- // Reconstruct `href` when `to` used, but no router
150
- if ( to ) {
151
- // Fallback to `to` prop (if `to` is a string)
152
- if ( typeof to === 'string' ) {
153
- return to
154
- }
155
- // Fallback to `to.path` prop (if `to` is an object)
156
- if ( typeof to === 'object' && typeof to . path === 'string' ) {
157
- return to . path
158
- }
159
- }
160
-
161
- // If nothing is provided use '#' as a fallback
162
- return '#'
163
- }
164
-
165
- function computeRel ( { target, rel } ) {
166
- if ( target === '_blank' && rel === null ) {
167
- return 'noopener'
168
- }
169
- return rel || null
170
- }
171
-
172
111
function clickHandlerFactory ( { disabled, tag, href, suppliedHandler, parent } ) {
173
- return function onClick ( e ) {
174
- if ( disabled && e instanceof Event ) {
112
+ return function onClick ( evt ) {
113
+ if ( disabled && evt instanceof Event ) {
175
114
// Stop event from bubbling up.
176
- e . stopPropagation ( )
115
+ evt . stopPropagation ( )
177
116
// Kill the event loop attached to this specific EventTarget.
178
- e . stopImmediatePropagation ( )
117
+ evt . stopImmediatePropagation ( )
179
118
} else {
180
- if ( isRouterLink ( tag ) && e . target . __vue__ ) {
181
- e . target . __vue__ . $emit ( 'click' , e )
119
+ if ( isRouterLink ( tag ) && evt . target . __vue__ ) {
120
+ // Router links do not emit instance 'click' events, so we
121
+ // add in an $emit('click', evt) on it's vue instance
122
+ evt . target . __vue__ . $emit ( 'click' , evt )
182
123
}
183
124
if ( typeof suppliedHandler === 'function' ) {
184
125
suppliedHandler ( ...arguments )
185
126
}
186
- parent . $root . $emit ( 'clicked::link' , e )
127
+ parent . $root . $emit ( 'clicked::link' , evt )
187
128
}
188
129
189
130
if ( ( ! isRouterLink ( tag ) && href === '#' ) || disabled ) {
190
- // Stop scroll-to-top behavior or navigation.
191
- e . preventDefault ( )
131
+ // Stop scroll-to-top behavior or navigation on regular links
132
+ // when href is just '#'
133
+ evt . preventDefault ( )
192
134
}
193
135
}
194
136
}
0 commit comments