| 1 |
/* |
| 2 |
* Copyright (C) 2024 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
/* This is the React component that renders all the iframes for the |
| 7 |
pages. |
| 8 |
|
| 9 |
We can't let React itself manipulate the iframe DOM elements, |
| 10 |
unfortunately, for these reasons: |
| 11 |
|
| 12 |
- We need to be super careful when setting the "src" attribute of |
| 13 |
an iframe element. Otherwise we get spurious browsing history |
| 14 |
entries that cause the Back button of browsers to behave |
| 15 |
erratically. |
| 16 |
|
| 17 |
- We need to adjust the window and document inside the iframe a bit. |
| 18 |
|
| 19 |
Thus, we use a giant useEffect hook to reimplement the incremental |
| 20 |
DOM updates that React would do for us. |
| 21 |
*/ |
| 22 |
|
| 23 |
import React, { useRef, useEffect } from 'react'; |
| 24 |
|
| 25 |
import { ShellState, ShellFrame } from "./state"; |
| 26 |
import { get_session_controller } from "./session"; |
| 27 |
|
| 28 |
export const Frames = ({ state, hidden }: { state: ShellState, hidden: boolean }) => { |
| 29 |
const content_ref = useRef<HTMLDivElement | null>(null); |
| 30 |
const { frames, current_frame } = state; |
| 31 |
|
| 32 |
useEffect(() => { |
| 33 |
if (!content_ref.current) |
| 34 |
return; |
| 35 |
|
| 36 |
const content = content_ref.current; |
| 37 |
|
| 38 |
function iframe_remove(elt: HTMLIFrameElement) { |
| 39 |
elt.remove(); |
| 40 |
} |
| 41 |
|
| 42 |
function iframe_new(name: string) { |
| 43 |
const elt = document.createElement("iframe"); |
| 44 |
elt.setAttribute("name", name); |
| 45 |
elt.style.display = "none"; |
| 46 |
content.appendChild(elt); |
| 47 |
return elt; |
| 48 |
} |
| 49 |
|
| 50 |
function setup_iframe(frame: ShellFrame, iframe: HTMLIFrameElement) { |
| 51 |
if (!iframe.contentWindow) |
| 52 |
return; |
| 53 |
|
| 54 |
get_session_controller().add_window(iframe.contentWindow); |
| 55 |
iframe.contentWindow.addEventListener("unload", () => teardown_iframe(frame), { once: true }); |
| 56 |
|
| 57 |
if (iframe.contentDocument && iframe.contentDocument.documentElement) { |
| 58 |
iframe.contentDocument.documentElement.lang = state.config.language; |
| 59 |
if (state.config.language_direction) |
| 60 |
iframe.contentDocument.documentElement.dir = state.config.language_direction; |
| 61 |
} |
| 62 |
|
| 63 |
if (!frame.ready) { |
| 64 |
frame.ready = true; |
| 65 |
state.update(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
function teardown_iframe(frame: ShellFrame) { |
| 70 |
if (frame.ready) { |
| 71 |
frame.ready = false; |
| 72 |
state.update(); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
const iframes_by_name: Record<string, HTMLIFrameElement> = {}; |
| 77 |
|
| 78 |
for (let i = 0; i < content.children.length; i++) { |
| 79 |
const c = content.children[i]; |
| 80 |
const name = c.getAttribute('name'); |
| 81 |
if (c.nodeName == "IFRAME" && name) { |
| 82 |
iframes_by_name[name] = c as HTMLIFrameElement; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Remove obsolete iframes |
| 87 |
for (const name in iframes_by_name) { |
| 88 |
if (!frames[name] || frames[name].url == null) |
| 89 |
iframe_remove(iframes_by_name[name]); |
| 90 |
} |
| 91 |
|
| 92 |
// Add new and update existing iframes |
| 93 |
for (const name in frames) { |
| 94 |
const frame = frames[name]; |
| 95 |
if (!frame.url) |
| 96 |
continue; |
| 97 |
|
| 98 |
let iframe = iframes_by_name[name]; |
| 99 |
|
| 100 |
if (!iframe) { |
| 101 |
iframe = iframe_new(name); |
| 102 |
iframe.setAttribute("class", "container-frame"); |
| 103 |
iframe.setAttribute("data-host", frame.host); |
| 104 |
iframe.addEventListener("load", () => setup_iframe(frame, iframe)); |
| 105 |
} |
| 106 |
|
| 107 |
if (iframe.getAttribute("title") != frame.title) |
| 108 |
iframe.setAttribute("title", frame.title); |
| 109 |
|
| 110 |
if (frame.loaded && iframe.getAttribute("data-loaded") == null) |
| 111 |
iframe.setAttribute("data-loaded", "1"); |
| 112 |
else if (!frame.loaded && iframe.getAttribute("data-loaded")) |
| 113 |
iframe.removeAttribute("data-loaded"); |
| 114 |
|
| 115 |
const src = frame.url + "#" + frame.hash; |
| 116 |
|
| 117 |
if (iframe.getAttribute('src') != src) { |
| 118 |
if (iframe.contentWindow) { |
| 119 |
// This prevents the browser from creating a new |
| 120 |
// history entry. It would do that whenever the "src" |
| 121 |
// of a frame is changed and the window location is |
| 122 |
// not consistent with the new "src" value. |
| 123 |
// |
| 124 |
// This matters when a "jump" command changes both |
| 125 |
// the current frame and the hash of the newly |
| 126 |
// current frame. |
| 127 |
iframe.contentWindow.location.replace(src); |
| 128 |
} |
| 129 |
iframe.setAttribute('src', src); |
| 130 |
} |
| 131 |
|
| 132 |
iframe.style.display = (!hidden && frame == current_frame && frame.ready) ? "block" : "none"; |
| 133 |
} |
| 134 |
}); |
| 135 |
|
| 136 |
return <div ref={content_ref} |
| 137 |
id="content" |
| 138 |
className="area-ct-content" |
| 139 |
role="main" |
| 140 |
tabIndex={-1} />; |
| 141 |
}; |