`;
container.innerHTML += listHTML;
}
});
}
function renderRecentNotes(notes) {
const container = document.getElementById('recent-notes-container');
container.innerHTML = '';
notes.sort((a,b) => b.createdDate - a.createdDate).slice(0, 5).forEach(note => {
container.innerHTML += `
${note.title}
${note.category} - Created: ${note.createdDate.toLocaleDateString()}
`;
});
}
// --- PDF and UI Helpers ---
function addEventListeners() {
dashboardTab.addEventListener('click', () => setActiveTab(dashboardTab));
configTab.addEventListener('click', () => setActiveTab(configTab));
updateDashboardBtn.addEventListener('click', () => {
updateDashboard();
setActiveTab(dashboardTab);
showAlert('Success', 'Dashboard has been updated with the new data.');
});
downloadPdfBtn.addEventListener('click', handlePdfDownload);
document.getElementById('alert-close-btn').addEventListener('click', hideAlert);
}
function setActiveTab(tabElement) {
[dashboardTab, configTab].forEach(t => t.classList.replace('tab-active', 'tab-inactive'));
[dashboardContent, configContent].forEach(c => c.classList.add('hidden'));
tabElement.classList.replace('tab-inactive', 'tab-active');
document.getElementById(tabElement.getAttribute('aria-controls')).classList.remove('hidden');
}
function showAlert(title, message) {
const modal = document.getElementById('alert-modal');
document.getElementById('alert-title').textContent = title;
document.getElementById('alert-message').textContent = message;
modal.classList.remove('hidden');
modal.classList.add('flex');
}
function hideAlert() {
document.getElementById('alert-modal').classList.add('hidden');
}
async function handlePdfDownload() {
const { jsPDF } = window.jspdf;
const pdfContent = document.getElementById('pdf-content');
if (!pdfContent) {
showAlert('Error', 'Could not find content to download.');
return;
}
const originalButtonText = downloadPdfBtn.textContent;
downloadPdfBtn.textContent = 'Generating...';
downloadPdfBtn.disabled = true;
try {
const canvas = await html2canvas(pdfContent, { scale: 2, useCORS: true, logging: false, backgroundColor: '#ffffff' });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const margin = 40;
const imgProps = pdf.getImageProperties(imgData);
const ratio = imgProps.width / imgProps.height;
let finalWidth = pdfWidth - margin * 2;
let finalHeight = finalWidth / ratio;
if (finalHeight > pdfHeight - margin * 2) {
finalHeight = pdfHeight - margin * 2;
finalWidth = finalHeight * ratio;
}
const x = (pdfWidth - finalWidth) / 2;
const y = margin;
pdf.addImage(imgData, 'PNG', x, y, finalWidth, finalHeight);
pdf.save(`pkm-dashboard-report.pdf`);
} catch (error) {
console.error('Error generating PDF:', error);
showAlert('PDF Error', 'An error occurred while generating the PDF.');
} finally {
downloadPdfBtn.textContent = originalButtonText;
downloadPdfBtn.disabled = false;
}
}
// --- Run Initialization ---
initialize();
});