add js for bulk creating nodes

This commit is contained in:
Yisroel Baum 2026-04-18 23:06:04 +03:00
parent d9c9b4439e
commit 9ed42654a3
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -58,6 +58,12 @@ function renderTree(nodes, textId) {
addBtn.addEventListener('click', () => toggleAddForm(li, node.id, textId));
li.appendChild(addBtn);
const bulkBtn = document.createElement('button');
bulkBtn.textContent = 'Bulk add children';
bulkBtn.className = 'bulk-add-children';
bulkBtn.addEventListener('click', () => toggleBulkAddForm(li, node.id, textId));
li.appendChild(bulkBtn);
if (node.children.length > 0) {
li.appendChild(renderTree(node.children, textId));
}
@ -102,3 +108,48 @@ function toggleAddForm(li, parentNodeId, textId) {
li.appendChild(input);
li.appendChild(saveBtn);
}
function toggleBulkAddForm(li, parentNodeId, textId) {
const existing = li.querySelector('input.bulk-title');
if (existing) {
existing.remove();
li.querySelector('input.bulk-count').remove();
li.querySelector('button.save-bulk').remove();
return;
}
const titleInput = document.createElement('input');
titleInput.type = 'text';
titleInput.className = 'bulk-title';
titleInput.placeholder = 'Title prefix';
const countInput = document.createElement('input');
countInput.type = 'number';
countInput.className = 'bulk-count';
countInput.placeholder = 'Count';
countInput.min = '1';
const saveBtn = document.createElement('button');
saveBtn.textContent = 'Save';
saveBtn.className = 'save-bulk';
saveBtn.addEventListener('click', () => {
const titlePrefix = titleInput.value.trim();
const count = parseInt(countInput.value);
if (!titlePrefix || !count || count < 1) return;
fetch('/api/nodes/bulk', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ textId: parseInt(textId), parentNodeId, titlePrefix, count }),
})
.then(res => {
if (!res.ok) throw new Error('Failed to bulk create nodes');
return res.json();
})
.then(() => fetchAndRenderNodes(textId));
});
li.appendChild(titleInput);
li.appendChild(countInput);
li.appendChild(saveBtn);
}