Skip to content

Commit 1cc3377

Browse files
committed
Implement v-link as delegate directive
1 parent 93e7a60 commit 1cc3377

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/directives/link.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,26 @@ export default function (Vue) {
2626
// don't redirect when preventDefault called
2727
if (e.defaultPrevented) return
2828

29-
if (e.button === 0) {
29+
if (e.button !== 0) return
30+
31+
if (this.el.tagName === 'A') {
32+
// v-link on <a v-link="'path'">
3033
e.preventDefault()
3134
if (this.destination != null) {
3235
router.go(this.destination)
3336
}
37+
} else {
38+
// v-link delegate on <div v-link>
39+
var el = e.target
40+
while (el && el.tagName !== 'A' && el !== this.el) {
41+
el = el.parentNode
42+
}
43+
if (!el || el.tagName !== 'A' || !el.href) return
44+
45+
if (sameOrigin(el)) {
46+
e.preventDefault()
47+
router.go(el.href)
48+
}
3449
}
3550
}
3651
this.el.addEventListener('click', this.handler)
@@ -88,4 +103,10 @@ export default function (Vue) {
88103
this.unwatch && this.unwatch()
89104
}
90105
})
106+
107+
function sameOrigin (link) {
108+
return link.protocol === location.protocol &&
109+
link.hostname === location.hostname &&
110+
link.port === location.port
111+
}
91112
}

0 commit comments

Comments
 (0)