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

128 lines
3.3 KiB

7 years ago
import { filterEmpty, parseStyleText } from './props-util'
7 years ago
export function cloneVNode (vnode, deep) {
7 years ago
const componentOptions = vnode.componentOptions
7 years ago
const data = vnode.data
let listeners = {}
if (componentOptions && componentOptions.listeners) {
7 years ago
listeners = { ...componentOptions.listeners }
7 years ago
}
let on = {}
if (data && data.on) {
7 years ago
on = { ...data.on }
7 years ago
}
7 years ago
7 years ago
const cloned = new vnode.constructor(
vnode.tag,
7 years ago
data ? { ...data, on } : data,
7 years ago
vnode.children,
vnode.text,
vnode.elm,
vnode.context,
7 years ago
componentOptions ? { ...componentOptions, listeners } : 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
}
export function cloneElement (n, nodeProps = {}, deep) {
7 years ago
let ele = n
if (Array.isArray(n)) {
ele = filterEmpty(n)[0]
}
if (!ele) {
return null
}
7 years ago
const node = cloneVNode(ele, deep)
const { props = {}, key, on = {}, children, directives = [] } = nodeProps
7 years ago
const data = node.data || {}
7 years ago
let cls = {}
let style = {}
const {
7 years ago
attrs = {},
7 years ago
ref,
7 years ago
domProps = {},
7 years ago
style: tempStyle = {},
class: tempCls = {},
7 years ago
scopedSlots = {},
7 years ago
} = nodeProps
7 years ago
if (typeof data.style === 'string') {
style = parseStyleText(data.style)
} else {
style = { ...data.style, ...style }
}
if (typeof tempStyle === 'string') {
style = { ...style, ...parseStyleText(style) }
} else {
style = { ...style, ...tempStyle }
}
if (typeof data.class === 'string' && data.class.trim() !== '') {
7 years ago
data.class.split(' ').forEach(c => { cls[c.trim()] = true })
} else {
cls = { ...data.class, ...cls }
}
if (typeof tempCls === 'string' && tempCls.trim() !== '') {
7 years ago
tempCls.split(' ').forEach(c => { cls[c.trim()] = true })
} else {
cls = { ...cls, ...tempCls }
}
7 years ago
node.data = Object.assign({}, data, {
7 years ago
style,
7 years ago
attrs: { ...data.attrs, ...attrs },
7 years ago
class: cls,
7 years ago
domProps: { ...data.domProps, ...domProps },
7 years ago
scopedSlots: { ...data.scopedSlots, ...scopedSlots },
directives: [...(data.directives || []), ...directives],
7 years ago
})
7 years ago
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 }
7 years ago
if (children) {
node.componentOptions.children = children
}
7 years ago
} 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