moy-dom
Version:
A flexiable Virtual DOM library for building modern web interface.
28 lines • 1.21 kB
JavaScript
/**
* [renderChildren render node's children for given changes]
* @param {[DOMElement]} node [description]
* @param {[Array]} changes [the given changes]
* @return {[undefined]} [undefined]
*/
export default function renderChildren(node, changes){
for(let {index, type, item, toIndex, fromIndex } of changes){
if(type === 0){ //remove
node.removeChild(node.childNodes[index])
}else if(type === 1){ // move
node.insertBefore(node.childNodes[toIndex], node.childNodes[fromIndex])
node.insertBefore(node.childNodes[fromIndex], node.childNodes[toIndex])
}else if(type === 2){ // insert
const insertNode = Object.prototype.toString.call(item) === '[object Element]' ?
item.render() :
document.createTextNode(item)
node.insertBefore(insertNode, node.childNodes[index])
}else if(type === 3){ // append
const insertNode = Object.prototype.toString.call(item) === '[object Element]' ?
item.render() :
document.createTextNode(item)
node.appendChild(insertNode)
}else{
throw new Error('Unknown reorder type ' + type)
}
}
}