You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/_util/vnode.js

108 lines
2.9 KiB

7 years ago
import clonedeep from 'lodash.clonedeep'
7 years ago
// export function cloneVNode (vnode, deep) {
// const cloned = new vnode.constructor(
// vnode.tag,
// clonedeep(vnode.data),
// vnode.children,
// vnode.text,
// vnode.elm,
// vnode.context,
// clonedeep(vnode.componentOptions),
// vnode.asyncFactory
// )
// cloned.ns = vnode.ns
// cloned.isStatic = vnode.isStatic
// cloned.key = vnode.key
// cloned.isComment = vnode.isComment
// cloned.isCloned = true
// if (deep && vnode.children) {
// cloned.children = cloneVNodes(vnode.children, deep)
// }
// return cloned
// }
7 years ago
export function cloneVNode (vnode, deep) {
7 years ago
const componentOptions = vnode.componentOptions
// if (componentOptions) {
// componentOptions.propsData = componentOptions.propsData ? clonedeep(componentOptions.propsData) : componentOptions.propsData
// }
7 years ago
const cloned = new vnode.constructor(
vnode.tag,
7 years ago
clonedeep(vnode.data),
7 years ago
vnode.children,
vnode.text,
vnode.elm,
vnode.context,
7 years ago
componentOptions,
7 years ago
vnode.asyncFactory
)
cloned.ns = vnode.ns
cloned.isStatic = vnode.isStatic
cloned.key = vnode.key
cloned.isComment = vnode.isComment
7 years ago
cloned.fnContext = vnode.fnContext
cloned.fnOptions = vnode.fnOptions
cloned.fnScopeId = vnode.fnScopeId
7 years ago
cloned.isCloned = true
7 years ago
if (deep) {
if (vnode.children) {
cloned.children = cloneVNodes(vnode.children, true)
}
if (componentOptions && componentOptions.children) {
componentOptions.children = cloneVNodes(componentOptions.children, true)
}
7 years ago
}
return cloned
}
export function cloneVNodes (vnodes, deep) {
const len = vnodes.length
const res = new Array(len)
for (let i = 0; i < len; i++) {
res[i] = cloneVNode(vnodes[i], deep)
}
return res
}
7 years ago
export function cloneElement (n, nodeProps, clone) {
const node = clone ? cloneVNode(n, true) : n
7 years ago
const { props = {}, key, on = {}} = nodeProps
7 years ago
if (node.componentOptions) {
7 years ago
node.componentOptions.propsData = node.componentOptions.propsData || {}
7 years ago
node.componentOptions.listeners = node.componentOptions.listeners || {}
7 years ago
Object.assign(node.componentOptions.propsData, props)
7 years ago
Object.assign(node.componentOptions.listeners, on)
7 years ago
}
7 years ago
7 years ago
const data = node.data || {}
const { style = data.style,
class: cls = data.class,
attrs = data.attrs,
7 years ago
ref,
7 years ago
} = nodeProps
7 years ago
node.data = Object.assign(data, { style, attrs, class: cls, on: { ...(data.on || {}), ...on }})
7 years ago
if (key !== undefined) {
node.key = key
7 years ago
node.data.key = key
7 years ago
}
7 years ago
if (typeof ref === 'string') {
node.data.ref = ref
}
7 years ago
return node
}
7 years ago
export function getComponentName (opts) {
return opts && (opts.Ctor.options.name || opts.tag)
}
7 years ago
export function isValidElement (ele) {
return !!ele.tag
}
export function getClass (ele) {
return ele.data && (ele.data.class || ele.data.staticClass)
}
export function getStyle (ele) {
return ele.data && (ele.data.style || ele.data.staticStyle)
}