// --- Global State Variables --- // MOVED to state.js // let chartInstancesScada = {}; // let chartInstancesDrawing = {}; // let currentProjectData = {}; // let selectedProjectName = null; // let detailsModalInstance = null; // let currentVisibleSection = 'scada'; // let eventSource = null; // let selectedProject = null; // let initialStatuses = initialServerData.status || {}; // const projectSelector = document.getElementById('projectSelector'); // Keep DOM element refs here for now // const manageFilesBtn = document.getElementById('manageFilesBtn'); // --- Chart Configurations --- // MOVED to chartManager.js // const scadaChartLabels = ['Found in SCADA', 'Not Found in SCADA']; // const scadaChartColors = ['rgb(13, 110, 253)', 'rgb(220, 53, 69)']; // const drawingChartLabels = ['Found in Drawing', 'Not Found in Drawing']; // const drawingChartColors = ['rgb(25, 135, 84)', 'rgb(220, 53, 69)']; // Map backend list keys for modal clicks (can be combined or kept separate if needed) // MOVED to state.js // const scadaListKeysMap = { ... }; // const drawingListKeysMap = { ... }; // --- Debounce Utility (Only need one) --- // MOVED to state.js // function debounce(func, wait) { ... } // --- NEW: Helper Functions for Processing State --- // Checks if the status message indicates ongoing processing // MOVED to state.js // function isProcessing(statusMsg) { ... } // Displays a loading indicator in a container element // MOVED to ui.js // function showLoadingIndicator(containerElement, message = "Processing data...") { ... } // Removes the loading indicator from a container // MOVED to ui.js // function clearLoadingIndicator(containerElement) { ... } // Sets the entire UI to reflect a processing state // MOVED to ui.js // function showProcessingStateUI(projectData) { ... } // Use uiShowProcessingState(projectData?.status) instead // Clears loading indicators and triggers the actual UI rendering // MOVED to ui.js // function showReadyStateUI(projectData) { ... } // Call uiClearProcessingState() and then call core update functions // --- Core UI Update Functions (Need selected project data) --- // These become orchestrators, calling ui.js and chartManager.js function updateUIScadaCore(projectData) { // Accepts data for the selected project console.log(`Running core SCADA UI redraw logic for project: ${selectedProjectName}`); const progressDetails = (projectData && projectData.progress) ? projectData.progress : { overall: {}, panels: {} }; const overallData = progressDetails.overall || {}; const overallTotal = overallData.total_csv || 0; const overallFoundScada = (overallData.found_both || 0) + (overallData.found_scada_only || 0); const overallNotFoundScada = (overallData.found_drawing_only || 0) + (overallData.missing_both || 0); const overallPercentageFound = overallTotal > 0 ? ((overallFoundScada / overallTotal) * 100).toFixed(1) : 0; const overallChartCounts = [overallFoundScada, overallNotFoundScada]; // Call UI functions uiUpdateProjectNameDisplay(selectedProjectName); uiUpdateOverallStatsText('scada', overallFoundScada, overallTotal, overallPercentageFound); const isSectionVisible = (currentVisibleSection === 'scada'); // Call Chart Manager functions using the correct function name and canvas ID updateOverallChart( 'scada', // context 'overall-scada-chart-canvas', // canvas ID overallChartCounts, // chart data overallTotal, // total for percentage display isSectionVisible // visibility flag ); const panelsContainer = uiElements.scadaPanelsProgress; // Use uiElements const panelsData = progressDetails.panels || {}; updatePanelCharts('scada', panelsContainer, panelsData, isSectionVisible); // Pass visibility flag console.log("Finished SCADA UI core redraw."); } function updateUIDrawingCore(projectData) { // Accepts data for the selected project console.log(`Running core Drawing UI redraw logic for project: ${selectedProjectName}`); const progressDetails = (projectData && projectData.progress) ? projectData.progress : { overall: {}, panels: {} }; const overallData = progressDetails.overall || {}; const overallTotal = overallData.total_csv || 0; const overallFoundDrawing = (overallData.found_both || 0) + (overallData.found_drawing_only || 0); const overallNotFoundDrawing = (overallData.found_scada_only || 0) + (overallData.missing_both || 0); const overallPercentageFound = overallTotal > 0 ? ((overallFoundDrawing / overallTotal) * 100).toFixed(1) : 0; const overallChartCounts = [overallFoundDrawing, overallNotFoundDrawing]; // Call UI functions uiUpdateProjectNameDisplay(selectedProjectName); uiUpdateOverallStatsText('drawing', overallFoundDrawing, overallTotal, overallPercentageFound); const isSectionVisible = (currentVisibleSection === 'drawings'); // Call Chart Manager functions using the correct function name and canvas ID updateOverallChart( 'drawing', // context 'overall-drawing-chart-canvas', // canvas ID overallChartCounts, // chart data overallTotal, // total for percentage display isSectionVisible // visibility flag ); const panelsContainer = uiElements.drawingPanelsProgress; // Use uiElements const panelsData = progressDetails.panels || {}; updatePanelCharts('drawing', panelsContainer, panelsData, isSectionVisible); // Pass visibility flag console.log("Finished Drawing UI core redraw."); } function updateUIConflictsCore(projectData) { // Accepts data for the selected project console.log(`Running core Conflicts UI redraw logic for project: ${selectedProjectName}`); const progressDetails = (projectData && projectData.progress) ? projectData.progress : { overall: {}, panels: {} }; const panelsData = progressDetails.panels || {}; // Call UI functions uiUpdateProjectNameDisplay(selectedProjectName); uiUpdateConflictsTable(panelsData); console.log("Finished Conflicts UI core redraw."); } // --- Process Update: Extracts data for selected project --- (Will move to sse.js) // MOVED to sse.js // function processUpdate(fullData) { ... } // --- Debounced version of the processing function --- (Will move to sse.js) // MOVED to sse.js // const debouncedProcessUpdate = debounce(processUpdate, 250); // --- Modal Display Function (Needs PROJECT context) --- // MOVED to modalManager.js // function showDetailsModal(projectName, identifier, categoryType, context) { ... } // --- Update Status Bar Helper --- // MOVED to ui.js // function updateStatusBar(projectName, statusMsg, commitHash) { ... } // --- Navigation Handling --- // MOVED to ui.js // function showSection(sectionId) { ... } // --- Initialize SSE Connection --- (Will move to sse.js) // MOVED to sse.js // function initializeSSE() { ... } // --- Project Selector Change Handler --- (Will move to events.js) // MOVED to events.js // function handleProjectChange() { ... } // --- NEW: File Management Functions --- // These action handlers are now called by event listeners (setup in events.js) // Handles the overall process of loading and displaying PDFs in the modal // Stays here as it coordinates API and Modal calls async function handleLoadAndDisplayPdfs(projectName) { const pdfListDiv = document.getElementById('existingPdfList'); if (!pdfListDiv) return; // ... robustness check ... pdfListDiv.innerHTML = '
Loading files...
'; uiClearManageFilesStatusMessages(); try { const data = await apiListPdfs(projectName); modalManagerPopulatePdfList(pdfListDiv, projectName, data.files || []); } catch (error) { console.error('Error loading PDF list:', error); pdfListDiv.innerHTML = '
Error loading files.
'; uiShowManageFilesStatus(`Error loading PDF list: ${error.message}`, 'danger'); } } // --- PDF Delete Handling (will move actual listener setup to events.js) --- // This function is now called BY the event listener in events.js // MOVED to events.js // async function handlePdfDeleteAction(projectName, filename) { ... } // --- PDF Upload Handling (will move listener setup to events.js) --- // This function is now called BY the event listener in events.js // MOVED to events.js // async function handlePdfUploadAction(form) { ... } // --- Trigger Analysis Handling (will move listener setup to events.js) --- // This function is now called BY the event listener in events.js // MOVED to events.js // async function handleTriggerAnalysisAction() { ... } // --- Delete Project Handling (will move listener setup to events.js) --- // This function is now called BY the event listener in events.js // MOVED to events.js // async function handleDeleteProjectAction() { ... } // --- Manifest Upload Handling (will move listener setup to events.js) --- // This function is now called BY the event listener in events.js // MOVED to events.js // async function handleManifestUploadAction(form) { ... } // --- Initialize Add Project Form --- (will move listener setup to events.js) // MOVED to events.js (The listener part) // function setupAddProjectForm() { ... } // --- Event Listeners Setup (Will move to events.js) --- // MOVED to events.js // document.addEventListener('DOMContentLoaded', () => { ... }); // --- Main Initialization --- document.addEventListener('DOMContentLoaded', () => { console.log("DOM Loaded, initializing application modules..."); // Initialize modal instances (must be done before listeners might use them) initializeModals(); // From modalManager.js // Initialize UI state and attach all event listeners initializeEventListeners(); // From events.js // Initialize Server-Sent Events connection sseInitialize(); // From sse.js console.log("Application initialization complete."); });