// Modal logic for editing a todo cell in a popup // Create and show a modal with the Add/Edit Todo form, focusing the relevant field function showEditTodoModal(rowIdx, key) { // Find the todo const todo = window.todos[rowIdx]; if (!todo) return; // Build modal overlay let modal = document.createElement('div'); modal.className = 'modal'; modal.style.zIndex = 2000; modal.innerHTML = ` `; // Close modal on overlay click or close button function closeModal() { modal.remove(); document.body.style.overflow = ''; } modal.querySelector('.modal-close-btn').onclick = closeModal; modal.onclick = (e) => { if (e.target === modal) closeModal(); }; // Save changes modal.querySelector('#modal-todo-form').onsubmit = function(evt) { evt.preventDefault(); const formData = new FormData(this); const updated = {}; for (let [k, v] of formData.entries()) updated[k] = v; // Type conversions updated.urgent = parseInt(updated.urgent) || 1; updated.importance = parseInt(updated.importance) || 1; updated.prio = updated.urgent * updated.importance; updated.timeEstimation = parseInt(updated.timeEstimation) || 0; updated.actualTime = parseInt(updated.actualTime) || 0; window.todos[rowIdx] = { ...window.todos[rowIdx], ...updated }; window.localStorage.setItem('todos-v1', JSON.stringify(window.todos)); closeModal(); window.renderTodos && window.renderTodos(); }; // Insert and focus correct field document.body.appendChild(modal); document.body.style.overflow = 'hidden'; setTimeout(() => { let focusId = 'modal-' + key.replace(/([A-Z])/g, '-$1').toLowerCase(); let focusElem = modal.querySelector('#' + focusId); if (focusElem) focusElem.focus(); }, 80); } // Attach to window for use in app.js document.addEventListener('DOMContentLoaded', () => { window.showEditTodoModal = showEditTodoModal; });