/* Редактор теста: рабочий минимум. * Работает с эндпоинтами /api/tests//{draft, ai/generate-test, ai/generate-question} * и /api/tests/ (PATCH chainActive). * * Полная мобильная отполировка UX (4 аккордеона, fixed footer, drag-n-drop) * запланирована отдельным спринтом E1.7. */ (() => { 'use strict'; const root = document.getElementById('editor-root'); if (!root) return; const TEST_ID = root.dataset.testId; const initial = JSON.parse(root.dataset.initial); const $ = (sel, parent = document) => parent.querySelector(sel); const $$ = (sel, parent = document) => Array.from(parent.querySelectorAll(sel)); const titleEl = $('#test-title'); const descEl = $('#test-description'); const thresholdEl = $('#test-threshold'); const questionsEl = $('#questions'); const qCountEl = $('#q-count'); const saveStatusEl = $('#save-status'); const aiStatusEl = $('#ai-status'); const chainActiveEl = $('#chain-active'); const tplQ = $('#tpl-question'); const tplO = $('#tpl-option'); let chainActive = true; // ─── render ───────────────────────────────────────────────────────── function renderQuestion(q) { const node = tplQ.content.firstElementChild.cloneNode(true); node._q = { id: q.id || null }; $('.q-text', node).value = q.text || ''; $('.q-multi', node).checked = !!q.hasMultipleAnswers; const optsEl = $('.q-options', node); (q.options || []).forEach((o) => optsEl.appendChild(renderOption(o))); bindQuestionEvents(node); return node; } function renderOption(o) { const node = tplO.content.firstElementChild.cloneNode(true); $('.opt-text', node).value = o.text || ''; $('.opt-correct', node).checked = !!o.isCorrect; $('.opt-delete', node).addEventListener('click', () => { node.remove(); }); return node; } function bindQuestionEvents(node) { $('.q-delete', node).addEventListener('click', () => { if (!confirm('Удалить вопрос?')) return; node.remove(); renumber(); }); $('.q-up', node).addEventListener('click', () => { if (node.previousElementSibling) { node.parentNode.insertBefore(node, node.previousElementSibling); renumber(); } }); $('.q-down', node).addEventListener('click', () => { if (node.nextElementSibling) { node.parentNode.insertBefore(node.nextElementSibling, node); renumber(); } }); $('.q-add-option', node).addEventListener('click', () => { $('.q-options', node).appendChild(renderOption({ text: '', isCorrect: false })); }); $('.q-ai', node).addEventListener('click', () => aiGenerateQuestion(node)); } function renumber() { $$('#questions .q-item').forEach((li, i) => { $('.q-num', li).textContent = `Вопрос #${i + 1}`; }); qCountEl.textContent = $$('#questions .q-item').length; } function loadInitial() { titleEl.value = initial.test.title || ''; descEl.value = initial.test.description || ''; thresholdEl.value = initial.test.passingThreshold == null ? '' : Number(initial.test.passingThreshold); questionsEl.innerHTML = ''; (initial.questions || []).forEach((q) => questionsEl.appendChild(renderQuestion(q))); renumber(); } // ─── collect ─────────────────────────────────────────────────────── function collectPayload() { const questions = $$('#questions .q-item').map((li, i) => ({ text: $('.q-text', li).value.trim(), question_order: i + 1, hasMultipleAnswers: $('.q-multi', li).checked, options: $$('.opt-item', li).map((op, j) => ({ text: $('.opt-text', op).value.trim(), isCorrect: $('.opt-correct', op).checked, option_order: j + 1, })), })); const payload = { title: titleEl.value.trim() || null, description: descEl.value.trim() || null, questions, }; const t = thresholdEl.value; if (t !== '' && Number.isFinite(Number(t))) payload.passingThreshold = Number(t); return payload; } function collectShape() { return $$('#questions .q-item').map((li) => ({ optionsCount: Math.max(2, $$('.opt-item', li).length || 4), hasMultipleAnswers: $('.q-multi', li).checked, })); } // ─── actions ─────────────────────────────────────────────────────── $('#add-question').addEventListener('click', () => { questionsEl.appendChild( renderQuestion({ text: '', hasMultipleAnswers: false, options: [ { text: '', isCorrect: true }, { text: '', isCorrect: false }, { text: '', isCorrect: false }, { text: '', isCorrect: false }, ], }), ); renumber(); }); $('#save-draft').addEventListener('click', async () => { saveStatusEl.textContent = 'Сохраняем…'; try { const r = await fetch(`/api/tests/${TEST_ID}/draft`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(collectPayload()), }); const data = await r.json(); if (!r.ok) throw new Error(data.error || 'Не удалось сохранить.'); if (chainActiveEl.checked !== chainActive) { const r2 = await fetch(`/api/tests/${TEST_ID}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ chainActive: chainActiveEl.checked }), }); if (r2.ok) chainActive = chainActiveEl.checked; } saveStatusEl.textContent = data.forked ? 'Сохранено (создана новая версия — есть попытки прохождения).' : 'Сохранено.'; setTimeout(() => (saveStatusEl.textContent = ''), 4000); } catch (e) { saveStatusEl.textContent = ''; alert(e.message || 'Не удалось сохранить.'); } }); $('#ai-generate-test').addEventListener('click', async () => { const shape = collectShape(); if (!shape.length) { alert('Сначала добавьте хотя бы один вопрос (его настройки определяют сетку).'); return; } aiStatusEl.textContent = 'Генерируем…'; try { const r = await fetch(`/api/tests/${TEST_ID}/ai/generate-test`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ testTitle: titleEl.value, testDescription: descEl.value, shape, }), }); const data = await r.json(); if (!r.ok) throw new Error(data.error || 'AI: ошибка.'); const draft = data.draft; if (draft.title) titleEl.value = draft.title; if (draft.description) descEl.value = draft.description; questionsEl.innerHTML = ''; (draft.questions || []).forEach((q) => questionsEl.appendChild(renderQuestion(q))); renumber(); aiStatusEl.textContent = `Готово: ${draft.questions?.length || 0} вопросов.`; setTimeout(() => (aiStatusEl.textContent = ''), 4000); } catch (e) { aiStatusEl.textContent = ''; alert(e.message || 'AI: ошибка.'); } }); // ─── импорт документа (E1.3) ─────────────────────────────────── $('#ai-import-file').addEventListener('change', async (ev) => { const file = ev.target.files && ev.target.files[0]; ev.target.value = ''; if (!file) return; aiStatusEl.textContent = `Загружаем «${file.name}»…`; try { const fd = new FormData(); fd.append('file', file); const r = await fetch('/api/tests/import/document', { method: 'POST', body: fd }); const data = await r.json(); if (!r.ok) throw new Error(data.error || 'Не удалось импортировать.'); const g = data.generation || {}; if (!g.available) { aiStatusEl.textContent = ''; const msg = g.message || 'AI недоступен.'; const preview = (g.textPreview || data.extractedText || '').slice(0, 600); alert(msg + (preview ? '\n\nПервые 600 символов:\n' + preview : '')); return; } const ok = confirm( `${g.message}\n\nПрименить как новый черновик?\n` + `Текущие вопросы будут заменены.`, ); if (!ok) { aiStatusEl.textContent = ''; return; } const draft = g.draft; if (draft.title) titleEl.value = draft.title; if (draft.description) descEl.value = draft.description; questionsEl.innerHTML = ''; (draft.questions || []).forEach((q) => questionsEl.appendChild(renderQuestion(q))); renumber(); aiStatusEl.textContent = `Применено: ${draft.questions?.length || 0} вопросов.`; setTimeout(() => (aiStatusEl.textContent = ''), 4000); } catch (e) { aiStatusEl.textContent = ''; alert(e.message || 'Не удалось импортировать.'); } }); // ─── AI v2 (E1.8): generate-by-title / check / improve ───────── function aiAlert(data, fallback) { const msg = (data && data.error) || fallback || 'AI: ошибка.'; if (data && data.settingsUrl) { if (confirm(msg + '\n\nОткрыть Настройки?')) { window.location.href = data.settingsUrl; } return; } alert(msg); } function escHtml(s) { return String(s == null ? '' : s) .replace(/&/g, '&').replace(//g, '>') .replace(/"/g, '"').replace(/'/g, '''); } const modal = $('#ai-modal'); const modalTitle = $('#ai-modal-title'); const modalBody = $('#ai-modal-body'); const modalActions = $('#ai-modal-actions'); $('#ai-modal-close').addEventListener('click', () => modal.close()); function openModal(title, bodyHtml, actions) { modalTitle.textContent = title; modalBody.innerHTML = bodyHtml; modalActions.innerHTML = ''; (actions || []).forEach((a) => { const b = document.createElement('button'); b.textContent = a.label; b.className = a.className || 'px-3 py-2 rounded-lg bg-ink-100 hover:bg-ink-200 text-sm'; b.addEventListener('click', () => a.onClick(b)); modalActions.appendChild(b); }); modal.showModal(); } $('#ai-generate-by-title').addEventListener('click', async () => { const title = titleEl.value.trim(); if (!title) { alert('Сначала заполните название теста.'); titleEl.focus(); return; } const nQRaw = prompt('Сколько вопросов сгенерировать?', '10'); if (nQRaw == null) return; const nQ = Math.max(3, Math.min(40, parseInt(nQRaw, 10) || 10)); const nORaw = prompt('Сколько вариантов в каждом вопросе?', '4'); if (nORaw == null) return; const nO = Math.max(2, Math.min(12, parseInt(nORaw, 10) || 4)); aiStatusEl.textContent = 'Генерируем по названию…'; try { const r = await fetch(`/api/tests/${TEST_ID}/ai/generate-by-title`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ testTitle: title, testDescription: descEl.value, questionsCount: nQ, optionsCount: nO, }), }); const data = await r.json(); if (!r.ok) { aiStatusEl.textContent = ''; return aiAlert(data); } const draft = data.draft; const ok = confirm( `Готово: «${draft.title}», вопросов — ${draft.questions.length}.\n` + 'Применить как черновик? Текущие вопросы будут заменены.', ); if (!ok) { aiStatusEl.textContent = ''; return; } if (draft.title) titleEl.value = draft.title; if (draft.description) descEl.value = draft.description; questionsEl.innerHTML = ''; (draft.questions || []).forEach((q) => questionsEl.appendChild(renderQuestion(q))); renumber(); aiStatusEl.textContent = `Применено: ${draft.questions.length} вопросов.`; setTimeout(() => (aiStatusEl.textContent = ''), 4000); } catch (e) { aiStatusEl.textContent = ''; aiAlert(null, e.message); } }); $('#ai-check').addEventListener('click', async () => { const payload = collectPayload(); if (!payload.questions.length) { alert('В тесте нет вопросов — нечего проверять.'); return; } aiStatusEl.textContent = 'Анализируем…'; try { const r = await fetch(`/api/tests/${TEST_ID}/ai/check`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ testTitle: titleEl.value, testDescription: descEl.value, questions: payload.questions, }), }); const data = await r.json(); aiStatusEl.textContent = ''; if (!r.ok) return aiAlert(data); const rev = data.review || {}; const verdict = rev.verdict || 'warn'; const verdictMap = { ok: ['Годен', 'bg-green-50 text-green-800 border-green-200'], warn: ['Есть замечания', 'bg-yellow-50 text-yellow-800 border-yellow-200'], bad: ['Серьёзные проблемы', 'bg-red-50 text-red-800 border-red-200'], }; const [verdictText, verdictCls] = verdictMap[verdict] || verdictMap.warn; let html = `
${verdictText}
${escHtml(rev.summary || '')}
`; if (Array.isArray(rev.sections) && rev.sections.length) { html += rev.sections.map((s) => `
${escHtml(s.title)}
    ${s.items.map((it) => `
  • ${escHtml(it)}
  • `).join('')}
`).join(''); } else { html += '

Замечаний нет.

'; } openModal('Проверка теста', html, [ { label: 'Закрыть', onClick: () => modal.close(), className: 'px-3 py-2 rounded-lg bg-brand-600 hover:bg-brand-700 text-white text-sm' }, ]); } catch (e) { aiStatusEl.textContent = ''; aiAlert(null, e.message); } }); $('#ai-improve').addEventListener('click', async () => { const payload = collectPayload(); if (!payload.questions.length) { alert('В тесте нет вопросов — нечего улучшать.'); return; } aiStatusEl.textContent = 'Улучшаем…'; try { const r = await fetch(`/api/tests/${TEST_ID}/ai/improve`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ testTitle: titleEl.value, testDescription: descEl.value, questions: payload.questions, }), }); const data = await r.json(); aiStatusEl.textContent = ''; if (!r.ok) return aiAlert(data); const items = data.items || []; if (!items.length) { openModal('Улучшение теста', '

Нечего улучшать.

', [ { label: 'Закрыть', onClick: () => modal.close() }, ]); return; } const changed = items.filter((i) => i.changed); if (!changed.length) { openModal('Улучшение теста', '

AI не предложил изменений.

', [ { label: 'Закрыть', onClick: () => modal.close() }, ]); return; } let html = `

Отметьте вопросы, к которым нужно применить улучшения. ${changed.length} из ${items.length}.

`; html += changed.map((it) => `
Было
${escHtml(it.original.text)}
    ${it.original.options.map((o) => `
  • ${o.isCorrect ? '✓ ' : ''}${escHtml(o.text)}
  • `).join('')}
Стало
${escHtml(it.suggested.text)}
    ${it.suggested.options.map((o) => `
  • ${o.isCorrect ? '✓ ' : ''}${escHtml(o.text)}
  • `).join('')}
`).join(''); openModal('Улучшение теста', html, [ { label: 'Отмена', onClick: () => modal.close() }, { label: 'Применить выбранное', className: 'px-3 py-2 rounded-lg bg-brand-600 hover:bg-brand-700 text-white text-sm', onClick: () => { const qs = $$('#questions .q-item'); modalBody.querySelectorAll('[data-idx]').forEach((row) => { if (!$('.apply-q', row).checked) return; const idx = parseInt(row.dataset.idx, 10); const it = items.find((x) => x.index === idx); if (!it || !qs[idx]) return; const node = qs[idx]; $('.q-text', node).value = it.suggested.text; $('.q-multi', node).checked = !!it.suggested.hasMultipleAnswers; const optsEl = $('.q-options', node); optsEl.innerHTML = ''; it.suggested.options.forEach((o) => optsEl.appendChild(renderOption(o))); }); modal.close(); aiStatusEl.textContent = 'Изменения применены. Не забудьте сохранить.'; setTimeout(() => (aiStatusEl.textContent = ''), 5000); }, }, ]); } catch (e) { aiStatusEl.textContent = ''; aiAlert(null, e.message); } }); async function aiGenerateQuestion(node) { const qText = $('.q-text', node).value.trim(); const optsCount = Math.max(2, $$('.opt-item', node).length || 4); const multi = $('.q-multi', node).checked; aiStatusEl.textContent = 'AI: один вопрос…'; try { const r = await fetch(`/api/tests/${TEST_ID}/ai/generate-question`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ testTitle: titleEl.value, testDescription: descEl.value, questionText: qText, optionsCount: optsCount, hasMultipleAnswers: multi, }), }); const data = await r.json(); if (!r.ok) throw new Error(data.error || 'AI: ошибка.'); $('.q-text', node).value = data.text || ''; if (data.mode === 'full' && Array.isArray(data.options)) { const optsEl = $('.q-options', node); optsEl.innerHTML = ''; data.options.forEach((o) => optsEl.appendChild(renderOption(o))); $('.q-multi', node).checked = !!data.hasMultipleAnswers; } aiStatusEl.textContent = data.mode === 'full' ? 'AI: вопрос сгенерирован.' : 'AI: формулировка обновлена.'; setTimeout(() => (aiStatusEl.textContent = ''), 4000); } catch (e) { aiStatusEl.textContent = ''; alert(e.message || 'AI: ошибка.'); } } // ─── chain active state (грузим summary, чтобы знать стартовое значение) ─── fetch(`/api/tests/${TEST_ID}/summary`) .then((r) => r.json()) .then((data) => { if (data && data.test && typeof data.test.chainActive === 'boolean') { chainActive = data.test.chainActive; chainActiveEl.checked = chainActive; } else { chainActiveEl.checked = true; chainActive = true; } }) .catch(() => { chainActiveEl.checked = true; }); loadInitial(); })();