Nuxt/lib/app/components/nuxt-child.js

89 lines
2.0 KiB
JavaScript
Raw Normal View History

2016-12-16 16:45:47 +00:00
import Vue from 'vue'
const transitionsKeys = [
'name',
'mode',
2017-08-18 13:44:34 +00:00
'appear',
2016-12-16 16:45:47 +00:00
'css',
'type',
2017-03-24 02:00:35 +00:00
'duration',
2016-12-16 16:45:47 +00:00
'enterClass',
'leaveClass',
2017-08-18 13:44:34 +00:00
'appearClass',
2016-12-16 16:45:47 +00:00
'enterActiveClass',
2017-01-02 17:05:53 +00:00
'enterActiveClass',
'leaveActiveClass',
2017-08-18 13:44:34 +00:00
'appearActiveClass',
2017-01-02 17:05:53 +00:00
'enterToClass',
2017-08-18 13:44:34 +00:00
'leaveToClass',
'appearToClass'
2016-12-16 16:45:47 +00:00
]
const listenersKeys = [
'beforeEnter',
'enter',
'afterEnter',
'enterCancelled',
'beforeLeave',
'leave',
'afterLeave',
2017-08-18 13:44:34 +00:00
'leaveCancelled',
'beforeAppear',
'appear',
'afterAppear',
'appearCancelled'
2016-12-16 16:45:47 +00:00
]
export default {
name: 'nuxt-child',
functional: true,
render (h, { parent, data }) {
2017-09-08 10:42:00 +00:00
const nuxt = parent.$root.nuxt
const component = parent.$route.matched[0].components.default
const layoutUid = parent._uid
const layoutName = component.options ? component.options.layout : null
2017-09-09 11:12:07 +00:00
// If we're changing layout render the stored vnode
2017-09-08 10:42:00 +00:00
if (nuxt._layoutUid === layoutUid &&
2017-09-09 11:12:07 +00:00
nuxt._layoutName !== layoutName) return nuxt._childVnode
2017-09-08 10:42:00 +00:00
nuxt._layoutUid = layoutUid
nuxt._layoutName = layoutName
2016-12-16 16:45:47 +00:00
data.nuxtChild = true
const _parent = parent
2016-12-16 16:45:47 +00:00
const transitions = parent.$nuxt.nuxt.transitions
const defaultTransition = parent.$nuxt.nuxt.defaultTransition
let depth = 0
while (parent) {
if (parent.$vnode && parent.$vnode.data.nuxtChild) {
depth++
}
parent = parent.$parent
}
data.nuxtChildDepth = depth
const transition = transitions[depth] || defaultTransition
let transitionProps = {}
transitionsKeys.forEach((key) => {
if (typeof transition[key] !== 'undefined') {
transitionProps[key] = transition[key]
}
})
let listeners = {}
listenersKeys.forEach((key) => {
if (typeof transition[key] === 'function') {
listeners[key] = transition[key].bind(_parent)
2016-12-16 16:45:47 +00:00
}
})
2017-09-08 10:42:00 +00:00
2017-09-09 11:12:07 +00:00
nuxt._childVnode = h('transition', {
2016-12-16 16:45:47 +00:00
props: transitionProps,
on: listeners
}, [
h('router-view', data)
])
2017-09-08 10:42:00 +00:00
2017-09-09 11:12:07 +00:00
return nuxt._childVnode
2016-12-16 16:45:47 +00:00
}
}