let documents = [];
let currentDocument = null;
let documentCards = null;
// Initialize card-based viewer
if (typeof DocumentCards !== 'undefined') {
documentCards = new DocumentCards('document-content');
}
// Load document list
async function loadDocuments() {
try {
const response = await fetch('/api/documents');
const data = await response.json();
documents = data.documents || [];
const listEl = document.getElementById('document-list');
if (documents.length === 0) {
listEl.innerHTML = '
No documents available
';
return;
}
console.log('Loaded documents:', documents.length);
// Find GLOSSARY and put it at the top
const glossary = documents.find(doc => doc.slug.includes('glossary'));
const otherDocs = documents.filter(doc => !doc.slug.includes('glossary'));
console.log('GLOSSARY found:', glossary ? glossary.title : 'NOT FOUND');
console.log('Other docs:', otherDocs.length);
let html = '';
// Add GLOSSARY prominently at top if it exists
if (glossary) {
html += `
📚 Start Here
`;
} else {
// If no glossary found, try case-insensitive fallback
const allGlossary = documents.find(doc => doc.slug.toLowerCase().includes('glossary'));
if (allGlossary) {
html += `
📚 Start Here
`;
}
}
// Add other documents
const docsToShow = glossary ? otherDocs : documents.filter(doc => !doc.slug.toLowerCase().includes('glossary'));
if (docsToShow.length > 0) {
html += `
Documentation
`;
html += docsToShow.map(doc => `
`).join('');
}
listEl.innerHTML = html;
console.log('Navigation HTML updated');
// Add event delegation for document links
listEl.addEventListener('click', function(e) {
const button = e.target.closest('.doc-link');
if (button && button.dataset.slug) {
e.preventDefault();
loadDocument(button.dataset.slug);
}
});
// Auto-load GLOSSARY if it exists, otherwise first document
if (glossary) {
loadDocument(glossary.slug);
} else {
const allGlossary = documents.find(doc => doc.slug.toLowerCase().includes('glossary'));
if (allGlossary) {
loadDocument(allGlossary.slug);
} else if (documents.length > 0) {
loadDocument(documents[0].slug);
}
}
} catch (error) {
console.error('Error loading documents:', error);
document.getElementById('document-list').innerHTML =
'
Error loading documents
';
}
}
// Load specific document
let isLoading = false;
async function loadDocument(slug) {
// Prevent multiple simultaneous loads
if (isLoading) return;
try {
isLoading = true;
// Show loading state
const contentEl = document.getElementById('document-content');
contentEl.innerHTML = `
Loading document...
`;
const response = await fetch(`/api/documents/${slug}`);
const data = await response.json();
if (!data.success) {
throw new Error(data.error || 'Failed to load document');
}
currentDocument = data.document;
// Update active state
document.querySelectorAll('.doc-link').forEach(el => {
if (el.dataset.slug === slug) {
el.classList.add('bg-blue-100', 'text-blue-900');
} else {
el.classList.remove('bg-blue-100', 'text-blue-900');
}
});
// Render with card-based viewer if available and document has sections
if (documentCards && currentDocument.sections && currentDocument.sections.length > 0) {
documentCards.render(currentDocument);
} else {
// Fallback to traditional view
contentEl.innerHTML = `