|
329 | 329 | } |
330 | 330 | }); |
331 | 331 |
|
| 332 | + function alertPatch(message) { |
| 333 | + // Create the modal container |
| 334 | + const modal = document.createElement('div'); |
| 335 | + modal.style.position = 'fixed'; |
| 336 | + modal.style.top = '0'; |
| 337 | + modal.style.left = '0'; |
| 338 | + modal.style.width = '100%'; |
| 339 | + modal.style.height = '100vh'; |
| 340 | + modal.style.backgroundColor = 'rgba(0,0,0,0.5)'; |
| 341 | + modal.style.display = 'flex'; |
| 342 | + modal.style.justifyContent = 'center'; |
| 343 | + modal.style.alignItems = 'center'; |
| 344 | + modal.style.zIndex = '1000000000'; |
| 345 | + |
| 346 | + // Create the modal content box |
| 347 | + const modalContent = document.createElement('div'); |
| 348 | + modalContent.style.backgroundColor = 'white'; |
| 349 | + modalContent.style.padding = '20px'; |
| 350 | + modalContent.style.borderRadius = '5px'; |
| 351 | + modalContent.style.minWidth = '300px'; |
| 352 | + modalContent.style.margin = 'auto'; |
| 353 | + modalContent.style.textAlign = 'center'; |
| 354 | + |
| 355 | + // Add title to the modal with the current page URL |
| 356 | + const title = document.createElement('h3'); |
| 357 | + title.textContent = "alert"; // not translated as window.alert is same in all languages. |
| 358 | + title.style.marginBottom = '10px'; |
| 359 | + |
| 360 | + // Add text to the modal |
| 361 | + const text = document.createElement('p'); |
| 362 | + text.textContent = message; |
| 363 | + text.style.marginBottom = '20px'; |
| 364 | + |
| 365 | + // Create OK button to close the modal |
| 366 | + const button = document.createElement('button'); |
| 367 | + button.textContent = 'OK'; |
| 368 | + button.style.padding = '10px 20px'; |
| 369 | + button.style.border = 'none'; |
| 370 | + button.style.backgroundColor = '#007BFF'; |
| 371 | + button.style.color = 'white'; |
| 372 | + button.style.borderRadius = '5px'; |
| 373 | + button.style.cursor = 'pointer'; |
| 374 | + |
| 375 | + button.onclick = function() { |
| 376 | + document.body.removeChild(modal); |
| 377 | + }; |
| 378 | + |
| 379 | + // Append elements |
| 380 | + modalContent.appendChild(title); |
| 381 | + modalContent.appendChild(text); |
| 382 | + modalContent.appendChild(button); |
| 383 | + modal.appendChild(modalContent); |
| 384 | + document.body.appendChild(modal); |
| 385 | + } |
| 386 | + |
| 387 | + function unsupported() { |
| 388 | + alertPatch(TRANSPORT_CONFIG.STRINGS.UNSUPPORTED_DOM_APIS_CONFIRM); |
| 389 | + } |
| 390 | + |
| 391 | + // all externally opened live previews have the phcodeLivePreview="true" query string parameter set. |
| 392 | + const currentUrl = new URL(window.location.href); |
| 393 | + const queryParams = new URLSearchParams(currentUrl.search); |
| 394 | + const isExternalBrowser = queryParams.get("phcodeLivePreview") === "true"; |
| 395 | + const isTauri = TRANSPORT_CONFIG.IS_NATIVE_APP; |
| 396 | + const platform = TRANSPORT_CONFIG.PLATFORM; |
| 397 | + |
| 398 | + let alertQueue = [], confirmOrPromptCalled = false; |
| 399 | + let addToQueue = true; |
| 400 | + if(!isExternalBrowser){ |
| 401 | + // this is an embedded iframe we always take hold of the alert api for better ux within the live preivew frame. |
| 402 | + window.__PHOENIX_EMBED_INFO = {isTauri, platform}; |
| 403 | + const shouldPatchAlert = (isTauri && platform === "mac"); |
| 404 | + if(shouldPatchAlert){ |
| 405 | + // In Mac embedded live preview iframe in tauri, alert, prompt, and confirm apis |
| 406 | + // are not available, so we need to patch the other apis in mac |
| 407 | + window.alert = function (...args) { |
| 408 | + // at this time, we cant add our html alert as body is not loaded yet. So we queue alerts. |
| 409 | + addToQueue && alertQueue.push(...args); |
| 410 | + }; |
| 411 | + window.confirm = function () { |
| 412 | + // confirm and prompt is no-op in mac, we just need to show that the api is not supported, so we just |
| 413 | + // keep a flag. |
| 414 | + confirmOrPromptCalled = true; |
| 415 | + }; |
| 416 | + window.prompt = function () { |
| 417 | + confirmOrPromptCalled = true; |
| 418 | + }; |
| 419 | + function drainAlertQueues() { |
| 420 | + addToQueue = false; |
| 421 | + if(confirmOrPromptCalled) { |
| 422 | + unsupported(); |
| 423 | + } |
| 424 | + for(let i=0; i<alertQueue.length; i++) { |
| 425 | + alertPatch(alertQueue[i]); |
| 426 | + } |
| 427 | + alertQueue = []; |
| 428 | + window.alert = alertPatch; |
| 429 | + window.confirm = unsupported; |
| 430 | + window.prompt = unsupported; |
| 431 | + } |
| 432 | + |
| 433 | + document.addEventListener('DOMContentLoaded', function() { |
| 434 | + drainAlertQueues(); |
| 435 | + }); |
| 436 | + } |
| 437 | + } |
| 438 | + |
332 | 439 | // this is for managing who am i context in iframes embedded in phoenix to have special handling. |
333 | 440 | window.addEventListener('message', function(event) { |
334 | 441 | if (!TRANSPORT_CONFIG.TRUSTED_ORIGINS_EMBED[event.origin]) { |
335 | 442 | return; // Ignore messages from unexpected origins |
336 | 443 | } |
337 | 444 | if(event.data.type === "WHO_AM_I_RESPONSE") { |
338 | | - window.__PHOENIX_EMBED_INFO = { |
339 | | - isTauri: event.data.isTauri, |
340 | | - platform: event.data.platform |
341 | | - }; |
| 445 | + if(!window.__PHOENIX_EMBED_INFO){ |
| 446 | + // this is set from transport config. We should be here |
| 447 | + console.error("Expected window.__PHOENIX_EMBED_INFO to be set, but not???"); |
| 448 | + } |
342 | 449 | } |
343 | 450 | }); |
344 | 451 | if(window.self !== window.parent){ |
|
0 commit comments