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

107 lines
2.9 KiB

7 years ago
import cloneDeep from 'lodash.clonedeep'
7 years ago
export function cloneVNode (vnode, deep) {
7 years ago
const componentOptions = vnode.componentOptions
7 years ago
// if (componentOptions && componentOptions.listeners) {
// componentOptions.listeners = cloneDeep(componentOptions.listeners)
// }
// const data = vnode.data ? cloneDeep(vnode.data) : vnode.data
7 years ago
const cloned = new vnode.constructor(
vnode.tag,
7 years ago
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
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 })
7 years ago
if (node.componentOptions) {
node.componentOptions.propsData = node.componentOptions.propsData || {}
node.componentOptions.listeners = node.componentOptions.listeners || {}
node.componentOptions.propsData = { ...node.componentOptions.propsData, ...props }
7 years ago
node.componentOptions.listeners = { ...node.componentOptions.listeners, ...on }
} else {
node.data.on = { ...(node.data.on || {}), ...on }
7 years ago
}
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
}
7 years ago
export function isEmptyElement (ele) {
return !(ele.tag || ele.text.trim() !== '')
}
7 years ago
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)
}
7 years ago
export function filterEmpty (children = []) {
return children.filter(c => c.tag || c.text.trim() !== '')
}
export function getEvents (child) {
let events = {}
if (child.componentOptions && child.componentOptions.listeners) {
events = child.componentOptions.listeners
} else if (child.data && child.data.on) {
events = child.data.on
}
7 years ago
return events
7 years ago
}