1+ // app.js
12import Modal from './design-system/components/modal/modal.js' ;
2- import SimulationLoader from './simulation-loader.js' ;
3- import EventBus from './event-bus.js' ;
4- import TabNotifier from './tab-notifier.js' ;
5- import ScenarioEngine from './scenario-engine.js' ;
63
74let websocket = null ;
85let helpModal = null ;
9- let loader = null ;
10- let eventBus = null ;
11- let notifier = null ;
12- let scenarioEngine = null ;
136
7+ // Initialize WebSocket connection
148function initializeWebSocket ( ) {
159 const protocol = window . location . protocol === 'https:' ? 'wss:' : 'ws:' ;
1610 const host = window . location . host ;
@@ -19,41 +13,39 @@ function initializeWebSocket() {
1913 try {
2014 websocket = new WebSocket ( wsUrl ) ;
2115
22- websocket . onopen = ( ) => {
16+ websocket . onopen = function ( event ) {
2317 console . log ( 'WebSocket connected' ) ;
2418 } ;
2519
26- websocket . onmessage = ( event ) => {
20+ websocket . onmessage = function ( event ) {
2721 try {
2822 const data = JSON . parse ( event . data ) ;
2923 if ( data . type === 'message' && data . message ) {
30- if ( loader ) {
31- loader . sendMessage ( data ) ;
32- } else {
33- alert ( data . message ) ;
34- }
24+ alert ( data . message ) ;
3525 }
3626 } catch ( error ) {
3727 console . error ( 'Error parsing WebSocket message:' , error ) ;
3828 }
3929 } ;
4030
41- websocket . onclose = ( ) => {
31+ websocket . onclose = function ( event ) {
4232 console . log ( 'WebSocket disconnected' ) ;
33+ // Attempt to reconnect after 3 seconds
4334 setTimeout ( ( ) => {
4435 console . log ( 'Attempting to reconnect WebSocket...' ) ;
4536 initializeWebSocket ( ) ;
4637 } , 3000 ) ;
4738 } ;
4839
49- websocket . onerror = ( error ) => {
40+ websocket . onerror = function ( error ) {
5041 console . error ( 'WebSocket error:' , error ) ;
5142 } ;
5243 } catch ( error ) {
5344 console . error ( 'Failed to create WebSocket connection:' , error ) ;
5445 }
5546}
5647
48+ // Load help content and initialize modal
5749async function initializeHelpModal ( ) {
5850 try {
5951 const response = await fetch ( './help-content.html' ) ;
@@ -66,13 +58,15 @@ async function initializeHelpModal() {
6658
6759 const helpButton = document . getElementById ( 'btn-help' ) ;
6860 if ( helpButton ) {
69- helpButton . addEventListener ( 'click' , ( ) => helpModal . open ( ) ) ;
61+ helpButton . addEventListener ( 'click' , ( ) => {
62+ helpModal . open ( ) ;
63+ } ) ;
7064 }
7165 } catch ( error ) {
7266 console . error ( 'Failed to load help content:' , error ) ;
7367 helpModal = Modal . createHelpModal ( {
7468 title : 'Help / User Guide' ,
75- content : '<p>Help content could not be loaded.</p>'
69+ content : '<p>Help content could not be loaded. Please check that help-content.html exists. </p>'
7670 } ) ;
7771 const helpButton = document . getElementById ( 'btn-help' ) ;
7872 if ( helpButton ) {
@@ -81,59 +75,14 @@ async function initializeHelpModal() {
8175 }
8276}
8377
84- async function initializeSimulations ( ) {
85- const containerEl = document . getElementById ( 'sim-container' ) ;
86- const tabBarEl = document . getElementById ( 'sim-tab-bar' ) ;
87-
88- eventBus = new EventBus ( ) ;
89-
90- loader = new SimulationLoader ( {
91- containerEl,
92- tabBarEl,
93- eventBus,
94- onActivate : ( simId ) => {
95- console . log ( `Simulation activated: ${ simId } ` ) ;
96- if ( notifier ) notifier . clearBadge ( simId ) ;
97- } ,
98- onDeactivate : ( simId ) => {
99- console . log ( `Simulation deactivated: ${ simId } ` ) ;
100- }
101- } ) ;
102-
103- notifier = new TabNotifier ( loader ) ;
104- scenarioEngine = new ScenarioEngine ( { eventBus, loader, notifier } ) ;
105-
106- try {
107- const configRes = await fetch ( '/simulations/simulations.json' ) ;
108- const config = await configRes . json ( ) ;
109-
110- await loader . loadConfig ( '/simulations/simulations.json' ) ;
111-
112- const urlParams = new URLSearchParams ( window . location . search ) ;
113- const scenarioParam = urlParams . get ( 'scenario' ) || config . scenario ;
114- if ( scenarioParam ) {
115- await scenarioEngine . loadScenario ( `/simulations/scenarios/${ scenarioParam } .js` ) ;
116- }
117- } catch ( error ) {
118- console . error ( 'Failed to load simulations config:' , error ) ;
119- containerEl . innerHTML = `
120- <div class="sim-empty">
121- <p>No simulations configured. Place simulations.json in the simulations/ directory.</p>
122- </div>
123- ` ;
124- }
125- }
126-
78+ // Initialize both help modal and WebSocket when DOM is ready
12779async function initialize ( ) {
12880 await initializeHelpModal ( ) ;
12981 initializeWebSocket ( ) ;
130- await initializeSimulations ( ) ;
13182}
13283
13384if ( document . readyState === 'loading' ) {
13485 document . addEventListener ( 'DOMContentLoaded' , initialize ) ;
13586} else {
13687 initialize ( ) ;
13788}
138-
139- export { eventBus , loader , notifier , scenarioEngine } ;
0 commit comments