-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
103 lines (89 loc) · 2.9 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const HIS_KEY = '$$vue_router_history'
const sessionStorage = window.sessionStorage
const DIRECTION = {
FORWARD: 'forward',
BACK: 'back'
}
function VueRouterTransition (Vue, VueRouter, {indexPath = '/'} = {}) {
const stackHistory = sessionStorage.getItem(HIS_KEY)
let stack = [indexPath]
if (stackHistory) {
stack = JSON.parse(stackHistory)
}
const _ = Vue.util
const routerViewDef = Vue.elementDirective('router-view')
const originalRouterViewDef = _.extend({}, routerViewDef)
const _onTransitionValidated = VueRouter.prototype._onTransitionValidated
const transitionDef =
// 0.12
Vue.directive('_transition') ||
// 1.0
Vue.internalDirectives.transition
// override vue-router and internal transition directive
_.extend(VueRouter.prototype, {
_onTransitionValidated (transition) {
const { from, to } = transition
const fromPath = from.path
const toPath = to.path
const fromStackIndex = stack.indexOf(fromPath)
const toStackIndex = stack.indexOf(toPath)
let direction
if (toStackIndex > -1) {
direction = toStackIndex > fromStackIndex ? DIRECTION.FORWARD : DIRECTION.BACK
} else {
direction = DIRECTION.FORWARD
stack.push(toPath)
sessionStorage.setItem(HIS_KEY, JSON.stringify(stack))
}
this.app.$$transitionInfo = {
direction,
routerTransition: to.$$routerTransition
}
_onTransitionValidated.apply(this, arguments)
}
})
_.extend(routerViewDef, {
transition (target, cb) {
const self = this
const { vm } = this
if (vm.$$routerTransition) {
const { $$transitionInfo } = vm.$root
const routerTransition = $$transitionInfo.routerTransition || vm.$$routerTransition
vm.$$transition = routerTransition[$$transitionInfo.direction]
Vue.nextTick(() => {
originalRouterViewDef.transition.call(self, target, cb)
})
} else {
originalRouterViewDef.transition.call(self, target, cb)
}
}
})
_.extend(transitionDef, {
bind () {
const { el, vm } = this
if (el.__r_transition__) {
/* istanbul ignore if */
if (vm._defineMeta) {
// 0.12
vm._defineMeta('$$transition', el.__r_transition__.forward)
} else {
// 1.0
_.defineReactive(vm, '$$transition', el.__r_transition__.forward)
}
this.literal = false
this.expression = '$$transition'
}
}
})
// create `r-transition` directive
Vue.directive('r-transition', {
priority: 1101,
update (value) {
this.el.__r_transition__ = this.vm.$$routerTransition = _.isPlainObject(value) && value || {
forward: DIRECTION.FORWARD,
back: DIRECTION.BACK
}
}
})
}
export default VueRouterTransition