`;
// Get templates related to this document type
const relatedTemplates = templateData.filter(tpl => tpl.document_type === doc.Doc_ID_Type);
let templatesHtml = `
`;
if (relatedTemplates.length > 0) {
templatesHtml += `
`;
flowPlaceholder.style.display = 'none';
try {
if (flowsViewWrapper.classList.contains('hidden-container')) return;
const clickableDefinition = definition.replace(/click ([A-Z0-9_\-]+) call displayDetailsAndGraphFromGraph/g,'click $1 call displayDetailsAndGraphFromGraph');
const graphId = `mermaid-flow-${flowId}-${Date.now()}`;
const { svg } = await mermaid.render(graphId, clickableDefinition);
mermaidFlowGraphContainer.innerHTML = svg;
// Make the SVG responsive
const svgElement = mermaidFlowGraphContainer.querySelector('svg');
if (svgElement) {
svgElement.setAttribute('width', '100%');
svgElement.setAttribute('height', '100%');
svgElement.style.maxHeight = '700px'; // Taller to accommodate complex flows
}
// Add title and description based on flow ID
const flowDisplayTitles = {
"p1_sad": "Phase 1 SAD Study Documents",
"nda_submission": "NDA/MAA Submission Process",
"ind_pathway": "IND Pathway Documents",
"clinical_program": "Clinical Program Development"
};
const flowDescriptions = {
"p1_sad": "This diagram shows the key documents needed for a Phase 1 Single Ascending Dose study, from preclinical inputs through to clinical execution and reporting.",
"nda_submission": "The NDA/MAA submission process flow showing how various documents and data packages are assembled into a regulatory submission.",
"ind_pathway": "Documents required from Discovery through Preclinical development to enable an IND/CTA submission.",
"clinical_program": "The integrated flow of clinical documentation across Phases 1-3 leading to regulatory submission."
};
const title = flowDisplayTitles[flowId] || `Flow ${flowId}`;
const description = flowDescriptions[flowId] || "Document workflow visualization";
// Add title and description above the graph
const titleContainer = document.createElement('div');
titleContainer.className = 'mb-4';
titleContainer.innerHTML = `
This diagram shows the key documents needed for a Phase 1 Single Ascending Dose study, from preclinical inputs through to clinical execution and reporting.
`;
exampleMermaidGraphContainer.insertBefore(titleContainer, exampleMermaidGraphContainer.firstChild);
// Make the SVG responsive
const svgElement = exampleMermaidGraphContainer.querySelector('svg');
if (svgElement) {
svgElement.setAttribute('width', '100%');
svgElement.setAttribute('height', '100%');
}
exampleFlowModal.style.display = "flex";
} catch (error) {
console.error("Mermaid rendering error for example:", error);
exampleMermaidGraphContainer.innerHTML = `
Error rendering example flow graph.
`;
exampleFlowModal.style.display = "flex";
}
}
// --- Global Search Functionality ---
// Add global search functionality
function performGlobalSearch(searchTerm) {
if (!searchTerm) return;
searchTerm = searchTerm.toLowerCase();
let results = documentsData.filter(doc =>
doc.Document_Name.toLowerCase().includes(searchTerm) ||
doc.Doc_ID_Type.toLowerCase().includes(searchTerm) ||
(doc.Purpose_Key_Content && doc.Purpose_Key_Content.toLowerCase().includes(searchTerm))
);
// Switch to document view with search results
switchToView('documentViewWrapper', 'All');
// Set the search input in document view to match the global search
searchInputDocView.value = searchTerm;
// Render the filtered list
renderDocumentList();
}
// --- Event Listeners ---
// Search input specific to document view
searchInputDocView?.addEventListener('input', renderDocumentList);
// Global header search
headerSearchInput?.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
performGlobalSearch(e.target.value);
}
});
// Home button listener
homeButton?.addEventListener('click', () => switchToView('home'));
// Breadcrumb listener (delegated)
breadcrumbNav?.addEventListener('click', (e) => {
if (e.target.tagName === 'A' && e.target.dataset.viewTarget) {
e.preventDefault();
switchToView(e.target.dataset.viewTarget);
}
});
// Dashboard card listeners (delegated to main content area)
mainContentArea?.addEventListener('click', (e) => {
const card = e.target.closest('.dashboard-card[data-target-view]');
if (card) {
const targetView = card.dataset.targetView;
const initialTab = card.dataset.initialTab; // Get initial tab if specified
if (targetView) {
switchToView(targetView, initialTab);
}
}
});
// Example Flow Buttons (Header and Flow View)
showExampleFlowBtnHeader?.addEventListener('click', showExampleFlow);
showExampleFlowBtnFlowView?.addEventListener('click', showExampleFlow);
// Modal Close Listeners
closeExampleModalBtn?.addEventListener('click', () => exampleFlowModal.style.display = "none");
window.addEventListener('click', (event) => { if (event.target == exampleFlowModal) exampleFlowModal.style.display = "none"; });
closeDetailsModalBtn?.addEventListener('click', () => { detailsModal.style.display = "none"; clearSelection(); });
window.addEventListener('click', (event) => { if (event.target == detailsModal) { detailsModal.style.display = "none"; clearSelection(); } });
// Modal Next/Prev Button Listeners
prevDocBtn?.addEventListener('click', () => { if (currentDocListIndices.prev) displayDetailsInModal(currentDocListIndices.prev); });
nextDocBtn?.addEventListener('click', () => { if (currentDocListIndices.next) displayDetailsInModal(currentDocListIndices.next); });
// --- Initialization ---
document.addEventListener('DOMContentLoaded', async () => {
console.log("DOM Loaded. Fetching data...");
mainContentArea.innerHTML += '
Loading Data...
'; // Add loading spinner
try {
// Load document data
const docsResponse = await fetch('documents.json');
if (!docsResponse.ok) {
throw new Error(`HTTP error! status: ${docsResponse.status}`);
}
documentsData = await docsResponse.json();
console.log(`Successfully loaded ${documentsData.length} documents from documents.json`);
// Load template data
try {
const templatesResponse = await fetch('document_templates.json');
if (templatesResponse.ok) {
templateData = await templatesResponse.json();
console.log(`Successfully loaded ${templateData.length} templates from document_templates.json`);
} else {
console.warn("Templates file not found. Document templates will not be available.");
templateData = [];
}
} catch (templateError) {
console.warn("Error loading templates:", templateError);
templateData = [];
}
// Remove spinner and initialize UI
document.getElementById('loadingSpinner')?.remove();
switchToView('home'); // Start on the 'Home' view
} catch (error) {
console.error("Failed to load documents.json:", error);
document.getElementById('loadingSpinner')?.remove();
// Display error message more prominently
mainContentArea.innerHTML = `
Error!Could not load document database (documents.json). Please ensure the file exists in the same folder and is valid JSON.