forked from lin-midas-pages/common_pages
When opening another window from a Midas page, the msidenav does not create correct links anymore. This change introduces a watcher which automatically corrects the links in the side nav bar whenever they change.
97 lines
3.4 KiB
JavaScript
97 lines
3.4 KiB
JavaScript
|
|
// Function to run onload to setup needed options and update period of GUI
|
|
// Called only once, onload.
|
|
function mvar_init() {
|
|
|
|
let mvars = getMElements("mvarvalue").concat(getMElements("mvarbox"),getMElements("mvarcheckbox"),
|
|
getMElements("mvarselect"));
|
|
for (let i = 0; i < mvars.length; i++) {
|
|
let o = mvars[i];
|
|
if (o.dataset.equipment !== undefined && o.dataset.varname !== undefined && o.dataset.buffer !== undefined ) {
|
|
var name_config="/Equipment/"+o.dataset.equipment+"/Settings/Names "+o.dataset.buffer;
|
|
//optional config
|
|
var unit_config="/Equipment/"+o.dataset.equipment+"/Settings/Unit "+o.dataset.buffer;
|
|
var format_config="/Equipment/"+o.dataset.equipment+"/Settings/Format "+o.dataset.buffer;
|
|
mjsonrpc_db_get_values([name_config,unit_config,format_config]).then(function(rpc) {
|
|
var names = rpc.result.data[0];
|
|
var units=rpc.result.data[1];
|
|
var formats=rpc.result.data[2];
|
|
if (Array.isArray(names)){
|
|
let index=names.indexOf(o.dataset.varname );
|
|
o.setAttribute("data-odb-path","/Equipment/"+o.dataset.equipment+"/Variables/"+o.dataset.buffer+"["+index.toString()+"]");
|
|
if (o.getAttribute('name')=='mvarvalue'){o.setAttribute("name","modbvalue");}
|
|
else if (o.getAttribute('name')=='mvarbox'){o.setAttribute("name","modbbox");}
|
|
else if (o.getAttribute('name')=='mvarcheckbox'){o.setAttribute("name","modbcheckbox");}
|
|
else if (o.getAttribute('name')=='mvarselect'){o.setAttribute("name","modbselect");}
|
|
|
|
//optional formatting:
|
|
if (Array.isArray(formats)){
|
|
o.setAttribute("data-format",formats[index]);
|
|
}
|
|
if (Array.isArray(units)){
|
|
let unit = document.createElement('span');
|
|
unit.innerHTML = ' '+ units[index];
|
|
o.parentElement.appendChild(unit);
|
|
}
|
|
} else {
|
|
o.innerHTML = "<div> Variable " +o.dataset.varname +" not found.</div>";
|
|
}
|
|
} )
|
|
|
|
} else {
|
|
o.innerHTML = "<div> Variable ill-defined.</div>";
|
|
}
|
|
}
|
|
// trigger refresh
|
|
mhttpd_refresh();
|
|
}
|
|
|
|
/**
|
|
* Fix MIDAS side navigation links for standalone custom pages.
|
|
*
|
|
* The standard MIDAS navigation uses relative URLs (e.g. "?cmd=Status").
|
|
* This works for pages opened through the normal MIDAS URL scheme
|
|
* ("/?cmd=..."), but fails for standalone HTML pages because the browser
|
|
* resolves the relative URL against the current filename:
|
|
*
|
|
* Beamline_elements_config.html + ?cmd=Status
|
|
* -> Beamline_elements_config.html?cmd=Status
|
|
*
|
|
* Convert these links to absolute paths so they always return to the MIDAS
|
|
* command dispatcher:
|
|
*
|
|
* ?cmd=Status -> /?cmd=Status
|
|
*
|
|
* This function is intended to be called after mhttpd_init() has created
|
|
* the side navigation.
|
|
*/
|
|
function fixMenuLinks()
|
|
{
|
|
document.querySelectorAll("#msidenav a[href^='?cmd=']").forEach(link => {
|
|
link.href = "/" + link.getAttribute("href");
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Monitor the MIDAS side navigation for changes and reapply the URL fix.
|
|
*
|
|
* mhttpd may rebuild the side navigation after initial page loading.
|
|
* A MutationObserver ensures that newly created menu links are corrected
|
|
* automatically without modifying the standard mhttpd.js library using
|
|
* fixMenuLinks()
|
|
*/
|
|
function watchMenuLinks()
|
|
{
|
|
const menu = document.getElementById("msidenav");
|
|
|
|
const observer = new MutationObserver(() => {
|
|
fixMenuLinks();
|
|
});
|
|
|
|
observer.observe(menu, {
|
|
childList: true,
|
|
subtree: true
|
|
});
|
|
|
|
fixMenuLinks();
|
|
} |