Separate plotting for web and electron apps
This commit is contained in:
1 parent
8155132bd9
commit
ce3ed4a235
10 files changed
+433
-302
No files matched your search
@@ -2094,36 +2094,27 @@ button:not(.tablinks):not(.action-btn):not(.editor-source-tab):not(.status-state
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
/* A parameter plot in Electron lives in its own native BrowserWindow. Reuse
|
||||
the web plot renderer, but let the operating-system title bar provide the
|
||||
window controls and let the plot fill the entire native window. */
|
||||
/* A parameter plot in Electron lives in its own native BrowserWindow. The
|
||||
shared renderer fills the page directly; the operating-system title bar is
|
||||
the only window shell. */
|
||||
.electron-parameter-plot-page-root,
|
||||
body.electron-parameter-plot-page {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
body.electron-parameter-plot-page {
|
||||
background: var(--mu-panel);
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.electron-parameter-plot-page .parameter-plot-window {
|
||||
border: 0;
|
||||
border-radius: 0;
|
||||
box-shadow: none;
|
||||
height: 100% !important;
|
||||
left: 0 !important;
|
||||
display: flex;
|
||||
margin: 0;
|
||||
max-height: none;
|
||||
max-width: none;
|
||||
position: static;
|
||||
resize: none;
|
||||
top: 0 !important;
|
||||
width: 100% !important;
|
||||
}
|
||||
|
||||
.electron-parameter-plot-page .parameter-plot-header {
|
||||
display: none;
|
||||
min-height: 0;
|
||||
overflow: hidden;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.electron-parameter-plot-page .parameter-plot-content {
|
||||
height: 100%;
|
||||
padding: 8px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* Multi-panel parameter plots. The toolbar controls the currently selected
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
<link rel="stylesheet" href="ZGUI.css">
|
||||
<script>
|
||||
if (typeof module !== "undefined" && module.exports) {
|
||||
document.write('<script src="muSRFitParameterPlotElec.js"><\/script>');
|
||||
document.write('<script src="muSRFitelec.js"><\/script>');
|
||||
} else {
|
||||
// The browser build loads the JSROOT bridge before the web runner so
|
||||
|
||||
+68
-247
@@ -8,52 +8,23 @@
|
||||
// logic: both Electron and the web runner only pass the generated table path.
|
||||
|
||||
let musrfitParameterPlotSequence = 0;
|
||||
let musrfitParameterPlotZIndex = 1300;
|
||||
|
||||
if (typeof window.MusrfitPlotlyURL === "undefined") {
|
||||
// Keep the browser version deterministic instead of depending on plotly-latest.
|
||||
// Electron uses the pinned plotly.js-dist package from package.json.
|
||||
window.MusrfitPlotlyURL = "https://cdn.plot.ly/plotly-3.5.1.min.js";
|
||||
}
|
||||
if (typeof window.MusrfitPlotlyPromise === "undefined") {
|
||||
window.MusrfitPlotlyPromise = null;
|
||||
function musrfitParameterPlotPlatform() {
|
||||
let platform = window.MusrfitParameterPlotPlatform || {};
|
||||
return platform;
|
||||
}
|
||||
|
||||
function loadMusrfitPlotly() {
|
||||
if (window.Plotly) {
|
||||
return Promise.resolve(window.Plotly);
|
||||
if (window.Plotly) return Promise.resolve(window.Plotly);
|
||||
let platform = musrfitParameterPlotPlatform();
|
||||
if (typeof platform.loadPlotly !== "function") {
|
||||
return Promise.reject(new Error("No Plotly loader is available for this platform"));
|
||||
}
|
||||
|
||||
// Electron has nodeIntegration enabled and already carries plotly.js-dist.
|
||||
if (typeof require === "function" && (typeof amIWeb !== "function" || !amIWeb())) {
|
||||
try {
|
||||
window.Plotly = require("plotly.js-dist");
|
||||
return Promise.resolve(window.Plotly);
|
||||
} catch (error) {
|
||||
console.log("Could not load local Plotly:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// Web mode loads Plotly lazily only when the user opens a parameter plot.
|
||||
if (!window.MusrfitPlotlyPromise) {
|
||||
window.MusrfitPlotlyPromise = new Promise(function(resolve, reject) {
|
||||
let script = document.createElement("script");
|
||||
script.src = window.MusrfitPlotlyURL;
|
||||
script.async = true;
|
||||
script.onload = function() {
|
||||
if (window.Plotly) {
|
||||
resolve(window.Plotly);
|
||||
} else {
|
||||
reject(new Error("Plotly loaded without exposing window.Plotly"));
|
||||
}
|
||||
};
|
||||
script.onerror = function() {
|
||||
reject(new Error("Could not load Plotly from " + window.MusrfitPlotlyURL));
|
||||
};
|
||||
document.head.appendChild(script);
|
||||
});
|
||||
}
|
||||
return window.MusrfitPlotlyPromise;
|
||||
return Promise.resolve(platform.loadPlotly()).then(function(plotly) {
|
||||
if (!plotly) throw new Error("The platform Plotly loader returned no Plotly instance");
|
||||
window.Plotly = plotly;
|
||||
return plotly;
|
||||
});
|
||||
}
|
||||
|
||||
function parseMusrfitParameterData(contents) {
|
||||
@@ -292,20 +263,11 @@ function musrfitParameterAxisLabel(name) {
|
||||
}
|
||||
|
||||
async function readMusrfitParameterData(parameterPath) {
|
||||
if (typeof amIWeb === "function" && amIWeb()) {
|
||||
let response = await fetch(parameterPath, { cache: "no-store" });
|
||||
if (!response.ok) {
|
||||
throw new Error("Could not load parameter data (HTTP " + response.status + ")");
|
||||
}
|
||||
return await response.text();
|
||||
let platform = musrfitParameterPlotPlatform();
|
||||
if (typeof platform.readDataSource !== "function") {
|
||||
throw new Error("No parameter-data reader is available for this platform");
|
||||
}
|
||||
|
||||
// Electron reads the same artifact directly from the local work directory.
|
||||
if (typeof require === "function") {
|
||||
let fs = require("fs");
|
||||
return fs.readFileSync(parameterPath, "utf8");
|
||||
}
|
||||
throw new Error("No parameter-data reader is available");
|
||||
return await platform.readDataSource({ kind: "path", path: parameterPath });
|
||||
}
|
||||
|
||||
function fillMusrfitParameterSelect(select, dataset, indices, selectedIndex, noneLabel) {
|
||||
@@ -326,25 +288,6 @@ function fillMusrfitParameterSelect(select, dataset, indices, selectedIndex, non
|
||||
select.value = String(selectedIndex);
|
||||
}
|
||||
|
||||
function layoutMusrfitParameterWindows() {
|
||||
// When the JSROOT viewer is loaded, reuse its layout routine so minimized
|
||||
// ROOT and parameter windows share the same top-right window stack.
|
||||
if (typeof layoutMusrfitRootWindows === "function") {
|
||||
layoutMusrfitRootWindows();
|
||||
return;
|
||||
}
|
||||
|
||||
let minimizedWindows = document.querySelectorAll('.web-plot-window[data-plot-state="minimized"]');
|
||||
let top = 12;
|
||||
let right = 12;
|
||||
for (let minimizedWindow of minimizedWindows) {
|
||||
minimizedWindow.style.left = "auto";
|
||||
minimizedWindow.style.right = right + "px";
|
||||
minimizedWindow.style.top = top + "px";
|
||||
top += minimizedWindow.offsetHeight + 8;
|
||||
}
|
||||
}
|
||||
|
||||
function musrfitParameterIsEditableTarget(target) {
|
||||
if (!target || !target.tagName) return false;
|
||||
let tagName = target.tagName.toLowerCase();
|
||||
@@ -438,7 +381,16 @@ function showMusrfitParameterAxisDialog(parameterPath, jobName = "", preferredPa
|
||||
if (!xColumns.includes(selectedIndex)) return;
|
||||
let xName = dataset.columns[selectedIndex];
|
||||
closeDialog();
|
||||
showMusrfitParameterPlotWindow(parameterPath, jobName, preferredParameters, xName);
|
||||
let platform = musrfitParameterPlotPlatform();
|
||||
if (typeof platform.openPlot !== "function") {
|
||||
throw new Error("No parameter-plot window opener is available for this platform");
|
||||
}
|
||||
platform.openPlot({
|
||||
parameterPath: parameterPath,
|
||||
jobName: jobName,
|
||||
preferredParameters: preferredParameters,
|
||||
preferredXColumn: xName
|
||||
});
|
||||
});
|
||||
}).catch(function(error) {
|
||||
message.className = "parameter-axis-message web-root-error";
|
||||
@@ -452,28 +404,23 @@ function showMusrfitParameterAxisDialog(parameterPath, jobName = "", preferredPa
|
||||
function showMusrfitParameterPlotWindow(parameterPath, jobName = "", preferredParameters = [], preferredXColumn = "") {
|
||||
if (!parameterPath) return;
|
||||
|
||||
if (!window.MusrfitStandaloneParameterPlot
|
||||
&& typeof window.openElectronAuxiliaryWindow === "function") {
|
||||
if (window.openElectronAuxiliaryWindow("parameterPlot", {
|
||||
parameterPath: parameterPath,
|
||||
jobName: jobName,
|
||||
preferredParameters: preferredParameters,
|
||||
preferredXColumn: preferredXColumn
|
||||
})) return;
|
||||
let platform = musrfitParameterPlotPlatform();
|
||||
if (typeof platform.createPlotWindow !== "function") {
|
||||
throw new Error("No parameter-plot window shell is available for this platform");
|
||||
}
|
||||
|
||||
musrfitParameterPlotSequence++;
|
||||
musrfitParameterPlotZIndex++;
|
||||
let shell = platform.createPlotWindow({
|
||||
title: jobName ? "Parameters: " + jobName : "Parameter plot",
|
||||
sequence: musrfitParameterPlotSequence
|
||||
});
|
||||
if (!shell || !shell.content) {
|
||||
throw new Error("The parameter-plot window shell did not provide a content container");
|
||||
}
|
||||
|
||||
let plotWindow = document.createElement("dialog");
|
||||
let header = document.createElement("div");
|
||||
let dragHandle = document.createElement("div");
|
||||
let title = document.createElement("strong");
|
||||
let windowControls = document.createElement("div");
|
||||
let minimizeButton = document.createElement("button");
|
||||
let maximizeButton = document.createElement("button");
|
||||
let closeButton = document.createElement("button");
|
||||
let content = document.createElement("div");
|
||||
let plotWindow = shell.root || shell.content;
|
||||
let content = shell.content;
|
||||
let keyTarget = shell.keyTarget || plotWindow;
|
||||
let toolbar = document.createElement("div");
|
||||
let refreshButton = document.createElement("button");
|
||||
let dataButton = document.createElement("button");
|
||||
@@ -495,31 +442,6 @@ function showMusrfitParameterPlotWindow(parameterPath, jobName = "", preferredPa
|
||||
let panelGrid = document.createElement("div");
|
||||
let message = document.createElement("div");
|
||||
|
||||
plotWindow.className = "web-plot-window parameter-plot-window";
|
||||
plotWindow.dataset.plotState = "normal";
|
||||
plotWindow.setAttribute("aria-modal", "false");
|
||||
plotWindow.tabIndex = -1;
|
||||
plotWindow.style.zIndex = musrfitParameterPlotZIndex;
|
||||
|
||||
header.className = "web-plot-header parameter-plot-header";
|
||||
dragHandle.className = "web-plot-drag-handle parameter-plot-drag-handle";
|
||||
title.textContent = jobName ? "Parameters: " + jobName : "Parameter plot";
|
||||
dragHandle.appendChild(title);
|
||||
windowControls.className = "web-plot-controls";
|
||||
for (let item of [
|
||||
[minimizeButton, "−", "Minimize plot"],
|
||||
[maximizeButton, "□", "Maximize plot"],
|
||||
[closeButton, "×", "Close plot"]
|
||||
]) {
|
||||
item[0].type = "button";
|
||||
item[0].innerHTML = item[1];
|
||||
item[0].title = item[2];
|
||||
windowControls.appendChild(item[0]);
|
||||
}
|
||||
header.appendChild(dragHandle);
|
||||
header.appendChild(windowControls);
|
||||
|
||||
content.className = "parameter-plot-content";
|
||||
toolbar.className = "parameter-plot-toolbar";
|
||||
|
||||
refreshButton.type = "button";
|
||||
@@ -596,16 +518,11 @@ function showMusrfitParameterPlotWindow(parameterPath, jobName = "", preferredPa
|
||||
content.appendChild(optionsPanel);
|
||||
content.appendChild(panelGrid);
|
||||
content.appendChild(message);
|
||||
plotWindow.appendChild(header);
|
||||
plotWindow.appendChild(content);
|
||||
|
||||
let plotly = null;
|
||||
let resizeObserver = null;
|
||||
let panelStates = [];
|
||||
let activePanelIndex = 0;
|
||||
let visiblePanelCount = 1;
|
||||
let normalBounds = null;
|
||||
let stateBeforeMinimize = "normal";
|
||||
|
||||
function sourceLabelFromPath(pathValue) {
|
||||
let clean = String(pathValue || "").split(/[?#]/)[0];
|
||||
@@ -619,11 +536,11 @@ function showMusrfitParameterPlotWindow(parameterPath, jobName = "", preferredPa
|
||||
|
||||
async function readPanelSource(source) {
|
||||
if (!source) throw new Error("No parameter-data source is selected");
|
||||
if (source.kind === "file" && source.file) {
|
||||
if (typeof source.file.text === "function") return await source.file.text();
|
||||
throw new Error("This browser cannot read the selected parameter-data file");
|
||||
let platform = musrfitParameterPlotPlatform();
|
||||
if (typeof platform.readDataSource !== "function") {
|
||||
throw new Error("No parameter-data reader is available for this platform");
|
||||
}
|
||||
return await readMusrfitParameterData(source.path);
|
||||
return await platform.readDataSource(source);
|
||||
}
|
||||
|
||||
function createPanelState(source, dataset, yNames, xName, mode = "markers") {
|
||||
@@ -882,23 +799,15 @@ function showMusrfitParameterPlotWindow(parameterPath, jobName = "", preferredPa
|
||||
}
|
||||
|
||||
async function chooseParameterDataSource(panel) {
|
||||
if (typeof require === "function" && (typeof amIWeb !== "function" || !amIWeb())) {
|
||||
let ipcRenderer = require("electron").ipcRenderer;
|
||||
let currentPath = panel && panel.source && panel.source.kind === "path" ? panel.source.path : "";
|
||||
let selectedPath = await ipcRenderer.invoke("selectParameterDataFile", currentPath);
|
||||
return selectedPath ? makePathSource(selectedPath) : null;
|
||||
let platform = musrfitParameterPlotPlatform();
|
||||
if (typeof platform.chooseDataSource !== "function") {
|
||||
throw new Error("No parameter-data chooser is available for this platform");
|
||||
}
|
||||
|
||||
return await new Promise(function(resolve) {
|
||||
let input = document.createElement("input");
|
||||
input.type = "file";
|
||||
input.accept = ".dat,.txt,text/plain";
|
||||
input.addEventListener("change", function() {
|
||||
let file = input.files && input.files[0];
|
||||
resolve(file ? { kind: "file", file: file, label: file.name } : null);
|
||||
}, { once: true });
|
||||
input.click();
|
||||
});
|
||||
let source = await platform.chooseDataSource(panel ? panel.source : null);
|
||||
if (source && source.kind === "path" && !source.label) {
|
||||
source.label = sourceLabelFromPath(source.path);
|
||||
}
|
||||
return source;
|
||||
}
|
||||
|
||||
function applyLayout(layoutValue) {
|
||||
@@ -1000,7 +909,7 @@ function showMusrfitParameterPlotWindow(parameterPath, jobName = "", preferredPa
|
||||
resizeObserver.observe(panelGrid);
|
||||
}
|
||||
|
||||
plotWindow.addEventListener("keydown", function(event) {
|
||||
function handleParameterPlotKeydown(event) {
|
||||
if (event.altKey || event.ctrlKey || event.metaKey || musrfitParameterIsEditableTarget(event.target)) return;
|
||||
let shortcut = event.key.length === 1 ? event.key.toLowerCase() : event.key;
|
||||
if (shortcut === "u" && plotly) {
|
||||
@@ -1011,120 +920,32 @@ function showMusrfitParameterPlotWindow(parameterPath, jobName = "", preferredPa
|
||||
}
|
||||
} else if (shortcut === "q" || shortcut === "Escape") {
|
||||
event.preventDefault();
|
||||
plotWindow.close();
|
||||
if (typeof shell.close === "function") shell.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
minimizeButton.addEventListener("click", function() {
|
||||
if (plotWindow.dataset.plotState === "minimized") {
|
||||
plotWindow.dataset.plotState = stateBeforeMinimize;
|
||||
minimizeButton.innerHTML = "−";
|
||||
if (stateBeforeMinimize === "maximized") {
|
||||
plotWindow.style.left = "12px";
|
||||
plotWindow.style.right = "auto";
|
||||
plotWindow.style.top = "12px";
|
||||
plotWindow.style.width = "calc(100vw - 24px)";
|
||||
plotWindow.style.height = "calc(100vh - var(--mu-status-height) - 36px)";
|
||||
} else if (normalBounds) {
|
||||
plotWindow.style.left = normalBounds.left + "px";
|
||||
plotWindow.style.right = "auto";
|
||||
plotWindow.style.top = normalBounds.top + "px";
|
||||
plotWindow.style.width = normalBounds.width + "px";
|
||||
plotWindow.style.height = normalBounds.height + "px";
|
||||
}
|
||||
layoutMusrfitParameterWindows();
|
||||
return;
|
||||
}
|
||||
if (plotWindow.dataset.plotState === "normal") {
|
||||
let bounds = plotWindow.getBoundingClientRect();
|
||||
normalBounds = { left: bounds.left, top: bounds.top, width: bounds.width, height: bounds.height };
|
||||
}
|
||||
stateBeforeMinimize = plotWindow.dataset.plotState;
|
||||
plotWindow.dataset.plotState = "minimized";
|
||||
minimizeButton.innerHTML = "⧉";
|
||||
plotWindow.style.width = "";
|
||||
plotWindow.style.height = "";
|
||||
layoutMusrfitParameterWindows();
|
||||
});
|
||||
if (keyTarget && typeof keyTarget.addEventListener === "function") {
|
||||
keyTarget.addEventListener("keydown", handleParameterPlotKeydown);
|
||||
}
|
||||
|
||||
maximizeButton.addEventListener("click", function() {
|
||||
if (plotWindow.dataset.plotState === "maximized") {
|
||||
plotWindow.dataset.plotState = "normal";
|
||||
maximizeButton.innerHTML = "□";
|
||||
if (normalBounds) {
|
||||
plotWindow.style.left = normalBounds.left + "px";
|
||||
plotWindow.style.right = "auto";
|
||||
plotWindow.style.top = normalBounds.top + "px";
|
||||
plotWindow.style.width = normalBounds.width + "px";
|
||||
plotWindow.style.height = normalBounds.height + "px";
|
||||
}
|
||||
return;
|
||||
function cleanupParameterPlot() {
|
||||
if (keyTarget && typeof keyTarget.removeEventListener === "function") {
|
||||
keyTarget.removeEventListener("keydown", handleParameterPlotKeydown);
|
||||
}
|
||||
if (plotWindow.dataset.plotState === "normal") {
|
||||
let bounds = plotWindow.getBoundingClientRect();
|
||||
normalBounds = { left: bounds.left, top: bounds.top, width: bounds.width, height: bounds.height };
|
||||
}
|
||||
plotWindow.dataset.plotState = "maximized";
|
||||
minimizeButton.innerHTML = "−";
|
||||
maximizeButton.innerHTML = "⧉";
|
||||
plotWindow.style.left = "12px";
|
||||
plotWindow.style.right = "auto";
|
||||
plotWindow.style.top = "12px";
|
||||
plotWindow.style.width = "calc(100vw - 24px)";
|
||||
plotWindow.style.height = "calc(100vh - var(--mu-status-height) - 36px)";
|
||||
layoutMusrfitParameterWindows();
|
||||
});
|
||||
|
||||
closeButton.addEventListener("click", function() { plotWindow.close(); });
|
||||
plotWindow.addEventListener("close", function() {
|
||||
if (resizeObserver) resizeObserver.disconnect();
|
||||
if (plotly) {
|
||||
for (let panel of panelStates) {
|
||||
if (panel.chart && panel.chart.data) plotly.purge(panel.chart);
|
||||
}
|
||||
}
|
||||
plotWindow.remove();
|
||||
layoutMusrfitParameterWindows();
|
||||
if (window.MusrfitStandaloneParameterPlot && typeof window.close === "function") window.close();
|
||||
});
|
||||
}
|
||||
|
||||
plotWindow.addEventListener("pointerdown", function() {
|
||||
musrfitParameterPlotZIndex++;
|
||||
plotWindow.style.zIndex = musrfitParameterPlotZIndex;
|
||||
try { plotWindow.focus({ preventScroll: true }); } catch (error) { plotWindow.focus(); }
|
||||
});
|
||||
if (typeof shell.onClose === "function") shell.onClose(cleanupParameterPlot);
|
||||
if (typeof shell.show === "function") shell.show();
|
||||
|
||||
let dragPosition = null;
|
||||
header.addEventListener("pointerdown", function(event) {
|
||||
if (plotWindow.dataset.plotState !== "normal" || windowControls.contains(event.target)) return;
|
||||
let bounds = plotWindow.getBoundingClientRect();
|
||||
dragPosition = {
|
||||
pointerX: event.clientX, pointerY: event.clientY,
|
||||
windowX: bounds.left, windowY: bounds.top
|
||||
};
|
||||
header.setPointerCapture(event.pointerId);
|
||||
event.preventDefault();
|
||||
});
|
||||
header.addEventListener("pointermove", function(event) {
|
||||
if (!dragPosition) return;
|
||||
let maximumX = Math.max(0, window.innerWidth - plotWindow.offsetWidth);
|
||||
let maximumY = Math.max(0, window.innerHeight - header.offsetHeight);
|
||||
let nextX = dragPosition.windowX + event.clientX - dragPosition.pointerX;
|
||||
let nextY = dragPosition.windowY + event.clientY - dragPosition.pointerY;
|
||||
plotWindow.style.left = Math.min(maximumX, Math.max(0, nextX)) + "px";
|
||||
plotWindow.style.top = Math.min(maximumY, Math.max(0, nextY)) + "px";
|
||||
});
|
||||
header.addEventListener("pointerup", function(event) {
|
||||
dragPosition = null;
|
||||
header.releasePointerCapture(event.pointerId);
|
||||
});
|
||||
|
||||
document.body.appendChild(plotWindow);
|
||||
plotWindow.show();
|
||||
try { plotWindow.focus({ preventScroll: true }); } catch (error) { plotWindow.focus(); }
|
||||
|
||||
let offset = ((musrfitParameterPlotSequence - 1) % 6) * 24;
|
||||
let initialX = window.innerWidth - plotWindow.offsetWidth - 12 - offset;
|
||||
plotWindow.style.left = Math.max(12, initialX) + "px";
|
||||
plotWindow.style.top = 12 + offset + "px";
|
||||
return {
|
||||
close: typeof shell.close === "function" ? shell.close : function() {},
|
||||
destroy: cleanupParameterPlot,
|
||||
root: plotWindow
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright (C) 2026 Zaher Salman
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// Electron-only adapters for the shared parameter-plot renderer. Keeping
|
||||
// filesystem, native file-dialog, and bundled-Plotly access here lets
|
||||
// muSRFitParameterPlot.js remain platform-neutral.
|
||||
(function() {
|
||||
const ipcRenderer = require("electron").ipcRenderer;
|
||||
const fs = require("fs");
|
||||
|
||||
if (!window.MusrfitParameterPlotPlatform) {
|
||||
window.MusrfitParameterPlotPlatform = {};
|
||||
}
|
||||
let platform = window.MusrfitParameterPlotPlatform;
|
||||
|
||||
platform.loadPlotly = function() {
|
||||
if (!window.Plotly) window.Plotly = require("plotly.js-dist");
|
||||
return window.Plotly;
|
||||
};
|
||||
|
||||
platform.readDataSource = async function(source) {
|
||||
let parameterPath = source && source.path ? source.path : String(source || "");
|
||||
if (!parameterPath) throw new Error("No parameter-data path is available");
|
||||
return fs.readFileSync(parameterPath, "utf8");
|
||||
};
|
||||
|
||||
platform.chooseDataSource = async function(currentSource) {
|
||||
let currentPath = currentSource && currentSource.kind === "path" ? currentSource.path : "";
|
||||
let selectedPath = await ipcRenderer.invoke("selectParameterDataFile", currentPath);
|
||||
return selectedPath ? { kind: "path", path: selectedPath } : null;
|
||||
};
|
||||
})();
|
||||
@@ -29,6 +29,14 @@ function openElectronAuxiliaryWindow(kind, options = {}) {
|
||||
}
|
||||
window.openElectronAuxiliaryWindow = openElectronAuxiliaryWindow;
|
||||
|
||||
// Parameter-plot opening is the only part of the plotting workflow owned by
|
||||
// the main Electron renderer. The actual plot UI is rendered by the shared
|
||||
// muSRFitParameterPlot.js inside the auxiliary BrowserWindow.
|
||||
if (!window.MusrfitParameterPlotPlatform) window.MusrfitParameterPlotPlatform = {};
|
||||
window.MusrfitParameterPlotPlatform.openPlot = function(options = {}) {
|
||||
return openElectronAuxiliaryWindow("parameterPlot", options);
|
||||
};
|
||||
|
||||
// Keep one mutable runner state on window. Only musrfit is serialized;
|
||||
// musrview windows/processes are allowed to run in parallel.
|
||||
if (!window.MusrfitRunner) {
|
||||
|
||||
+243
@@ -6,6 +6,249 @@
|
||||
if (typeof window.MusrfitWebRequestActive === "undefined") {
|
||||
window.MusrfitWebRequestActive = false;
|
||||
}
|
||||
|
||||
// Web-only shell and I/O adapters for the shared parameter-plot renderer.
|
||||
// Plot construction, panel state, Plotly traces, error bars, and controls stay
|
||||
// in muSRFitParameterPlot.js; this file owns only browser integration.
|
||||
if (!window.MusrfitParameterPlotPlatform) window.MusrfitParameterPlotPlatform = {};
|
||||
if (typeof window.MusrfitPlotlyURL === "undefined") {
|
||||
window.MusrfitPlotlyURL = "https://cdn.plot.ly/plotly-3.5.1.min.js";
|
||||
}
|
||||
if (typeof window.MusrfitPlotlyPromise === "undefined") {
|
||||
window.MusrfitPlotlyPromise = null;
|
||||
}
|
||||
var webMusrfitParameterPlotZIndex = 1300;
|
||||
|
||||
function layoutWebMusrfitParameterWindows() {
|
||||
if (typeof layoutMusrfitRootWindows === "function") {
|
||||
layoutMusrfitRootWindows();
|
||||
return;
|
||||
}
|
||||
let minimizedWindows = document.querySelectorAll('.web-plot-window[data-plot-state="minimized"]');
|
||||
let top = 12;
|
||||
let right = 12;
|
||||
for (let minimizedWindow of minimizedWindows) {
|
||||
minimizedWindow.style.left = "auto";
|
||||
minimizedWindow.style.right = right + "px";
|
||||
minimizedWindow.style.top = top + "px";
|
||||
top += minimizedWindow.offsetHeight + 8;
|
||||
}
|
||||
}
|
||||
|
||||
function createWebMusrfitParameterPlotWindow(options = {}) {
|
||||
webMusrfitParameterPlotZIndex++;
|
||||
|
||||
let plotWindow = document.createElement("dialog");
|
||||
let header = document.createElement("div");
|
||||
let dragHandle = document.createElement("div");
|
||||
let title = document.createElement("strong");
|
||||
let windowControls = document.createElement("div");
|
||||
let minimizeButton = document.createElement("button");
|
||||
let maximizeButton = document.createElement("button");
|
||||
let closeButton = document.createElement("button");
|
||||
let content = document.createElement("div");
|
||||
let closeHandlers = [];
|
||||
let normalBounds = null;
|
||||
let stateBeforeMinimize = "normal";
|
||||
|
||||
plotWindow.className = "web-plot-window parameter-plot-window";
|
||||
plotWindow.dataset.plotState = "normal";
|
||||
plotWindow.setAttribute("aria-modal", "false");
|
||||
plotWindow.tabIndex = -1;
|
||||
plotWindow.style.zIndex = webMusrfitParameterPlotZIndex;
|
||||
|
||||
header.className = "web-plot-header parameter-plot-header";
|
||||
dragHandle.className = "web-plot-drag-handle parameter-plot-drag-handle";
|
||||
title.textContent = options.title || "Parameter plot";
|
||||
dragHandle.appendChild(title);
|
||||
windowControls.className = "web-plot-controls";
|
||||
for (let item of [
|
||||
[minimizeButton, "−", "Minimize plot"],
|
||||
[maximizeButton, "□", "Maximize plot"],
|
||||
[closeButton, "×", "Close plot"]
|
||||
]) {
|
||||
item[0].type = "button";
|
||||
item[0].innerHTML = item[1];
|
||||
item[0].title = item[2];
|
||||
windowControls.appendChild(item[0]);
|
||||
}
|
||||
header.appendChild(dragHandle);
|
||||
header.appendChild(windowControls);
|
||||
|
||||
content.className = "parameter-plot-content";
|
||||
plotWindow.appendChild(header);
|
||||
plotWindow.appendChild(content);
|
||||
|
||||
minimizeButton.addEventListener("click", function() {
|
||||
if (plotWindow.dataset.plotState === "minimized") {
|
||||
plotWindow.dataset.plotState = stateBeforeMinimize;
|
||||
minimizeButton.innerHTML = "−";
|
||||
if (stateBeforeMinimize === "maximized") {
|
||||
plotWindow.style.left = "12px";
|
||||
plotWindow.style.right = "auto";
|
||||
plotWindow.style.top = "12px";
|
||||
plotWindow.style.width = "calc(100vw - 24px)";
|
||||
plotWindow.style.height = "calc(100vh - var(--mu-status-height) - 36px)";
|
||||
} else if (normalBounds) {
|
||||
plotWindow.style.left = normalBounds.left + "px";
|
||||
plotWindow.style.right = "auto";
|
||||
plotWindow.style.top = normalBounds.top + "px";
|
||||
plotWindow.style.width = normalBounds.width + "px";
|
||||
plotWindow.style.height = normalBounds.height + "px";
|
||||
}
|
||||
layoutWebMusrfitParameterWindows();
|
||||
return;
|
||||
}
|
||||
if (plotWindow.dataset.plotState === "normal") {
|
||||
let bounds = plotWindow.getBoundingClientRect();
|
||||
normalBounds = { left: bounds.left, top: bounds.top, width: bounds.width, height: bounds.height };
|
||||
}
|
||||
stateBeforeMinimize = plotWindow.dataset.plotState;
|
||||
plotWindow.dataset.plotState = "minimized";
|
||||
minimizeButton.innerHTML = "⧉";
|
||||
plotWindow.style.width = "";
|
||||
plotWindow.style.height = "";
|
||||
layoutWebMusrfitParameterWindows();
|
||||
});
|
||||
|
||||
maximizeButton.addEventListener("click", function() {
|
||||
if (plotWindow.dataset.plotState === "maximized") {
|
||||
plotWindow.dataset.plotState = "normal";
|
||||
maximizeButton.innerHTML = "□";
|
||||
if (normalBounds) {
|
||||
plotWindow.style.left = normalBounds.left + "px";
|
||||
plotWindow.style.right = "auto";
|
||||
plotWindow.style.top = normalBounds.top + "px";
|
||||
plotWindow.style.width = normalBounds.width + "px";
|
||||
plotWindow.style.height = normalBounds.height + "px";
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (plotWindow.dataset.plotState === "normal") {
|
||||
let bounds = plotWindow.getBoundingClientRect();
|
||||
normalBounds = { left: bounds.left, top: bounds.top, width: bounds.width, height: bounds.height };
|
||||
}
|
||||
plotWindow.dataset.plotState = "maximized";
|
||||
minimizeButton.innerHTML = "−";
|
||||
maximizeButton.innerHTML = "⧉";
|
||||
plotWindow.style.left = "12px";
|
||||
plotWindow.style.right = "auto";
|
||||
plotWindow.style.top = "12px";
|
||||
plotWindow.style.width = "calc(100vw - 24px)";
|
||||
plotWindow.style.height = "calc(100vh - var(--mu-status-height) - 36px)";
|
||||
layoutWebMusrfitParameterWindows();
|
||||
});
|
||||
|
||||
closeButton.addEventListener("click", function() { plotWindow.close(); });
|
||||
plotWindow.addEventListener("close", function() {
|
||||
for (let handler of closeHandlers) handler();
|
||||
plotWindow.remove();
|
||||
layoutWebMusrfitParameterWindows();
|
||||
});
|
||||
|
||||
plotWindow.addEventListener("pointerdown", function() {
|
||||
webMusrfitParameterPlotZIndex++;
|
||||
plotWindow.style.zIndex = webMusrfitParameterPlotZIndex;
|
||||
try { plotWindow.focus({ preventScroll: true }); } catch (error) { plotWindow.focus(); }
|
||||
});
|
||||
|
||||
let dragPosition = null;
|
||||
header.addEventListener("pointerdown", function(event) {
|
||||
if (plotWindow.dataset.plotState !== "normal" || windowControls.contains(event.target)) return;
|
||||
let bounds = plotWindow.getBoundingClientRect();
|
||||
dragPosition = {
|
||||
pointerX: event.clientX, pointerY: event.clientY,
|
||||
windowX: bounds.left, windowY: bounds.top
|
||||
};
|
||||
header.setPointerCapture(event.pointerId);
|
||||
event.preventDefault();
|
||||
});
|
||||
header.addEventListener("pointermove", function(event) {
|
||||
if (!dragPosition) return;
|
||||
let maximumX = Math.max(0, window.innerWidth - plotWindow.offsetWidth);
|
||||
let maximumY = Math.max(0, window.innerHeight - header.offsetHeight);
|
||||
let nextX = dragPosition.windowX + event.clientX - dragPosition.pointerX;
|
||||
let nextY = dragPosition.windowY + event.clientY - dragPosition.pointerY;
|
||||
plotWindow.style.left = Math.min(maximumX, Math.max(0, nextX)) + "px";
|
||||
plotWindow.style.top = Math.min(maximumY, Math.max(0, nextY)) + "px";
|
||||
});
|
||||
header.addEventListener("pointerup", function(event) {
|
||||
dragPosition = null;
|
||||
if (header.hasPointerCapture(event.pointerId)) header.releasePointerCapture(event.pointerId);
|
||||
});
|
||||
|
||||
return {
|
||||
root: plotWindow,
|
||||
content: content,
|
||||
keyTarget: plotWindow,
|
||||
close: function() { plotWindow.close(); },
|
||||
onClose: function(handler) { if (typeof handler === "function") closeHandlers.push(handler); },
|
||||
show: function() {
|
||||
document.body.appendChild(plotWindow);
|
||||
plotWindow.show();
|
||||
try { plotWindow.focus({ preventScroll: true }); } catch (error) { plotWindow.focus(); }
|
||||
let offset = ((Number(options.sequence) || 1) - 1) % 6 * 24;
|
||||
let initialX = window.innerWidth - plotWindow.offsetWidth - 12 - offset;
|
||||
plotWindow.style.left = Math.max(12, initialX) + "px";
|
||||
plotWindow.style.top = 12 + offset + "px";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
window.MusrfitParameterPlotPlatform.loadPlotly = function() {
|
||||
if (window.Plotly) return Promise.resolve(window.Plotly);
|
||||
if (!window.MusrfitPlotlyPromise) {
|
||||
window.MusrfitPlotlyPromise = new Promise(function(resolve, reject) {
|
||||
let script = document.createElement("script");
|
||||
script.src = window.MusrfitPlotlyURL;
|
||||
script.async = true;
|
||||
script.onload = function() {
|
||||
if (window.Plotly) resolve(window.Plotly);
|
||||
else reject(new Error("Plotly loaded without exposing window.Plotly"));
|
||||
};
|
||||
script.onerror = function() {
|
||||
reject(new Error("Could not load Plotly from " + window.MusrfitPlotlyURL));
|
||||
};
|
||||
document.head.appendChild(script);
|
||||
});
|
||||
}
|
||||
return window.MusrfitPlotlyPromise;
|
||||
};
|
||||
|
||||
window.MusrfitParameterPlotPlatform.readDataSource = async function(source) {
|
||||
if (source && source.kind === "file" && source.file) {
|
||||
if (typeof source.file.text === "function") return await source.file.text();
|
||||
throw new Error("This browser cannot read the selected parameter-data file");
|
||||
}
|
||||
let parameterPath = source && source.path ? source.path : String(source || "");
|
||||
let response = await fetch(parameterPath, { cache: "no-store" });
|
||||
if (!response.ok) throw new Error("Could not load parameter data (HTTP " + response.status + ")");
|
||||
return await response.text();
|
||||
};
|
||||
|
||||
window.MusrfitParameterPlotPlatform.chooseDataSource = async function() {
|
||||
return await new Promise(function(resolve) {
|
||||
let input = document.createElement("input");
|
||||
input.type = "file";
|
||||
input.accept = ".dat,.txt,text/plain";
|
||||
input.addEventListener("change", function() {
|
||||
let file = input.files && input.files[0];
|
||||
resolve(file ? { kind: "file", file: file, label: file.name } : null);
|
||||
}, { once: true });
|
||||
input.click();
|
||||
});
|
||||
};
|
||||
|
||||
window.MusrfitParameterPlotPlatform.createPlotWindow = createWebMusrfitParameterPlotWindow;
|
||||
window.MusrfitParameterPlotPlatform.openPlot = function(options = {}) {
|
||||
showMusrfitParameterPlotWindow(
|
||||
options.parameterPath || "",
|
||||
options.jobName || "",
|
||||
Array.isArray(options.preferredParameters) ? options.preferredParameters : [],
|
||||
options.preferredXColumn || ""
|
||||
);
|
||||
return true;
|
||||
};
|
||||
function findWebURLSelectValue(selectElement, requestedValue) {
|
||||
// URL values come from links written by people and older CGI pages, so match
|
||||
// instrument names case-insensitively while still returning the exact option
|
||||
|
||||
@@ -48,6 +48,7 @@
|
||||
"muSRFitelec.js",
|
||||
"muSRFitRoot.js",
|
||||
"muSRFitParameterPlot.js",
|
||||
"muSRFitParameterPlotElec.js",
|
||||
"assets/**/*",
|
||||
"LICENSE",
|
||||
"package.json",
|
||||
|
||||
@@ -3,6 +3,38 @@
|
||||
|
||||
const { ipcRenderer } = require("electron");
|
||||
|
||||
if (!window.MusrfitParameterPlotPlatform) window.MusrfitParameterPlotPlatform = {};
|
||||
|
||||
// In Electron the operating-system BrowserWindow is the plot window. The
|
||||
// shared renderer therefore receives only a content host and never creates or
|
||||
// manages a second in-page window shell.
|
||||
window.MusrfitParameterPlotPlatform.createPlotWindow = function() {
|
||||
document.body.textContent = "";
|
||||
const content = document.createElement("div");
|
||||
content.className = "parameter-plot-content";
|
||||
document.body.appendChild(content);
|
||||
|
||||
const closeHandlers = [];
|
||||
let closed = false;
|
||||
function runCloseHandlers() {
|
||||
if (closed) return;
|
||||
closed = true;
|
||||
for (const handler of closeHandlers) handler();
|
||||
}
|
||||
window.addEventListener("beforeunload", runCloseHandlers, { once: true });
|
||||
|
||||
return {
|
||||
root: document.body,
|
||||
content: content,
|
||||
keyTarget: document,
|
||||
close: function() { window.close(); },
|
||||
onClose: function(handler) {
|
||||
if (typeof handler === "function") closeHandlers.push(handler);
|
||||
},
|
||||
show: function() {}
|
||||
};
|
||||
};
|
||||
|
||||
ipcRenderer.on("parameterPlotData", (event, options) => {
|
||||
options = options || {};
|
||||
document.title = options.jobName ? "Parameters: " + options.jobName : "Parameter plot";
|
||||
|
||||
+2
-6
@@ -1,16 +1,12 @@
|
||||
<!-- Copyright (C) 2026 Zaher Salman
|
||||
SPDX-License-Identifier: GPL-3.0-or-later -->
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<html class="electron-parameter-plot-page-root">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<link rel="stylesheet" href="ZGUI.css">
|
||||
<title>Parameter plot</title>
|
||||
<script>
|
||||
// Prevent the shared plotting function from recursively asking Electron to
|
||||
// create another native window when it runs inside the plot window itself.
|
||||
window.MusrfitStandaloneParameterPlot = true;
|
||||
</script>
|
||||
<script defer src="muSRFitParameterPlotElec.js"></script>
|
||||
<script defer src="muSRFitParameterPlot.js"></script>
|
||||
<script defer src="parameter-plot-window.js"></script>
|
||||
</head>
|
||||
|
||||
@@ -86,11 +86,12 @@ function testIntegratedParameterTableHook() {
|
||||
assert.match(lib, /selectParameterForPlot/);
|
||||
assert.match(lib, /showMusrfitParameterAxisDialog\([\s\S]*selectedParameterPlotNames\.slice\(\)/);
|
||||
assert.match(plot, /function showMusrfitParameterAxisDialog/);
|
||||
assert.match(plot, /showMusrfitParameterPlotWindow\(parameterPath, jobName, preferredParameters, xName\)/);
|
||||
assert.match(plot, /platform\.openPlot\(\{/);
|
||||
assert.match(plot, /preferredParameters = \[\]/);
|
||||
assert.match(plot, /preferredXColumn = ""/);
|
||||
assert.match(plot, /musrfitParameterColumnIndex\(dataset, name\)/);
|
||||
assert.match(plot, /openElectronAuxiliaryWindow\("parameterPlot"/);
|
||||
assert.match(plot, /platform\.createPlotWindow/);
|
||||
assert.doesNotMatch(plot, /openElectronAuxiliaryWindow|require\("electron"\)|amIWeb\(|fetch\(/);
|
||||
assert.match(plot, /refreshButton\.textContent = "Refresh"/);
|
||||
assert.match(plot, /readMusrfitParameterData\(parameterPath\)/);
|
||||
assert.match(plot, /layoutSelect/);
|
||||
@@ -100,7 +101,6 @@ function testIntegratedParameterTableHook() {
|
||||
assert.doesNotMatch(plot, /parameter-plot-panel-caption/);
|
||||
assert.match(plot, /parameter-plot-grid/);
|
||||
assert.match(plot, /addButton\.addEventListener/);
|
||||
assert.match(plot, /selectParameterDataFile/);
|
||||
assert.match(plot, /musrfitParameterErrorColumns/);
|
||||
assert.match(plot, /mode: mode \|\| "markers"/);
|
||||
assert.match(plot, /buildMusrfitParameterTrace\(panel\.dataset, xIndex, yIndex, panel\.mode\)/);
|
||||
@@ -116,30 +116,36 @@ function testIntegratedParameterTableHook() {
|
||||
}
|
||||
|
||||
|
||||
function testElectronUsesNativePlotWindow() {
|
||||
const code = fs.readFileSync(path.join(repoRoot, "muSRFitParameterPlot.js"), "utf8");
|
||||
const sent = [];
|
||||
const sandbox = {
|
||||
console: console,
|
||||
window: {
|
||||
openElectronAuxiliaryWindow: function(kind, options) {
|
||||
sent.push([kind, options]);
|
||||
return true;
|
||||
}
|
||||
},
|
||||
document: {},
|
||||
Promise: Promise,
|
||||
Set: Set
|
||||
};
|
||||
sandbox.global = sandbox;
|
||||
vm.runInNewContext(code, sandbox, { filename: "muSRFitParameterPlot.js" });
|
||||
sandbox.showMusrfitParameterPlotWindow("/tmp/514_asy_par.dat", "514_asy.msr", ["Asy", "Lam"], "dataT");
|
||||
function testPlatformSeparation() {
|
||||
const plot = fs.readFileSync(path.join(repoRoot, "muSRFitParameterPlot.js"), "utf8");
|
||||
const elec = fs.readFileSync(path.join(repoRoot, "muSRFitelec.js"), "utf8");
|
||||
const elecAdapter = fs.readFileSync(path.join(repoRoot, "muSRFitParameterPlotElec.js"), "utf8");
|
||||
const web = fs.readFileSync(path.join(repoRoot, "muSRFitweb.js"), "utf8");
|
||||
const windowScript = fs.readFileSync(path.join(repoRoot, "parameter-plot-window.js"), "utf8");
|
||||
const windowHtml = fs.readFileSync(path.join(repoRoot, "parameter-plot.html"), "utf8");
|
||||
|
||||
assert.strictEqual(sent.length, 1);
|
||||
assert.strictEqual(sent[0][0], "parameterPlot");
|
||||
assert.strictEqual(sent[0][1].parameterPath, "/tmp/514_asy_par.dat");
|
||||
assert.deepStrictEqual(Array.from(sent[0][1].preferredParameters), ["Asy", "Lam"]);
|
||||
assert.strictEqual(sent[0][1].preferredXColumn, "dataT");
|
||||
assert.doesNotMatch(plot, /require\(|openElectronAuxiliaryWindow|MusrfitStandaloneParameterPlot|amIWeb\(|fetch\(/);
|
||||
assert.match(plot, /platform\.openPlot\(\{/);
|
||||
assert.match(plot, /platform\.createPlotWindow/);
|
||||
assert.match(plot, /platform\.readDataSource/);
|
||||
assert.match(plot, /platform\.chooseDataSource/);
|
||||
assert.match(plot, /platform\.loadPlotly/);
|
||||
|
||||
assert.match(elec, /MusrfitParameterPlotPlatform\.openPlot/);
|
||||
assert.match(elec, /openElectronAuxiliaryWindow\("parameterPlot", options\)/);
|
||||
assert.match(elecAdapter, /require\("plotly\.js-dist"\)/);
|
||||
assert.match(elecAdapter, /readDataSource/);
|
||||
assert.match(elecAdapter, /selectParameterDataFile/);
|
||||
|
||||
assert.match(web, /createWebMusrfitParameterPlotWindow/);
|
||||
assert.match(web, /MusrfitParameterPlotPlatform\.createPlotWindow/);
|
||||
assert.match(web, /MusrfitParameterPlotPlatform\.openPlot/);
|
||||
assert.match(web, /MusrfitParameterPlotPlatform\.readDataSource/);
|
||||
|
||||
assert.match(windowScript, /MusrfitParameterPlotPlatform\.createPlotWindow/);
|
||||
assert.match(windowScript, /showMusrfitParameterPlotWindow/);
|
||||
assert.match(windowHtml, /muSRFitParameterPlotElec\.js/);
|
||||
assert.doesNotMatch(windowHtml, /MusrfitStandaloneParameterPlot/);
|
||||
}
|
||||
|
||||
function testElectronParameterExport() {
|
||||
@@ -210,6 +216,6 @@ function testElectronParameterExport() {
|
||||
|
||||
testParserAndSmartDefaults();
|
||||
testIntegratedParameterTableHook();
|
||||
testElectronUsesNativePlotWindow();
|
||||
testPlatformSeparation();
|
||||
testElectronParameterExport();
|
||||
console.log("Parameter plot tests passed");
|
||||
Reference in new issue
Block a user