|
| 1 | +/* eslint-disable */ |
| 2 | +import { warn } from '../lib/util' |
| 3 | + |
| 4 | +function getRealChild (vnode: ?VNode): ?VNode { |
| 5 | + const compOptions: ?VNodeComponentOptions = vnode && vnode.componentOptions |
| 6 | + if (compOptions && compOptions.Ctor.options.abstract) { |
| 7 | + return getRealChild(getFirstComponentChild(compOptions.children)) |
| 8 | + } else { |
| 9 | + return vnode |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +function getFirstComponentChild (children: ?Array<VNode>): ?VNode { |
| 14 | + if (Array.isArray(children)) { |
| 15 | + for (let i = 0; i < children.length; i++) { |
| 16 | + const c = children[i] |
| 17 | + if (c && (c.componentOptions || isAsyncPlaceholder(c))) { |
| 18 | + return c |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function isAsyncPlaceholder (node: VNode): boolean { |
| 25 | + return node.isComment && node.asyncFactory |
| 26 | +} |
| 27 | +const camelizeRE = /-(\w)/g |
| 28 | +export const camelize = (str: string): string => { |
| 29 | + return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '') |
| 30 | +} |
| 31 | + |
| 32 | +function extractTransitionData (comp: Component): Object { |
| 33 | + const data = {} |
| 34 | + const options: ComponentOptions = comp.$options |
| 35 | + // props |
| 36 | + for (const key in options.propsData) { |
| 37 | + data[key] = comp[key] |
| 38 | + } |
| 39 | + // events. |
| 40 | + // extract listeners and pass them directly to the transition methods |
| 41 | + const listeners: ?Object = options._parentListeners |
| 42 | + for (const key in listeners) { |
| 43 | + data[camelize(key)] = listeners[key] |
| 44 | + } |
| 45 | + return data |
| 46 | +} |
| 47 | + |
| 48 | +function hasParentTransition (vnode: VNode): ?boolean { |
| 49 | + while ((vnode = vnode.parent)) { |
| 50 | + if (vnode.data.transition) { |
| 51 | + return true |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +export default { |
| 57 | + render (h: Function) { |
| 58 | + let children: ?Array<VNode> = this.$options._renderChildren |
| 59 | + if (!children) { |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + // filter out text nodes (possible whitespaces) |
| 64 | + children = children.filter((c: VNode) => c.tag || isAsyncPlaceholder(c)) |
| 65 | + /* istanbul ignore if */ |
| 66 | + if (!children.length) { |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + // warn multiple elements |
| 71 | + if (children.length > 1) { |
| 72 | + warn( |
| 73 | + '<transition> can only be used on a single element. Use ' + |
| 74 | + '<transition-group> for lists.', |
| 75 | + this.$parent |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + const mode: string = this.mode |
| 80 | + |
| 81 | + // warn invalid mode |
| 82 | + if (mode && mode !== 'in-out' && mode !== 'out-in' |
| 83 | + ) { |
| 84 | + warn( |
| 85 | + 'invalid <transition> mode: ' + mode, |
| 86 | + this.$parent |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + const rawChild: VNode = children[0] |
| 91 | + |
| 92 | + // if this is a component root node and the component's |
| 93 | + // parent container node also has transition, skip. |
| 94 | + if (hasParentTransition(this.$vnode)) { |
| 95 | + return rawChild |
| 96 | + } |
| 97 | + |
| 98 | + // apply transition data to child |
| 99 | + // use getRealChild() to ignore abstract components e.g. keep-alive |
| 100 | + const child: ?VNode = getRealChild(rawChild) |
| 101 | + |
| 102 | + if (!child) { |
| 103 | + return rawChild |
| 104 | + } |
| 105 | + |
| 106 | + (child.data || (child.data = {})).transition = extractTransitionData(this) |
| 107 | + |
| 108 | + // mark v-show |
| 109 | + // so that the transition module can hand over the control to the directive |
| 110 | + if (child.data.directives && child.data.directives.some(d => d.name === 'show')) { |
| 111 | + child.data.show = true |
| 112 | + } |
| 113 | + |
| 114 | + return rawChild |
| 115 | + } |
| 116 | +} |
0 commit comments