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

118 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-01-17 08:12:53 +00:00
import cloneDeep from 'lodash.clonedeep'
2017-12-14 04:13:15 +00:00
export function cloneVNode (vnode, deep) {
2018-01-08 10:31:04 +00:00
const componentOptions = vnode.componentOptions
2018-01-18 10:58:36 +00:00
// if (componentOptions && componentOptions.listeners) {
// componentOptions.listeners = cloneDeep(componentOptions.listeners)
// }
// const data = vnode.data ? cloneDeep(vnode.data) : vnode.data
2017-12-14 04:13:15 +00:00
const cloned = new vnode.constructor(
vnode.tag,
2018-01-17 08:12:53 +00:00
vnode.data,
2017-12-14 04:13:15 +00:00
vnode.children,
vnode.text,
vnode.elm,
vnode.context,
2018-01-08 10:31:04 +00:00
componentOptions,
2017-12-14 04:13:15 +00:00
vnode.asyncFactory
)
cloned.ns = vnode.ns
cloned.isStatic = vnode.isStatic
cloned.key = vnode.key
cloned.isComment = vnode.isComment
2018-01-08 10:31:04 +00:00
cloned.fnContext = vnode.fnContext
cloned.fnOptions = vnode.fnOptions
cloned.fnScopeId = vnode.fnScopeId
2017-12-14 04:13:15 +00:00
cloned.isCloned = true
2018-01-08 10:31:04 +00: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 04:13:15 +00: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 10:31:04 +00:00
export function cloneElement (n, nodeProps, clone) {
2018-01-29 10:57:20 +00:00
let ele = n
if (Array.isArray(n)) {
ele = filterEmpty(n)[0]
}
if (!ele) {
return null
}
const node = clone ? cloneVNode(ele, true) : ele
2018-01-25 09:40:46 +00:00
const { props = {}, key, on = {}} = nodeProps
2017-12-25 10:08:36 +00:00
const data = node.data || {}
const { style = data.style,
class: cls = data.class,
attrs = data.attrs,
2018-01-09 06:21:15 +00:00
ref,
2017-12-25 10:08:36 +00:00
} = nodeProps
2018-01-17 11:15:18 +00:00
node.data = Object.assign({}, data, { style, attrs, class: cls })
2018-01-17 08:12:53 +00:00
if (node.componentOptions) {
node.componentOptions.propsData = node.componentOptions.propsData || {}
node.componentOptions.listeners = node.componentOptions.listeners || {}
node.componentOptions.propsData = { ...node.componentOptions.propsData, ...props }
2018-01-25 09:40:46 +00:00
node.componentOptions.listeners = { ...node.componentOptions.listeners, ...on }
} else {
node.data.on = { ...(node.data.on || {}), ...on }
2018-01-17 08:12:53 +00:00
}
2017-12-14 04:13:15 +00:00
if (key !== undefined) {
node.key = key
2018-01-03 10:30:12 +00:00
node.data.key = key
2017-12-14 04:13:15 +00:00
}
2018-01-09 06:21:15 +00:00
if (typeof ref === 'string') {
node.data.ref = ref
}
2017-12-14 04:13:15 +00:00
return node
}
2018-01-09 06:21:15 +00:00
export function getComponentName (opts) {
return opts && (opts.Ctor.options.name || opts.tag)
}
2018-01-11 10:53:51 +00:00
export function isValidElement (ele) {
return !!ele.tag
}
2018-01-21 03:52:34 +00:00
export function isEmptyElement (ele) {
return !(ele.tag || ele.text.trim() !== '')
}
2018-01-11 10:53:51 +00:00
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 08:12:53 +00:00
export function filterEmpty (children = []) {
return children.filter(c => c.tag || c.text.trim() !== '')
}
2018-01-29 10:57:20 +00:00
export function getPropsData (ele) {
return ele.componentOptions && ele.componentOptions.propsData
}
2018-01-17 08:12:53 +00:00
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-29 10:57:20 +00:00
return { ...events }
2018-01-17 08:12:53 +00:00
}