Files
port-ui/app/static/js/iframe.js

159 lines
5.5 KiB
JavaScript
Raw Normal View History

2025-03-19 17:16:44 +01:00
// Global variables to store elements and original state
let mainElement, originalContent, originalMainStyle, container, customScrollbar, scrollbarContainer;
// Synchronize the height of the iframe to match the scroll-container or main element
function syncIframeHeight() {
const iframe = mainElement.querySelector("iframe");
if (iframe) {
console.log("Setting iframe height based on scroll-container inline styles...");
if (scrollbarContainer) {
// Prefer inline height, otherwise inline max-height
const inlineHeight = scrollbarContainer.style.height;
const inlineMax = scrollbarContainer.style.maxHeight;
const target = inlineHeight || inlineMax;
if (target) {
console.log("Using scroll-container inline style:", target);
iframe.style.height = target;
} else {
console.warn("No inline height or max-height set on scroll-container. Using main element height instead.");
iframe.style.height = mainElement.style.height;
}
} else {
console.log("No scroll-container found. Using main element height:", mainElement.style.height);
iframe.style.height = mainElement.style.height;
}
} else {
console.log("No iframe to resize.");
2025-03-19 16:53:49 +01:00
}
}
2025-03-19 16:14:06 +01:00
// Function to open a URL in an iframe
function openIframe(url) {
// Hide the container (and its scroll-container) so the iframe can appear in its place
if (scrollbarContainer) {
scrollbarContainer.style.display = 'none';
2025-03-19 16:53:49 +01:00
}
2025-03-19 16:14:06 +01:00
// Hide any custom scrollbar element if present
2025-03-19 17:16:44 +01:00
if (customScrollbar) {
customScrollbar.style.display = 'none';
2025-03-19 17:16:44 +01:00
}
2025-03-18 14:59:54 +01:00
// Create or retrieve the iframe in the main element
2025-03-19 17:16:44 +01:00
let iframe = mainElement.querySelector("iframe");
if (!iframe) {
iframe = document.createElement("iframe");
iframe.width = "100%";
iframe.style.border = "none";
iframe.style.overflow = "auto"; // Enable internal scrolling
iframe.scrolling = "auto";
2025-03-19 17:16:44 +01:00
mainElement.appendChild(iframe);
syncIframeHeight();
2025-03-19 17:16:44 +01:00
}
2025-03-18 14:59:54 +01:00
// Set the iframe's source URL
2025-03-19 17:16:44 +01:00
iframe.src = url;
2025-07-05 11:54:20 +02:00
// Push the new URL state without reloading the page
2025-07-05 11:54:20 +02:00
const newUrl = new URL(window.location);
newUrl.searchParams.set('iframe', url);
window.history.pushState({ iframe: url }, '', newUrl);
2025-03-19 17:16:44 +01:00
}
2025-03-18 14:59:54 +01:00
// Function to restore the original content and show the container again
2025-07-05 13:17:38 +02:00
function restoreOriginal() {
// Remove the iframe from the DOM
const iframe = mainElement.querySelector("iframe");
if (iframe) {
iframe.remove();
2025-07-05 13:17:38 +02:00
}
// Show the original container
if (scrollbarContainer) {
scrollbarContainer.style.display = '';
2025-07-05 13:17:38 +02:00
}
// Restore any custom scrollbar
2025-07-05 13:17:38 +02:00
if (customScrollbar) {
customScrollbar.style.display = '';
2025-07-05 13:17:38 +02:00
}
// Restore the original inline style of the main element
if (originalMainStyle !== null) {
mainElement.setAttribute("style", originalMainStyle);
} else {
mainElement.removeAttribute("style");
2025-07-05 13:17:38 +02:00
}
// Update the URL to remove the iframe parameter
2025-07-05 13:17:38 +02:00
const newUrl = new URL(window.location);
newUrl.searchParams.delete("iframe");
window.history.pushState({}, '', newUrl);
2025-07-05 13:17:38 +02:00
}
// Initialize event listeners after DOM content is loaded
document.addEventListener("DOMContentLoaded", function() {
// Cache references to elements and original state
2025-03-19 17:16:44 +01:00
mainElement = document.querySelector("main");
originalContent = mainElement.innerHTML;
originalMainStyle = mainElement.getAttribute("style"); // may be null
2025-03-19 17:16:44 +01:00
container = document.querySelector(".container");
customScrollbar = document.getElementById("custom-scrollbar");
scrollbarContainer = container.querySelector(".scroll-container")
2025-03-18 15:03:03 +01:00
// Attach click handlers to links that should open in an iframe
2025-07-05 13:17:38 +02:00
document.querySelectorAll(".iframe-link").forEach(link => {
link.addEventListener("click", function(event) {
event.preventDefault(); // prevent full page navigation
2025-07-05 13:17:38 +02:00
openIframe(this.href);
2025-07-05 18:00:23 +02:00
updateUrlFullWidth(true);
2025-03-18 14:59:54 +01:00
});
});
// Clicking the header's H1 will restore the original view
2025-03-19 17:16:44 +01:00
const headerH1 = document.querySelector("header h1");
if (headerH1) {
2025-03-19 17:20:08 +01:00
headerH1.style.cursor = "pointer";
2025-07-05 13:17:38 +02:00
headerH1.addEventListener("click", restoreOriginal);
2025-03-19 16:14:06 +01:00
}
// On full page load, check URL parameters to auto-open an iframe
window.addEventListener("load", function() {
const params = new URLSearchParams(window.location.search);
const iframeUrl = params.get('iframe');
2025-07-05 13:17:38 +02:00
if (iframeUrl) {
openIframe(iframeUrl);
}
});
2025-07-05 11:54:20 +02:00
});
// Handle browser back/forward navigation
window.addEventListener('popstate', function(event) {
const params = new URLSearchParams(window.location.search);
const iframeUrl = params.get('iframe');
2025-07-05 11:54:20 +02:00
if (iframeUrl) {
openIframe(iframeUrl);
} else {
restoreOriginal();
2025-07-05 11:54:20 +02:00
}
2025-03-18 14:59:54 +01:00
});
/**
* Opens the current iframe URL in a new browser tab.
*/
function openIframeInNewTab() {
const params = new URLSearchParams(window.location.search);
const iframeUrl = params.get('iframe');
if (iframeUrl) {
window.open(iframeUrl, '_blank');
} else {
alert('No iframe is currently open.');
}
}
// expose globally so your templates onclick can find it
window.openIframeInNewTab = openIframeInNewTab;
// Adjust iframe height on window resize
window.addEventListener('resize', syncIframeHeight);