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,16 +8,23 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||||
h1.textContent = text.name;
|
h1.textContent = text.name;
|
||||||
document.getElementById('text-detail').appendChild(h1);
|
document.getElementById('text-detail').appendChild(h1);
|
||||||
|
|
||||||
return fetch('/api/nodes/' + textId );
|
return fetchAndRenderNodes(textId);
|
||||||
})
|
|
||||||
.then(res => res.json())
|
|
||||||
.then(nodes => {
|
|
||||||
const tree = buildTree(nodes);
|
|
||||||
const ul = renderTree(tree);
|
|
||||||
document.getElementById('text-detail').appendChild(ul);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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, textId);
|
||||||
|
document.getElementById('text-detail').appendChild(ul);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function buildTree(nodes) {
|
function buildTree(nodes) {
|
||||||
const map = {};
|
const map = {};
|
||||||
nodes.forEach(node => {
|
nodes.forEach(node => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue