Hkoradiya123
Initial commit
5463318
Raw History Blame Contribute Delete
8.1 kB
document.addEventListener('DOMContentLoaded', () => {
const btnProcessUpload = document.getElementById('btnProcessUpload');
const fileUpload = document.getElementById('fileUpload');
const extractionPreview = document.getElementById('extractionPreview');
const previewText = document.getElementById('previewText');
const configSection = document.getElementById('configSection');
const btnGenerate = document.getElementById('btnGenerate');
const genSpinner = document.getElementById('genSpinner');
const resultArea = document.getElementById('resultArea');
const paperPreview = document.getElementById('paperPreview');
const btnAddSection = document.getElementById('btnAddSection');
const patternLines = document.getElementById('patternLines');
const btnDownloadPdf = document.getElementById('btnDownloadPdf');
const step2Indicator = document.getElementById('step2Indicator');
const step3Indicator = document.getElementById('step3Indicator');
const fileNameDisplay = document.getElementById('fileNameDisplay');
const genIcon = document.getElementById('genIcon');
let extractedFullContent = "";
let lastGeneratedData = null;
// File name display update
fileUpload.addEventListener('change', (e) => {
if (e.target.files.length > 0) {
fileNameDisplay.textContent = e.target.files[0].name;
fileNameDisplay.classList.add('text-primary', 'fw-bold');
}
});
// 1. Handle File Upload and Extraction
btnProcessUpload.addEventListener('click', async () => {
if (!fileUpload.files[0]) {
alert('Please select a file first.');
return;
}
const formData = new FormData();
formData.append('file', fileUpload.files[0]);
btnProcessUpload.disabled = true;
btnProcessUpload.innerHTML = '<i class="fas fa-circle-notch fa-spin me-2"></i>Extracting...';
try {
const response = await fetch('/upload', {
method: 'POST',
body: formData
});
const data = await response.json();
if (data.error) {
alert(data.error);
} else {
extractedFullContent = data.content_preview;
previewText.textContent = data.content_preview + "...";
document.getElementById('extractedFileName').textContent = fileUpload.files[0].name;
extractionPreview.classList.remove('d-none');
configSection.classList.remove('d-none');
step2Indicator.classList.add('step-active');
window.currentFileId = data.file_id;
// Smooth scroll to config
setTimeout(() => configSection.scrollIntoView({ behavior: 'smooth', block: 'start' }), 300);
}
} catch (err) {
console.error(err);
alert('Upload failed.');
} finally {
btnProcessUpload.disabled = false;
btnProcessUpload.innerHTML = '<span>Content Extracted</span> <i class="fas fa-check ms-2"></i>';
btnProcessUpload.classList.replace('btn-primary-custom', 'btn-success');
}
});
// 2. Add More Sections
btnAddSection.addEventListener('click', () => {
const newLine = document.createElement('div');
newLine.className = 'row g-2 mb-2 pattern-line';
newLine.innerHTML = `
<div class="col-md-5">
<input type="text" class="form-control" placeholder="Section Name">
</div>
<div class="col-md-2">
<input type="number" class="form-control" placeholder="Count">
</div>
<div class="col-md-2">
<input type="number" class="form-control" placeholder="Marks">
</div>
<div class="col-md-3">
<select class="form-select">
<option value="MCQ">MCQ</option>
<option value="Short Answer">Short Answer</option>
<option value="Long Answer">Long Answer</option>
</select>
</div>
`;
patternLines.appendChild(newLine);
});
// 3. Generate Questions
btnGenerate.addEventListener('click', async () => {
const blooms = Array.from(document.querySelectorAll('.form-check-input:checked')).map(c => c.value);
const difficulty = document.getElementById('difficultyLevel').value;
const sections = Array.from(document.querySelectorAll('.pattern-line')).map(line => {
const inputs = line.querySelectorAll('input, select');
return {
name: inputs[0].value,
count: parseInt(inputs[1].value),
marks: parseInt(inputs[2].value),
type: inputs[3].value
};
});
const payload = {
content: extractedFullContent,
file_id: window.currentFileId,
blooms_levels: blooms,
difficulty: difficulty,
pattern: sections
};
btnGenerate.disabled = true;
genSpinner.classList.remove('d-none');
genIcon.classList.add('d-none');
try {
const response = await fetch('/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
const data = await response.json();
if (data.error) {
alert(data.error);
} else {
lastGeneratedData = data;
renderPreview(data);
resultArea.classList.remove('d-none');
step3Indicator.classList.add('step-active');
resultArea.scrollIntoView({ behavior: 'smooth' });
}
} catch (err) {
console.error(err);
alert('Generation failed.');
} finally {
btnGenerate.disabled = false;
genSpinner.classList.add('d-none');
genIcon.classList.remove('d-none');
}
});
// 4. Render Preview
function renderPreview(data) {
let html = `<h2 class="text-center mb-4">${data.title || 'Examination Paper'}</h2>`;
data.sections.forEach(section => {
html += `<h4 class="mt-4" style="border-bottom: 2px solid #333;">${section.section_name}</h4>`;
if (section.instructions) html += `<p class="fst-italic">${section.instructions}</p>`;
section.questions.forEach((q, idx) => {
html += `
<div class="mb-3">
<strong>Q${idx + 1}. ${q.question_text}</strong>
<span class="float-end">[${q.marks} Marks]</span>
<div class="text-muted small">Level: ${q.bloom_level}</div>
`;
if (q.type === 'MCQ' && q.options) {
html += '<div class="ms-4 mt-2">';
q.options.forEach((opt, i) => {
html += `<div>${String.fromCharCode(65 + i)}. ${opt}</div>`;
});
html += '</div>';
}
html += '</div>';
});
});
paperPreview.innerHTML = html;
}
// 5. Download PDF
btnDownloadPdf.addEventListener('click', async () => {
if (!lastGeneratedData) return;
// This is a simplified approach. In a real app, you would hit a /download endpoint.
const response = await fetch('/download', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(lastGeneratedData)
});
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `ExamPaper_${Date.now()}.pdf`;
document.body.appendChild(a);
a.click();
a.remove();
});
});