// Host side of NyxxWorks in the A1111 family: a button that shows NyxxWorks as a layer over the // whole host page, in the same tab. The layer is made on the first press and only hidden after // that, never removed: the host page keeps its state under it, and NyxxWorks keeps its own // while hidden. /nyxxworks/ui opened on its own is still a whole page. // Host JS: no arrow functions, no template literals. (function () { var PAGE = '/nyxxworks/ui?in=host'; var ID = 'nyxxworks_open'; // The page's own colors, as the one place they are written here: its --canvas, the ground // of the layer while the page loads, and its --primary, the dot while a job runs. var CANVAS = 'hsl(222, 20%, 6%)'; var PRIMARY = 'hsl(24, 95%, 53%)'; // Above everything of the host: its lightbox is at 1001, the highest seen on reForge with // its extensions at 10003. var LAYER_Z = 100000; var button = null; var dot = null; var layer = null; var frame = null; var shown = false; var unkept = false; var overflow = ''; function app() { return (typeof gradioApp === 'function') ? gradioApp() : document; } function scroller() { return document.scrollingElement || document.documentElement; } // The browser's own question on closing or reloading the tab, while a tool holds work. function ask(event) { event.preventDefault(); event.returnValue = ''; return ''; } function focusFrame() { frame.focus(); try { frame.contentWindow.focus(); } catch (e) { // The frame is of the same origin; nothing to do if it cannot take the focus yet. } } function make() { layer = document.createElement('div'); layer.id = 'nyxxworks_layer'; // The size of the window, not of the room the host's scroll bar leaves: the bar comes // back while the layer is hidden, and the page must keep one size either way. layer.style.cssText = 'position:fixed;left:0;top:0;width:100vw;height:100vh;z-index:' + LAYER_Z + ';background:' + CANVAS + ';'; var loading = document.createElement('div'); loading.textContent = 'Loading NyxxWorks…'; loading.style.cssText = 'position:absolute;inset:0;display:flex;align-items:center;justify-content:center;' + 'font-size:14px;color:var(--body-text-color-subdued, #9ca3af);'; frame = document.createElement('iframe'); frame.src = PAGE; frame.title = 'NyxxWorks'; frame.style.cssText = 'position:absolute;inset:0;width:100%;height:100%;border:0;visibility:hidden;'; frame.addEventListener('load', function () { if (loading.parentNode) { loading.parentNode.removeChild(loading); } frame.style.visibility = ''; if (shown) { focusFrame(); } }); layer.appendChild(loading); layer.appendChild(frame); document.body.appendChild(layer); } function show() { if (!layer) { make(); } if (shown) { return; } shown = true; layer.style.visibility = 'visible'; layer.style.pointerEvents = ''; layer.removeAttribute('inert'); overflow = scroller().style.overflow; scroller().style.overflow = 'hidden'; focusFrame(); } // Hidden, not taken out of the layout: the page keeps its size, so nothing in it is laid // out anew and nothing jumps when it is shown again. function hide() { if (!shown) { return; } shown = false; layer.style.visibility = 'hidden'; layer.style.pointerEvents = 'none'; layer.setAttribute('inert', ''); scroller().style.overflow = overflow; if (button) { button.focus(); } } function listen(event) { // Only the page in the layer is heard. if (!frame || event.origin !== window.location.origin || event.source !== frame.contentWindow) { return; } var data = event.data || {}; if (data.type === 'NYXX_HIDE') { hide(); } else if (data.type === 'NYXX_STATE') { if (!!data.unkept !== unkept) { unkept = !!data.unkept; if (unkept) { window.addEventListener('beforeunload', ask); } else { window.removeEventListener('beforeunload', ask); } } if (dot) { dot.style.display = data.busy ? 'inline-block' : 'none'; } if (button) { button.title = data.busy ? 'Open NyxxWorks over this page (a job is running)' : 'Open NyxxWorks over this page'; } } } // The row of the host's tabs: the button sits at its end and looks like a tab button, but // it is no tab of Gradio's, so it never changes which tab is selected. function place() { if (app().querySelector('#' + ID)) { return; } var row = app().querySelector('#tabs > .tab-nav'); if (!row) { return; } var model = row.querySelector('button'); button = document.createElement('button'); button.id = ID; button.type = 'button'; button.className = model ? model.className.split(/\s+/).filter(function (name) { return name && name !== 'selected'; }).join(' ') : ''; button.title = 'Open NyxxWorks over this page'; button.appendChild(document.createTextNode('NyxxWorks')); dot = document.createElement('span'); dot.setAttribute('aria-hidden', 'true'); dot.style.cssText = 'display:none;width:6px;height:6px;margin-left:6px;border-radius:50%;vertical-align:middle;background:' + PRIMARY + ';'; button.appendChild(dot); button.addEventListener('click', function (event) { event.preventDefault(); event.stopPropagation(); show(); }); row.appendChild(button); } // While the layer is shown nothing of the page under it may hold the focus, or the host's // keys would act on a page nobody sees: the focus goes back into the layer. document.addEventListener('focusin', function (event) { if (shown && layer && !layer.contains(event.target)) { focusFrame(); } }); window.addEventListener('message', listen); if (typeof onUiLoaded === 'function') { onUiLoaded(place); } else { document.addEventListener('DOMContentLoaded', place); } })();