move fetch nodes by text id into its own function
re renders if theres a root list of nodes existing
This commit is contained in:
parent
628c633823
commit
f277ae7983
1 changed files with 14 additions and 7 deletions
|
|
@ -8,15 +8,22 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||
h1.textContent = text.name;
|
||||
document.getElementById('text-detail').appendChild(h1);
|
||||
|
||||
return fetch('/api/nodes/' + textId );
|
||||
})
|
||||
return fetchAndRenderNodes(textId);
|
||||
});
|
||||
});
|
||||
|
||||
function fetchAndRenderNodes(textId) {
|
||||
return fetch('/api/nodes/' + textId)
|
||||
.then(res => res.json())
|
||||
.then(nodes => {
|
||||
const existing = document.querySelector('#text-detail > ul');
|
||||
if (existing) existing.remove();
|
||||
|
||||
const tree = buildTree(nodes);
|
||||
const ul = renderTree(tree);
|
||||
const ul = renderTree(tree, textId);
|
||||
document.getElementById('text-detail').appendChild(ul);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function buildTree(nodes) {
|
||||
const map = {};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue