better mapTransitions

prevents some unhandled cases
This commit is contained in:
Pooya Parsa 2017-06-13 00:06:05 +04:30
parent 0511e90a70
commit e753f93f97

View File

@ -14,21 +14,25 @@ let router
<%= (store ? 'let store' : '') %>
function mapTransitions(Components, to, from) {
const resolveTransitions = component => (typeof component.options.transition === 'function')
? component.options.transition(to, from)
: component.options.transition
const resolveRoute = route => resolveTransitions(route.matched[0].components.default)
function componentTransitions(component) {
if (!component || !component.options || !component.options.transition) {
return {}
}
if(typeof component.options.transition === 'function') {
return component.options.transition(to, from)
}
return component.options.transition
}
return Components.map((Component) => {
const transitions = Object.assign({}, to ? resolveRoute(to) : resolveTransitions(Component))
const from_transitions = from ? resolveRoute(from) : {}
// Combine transitions & prefer leave* transitions of from route
Object.keys(from_transitions).forEach(key=> {
if (from_transitions[key] && key.toLowerCase().indexOf('leave') !== -1) {
transitions[key] = from_transitions[key]
}
})
// Clone original object to prevent overrides
const transitions = Object.assign({}, componentTransitions(Component))
// Combine transitions & prefer *leave* transitions of 'from' route
if(from && from.matched.length && from.matched[0].components.default) {
const from_transitions = componentTransitions(from.matched[0].components.default)
Object.keys(from_transitions)
.filter(key => from_transitions[key] && key.toLowerCase().indexOf('leave') !== -1)
.forEach(key => { transitions[key] = from_transitions[key] })
}
return transitions
})
}