You haven\'t saved any facts yet.
';
return;
}
sfe_data.savedFacts.forEach(fact => {
const itemHTML = `
${sfe_escapeHTML(fact.category)}
${sfe_escapeHTML(fact.text)}
Remove
`;
sfe_savedFactsList.innerHTML += itemHTML;
});
}
/**
* Removes a fact from the savedFacts list
*/
window.sfe_removeSavedFact = function(factId) {
sfe_data.savedFacts = sfe_data.savedFacts.filter(fact => fact.id !== factId);
sfe_renderSavedFactsList();
// If the removed fact is the one currently displayed, update the save button
if (sfe_currentFact && sfe_currentFact.id === factId) {
sfe_updateSaveButtonState();
}
}
// --- CONFIGURATION FUNCTIONS ---
/**
* Renders the list of all facts in the "Manage Facts" section
*/
function sfe_renderManageFactsList() {
if (!sfe_manageFactsList) return;
sfe_manageFactsList.innerHTML = '';
if (sfe_data.facts.length === 0) {
sfe_manageFactsList.innerHTML = '
No facts in the database.
';
return;
}
sfe_data.facts.forEach(fact => {
const itemHTML = `
${sfe_escapeHTML(fact.category)}
${sfe_escapeHTML(fact.text)}
Delete
`;
sfe_manageFactsList.innerHTML += itemHTML;
});
}
/**
* Handles the submission of the "Add New Fact" form
*/
window.sfe_addFact = function(event) {
event.preventDefault();
if (!sfe_newFactCategory || !sfe_newFactText) return;
const category = sfe_newFactCategory.value;
const text = sfe_newFactText.value.trim();
if (category && text) {
const newId = sfe_data.facts.length > 0 ? Math.max(...sfe_data.facts.map(f => f.id)) + 1 : 1;
const newFact = { id: newId, category: category, text: text };
sfe_data.facts.push(newFact);
sfe_renderManageFactsList();
// Reset form
sfe_newFactText.value = '';
sfe_newFactCategory.value = 'Space';
}
}
/**
* Deletes a fact from the main `facts` list and `savedFacts` list
*/
window.sfe_deleteFact = function(factId) {
if (confirm("Are you sure you want to permanently delete this fact?")) {
// Remove from main list
sfe_data.facts = sfe_data.facts.filter(fact => fact.id !== factId);
// Remove from saved list
sfe_removeSavedFact(factId); // This also re-renders the saved list
// Re-render manage list
sfe_renderManageFactsList();
// If the deleted fact is the one on display, show a new one
if (sfe_currentFact && sfe_currentFact.id === factId) {
sfe_showNewFact();
}
}
}
// --- TAB & NAVIGATION FUNCTIONS ---
/**
* Switches the active tab
*/
window.sfe_switchTab = function(tabIndex) {
sfe_currentTabIndex = tabIndex;
sfe_tabContents.forEach((content, index) => {
if (index === tabIndex) {
content.classList.add('sfe-active');
} else {
content.classList.remove('sfe-active');
}
});
sfe_tabs.forEach((tab, index) => {
if (index === tabIndex) {
tab.classList.add('sfe-active');
} else {
tab.classList.remove('sfe-active');
}
});
sfe_updateNavButtons();
}
/**
* Navigates to the next or previous tab
*/
window.sfe_navigateTabs = function(direction) {
if (direction === 'next' && sfe_currentTabIndex < sfe_numTabs - 1) {
sfe_switchTab(sfe_currentTabIndex + 1);
} else if (direction === 'prev' && sfe_currentTabIndex > 0) {
sfe_switchTab(sfe_currentTabIndex - 1);
}
}
/**
* Updates the visibility of the Next/Previous buttons
*/
function sfe_updateNavButtons() {
sfe_prevBtn.style.visibility = (sfe_currentTabIndex === 0) ? 'hidden' : 'visible';
sfe_nextBtn.style.visibility = (sfe_currentTabIndex === sfe_numTabs - 1) ? 'hidden' : 'visible';
}
// --- PDF GENERATION ---
/**
* Generates and downloads a PDF of the saved facts
*/
window.sfe_generatePdf = function() {
if (typeof jspdf === 'undefined' || typeof jspdf.jsPDF === 'undefined') {
alert('Error: PDF generation library (jsPDF) not loaded.');
return;
}
if (typeof window.jspdf.plugin.autotable === 'undefined') {
alert('Error: PDF generation library (jsPDF-AutoTable) not loaded.');
return;
}
if (sfe_data.savedFacts.length === 0) {
alert("You have not saved any facts to download. Save some facts first!");
return;
}
try {
const { jsPDF } = jspdf;
const doc = new jsPDF();
// --- PDF Content ---
doc.setFont('helvetica', 'bold');
doc.setFontSize(20);
doc.setTextColor(varGet('--sfe-primary-color', '#0073e6'));
doc.text("My Saved Science Facts", 105, 20, { align: 'center' });
doc.setFontSize(11);
doc.setTextColor(varGet('--sfe-text-color', '#333'));
doc.text(`Total Facts Saved: ${sfe_data.savedFacts.length}`, 14, 30);
// --- PDF Table ---
const tableColumn = ["Category", "Fact"];
const tableRows = [];
sfe_data.savedFacts.forEach(fact => {
const factData = [
fact.category,
fact.text
];
tableRows.push(factData);
});
doc.autoTable({
head: [tableColumn],
body: tableRows,
startY: 40,
theme: 'striped',
headStyles: {
fillColor: varGet('--sfe-primary-color', '#0073e6'),
textColor: varGet('--sfe-text-light', '#fff')
},
styles: {
font: 'helvetica',
cellPadding: 3,
overflow: 'linebreak'
},
columnStyles: {
0: { cellWidth: 30 }, // Category
1: { cellWidth: 150 } // Fact text
},
alternateRowStyles: {
fillColor: varGet('--sfe-light-primary', '#e6f1fc')
}
});
// --- PDF Download ---
doc.save('my_saved_science_facts.pdf');
} catch (e) {
console.error("PDF Generation Error: ", e);
alert("An error occurred while generating the PDF.");
}
}
// --- UTILITY FUNCTIONS ---
/**
* Gets a CSS variable from the root, with a fallback
*/
function varGet(varName, fallback = '#000') {
try {
// Ensure we get computed style from the tool's container itself or root
const container = document.getElementById('sfe-tool-container-main');
const styleSource = container ? container : document.documentElement;
return getComputedStyle(styleSource).getPropertyValue(varName).trim() || fallback;
} catch (e) {
return fallback;
}
}
/**
* Simple HTML escaper to prevent XSS
*/
function sfe_escapeHTML(str) {
if (!str) return '';
return str.replace(/[&<>"']/g, function(m) {
return {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
}[m];
});
}
// --- INITIALIZATION ---
function sfe_initTool() {
sfe_switchTab(0); // Set initial tab
sfe_showNewFact(); // Load the first fact
sfe_renderSavedFactsList(); // Render empty saved list
sfe_renderManageFactsList(); // Render config list
}
sfe_initTool(); // Run the tool
}); // End of DOMContentLoaded