diff --git a/public/js/text.js b/public/js/text.js index fc62744..f017bd4 100644 --- a/public/js/text.js +++ b/public/js/text.js @@ -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); +}