|
7 | 7 | (function() { |
8 | 8 | 'use strict'; |
9 | 9 |
|
| 10 | + let loadingHidden = false; |
| 11 | + let blazorReady = false; |
| 12 | + let initialRenderComplete = false; |
| 13 | + |
10 | 14 | // Load required stylesheets |
11 | 15 | function loadStyles() { |
12 | 16 | const styles = [ |
|
79 | 83 | document.head.insertAdjacentHTML('beforeend', loadingStyle); |
80 | 84 | } |
81 | 85 |
|
82 | | - // Initialize Blazor Server with timeout fallback |
| 86 | + // Update loading text (minimal usage) |
| 87 | + function updateLoadingText(text) { |
| 88 | + const loadingTextEl = document.getElementById('elsa-loading-text'); |
| 89 | + if (loadingTextEl) { |
| 90 | + loadingTextEl.textContent = text; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Hide loading screen when actually ready |
| 95 | + function hideLoadingScreen() { |
| 96 | + if (!loadingHidden) { |
| 97 | + loadingHidden = true; |
| 98 | + console.log('Blazor Server ready - hiding loading screen'); |
| 99 | + document.body.classList.add('blazor-ready'); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Check if we should hide loading screen |
| 104 | + function checkReadiness() { |
| 105 | + if (blazorReady && initialRenderComplete && !loadingHidden) { |
| 106 | + hideLoadingScreen(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Detect when Blazor Server connection is established |
| 111 | + function detectBlazorConnection() { |
| 112 | + // Check for Blazor global object and connection |
| 113 | + if (typeof window.Blazor !== 'undefined') { |
| 114 | + // Monitor for SignalR connection |
| 115 | + const originalLog = console.log; |
| 116 | + console.log = function(...args) { |
| 117 | + const message = args.join(' '); |
| 118 | + if (message.includes('SignalR') || message.includes('connected') || message.includes('circuit')) { |
| 119 | + blazorReady = true; |
| 120 | + checkReadiness(); |
| 121 | + } |
| 122 | + originalLog.apply(console, args); |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | + // Check for Blazor Server specific DOM changes |
| 127 | + const observer = new MutationObserver(function(mutations) { |
| 128 | + mutations.forEach(function(mutation) { |
| 129 | + if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { |
| 130 | + for (let node of mutation.addedNodes) { |
| 131 | + if (node.nodeType === Node.ELEMENT_NODE) { |
| 132 | + // Check for Blazor component markers |
| 133 | + if (node.hasAttribute && ( |
| 134 | + node.hasAttribute('_bl_') || |
| 135 | + node.querySelector && node.querySelector('[_bl_]') || |
| 136 | + node.classList && node.classList.contains('mud-main-content') || |
| 137 | + node.tagName === 'APP' |
| 138 | + )) { |
| 139 | + if (!blazorReady) { |
| 140 | + blazorReady = true; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + observer.observe(document.body, { |
| 150 | + childList: true, |
| 151 | + subtree: true, |
| 152 | + attributes: true |
| 153 | + }); |
| 154 | + |
| 155 | + // Stop observing after readiness is detected |
| 156 | + setTimeout(() => { |
| 157 | + if (blazorReady || loadingHidden) { |
| 158 | + observer.disconnect(); |
| 159 | + } |
| 160 | + }, 10000); |
| 161 | + } |
| 162 | + |
| 163 | + // Detect when initial render is complete |
| 164 | + function detectRenderCompletion() { |
| 165 | + // Check for common Blazor/MudBlazor elements |
| 166 | + function checkForMainContent() { |
| 167 | + const indicators = [ |
| 168 | + '.mud-main-content', |
| 169 | + '.mud-layout', |
| 170 | + '[role="main"]', |
| 171 | + '.elsa-main-layout', |
| 172 | + '.mud-drawer', |
| 173 | + '.mud-appbar' |
| 174 | + ]; |
| 175 | + |
| 176 | + for (let selector of indicators) { |
| 177 | + const element = document.querySelector(selector); |
| 178 | + if (element && element.offsetHeight > 0) { |
| 179 | + initialRenderComplete = true; |
| 180 | + checkReadiness(); |
| 181 | + return true; |
| 182 | + } |
| 183 | + } |
| 184 | + return false; |
| 185 | + } |
| 186 | + |
| 187 | + // Use requestAnimationFrame to detect when rendering settles |
| 188 | + let frameCount = 0; |
| 189 | + let lastBodyHeight = 0; |
| 190 | + let stableFrames = 0; |
| 191 | + |
| 192 | + function checkRenderStability() { |
| 193 | + frameCount++; |
| 194 | + const currentHeight = document.body.offsetHeight; |
| 195 | + |
| 196 | + if (currentHeight === lastBodyHeight && currentHeight > 100) { |
| 197 | + stableFrames++; |
| 198 | + if (stableFrames >= 5) { // 5 stable frames |
| 199 | + if (!initialRenderComplete && checkForMainContent()) { |
| 200 | + return; |
| 201 | + } else if (!initialRenderComplete && frameCount > 30) { |
| 202 | + initialRenderComplete = true; |
| 203 | + checkReadiness(); |
| 204 | + return; |
| 205 | + } |
| 206 | + } |
| 207 | + } else { |
| 208 | + stableFrames = 0; |
| 209 | + lastBodyHeight = currentHeight; |
| 210 | + } |
| 211 | + |
| 212 | + if (frameCount < 100 && !initialRenderComplete) { |
| 213 | + requestAnimationFrame(checkRenderStability); |
| 214 | + } else if (!initialRenderComplete) { |
| 215 | + // Fallback after 100 frames |
| 216 | + initialRenderComplete = true; |
| 217 | + checkReadiness(); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + // Start checking on next frame |
| 222 | + requestAnimationFrame(checkRenderStability); |
| 223 | + |
| 224 | + // Also check periodically |
| 225 | + const intervalCheck = setInterval(() => { |
| 226 | + if (checkForMainContent()) { |
| 227 | + clearInterval(intervalCheck); |
| 228 | + } else if (frameCount > 100) { |
| 229 | + clearInterval(intervalCheck); |
| 230 | + } |
| 231 | + }, 200); |
| 232 | + } |
| 233 | + |
| 234 | + // Enhanced Blazor Server initialization |
83 | 235 | function initializeBlazorServer(maxWaitMs) { |
84 | | - maxWaitMs = maxWaitMs || 10000; |
| 236 | + maxWaitMs = maxWaitMs || 8000; // Fallback timeout |
| 237 | + |
| 238 | + // Start detection methods |
| 239 | + setTimeout(() => detectBlazorConnection(), 100); |
| 240 | + setTimeout(() => detectRenderCompletion(), 500); |
| 241 | + |
| 242 | + // Ultimate fallback timeout |
85 | 243 | setTimeout(function() { |
86 | | - document.body.classList.add('blazor-ready'); |
| 244 | + if (!loadingHidden) { |
| 245 | + console.warn('Fallback timeout reached - forcing loading screen to hide'); |
| 246 | + blazorReady = true; |
| 247 | + initialRenderComplete = true; |
| 248 | + hideLoadingScreen(); |
| 249 | + } |
87 | 250 | }, maxWaitMs); |
88 | 251 | } |
89 | 252 |
|
|
92 | 255 | loadStyles(); |
93 | 256 | injectLoadingScreen(); |
94 | 257 | loadScripts(); |
95 | | - initializeBlazorServer(10000); |
| 258 | + initializeBlazorServer(8000); |
96 | 259 | } |
97 | 260 |
|
98 | 261 | // Run initialization |
|
104 | 267 |
|
105 | 268 | // Expose API for manual control if needed |
106 | 269 | window.ElsaStudio = window.ElsaStudio || {}; |
107 | | - window.ElsaStudio.hideLoading = function() { |
108 | | - document.body.classList.add('blazor-ready'); |
| 270 | + window.ElsaStudio.hideLoading = hideLoadingScreen; |
| 271 | + window.ElsaStudio.updateLoadingText = updateLoadingText; |
| 272 | + window.ElsaStudio.forceReady = function() { |
| 273 | + blazorReady = true; |
| 274 | + initialRenderComplete = true; |
| 275 | + checkReadiness(); |
109 | 276 | }; |
110 | 277 |
|
111 | 278 | })(); |
0 commit comments