${dynasty.details}
${dynasty.image_placeholder || 'Image placeholder'}
`;
detailArea.style.display = 'block';
gwch_currentPdfContentGenerator = () => gwch_getDynastyPdfContent(dynastyId);
}
}
function gwch_populateSectionsList() {
const listContainer = document.getElementById('gwch_sectionsListContainer');
if (!listContainer) return;
listContainer.innerHTML = '';
gwch_greatWallData.famousSections.forEach(section => {
const el = document.createElement('div');
el.className = 'gwch_section_list_item';
el.textContent = section.name;
el.onclick = () => gwch_displaySectionDetails(section.id, el);
listContainer.appendChild(el);
});
}
function gwch_displaySectionDetails(sectionId, clickedElement) {
gwch_setActiveItem('#gwch_sectionsListContainer', clickedElement);
const section = gwch_greatWallData.famousSections.find(s => s.id === sectionId);
const detailArea = document.getElementById('gwch_sectionDetailArea');
if (section && detailArea) {
let featuresHTML = section.features ? `
${section.features.map(f => `- ${f}
`).join('')}
` : '
No specific features listed.
';
let dynasties = section.dynasty_ids.map(d_id => gwch_greatWallData.timeline.find(d => d.id === d_id)?.period || d_id).join(', ');
detailArea.innerHTML = `
${section.name}
Location: ${section.location}
Primarily Associated with Dynasty/Period: ${dynasties}
${section.description}
Key Features:
${featuresHTML}
${section.image_placeholder || 'Image placeholder'}
`;
detailArea.style.display = 'block';
gwch_currentPdfContentGenerator = () => gwch_getSectionPdfContent(sectionId);
}
}
function gwch_populateThemesList() {
const listContainer = document.getElementById('gwch_themesListContainer');
if (!listContainer) return;
listContainer.innerHTML = '';
gwch_greatWallData.themes.forEach(theme => {
const el = document.createElement('div');
el.className = 'gwch_theme_list_item';
el.textContent = theme.title;
el.onclick = () => gwch_displayThemeDetails(theme.id, el);
listContainer.appendChild(el);
});
}
function gwch_displayThemeDetails(themeId, clickedElement) {
gwch_setActiveItem('#gwch_themesListContainer', clickedElement);
const theme = gwch_greatWallData.themes.find(t => t.id === themeId);
const detailArea = document.getElementById('gwch_themeDetailArea');
if (theme && detailArea) {
let contentHTML = '';
theme.content.forEach(item => {
contentHTML += `
${item.heading}
${item.text}
`;
});
detailArea.innerHTML = `
${theme.title}
${contentHTML}`;
detailArea.style.display = 'block';
gwch_currentPdfContentGenerator = () => gwch_getThemePdfContent(themeId);
}
}
// --- PDF Generation ---
function gwch_getIntroPdfContent() {
return [
{ type: 'heading', content: "Great Wall of China: Introduction", size: 18 },
{ type: 'text', content: gwch_greatWallData.overview, size: 11 }
];
}
function gwch_getDynastyPdfContent(dynastyId) {
const dynasty = gwch_greatWallData.timeline.find(d => d.id === dynastyId);
if (!dynasty) return [{ type: 'text', content: "Dynasty information not found.", size: 12 }];
return [
{ type: 'heading', content: `Great Wall: ${dynasty.period}`, size: 18 },
{ type: 'text', content: `Summary: ${dynasty.summary}`, size: 11 },
{ type: 'spacer', height: 5 },
{ type: 'text', content: dynasty.details, size: 11 },
{ type: 'spacer', height: 5 },
{ type: 'text', content: `Image reference: ${dynasty.image_placeholder || 'N/A'}`, size: 9, style: 'italic' }
];
}
function gwch_getSectionPdfContent(sectionId) {
const section = gwch_greatWallData.famousSections.find(s => s.id === sectionId);
if (!section) return [{ type: 'text', content: "Section information not found.", size: 12 }];
let dynasties = section.dynasty_ids.map(d_id => gwch_greatWallData.timeline.find(d => d.id === d_id)?.period || d_id).join(', ');
let pdfItems = [
{ type: 'heading', content: `Famous Section: ${section.name}`, size: 18 },
{ type: 'text', content: `Location: ${section.location}`, size: 11 },
{ type: 'text', content: `Associated Dynasties: ${dynasties}`, size: 11 },
{ type: 'spacer', height: 5 },
{ type: 'text', content: section.description, size: 11 },
{ type: 'spacer', height: 5 },
{ type: 'heading', content: "Key Features:", size: 14, style:'bold'},
];
if(section.features && section.features.length > 0){
section.features.forEach(f => pdfItems.push({type: 'listItem', content: f, size: 11}));
} else {
pdfItems.push({type: 'text', content: "No specific features listed.", size: 11});
}
pdfItems.push({ type: 'spacer', height: 5 });
pdfItems.push({ type: 'text', content: `Image reference: ${section.image_placeholder || 'N/A'}`, size: 9, style: 'italic' });
return pdfItems;
}
function gwch_getThemePdfContent(themeId) {
const theme = gwch_greatWallData.themes.find(t => t.id === themeId);
if (!theme) return [{ type: 'text', content: "Theme information not found.", size: 12 }];
let pdfItems = [
{ type: 'heading', content: `Theme: ${theme.title}`, size: 18 }
];
theme.content.forEach(item => {
pdfItems.push({ type: 'spacer', height: 3 });
pdfItems.push({ type: 'heading', content: item.heading, size: 14, style:'bold' });
pdfItems.push({ type: 'text', content: item.text, size: 11 });
});
return pdfItems;
}
function gwch_downloadPDF() {
if (typeof window.jspdf === 'undefined' || typeof window.jspdf.jsPDF === 'undefined') {
alert('PDF library (jsPDF) is not loaded. Please check your internet connection.');
return;
}
const { jsPDF: JSPDF } = window.jspdf;
const doc = new JSPDF();
let yPos = 15;
const leftMargin = 15;
const contentWidth = doc.internal.pageSize.getWidth() - (leftMargin * 2);
const pdfContent = gwch_currentPdfContentGenerator(); // Get current content items
pdfContent.forEach(item => {
if (yPos > 270) { // Basic page break
doc.addPage();
yPos = 15;
}
if (item.type === 'heading') {
doc.setFontSize(item.size);
doc.setFont(undefined, item.style || 'bold');
const lines = doc.splitTextToSize(item.content, contentWidth);
doc.text(lines, leftMargin, yPos);
yPos += (lines.length * (item.size * 0.35)) + (item.size * 0.2);
} else if (item.type === 'text') {
doc.setFontSize(item.size);
doc.setFont(undefined, item.style || 'normal');
const lines = doc.splitTextToSize(item.content, contentWidth);
doc.text(lines, leftMargin, yPos);
yPos += (lines.length * (item.size * 0.35)) + 2;
} else if (item.type === 'listItem') {
doc.setFontSize(item.size);
doc.setFont(undefined, item.style || 'normal');
const lines = doc.splitTextToSize(`• ${item.content}`, contentWidth - 5); // Indent list items
doc.text(lines, leftMargin + 5, yPos);
yPos += (lines.length * (item.size * 0.35)) + 1;
} else if (item.type === 'spacer') {
yPos += item.height;
}
});
doc.save("GreatWall_Explorer_Info.pdf");
}