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

128 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-03-03 11:14:03 +00:00
import { filterEmpty, parseStyleText } from './props-util'
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-02-05 03:18:50 +00:00
const data = vnode.data
let listeners = {}
if (componentOptions && componentOptions.listeners) {
2018-02-06 07:21:13 +00:00
listeners = { ...componentOptions.listeners }
2018-02-05 03:18:50 +00:00
}
let on = {}
if (data && data.on) {
2018-02-06 07:21:13 +00:00
on = { ...data.on }
2018-02-05 03:18:50 +00:00
}
2018-01-18 10:58:36 +00:00
2017-12-14 04:13:15 +00:00
const cloned = new vnode.constructor(
vnode.tag,
2018-02-05 03:18:50 +00:00
data ? { ...data, on } : data,
2017-12-14 04:13:15 +00:00
vnode.children,
vnode.text,
vnode.elm,
vnode.context,
2018-02-05 03:18:50 +00:00
componentOptions ? { ...componentOptions, listeners } : 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-03-01 02:21:06 +00:00
export function cloneElement (n, nodeProps, deep) {
2018-01-29 10:57:20 +00:00
let ele = n
if (Array.isArray(n)) {
ele = filterEmpty(n)[0]
}
if (!ele) {
return null
}
2018-03-01 02:21:06 +00:00
const node = cloneVNode(ele, deep)
2018-02-09 10:42:19 +00:00
const { props = {}, key, on = {}, children } = nodeProps
2017-12-25 10:08:36 +00:00
const data = node.data || {}
2018-03-03 11:14:03 +00:00
let cls = {}
let style = {}
const {
2018-03-02 14:12:52 +00:00
attrs = {},
2018-01-09 06:21:15 +00:00
ref,
2018-03-02 14:12:52 +00:00
domProps = {},
2018-03-03 11:14:03 +00:00
style: tempStyle = {},
class: tempCls = {},
2018-03-13 02:19:00 +00:00
scopedSlots = {},
2017-12-25 10:08:36 +00:00
} = nodeProps
2018-03-03 11:14:03 +00:00
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') {
cls[data.class] = true
data.class.split(' ').forEach(c => { cls[c.trim()] = true })
} else {
cls = { ...data.class, ...cls }
}
if (typeof tempCls === 'string') {
tempCls.split(' ').forEach(c => { cls[c.trim()] = true })
} else {
cls = { ...cls, ...tempCls }
}
2018-03-02 14:12:52 +00:00
node.data = Object.assign({}, data, {
2018-03-03 11:14:03 +00:00
style,
2018-03-02 14:12:52 +00:00
attrs: { ...data.attrs, ...attrs },
2018-03-03 11:14:03 +00:00
class: cls,
2018-03-02 14:12:52 +00:00
domProps: { ...data.domProps, ...domProps },
2018-03-13 02:19:00 +00:00
scopedSlots: { ...data.scopedSlots, ...scopedSlots },
2018-03-02 14:12:52 +00:00
})
2018-03-03 11:14:03 +00:00
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 }
2018-02-09 10:42:19 +00:00
if (children) {
node.componentOptions.children = children
}
2018-01-25 09:40:46 +00:00
} 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-11 10:53:51 +00:00