File size: 3,735 Bytes
cdfc1f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**

 * Main Application Entry Point

 * Initializes and starts the Electron Cloud Visualizer

 */

let appController = null;

/**

 * Display error message to user

 * @param {string} message

 */
function displayError(message) {
    const errorContainer = document.getElementById('error-container');
    const errorText = document.getElementById('error-text');
    
    if (errorContainer && errorText) {
        errorText.textContent = message;
        errorContainer.style.display = 'flex';
    }
    
    console.error('Application Error:', message);
}

/**

 * Hide loading overlay

 */
function hideLoadingOverlay() {
    const loadingOverlay = document.getElementById('loading-overlay');
    if (loadingOverlay) {
        loadingOverlay.style.display = 'none';
    }
}

/**

 * Show loading overlay

 */
function showLoadingOverlay() {
    const loadingOverlay = document.getElementById('loading-overlay');
    if (loadingOverlay) {
        loadingOverlay.style.display = 'flex';
    }
}

/**

 * Initialize the application

 */
async function initializeApp() {
    const startTime = performance.now();
    
    console.log('Starting initialization...');
    
    try {
        // Get canvas element
        const canvas = document.getElementById('canvas');
        if (!canvas) {
            throw new Error('Canvas element not found');
        }
        
        console.log('Canvas found');
        
        // Show loading indicator if initialization takes > 500ms
        const loadingTimeout = setTimeout(() => {
            showLoadingOverlay();
        }, 500);
        
        console.log('Creating ApplicationController...');
        
        // Create and initialize application controller
        appController = new ApplicationController();
        
        console.log('Initializing application...');
        await appController.initialize(canvas);
        
        console.log('Application initialized successfully');
        
        // Clear loading timeout
        clearTimeout(loadingTimeout);
        
        // Hide loading overlay
        hideLoadingOverlay();
        
        const elapsed = performance.now() - startTime;
        console.log(`Application initialized in ${elapsed.toFixed(0)}ms`);
        
        // Log initial state
        console.log('Initial state:', appController.getState());
        
    } catch (error) {
        console.error('Failed to initialize application:', error);
        console.error('Error stack:', error.stack);
        hideLoadingOverlay();
        displayError(error.message || 'Failed to initialize application. Please refresh the page and try again.');
    }
}

/**

 * Handle window unload

 */
function handleUnload() {
    if (appController) {
        appController.dispose();
    }
}

// Initialize when DOM is ready
if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', initializeApp);
} else {
    initializeApp();
}

// Clean up on page unload
window.addEventListener('beforeunload', handleUnload);

// Handle window visibility changes (pause/resume)
document.addEventListener('visibilitychange', () => {
    if (document.hidden) {
        console.log('Application paused (tab hidden)');
    } else {
        console.log('Application resumed (tab visible)');
    }
});

// Expose app controller for debugging
window.electronCloudApp = {
    getController: () => appController,
    getState: () => appController ? appController.getState() : null,
    reset: () => appController ? appController.reset() : null
};

console.log('Electron Cloud Visualizer loaded. Access via window.electronCloudApp');