import * as Utils from '../services/Utils.js'; export const DialogService = { _adjustDialogForKeyboard(dialog) { const vv = window.visualViewport; if (!vv) return; const onResize = () => { const keyboardHeight = window.innerHeight - vv.height; const availableHeight = vv.height; const marginTop = Math.max(10, (availableHeight - dialog.offsetHeight) / 2); if (keyboardHeight > 100) { dialog.style.marginTop = marginTop + 'px'; dialog.style.marginBottom = 'auto'; if (dialog.classList.contains('dialog--textarea')) { dialog.style.maxHeight = (availableHeight - 20) + 'px'; } } else { dialog.style.marginTop = marginTop + 'px'; dialog.style.marginBottom = 'auto'; if (dialog.classList.contains('dialog--textarea')) { dialog.style.maxHeight = ''; } } }; vv.addEventListener('resize', onResize); dialog.addEventListener('close', () => { vv.removeEventListener('resize', onResize); dialog.style.marginTop = ''; dialog.style.marginBottom = ''; if (dialog.classList.contains('dialog--textarea')) { dialog.style.maxHeight = ''; } }, { once: true }); requestAnimationFrame(onResize); }, confirm(message, confirmText = "Confirm") { return new Promise((resolve) => { // Create dialog element const dialog = document.createElement("dialog"); dialog.className = "dialog"; dialog.innerHTML = `

${Utils.escapeHtml(message)}

`; document.body.appendChild(dialog); const cancelBtn = dialog.querySelector(".btn.btn-ghost"); const confirmBtn = dialog.querySelector(".btn.btn-outline"); const cleanup = () => { dialog.remove(); }; const handleCancel = () => { dialog.close(); cleanup(); resolve(false); }; const handleConfirm = () => { dialog.close(); cleanup(); resolve(true); }; cancelBtn.addEventListener("click", handleCancel); confirmBtn.addEventListener("click", handleConfirm); // Close on backdrop click dialog.addEventListener("click", (e) => { if (e.target === dialog) { handleCancel(); } }); // Close on Escape key (native dialog behavior) dialog.addEventListener("close", () => { cleanup(); resolve(false); }); // Show the dialog with animation dialog.showModal(); this._adjustDialogForKeyboard(dialog); }); }, prompt(message, defaultValue = "") { return new Promise((resolve) => { const dialog = document.createElement("dialog"); dialog.className = "dialog"; dialog.innerHTML = `

${Utils.escapeHtml(message)}

`; document.body.appendChild(dialog); const input = dialog.querySelector(".app-dialog-input"); const cancelBtn = dialog.querySelector(".btn.btn-ghost"); const confirmBtn = dialog.querySelector(".btn.btn-outline"); // Focus input after dialog is shown setTimeout(() => input.focus(), 0); const cleanup = () => { dialog.remove(); }; const handleCancel = () => { dialog.close(); cleanup(); resolve(null); }; const handleConfirm = () => { const value = input.value.trim(); dialog.close(); cleanup(); resolve(value || null); }; cancelBtn.addEventListener("click", handleCancel); confirmBtn.addEventListener("click", handleConfirm); input.addEventListener("keydown", (e) => { if (e.key === "Enter") { e.preventDefault(); e.stopPropagation(); handleConfirm(); } else if (e.key === "Escape") { e.preventDefault(); e.stopPropagation(); handleCancel(); } }); dialog.addEventListener("click", (e) => { if (e.target === dialog) { handleCancel(); } }); dialog.addEventListener("close", () => { cleanup(); resolve(null); }); dialog.showModal(); this._adjustDialogForKeyboard(dialog); }); }, promptTextarea(message, defaultValue = "", onChange = null) { return new Promise((resolve) => { const dialog = document.createElement("dialog"); dialog.className = "dialog dialog--textarea"; dialog.innerHTML = `

${Utils.escapeHtml(message)}

`; document.body.appendChild(dialog); const textarea = dialog.querySelector(".app-dialog-textarea"); const closeBtn = dialog.querySelector(".btn.btn-ghost"); let debounceTimer = null; let lastSavedValue = defaultValue; const doSave = (value) => { if (value === lastSavedValue) return; lastSavedValue = value; if (onChange) onChange(value); }; const debouncedSave = (value) => { clearTimeout(debounceTimer); debounceTimer = setTimeout(() => doSave(value), 300); }; const cleanup = () => { clearTimeout(debounceTimer); dialog.remove(); }; const handleClose = () => { // Flush any pending save const value = textarea.value.trim(); if (value && value !== lastSavedValue) { doSave(value); } dialog.close(); cleanup(); resolve(null); }; closeBtn.addEventListener("click", handleClose); textarea.addEventListener("input", () => { const value = textarea.value.trim(); if (!value) return; debouncedSave(value); }); textarea.addEventListener("keydown", (e) => { if (e.key === "Escape") { e.preventDefault(); e.stopPropagation(); handleClose(); } }); dialog.addEventListener("click", (e) => { if (e.target === dialog) { handleClose(); } }); dialog.addEventListener("close", () => { cleanup(); resolve(null); }); dialog.showModal(); setTimeout(() => { textarea.focus(); textarea.setSelectionRange(textarea.value.length, textarea.value.length); }, 0); this._adjustDialogForKeyboard(dialog); }); }, showActions(actions) { /** * Show a vertical action sheet dialog * @param {Array<{label: string, icon?: string, action: string, danger?: boolean}>} actions * @returns {Promise} The selected action key or null */ return new Promise((resolve) => { const dialog = document.createElement("dialog"); dialog.className = "dialog dialog--actions"; const rows = actions.map((a) => ` `).join(''); dialog.innerHTML = `
${rows}
`; document.body.appendChild(dialog); const cleanup = () => { dialog.remove(); }; const handleCancel = () => { dialog.close(); cleanup(); resolve(null); }; dialog.querySelectorAll('.dialog-action-row').forEach(btn => { btn.addEventListener('click', () => { dialog.close(); cleanup(); resolve(btn.dataset.action); }); }); dialog.querySelector('.dialog-action-cancel').addEventListener('click', handleCancel); dialog.addEventListener('click', (e) => { if (e.target === dialog) handleCancel(); }); dialog.addEventListener('close', () => { cleanup(); resolve(null); }); dialog.showModal(); this._adjustDialogForKeyboard(dialog); }); }, pickItem(items, options = {}) { /** * Show a scrollable item picker dialog * @param {Array<{id: string, title?: string, subtitle?: string}>} items * @param {Object} options * @param {string} [options.title] * @param {string} [options.emptyText] * @returns {Promise} Selected item or null */ return new Promise((resolve) => { const dialog = document.createElement("dialog"); dialog.className = "dialog dialog--picker"; const titleHtml = options.title ? `

${Utils.escapeHtml(options.title)}

` : ''; const emptyText = options.emptyText || 'No items available'; let listHtml; if (items.length === 0) { listHtml = `
${Utils.escapeHtml(emptyText)}
`; } else { listHtml = items.map(item => ` `).join(''); } dialog.innerHTML = `
${titleHtml}
${listHtml}
`; document.body.appendChild(dialog); const cleanup = () => { dialog.remove(); }; const handleCancel = () => { dialog.close(); cleanup(); resolve(null); }; dialog.querySelectorAll('.dialog-picker-item').forEach(btn => { btn.addEventListener('click', () => { const id = btn.dataset.id; const selected = items.find(i => i.id === id) || null; dialog.close(); cleanup(); resolve(selected); }); }); dialog.querySelector('.dialog-picker-cancel').addEventListener('click', handleCancel); dialog.addEventListener('click', (e) => { if (e.target === dialog) handleCancel(); }); dialog.addEventListener('close', () => { cleanup(); resolve(null); }); dialog.showModal(); this._adjustDialogForKeyboard(dialog); }); }, };