ant-design-vue/components/_util/vnode.js

104 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-01-17 16:12:53 +08:00
import cloneDeep from 'lodash.clonedeep'
2017-12-14 12:13:15 +08:00
export function cloneVNode (vnode, deep) {
2018-01-08 18:31:04 +08:00
const componentOptions = vnode.componentOptions
2018-01-18 18:58:36 +08:00
// if (componentOptions && componentOptions.listeners) {
// componentOptions.listeners = cloneDeep(componentOptions.listeners)
// }
// const data = vnode.data ? cloneDeep(vnode.data) : vnode.data
2017-12-14 12:13:15 +08:00
const cloned = new vnode.constructor(
vnode.tag,
2018-01-17 16:12:53 +08:00
vnode.data,
2017-12-14 12:13:15 +08:00
vnode.children,
vnode.text,
vnode.elm,
vnode.context,
2018-01-08 18:31:04 +08:00
componentOptions,
2017-12-14 12:13:15 +08:00
vnode.asyncFactory
)
cloned.ns = vnode.ns
cloned.isStatic = vnode.isStatic
cloned.key = vnode.key
cloned.isComment = vnode.isComment
2018-01-08 18:31:04 +08:00
cloned.fnContext = vnode.fnContext
cloned.fnOptions = vnode.fnOptions
cloned.fnScopeId = vnode.fnScopeId
2017-12-14 12:13:15 +08:00
cloned.isCloned = true
2018-01-08 18:31:04 +08:00
if (deep) {
if (vnode.children) {
cloned.children = cloneVNodes(vnode.children, true)
}
if (componentOptions && componentOptions.children) {
componentOptions.children = cloneVNodes(componentOptions.children, true)
}
2017-12-14 12:13:15 +08:00
}
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
}
2018-01-08 18:31:04 +08:00
export function cloneElement (n, nodeProps, clone) {
const node = clone ? cloneVNode(n, true) : n
2018-01-19 11:47:06 +08:00
const { props = {}, key, on = {}} = nodeProps
2017-12-25 18:08:36 +08:00
const data = node.data || {}
const { style = data.style,
class: cls = data.class,
attrs = data.attrs,
2018-01-09 14:21:15 +08:00
ref,
2017-12-25 18:08:36 +08:00
} = nodeProps
2018-01-17 19:15:18 +08:00
node.data = Object.assign({}, data, { style, attrs, class: cls })
2018-01-17 16:12:53 +08:00
if (node.componentOptions) {
node.componentOptions.propsData = node.componentOptions.propsData || {}
node.componentOptions.listeners = node.componentOptions.listeners || {}
node.componentOptions.propsData = { ...node.componentOptions.propsData, ...props }
node.componentOptions.listeners = { ...node.componentOptions.listeners, ...on }
} else {
node.data.on = { ...(node.data.on || {}), ...on }
}
2017-12-14 12:13:15 +08:00
if (key !== undefined) {
node.key = key
2018-01-03 18:30:12 +08:00
node.data.key = key
2017-12-14 12:13:15 +08:00
}
2018-01-09 14:21:15 +08:00
if (typeof ref === 'string') {
node.data.ref = ref
}
2017-12-14 12:13:15 +08:00
return node
}
2018-01-09 14:21:15 +08:00
export function getComponentName (opts) {
return opts && (opts.Ctor.options.name || opts.tag)
}
2018-01-11 18:53:51 +08:00
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)
}
2018-01-17 16:12:53 +08:00
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
}
2018-01-18 18:58:36 +08:00
return events
2018-01-17 16:12:53 +08:00
}