Add renderTree function

This commit is contained in:
Peter Stuifzand 2020-10-31 20:56:45 +01:00
parent 5f375992b4
commit e147c3acd6

View File

@ -176,6 +176,38 @@ function editor(root, inputData, options) {
return el; return el;
} }
// TODO: build an actual tree of list items
function renderTree(rootElement, rootData) {
const el = (tag, children = []) => {
let elt = document.createElement(tag)
_.each(children, item => {
elt.appendChild(item)
})
return elt
}
/**
* @param {Item[]} items
* @returns {*}
*/
let buildTree = (items) => {
return _.map(items, (item) => {
return el("li", [
el("div", [document.createTextNode(item.text)]),
el("ul", buildTree(item.children))
])
})
}
let tree = rootData.tree();
$(rootElement).children().remove()
let $list = buildTree(tree)
let $ul = $('<ul>')
$ul.append($list)
$(rootElement).append($ul)
}
/** /**
* @param {Element} rootElement * @param {Element} rootElement
* @param rootData * @param rootData