` : ''}
${record.bioNotes ? `` : ''}
`;
archiveViewDiv.appendChild(card);
});
}
function handlePdfDownload() {
if (!jsPDF_constructor_for_tool) {
alert("PDF library is not available."); return;
}
const recordsToExport = getOrganizedRecords();
const pdfDocTitle = displayTitleInput.value || "US Presidential History";
if (recordsToExport.length === 0) {
alert("No records to export based on current filters/sort."); return;
}
const pdf = new jsPDF_constructor_for_tool({ orientation: 'p', unit: 'mm', format: 'a4' });
const primaryColor = primaryColorInput.value;
const textColor = textColorInput.value;
const pageBgColor = bgColorInput.value;
pdf.setFillColor(pageBgColor);
pdf.rect(0, 0, pdf.internal.pageSize.getWidth(), pdf.internal.pageSize.getHeight(), 'F');
const margin = 15;
const contentWidth = pdf.internal.pageSize.getWidth() - 2 * margin;
let currentY = margin;
pdf.setFont("helvetica", "bold"); // Using a standard font
pdf.setFontSize(20);
pdf.setTextColor(primaryColor);
const titleLines = pdf.splitTextToSize(pdfDocTitle, contentWidth);
pdf.text(titleLines, pdf.internal.pageSize.getWidth() / 2, currentY, { align: 'center' });
currentY += (titleLines.length * 20 * 0.35) + 12; // Adjust spacing based on font size
recordsToExport.forEach(record => {
let estimatedBlockHeight = 0;
pdf.setFont("helvetica", "bold"); pdf.setFontSize(14); estimatedBlockHeight += 6; // Name
pdf.setFont("helvetica", "normal"); pdf.setFontSize(10); estimatedBlockHeight += 5; // Term/Party
if(record.vicePresident) estimatedBlockHeight += (pdf.splitTextToSize(record.vicePresident, contentWidth).length * 10 * 0.35) + 4;
if(record.keyEvents) { pdf.setFontSize(11); estimatedBlockHeight += 5; pdf.setFontSize(10); estimatedBlockHeight += (pdf.splitTextToSize(record.keyEvents, contentWidth).length * 10 * 0.35) + 4; }
if(record.notableQuotes) { pdf.setFontSize(11); estimatedBlockHeight += 5; pdf.setFontSize(10); estimatedBlockHeight += (pdf.splitTextToSize(record.notableQuotes, contentWidth).length * 10 * 0.35) + 4; }
if(record.bioNotes) { pdf.setFontSize(11); estimatedBlockHeight += 5; pdf.setFontSize(10); estimatedBlockHeight += (pdf.splitTextToSize(record.bioNotes, contentWidth).length * 10 * 0.35) + 4; }
estimatedBlockHeight += 10; // Spacing
if (currentY + estimatedBlockHeight > pdf.internal.pageSize.getHeight() - margin) {
pdf.addPage(); currentY = margin;
pdf.setFillColor(pageBgColor); pdf.rect(0, 0, pdf.internal.pageSize.getWidth(), pdf.internal.pageSize.getHeight(), 'F');
}
// President Name
pdf.setFont("helvetica", "bold"); pdf.setFontSize(14); pdf.setTextColor(primaryColor);
const nameLines = pdf.splitTextToSize(record.presidentName, contentWidth);
pdf.text(nameLines, margin, currentY);
currentY += (nameLines.length * 14 * 0.35) + 2;
// Term & Party
pdf.setFont("helvetica", "italic"); pdf.setFontSize(10); pdf.setTextColor(textColor);
const termPartyText = `Term: ${record.termStartYear} – ${record.termEndYear || 'Present'} | Party: ${record.party}`;
pdf.text(termPartyText, margin, currentY);
currentY += (10 * 0.35) + 4;
pdf.setFont("helvetica", "normal"); // Reset font style
// Vice President
if (record.vicePresident) {
pdf.setFontSize(10); pdf.setTextColor(textColor);
pdf.setFont("helvetica", "bold"); pdf.text("Vice President(s):", margin, currentY); currentY += (10 * 0.35) + 1;
pdf.setFont("helvetica", "normal");
const vpLines = pdf.splitTextToSize(record.vicePresident, contentWidth - 5); // Indent slightly
pdf.text(vpLines, margin + 5, currentY);
currentY += (vpLines.length * 10 * 0.35) + 3;
}
// Function to render multi-line text sections
const renderSection = (label, text) => {
if (text) {
pdf.setFontSize(11); pdf.setTextColor(primaryColor); pdf.setFont("helvetica", "bold");
pdf.text(label, margin, currentY); currentY += (11 * 0.35) + 1;
pdf.setFontSize(10); pdf.setTextColor(textColor); pdf.setFont("helvetica", "normal");
const lines = pdf.splitTextToSize(text, contentWidth -5);
pdf.text(lines, margin + 5, currentY);
currentY += (lines.length * 10 * 0.35) + 4;
}
};
renderSection("Key Events/Policies:", record.keyEvents);
renderSection("Notable Quotes:", record.notableQuotes);
renderSection("Biographical Notes:", record.bioNotes);
currentY += 6; // Extra space between records
});
let safePdfTitle = pdfDocTitle.replace(/[^a-z0-9]/gi, '_').toLowerCase() || "us_presidential_history";
if (safePdfTitle.length > 50) safePdfTitle = safePdfTitle.substring(0, 50);
pdf.save(safePdfTitle + '.pdf');
}
// Event Listeners
tabs.forEach(tab => {
tab.addEventListener('click', (e) => switchTab(e.currentTarget.dataset.tab));
});
document.querySelectorAll('.uspa-next-tab, .uspa-prev-tab').forEach(button => {
button.addEventListener('click', (e) => {
const targetTabId = e.currentTarget.dataset.nexttab || e.currentTarget.dataset.prevtab;
if (targetTabId) switchTab(targetTabId);
});
});
if (addRecordForm) addRecordForm.addEventListener('submit', handleAddRecordFormSubmit);
if (clearFormButton) clearFormButton.addEventListener('click', () => addRecordForm.reset());
// Tab 2 Listeners
if(applyOrganizationButton) applyOrganizationButton.addEventListener('click', renderArchiveView);
if(clearOrganizationButton) clearOrganizationButton.addEventListener('click', () => {
sortBySelect.value = 'termStartYear_asc';
filterPartySelect.value = '';
filterKeywordInput.value = '';
renderArchiveView();
});
// Live update for keyword as user types
if(filterKeywordInput) filterKeywordInput.addEventListener('input', renderArchiveView);
[displayTitleInput, primaryColorInput, textColorInput, bgColorInput, cardBgColorInput].forEach(input => {
if (input) {
input.addEventListener('input', renderArchiveView);
if (input.type === 'color') input.addEventListener('change', renderArchiveView);
}
});
if (downloadPdfButton && !downloadPdfButton.disabled) {
downloadPdfButton.addEventListener('click', handlePdfDownload);
}
// Initialization
loadRecordsFromLocalStorage();
updatePartySuggestions();
renderCurrentRecordsList();
switchTab('recordsManager');
renderArchiveView(); // Prepare archive view content
updateCssVariables();
});
Biographical Notes:
${record.bioNotes.replace(/\n/g, '
')}
