| 1 |
/** |
| 2 |
* MetaSync iframe height auto-resize. |
| 3 |
* |
| 4 |
* Extracted from admin/class-metasync-admin.php (Phase 5, #887). |
| 5 |
*/ |
| 6 |
function adjustIframeHeight(iframe) { |
| 7 |
var attempts = 0; |
| 8 |
var maxAttempts = 20; // Try for up to 10 seconds |
| 9 |
|
| 10 |
function tryAdjustHeight() { |
| 11 |
try { |
| 12 |
attempts++; |
| 13 |
|
| 14 |
// Try to access iframe content height |
| 15 |
var iframeDocument = iframe.contentDocument || iframe.contentWindow.document; |
| 16 |
if (iframeDocument) { |
| 17 |
// Wait for content to load by checking if body has meaningful content |
| 18 |
var body = iframeDocument.body; |
| 19 |
var hasContent = body && (body.children.length > 1 || body.innerText.trim().length > 100); |
| 20 |
|
| 21 |
if (!hasContent && attempts < maxAttempts) { |
| 22 |
// Content still loading, try again |
| 23 |
setTimeout(tryAdjustHeight, 500); |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
var height = Math.max( |
| 28 |
body ? body.scrollHeight : 0, |
| 29 |
body ? body.offsetHeight : 0, |
| 30 |
iframeDocument.documentElement.clientHeight, |
| 31 |
iframeDocument.documentElement.scrollHeight, |
| 32 |
iframeDocument.documentElement.offsetHeight |
| 33 |
); |
| 34 |
|
| 35 |
// Only apply if we got a reasonable height |
| 36 |
if (height > 600) { |
| 37 |
iframe.style.height = height + 'px'; |
| 38 |
} else if (attempts < maxAttempts) { |
| 39 |
// Height too small, content probably still loading |
| 40 |
setTimeout(tryAdjustHeight, 500); |
| 41 |
return; |
| 42 |
} |
| 43 |
} else { |
| 44 |
// Can't access content, try again or fallback |
| 45 |
if (attempts < maxAttempts) { |
| 46 |
setTimeout(tryAdjustHeight, 500); |
| 47 |
return; |
| 48 |
} |
| 49 |
} |
| 50 |
} catch (e) { |
| 51 |
// Cross-origin restrictions - use viewport height |
| 52 |
iframe.style.height = '100vh'; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// Start the height adjustment process |
| 57 |
tryAdjustHeight(); |
| 58 |
} |
| 59 |
|
| 60 |
// Bind to the dashboard iframe via addEventListener |
| 61 |
(function() { |
| 62 |
var iframe = document.getElementById('metasync-dashboard-iframe'); |
| 63 |
if (iframe) { |
| 64 |
iframe.addEventListener('load', function() { |
| 65 |
adjustIframeHeight(iframe); |
| 66 |
}); |
| 67 |
} |
| 68 |
|
| 69 |
// Also listen for window resize |
| 70 |
window.addEventListener('resize', function() { |
| 71 |
var iframe = document.getElementById('metasync-dashboard-iframe'); |
| 72 |
if (iframe) { |
| 73 |
adjustIframeHeight(iframe); |
| 74 |
} |
| 75 |
}); |
| 76 |
|
| 77 |
// Additional attempt after 3 seconds (for very slow loading apps) |
| 78 |
setTimeout(function() { |
| 79 |
var iframe = document.getElementById('metasync-dashboard-iframe'); |
| 80 |
if (iframe) { |
| 81 |
adjustIframeHeight(iframe); |
| 82 |
} |
| 83 |
}, 3000); |
| 84 |
})(); |
| 85 |
|