${rcfel_escapeHTML(item.status)}
`;
}).join('');
sectionDiv.innerHTML = `
${rcfel_escapeHTML(cat.name)}
${itemsHTML}
`;
rcfel_dashboardOutput.appendChild(sectionDiv);
});
rcfel_addDashboardListeners();
}
/**
* Updates the status for a specific item in the rcfel_data object
*/
function rcfel_updateElementStatus(id, newStatus) {
const elementIndex = rcfel_data.elements.findIndex(item => item.id === id);
if (elementIndex !== -1) {
rcfel_data.elements[elementIndex].status = newStatus;
rcfel_renderDashboard(); // Re-render to update badges
}
}
function rcfel_addDashboardListeners() {
rcfel_dashboardOutput.querySelectorAll('.rcfel-dashboard-status').forEach(select => {
select.addEventListener('change', (e) => {
const id = parseInt(e.target.closest('.w-48').getAttribute('data-id'), 10);
const newStatus = e.target.value;
rcfel_updateElementStatus(id, newStatus);
});
});
}
/**
* Renders a clone for PDF generation
*/
function rcfel_renderPdfClone() {
rcfel_pdfRenderClone.innerHTML = `
Research Consent Form Checklist
Final status of required and optional elements.
`;
// Render data into the clone's content area
const contentArea = rcfel_pdfRenderClone.querySelector('.space-y-6');
rcfel_renderDashboard(contentArea); // Render full list to the clone
}
/**
* Generates and downloads a PDF of the checklist
*/
async function rcfel_downloadPDF() {
if (rcfel_data.elements.length === 0) {
alert("The checklist is empty. Please define elements before downloading.");
return;
}
if (typeof jspdf === 'undefined' || typeof html2canvas === 'undefined') {
alert("Error: PDF libraries failed to load.");
return;
}
rcfel_renderPdfClone();
const { jsPDF } = window.jspdf;
try {
const canvas = await html2canvas(rcfel_pdfRenderClone, { scale: 1.5, useCORS: true });
const imgData = canvas.toDataURL('image/png');
const imgProps = { width: canvas.width, height: canvas.height };
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const margin = 40;
const contentWidth = pdfWidth - (margin * 2);
const contentHeight = (contentWidth * imgProps.height) / imgProps.width;
let heightLeft = contentHeight;
let position = 0;
pdf.addImage(imgData, 'PNG', margin, position + margin, contentWidth, contentHeight);
heightLeft -= (pdfHeight - margin * 2);
while (heightLeft > 0) {
position -= (pdfHeight - margin * 2);
pdf.addPage();
pdf.addImage(imgData, 'PNG', margin, position + margin, contentWidth, contentHeight);
heightLeft -= (pdfHeight - margin * 2);
}
pdf.save('Consent_Form_Checklist.pdf');
} catch (error) {
console.error("PDF generation failed:", error);
alert("An error occurred while generating the PDF.");
}
}
// --- EVENT LISTENERS ---
// Tab link clicks
rcfel_tabLinks.forEach((link, index) => {
link.addEventListener('click', () => rcfel_switchTab(index));
});
// Next/Prev button clicks
if (rcfel_prevButton) {
rcfel_prevButton.addEventListener('click', () => {
if (rcfel_currentTab > 0) rcfel_switchTab(rcfel_currentTab - 1);
});
}
if (rcfel_nextButton) {
rcfel_nextButton.addEventListener('click', () => {
if (rcfel_currentTab === rcfel_tabLinks.length - 1) {
rcfel_updateDataFromConfig();
rcfel_switchTab(0);
} else {
if (rcfel_currentTab < rcfel_tabLinks.length - 1) rcfel_switchTab(rcfel_currentTab + 1);
}
});
}
// PDF download
if (rcfel_downloadPdfButton) {
rcfel_downloadPdfButton.addEventListener('click', rcfel_downloadPDF);
}
// --- Config Tab Listeners ---
if (rcfel_addElementButton) {
rcfel_addElementButton.addEventListener('click', () => {
rcfel_elementsContainer.appendChild(rcfel_createElementInput({ id: rcfel_elementIdCounter++, name: '', category: CATEGORY_OPTIONS[0].name, required: 'Optional', status: DEFAULT_STATUS }));
});
}
if (rcfel_configTab) {
// Handle remove
rcfel_configTab.addEventListener('click', (e) => {
const removeButton = e.target.closest('.rcfel-remove-item');
if (removeButton) {
const entryDiv = removeButton.closest('.border[data-id]');
const idToRemove = parseInt(entryDiv.getAttribute('data-id'));
rcfel_data.elements = rcfel_data.elements.filter(s => s.id !== idToRemove);
entryDiv.remove();
// Optional: Re-render config to show a blank input if list is empty
}
});
}
// --- INITIALIZATION ---
rcfel_initSampleData();
rcfel_renderConfig();
rcfel_renderDashboard();
// Set initial tab state
rcfel_tabPanes.forEach((pane, index) => {
pane.classList.toggle('hidden', index !== 0);
pane.classList.toggle('rcfel-active', index === 0);
});
rcfel_tabLinks.forEach((link, index) => {
TAB_CLASSES.active.forEach(cls => link.classList.remove(cls));
TAB_CLASSES.inactive.forEach(cls => link.classList.remove(cls));
if (index === 0) {
TAB_CLASSES.active.forEach(cls => link.classList.add(cls));
} else {
TAB_CLASSES.inactive.forEach(cls => link.classList.add(cls));
}
});
});