forked from meteor-vue/blaze-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router-link.js
74 lines (65 loc) · 2.12 KB
/
router-link.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
import './router-link.html';
// Top view is a Vue component instance.
function topView(view) {
while (view.originalParentView || view.parentView) {
view = view.originalParentView || view.parentView;
}
return view;
}
const routerLinkOptions = [
'to', 'replace', 'append', 'tag', 'active-class', 'exact', 'event', 'exact-active-class',
];
Template.RouterLink.helpers({
href() {
const vm = topView(Template.instance().view);
const current = vm.$route;
const args = Template.currentData();
const {href} = vm.$router.resolve(args.to, current, args.append);
return href;
},
attributes() {
const args = Template.currentData();
return Object.keys(args)
.filter(argName => routerLinkOptions.indexOf(argName) === -1)
.reduce((attrs, key) => {
return Object.assign(attrs, { [key]: args[key] });
}, {});
},
});
// Taken from vue-router's router-link component's code.
function guardEvent(event) {
// Don't redirect with control keys.
if (event.metaKey || event.altKey || event.ctrlKey || event.shiftKey) return;
// Don't redirect when preventDefault called.
if (event.defaultPrevented) return;
// Don't redirect on right click.
if (event.button !== undefined && event.button !== 0) return;
// Don't redirect if `target="_blank"`.
if (event.currentTarget && event.currentTarget.getAttribute) {
const target = event.currentTarget.getAttribute('target');
if (/\b_blank\b/i.test(target)) return;
}
// This may be a Weex event which doesn't have this method.
if (event.preventDefault) {
event.preventDefault();
}
return true;
}
Template.RouterLink.events({
'click a'(event) {
// Some other anchor's click?
if (Template.instance().firstNode !== event.currentTarget) return;
if (!guardEvent(event)) return;
const vm = topView(Template.instance().view);
const current = vm.$route;
const router = vm.$router;
const args = Template.currentData();
const {location} = vm.$router.resolve(args.to, current, args.append);
if (args.replace) {
router.replace(location);
}
else {
router.push(location);
}
}
});