Add renderTree function
This commit is contained in:
parent
5f375992b4
commit
e147c3acd6
|
@ -176,6 +176,38 @@ function editor(root, inputData, options) {
|
|||
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 rootData
|
||||
|
|
Loading…
Reference in New Issue
Block a user