Scientific Method Worksheet Generator
SCIENTIFIC METHOD WORKSHEET
Project | Researcher | Dates
Click "Generate Worksheet" to preview your structured plan.
${step.desc}
`; methodStepsArea.appendChild(container); }); }; // --- Tab 3: Configuration Management --- const renderConfigTable = () => { configTableBody.innerHTML = ''; methodSteps.forEach(step => { const row = document.createElement('tr'); row.innerHTML = `Please enter project details in the Builder tab.
'; return; } methodSteps.forEach((step, index) => { const container = document.createElement('div'); container.className = 'review-step'; container.innerHTML = `${index + 1}. ${step.title}
${content[step.id] || 'N/A (No content provided for this step.)'}
`;
reviewStepsContent.appendChild(container);
});
// Only switch tab if the button was explicitly pressed
if (document.activeElement === generateWorksheetBtn) {
switchTab(1);
}
};
const downloadPDF = () => {
const meta = getMeta();
const content = collectWorksheetContent();
const { jsPDF } = window.jspdf;
const doc = new jsPDF('p', 'pt', 'a4');
let currentY = 40;
const margin = 40;
const pageWidth = doc.internal.pageSize.width;
const checkPageBreak = (spaceNeeded) => {
if (currentY + spaceNeeded > doc.internal.pageSize.height - margin) {
doc.addPage();
currentY = margin;
}
};
// Helper to add text and manage Y position
const addText = (text, size = 11, style = 'normal', color = [0, 0, 0], indent = 0) => {
doc.setFontSize(size);
doc.setFont('Helvetica', style);
doc.setTextColor(color[0], color[1], color[2]);
const lines = doc.splitTextToSize(text, pageWidth - margin * 2 - indent);
checkPageBreak(lines.length * (size * 1.2));
doc.text(lines, margin + indent, currentY);
currentY += (lines.length * (size * 1.2));
doc.setTextColor(0); // Reset color
};
// --- PDF Header ---
doc.setFontSize(20);
doc.setFont('Helvetica', 'bold');
doc.setTextColor(0, 123, 255);
doc.text(`SCIENTIFIC METHOD WORKSHEET`, pageWidth / 2, currentY, { align: 'center' });
currentY += 10;
doc.setFontSize(14);
doc.text(meta.projectName.toUpperCase(), pageWidth / 2, currentY, { align: 'center' });
currentY += 15;
doc.setFontSize(10);
doc.setFont('Helvetica', 'normal');
doc.setTextColor(108, 117, 125);
doc.text(`Researcher: ${meta.researcherName} | Dates: ${formatDate(meta.dateStart)} - ${formatDate(meta.dateEnd)}`, pageWidth / 2, currentY, { align: 'center' });
currentY += 30;
doc.setTextColor(0);
// --- Steps Content ---
methodSteps.forEach((step, index) => {
checkPageBreak(40); // Space for title and initial content
// Step Title
doc.setFontSize(14);
doc.setFont('Helvetica', 'bold');
doc.setTextColor(23, 162, 184); /* Info color */
doc.text(`${index + 1}. ${step.title}`, margin, currentY);
currentY += 10;
// Content
doc.setDrawColor(222, 226, 230); // Border color
doc.setLineWidth(1);
const stepContent = content[step.id] || 'N/A (No content provided for this step.)';
// Estimate height of content block for box drawing
const lines = doc.splitTextToSize(stepContent, maxWidth - 10); // Content is indented 10
const contentHeight = (lines.length * 10) + 10;
// Draw box (simulated background)
doc.setFillColor(241, 241, 241);
doc.rect(margin, currentY, maxWidth, contentHeight, 'F');
doc.setFontSize(11);
doc.setFont('Helvetica', 'normal');
doc.setTextColor(51);
doc.text(lines, margin + 5, currentY + 7);
currentY += contentHeight + 15; // Move down after content block
});
doc.save(`${meta.projectName.replace(/\s/g, '_')}_Worksheet.pdf`);
}
// --- Tab Navigation ---
const switchTab = (tabIndex) => {
tabs.forEach((tab, index) => {
tab.classList.toggle('active', index === tabIndex);
contents[index].classList.toggle('active', index === tabIndex);
});
currentTab = tabIndex;
updateNavButtons();
if (tabIndex === 1) { // Review tab
generateWorksheet();
}
}
const updateNavButtons = () => {
prevBtn.disabled = currentTab === 0;
nextBtn.disabled = currentTab === tabs.length - 1;
}
// Attach listeners to tab buttons
tabs.forEach((tab, index) => {
tab.addEventListener('click', () => {
// Find actual index based on position in DOM
const tabNode = tab.closest('.worksheet-tab-button');
const newIndex = Array.from(tabNode.parentNode.children).indexOf(tabNode);
switchTab(newIndex);
});
});
nextBtn.addEventListener('click', () => { if (currentTab < tabs.length - 1) switchTab(currentTab + 1); });
prevBtn.addEventListener('click', () => { if (currentTab > 0) switchTab(currentTab - 1); });
// --- Event Listeners ---
generateWorksheetBtn.addEventListener('click', () => {
// Run generation and switch to review tab
generateWorksheet();
switchTab(1);
});
pdfDownloadBtn.addEventListener('click', downloadPDF);
// FIX: Ensure metaForm is defined before attaching listener
if (metaForm) {
metaForm.addEventListener('input', generateWorksheet); // Update preview on meta change
}
// --- Initial Setup ---
const today = new Date();
const yyyy = today.getFullYear();
const mm = String(today.getMonth() + 1).padStart(2, '0');
const dd = String(today.getDate()).padStart(2, '0');
dateStartInput.value = `${yyyy}-${mm}-${dd}`;
renderConfigTable();
renderBuilderSteps();
updateNavButtons();
generateWorksheet();
});
