@@ -4,6 +4,15 @@ console.log("Analysis Module: Parsing...");
44document . addEventListener ( "DOMContentLoaded" , ( ) => {
55 console . log ( "Analysis Module: DOMContentLoaded" ) ;
66
7+ function getGraphDataSafe ( ) {
8+ if ( typeof window === 'undefined' ) return null ;
9+ const candidate = window . graphData ;
10+ if ( ! candidate || ! Array . isArray ( candidate . nodes ) || ! Array . isArray ( candidate . edges ) ) {
11+ return null ;
12+ }
13+ return candidate ;
14+ }
15+
716 const UI = {
817 panel : document . getElementById ( "analysis-panel" ) ,
918 resizer : document . getElementById ( "analysis-resizer" ) ,
@@ -38,7 +47,8 @@ document.addEventListener("DOMContentLoaded", () => {
3847
3948 // --- 0. Init Cluster Options ---
4049 function initClusters ( ) {
41- if ( ! UI . clusterFilter || typeof graphData === 'undefined' ) return ;
50+ const graphData = getGraphDataSafe ( ) ;
51+ if ( ! UI . clusterFilter || ! graphData ) return ;
4252
4353 // Find unique clusters
4454 const clusters = new Set ( ) ;
@@ -64,7 +74,8 @@ document.addEventListener("DOMContentLoaded", () => {
6474 // --- 1. Quick Distribution (Immediate) ---
6575 function initQuickDist ( ) {
6676 if ( ! UI . quickDist ) return ;
67- if ( typeof graphData === 'undefined' ) return ;
77+ const graphData = getGraphDataSafe ( ) ;
78+ if ( ! graphData ) return ;
6879
6980 const degrees = graphData . nodes . map ( n => n . inDegree + n . outDegree ) ;
7081 const maxDeg = Math . max ( ...degrees , 1 ) ;
@@ -283,7 +294,8 @@ document.addEventListener("DOMContentLoaded", () => {
283294
284295 // Get nodes filtered ONLY by cluster (ignore threshold for histogram context)
285296 // Guard: Check if graphData is available (may not be in MINI mode)
286- if ( typeof graphData === 'undefined' || ! graphData || ! graphData . nodes ) {
297+ const graphData = getGraphDataSafe ( ) ;
298+ if ( ! graphData ) {
287299 console . warn ( '[Analysis] graphData not available, skipping histogram render.' ) ;
288300 return ;
289301 }
@@ -421,6 +433,9 @@ document.addEventListener("DOMContentLoaded", () => {
421433 }
422434
423435 function showNodeDetails ( nodeId ) {
436+ const graphData = getGraphDataSafe ( ) ;
437+ if ( ! graphData ) return ;
438+
424439 // Open Panel if closed
425440 if ( ! UI . panel . classList . contains ( "open" ) ) {
426441 UI . btn . click ( ) ;
@@ -482,6 +497,11 @@ document.addEventListener("DOMContentLoaded", () => {
482497
483498 // --- 6. Export & Filter Logic ---
484499 function getFilteredData ( ) {
500+ const graphData = getGraphDataSafe ( ) ;
501+ if ( ! graphData ) {
502+ return { nodes : [ ] , edges : [ ] } ;
503+ }
504+
485505 let nodes = graphData . nodes ;
486506
487507 // 1. Cluster Filter
@@ -515,7 +535,14 @@ document.addEventListener("DOMContentLoaded", () => {
515535 function updateStats ( ) {
516536 const { nodes } = getFilteredData ( ) ;
517537 if ( UI . count ) {
518- const tot = graphData . nodes . length ;
538+ const graphData = getGraphDataSafe ( ) ;
539+ const tot = graphData ? graphData . nodes . length : 0 ;
540+ if ( tot <= 0 ) {
541+ UI . count . innerText = "0 / 0 (0.0%)" ;
542+ renderTable ( nodes ) ;
543+ renderHistogram ( ) ;
544+ return ;
545+ }
519546 const pct = ( ( nodes . length / tot ) * 100 ) . toFixed ( 1 ) ;
520547 // Use translation for "Selected:" label is handled in HTML, here just numbers
521548 // But wait, the label is separate <span data-i18n="selected">
0 commit comments