/* modals.js — управление модальными окнами Help и Invoke */ // makeDraggable — делает .panel внутри modalId перетаскиваемым за заголовок (h3). function makeDraggable(modalId) { var modal = document.getElementById(modalId); if (!modal) return; var panel = modal.querySelector('.panel'); if (!panel) return; var handle = panel.querySelector('h3'); if (!handle) return; handle.style.cursor = 'move'; handle.style.userSelect = 'none'; var startX, startY, startLeft, startTop; handle.addEventListener('mousedown', function(e) { e.preventDefault(); var rect = panel.getBoundingClientRect(); // Переводим в position:fixed если ещё не if (!panel.style.left) { panel.style.position = 'fixed'; panel.style.left = rect.left + 'px'; panel.style.top = rect.top + 'px'; panel.style.margin = '0'; } startX = e.clientX; startY = e.clientY; startLeft = parseInt(panel.style.left, 10) || rect.left; startTop = parseInt(panel.style.top, 10) || rect.top; function onMove(e) { var dx = e.clientX - startX; var dy = e.clientY - startY; panel.style.left = (startLeft + dx) + 'px'; panel.style.top = (startTop + dy) + 'px'; } function onUp() { document.removeEventListener('mousemove', onMove); document.removeEventListener('mouseup', onUp); } document.addEventListener('mousemove', onMove); document.addEventListener('mouseup', onUp); }); } // Инициализация после загрузки DOM document.addEventListener('DOMContentLoaded', function() { ['create-code-modal', 'create-archive-modal', 'edit-modal', 'help-modal', 'invoke-modal'].forEach(makeDraggable); }); // showModalError — показывает ошибку внутри модалки. // errorDivId — id элемента с классом modal-error. function showModalError(errorDivId, message) { var el = document.getElementById(errorDivId); if (!el) return; el.textContent = message; el.style.display = 'block'; } function hideModalError(errorDivId) { var el = document.getElementById(errorDivId); if (el) { el.style.display = 'none'; el.textContent = ''; } } function openHelp() { document.getElementById('help-modal').classList.add('open'); } function closeHelp() { document.getElementById('help-modal').classList.remove('open'); } function openInvoke(name) { S.currentInvoke = name; document.getElementById('i-title').textContent = 'Вызов: ' + name; document.getElementById('i-resp').value = ''; document.getElementById('i-status').textContent = ''; document.getElementById('i-meta').style.display = 'none'; document.getElementById('i-meta').textContent = ''; document.getElementById('invoke-modal').classList.add('open'); } function closeInvoke() { document.getElementById('invoke-modal').classList.remove('open'); S.currentInvoke = null; } async function submitInvoke() { if (!S.currentInvoke) return; const btn = document.getElementById('i-submit'); const statusEl = document.getElementById('i-status'); const metaEl = document.getElementById('i-meta'); const respEl = document.getElementById('i-resp'); btn.disabled = true; respEl.value = ''; metaEl.style.display = 'none'; metaEl.textContent = ''; let elapsed = 0; statusEl.textContent = 'Вызов...'; const timer = setInterval(() => { elapsed++; if (elapsed < 5) { statusEl.textContent = 'Вызов... ' + elapsed + 'с'; } else if (elapsed < 10) { statusEl.textContent = '⏳ Возможен cold start — прогрев пула... ' + elapsed + 'с'; } else { statusEl.textContent = '⏳ Возможны cold start или specialization... ' + elapsed + 'с'; } }, 1000); try { const raw = document.getElementById('i-body').value.trim(); let parsed = {}; if (raw) parsed = JSON.parse(raw); const result = await requestJSON(API_BASE + '/functions/' + encodeURIComponent(S.currentInvoke) + '/invoke', 'POST', parsed); var latencyMs = Number(result.latency_ms || 0); var measuredSeconds = latencyMs > 0 ? Math.max(1, Math.round(latencyMs / 1000)) : elapsed; statusEl.textContent = '✓ Выполнено за ' + formatSeconds(measuredSeconds); metaEl.style.display = 'block'; metaEl.textContent = [ 'HTTP status: ' + (result.status || 'n/a'), 'Latency: ' + (latencyMs || 'n/a') + ' ms', 'Причина задержки: ' + explainDelay(measuredSeconds) ].join('\n'); respEl.value = JSON.stringify(result, null, 2); } catch (e) { statusEl.textContent = '✗ Ошибка после ' + formatSeconds(elapsed); metaEl.style.display = 'block'; metaEl.textContent = [ 'Последняя ошибка: ' + e.message, 'Вероятная причина задержки: ' + explainDelay(elapsed) ].join('\n'); respEl.value = 'Ошибка вызова: ' + e.message; } finally { clearInterval(timer); btn.disabled = false; } }