`;
infoCardsContainer.appendChild(card);
});
}
function populateSelectOptions() {
nutrientSelect.innerHTML = ''; // Reset
Object.keys(nutrientData).forEach(key => {
const option = document.createElement('option');
option.value = key;
option.textContent = nutrientData[key].name;
nutrientSelect.appendChild(option);
});
}
function populateChecklist() {
checklistUl.innerHTML = ''; // Clear
Object.keys(nutrientData).forEach(key => {
const nutrient = nutrientData[key];
const li = document.createElement('li');
const checkboxId = `vv-check-${key}`;
li.innerHTML = `
`;
checklistUl.appendChild(li);
});
}
function handleNutrientSelection() {
const selectedKey = nutrientSelect.value;
sourceListUl.innerHTML = ''; // Clear previous list
if (selectedKey && nutrientData[selectedKey]) {
const nutrient = nutrientData[selectedKey];
sourceListHeading.textContent = `Sources for ${nutrient.name}:`;
nutrient.sources.forEach(source => {
const li = document.createElement('li');
li.textContent = source;
sourceListUl.appendChild(li);
});
} else {
sourceListHeading.textContent = 'Sources will appear here...';
}
}
function handleResetChecklist() {
const checkboxes = checklistUl.querySelectorAll('input[type="checkbox"]');
checkboxes.forEach(checkbox => checkbox.checked = false);
}
// --- PDF Generation ---
function handlePdfDownload() {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF();
// --- PDF Styling ---
const primaryColor = getComputedStyle(document.documentElement).getPropertyValue('--vv-primary-color').trim();
const secondaryColor = getComputedStyle(document.documentElement).getPropertyValue('--vv-secondary-color').trim();
const textColor = getComputedStyle(document.documentElement).getPropertyValue('--vv-text-color').trim();
const labelColor = getComputedStyle(document.documentElement).getPropertyValue('--vv-label-color').trim();
let yPos = 20;
const xMargin = 15;
const lineSpacing = 7; // Adjust spacing for potentially more text
const sectionSpacing = 10;
const contentWidth = pdf.internal.pageSize.getWidth() - 2 * xMargin;
// --- Title ---
pdf.setFontSize(18);
pdf.setTextColor(primaryColor);
pdf.text("Vegan/Vegetarian Nutrient Guide", pdf.internal.pageSize.getWidth() / 2, yPos, { align: 'center' });
yPos += lineSpacing * 1.5;
// --- Timestamp ---
pdf.setFontSize(9);
pdf.setTextColor(labelColor);
pdf.text(`Generated: ${new Date().toLocaleString('en-IN', { timeZone: 'Asia/Kolkata' })} IST`, pdf.internal.pageSize.getWidth() - xMargin, yPos, { align: 'right' });
yPos += sectionSpacing * 1.5; // More space before content
// --- Disclaimer (Top) ---
pdf.setFontSize(9);
pdf.setTextColor("#b71c1c"); // Reddish
const disclaimer1 = "IMPORTANT: This guide provides general information & examples. RDAs vary greatly. Consult a qualified healthcare provider or registered dietitian for personalized advice, especially regarding supplements (e.g., B12, Vit D). Not medical advice.";
const splitDisclaimer1 = pdf.splitTextToSize(disclaimer1, contentWidth);
pdf.text(splitDisclaimer1, xMargin, yPos);
yPos += (splitDisclaimer1.length * lineSpacing * 0.8) + sectionSpacing;
// --- Nutrient Sections ---
Object.keys(nutrientData).forEach(key => {
const nutrient = nutrientData[key];
if (yPos > 250) { // Check space before starting new section
pdf.addPage();
yPos = 20;
}
// Nutrient Name Header
pdf.setFontSize(14);
pdf.setTextColor(secondaryColor);
pdf.text(nutrient.name, xMargin, yPos);
yPos += lineSpacing * 1.2;
// Importance
pdf.setFontSize(10);
pdf.setTextColor(labelColor);
pdf.text("Importance:", xMargin, yPos);
pdf.setTextColor(textColor);
const splitImportance = pdf.splitTextToSize(nutrient.importance, contentWidth - 25); // Indent text
pdf.text(splitImportance, xMargin + 25, yPos);
yPos += (splitImportance.length * lineSpacing * 0.8) + 3; // Adjust based on lines
// Example RDA
pdf.setTextColor(labelColor);
pdf.text("Example RDA:", xMargin, yPos);
pdf.setTextColor(textColor);
const rdaText = `${nutrient.rda_example} (${nutrient.rda_disclaimer})`;
const splitRDA = pdf.splitTextToSize(rdaText, contentWidth - 25);
pdf.text(splitRDA, xMargin + 25, yPos);
yPos += (splitRDA.length * lineSpacing * 0.8) + 3;
// Sources
pdf.setTextColor(labelColor);
pdf.text("Sources:", xMargin, yPos);
pdf.setTextColor(textColor);
// Format sources slightly better - maybe comma separated or short list
const sourcesText = nutrient.sources.join(', ');
const splitSources = pdf.splitTextToSize(sourcesText, contentWidth - 25);
pdf.text(splitSources, xMargin + 25, yPos);
yPos += (splitSources.length * lineSpacing * 0.8) + 3;
// Notes
if (nutrient.notes) {
pdf.setTextColor(labelColor);
pdf.text("Notes:", xMargin, yPos);
pdf.setTextColor(textColor);
const splitNotes = pdf.splitTextToSize(nutrient.notes, contentWidth - 25);
pdf.text(splitNotes, xMargin + 25, yPos);
yPos += (splitNotes.length * lineSpacing * 0.8) + 3;
}
yPos += sectionSpacing; // Space between nutrients
});
// --- Final Disclaimer (Bottom) ---
if (yPos > 260) { // Check space before adding footer disclaimer
pdf.addPage();
yPos = 20;
}
pdf.setFontSize(9);
pdf.setTextColor("#b71c1c"); // Reddish
const disclaimer2 = "Reminder: Always consult a qualified professional for personalized dietary advice and before taking supplements.";
const splitDisclaimer2 = pdf.splitTextToSize(disclaimer2, contentWidth);
pdf.text(splitDisclaimer2, xMargin, yPos);
// --- Save PDF ---
pdf.save("Vegan_Vegetarian_Nutrient_Guide.pdf");
}
// --- Initialization ---
populateNutrientCards();
populateSelectOptions();
populateChecklist();
handleNutrientSelection(); // Show initial placeholder in source list
// Attach Event Listeners
nutrientSelect.addEventListener('change', handleNutrientSelection);
resetChecklistButton.addEventListener('click', handleResetChecklist);
downloadPdfButton.addEventListener('click', handlePdfDownload);
});
