nervjs
Version:
A react-like framework based on virtual-dom
54 lines (51 loc) • 1.51 kB
JavaScript
import { isString, isFunction } from '~'
import { isWidget, isVNode, isHook } from './types'
class VNode {
constructor (tagName, props, children, key, namespace, owner) {
this.tagName = tagName || 'DIV'
this.props = props || {}
this.children = children || []
this.key = key || null
this.namespace = (isString(namespace)) ? namespace : null
this._owner = owner
let count = this.children.length || 0
let descendants = 0
let hasWidgets = false
let descendantHooks = false
let hooks
for (let propName in props) {
if (props.hasOwnProperty(propName)) {
let property = props[propName]
if (isHook(property) && property.unhook) {
if (!hooks) {
hooks = {}
}
hooks[propName] = property
}
}
}
if (count) {
this.children.forEach((child) => {
if (isVNode(child)) {
descendants += child.count || 0
if (!hasWidgets && child.hasWidgets) {
hasWidgets = true
}
if (!descendantHooks && (child.hooks || child.descendantHooks)) {
descendantHooks = true
}
} else if (!hasWidgets && isWidget(child)) {
if (isFunction(child.destroy)) {
hasWidgets = true
}
}
})
}
this.count = count + descendants
this.hasWidgets = hasWidgets
this.hooks = hooks
this.descendantHooks = descendantHooks
}
type = 'VirtualNode'
}
export default VNode