// The VM page (instance.html and its tabs). Page-wide values are data
// attributes of #vm-summary; the others are on the element they belong to.
(function () {
var summary = document.getElementById("vm-summary");
// Open the tab whose control targets "#"
function show_tab(target) {
var controls = document.querySelectorAll('[data-bs-target], a[data-bs-toggle][href]');
for (var i = 0; i < controls.length; i++) {
var c = controls[i];
if ((c.getAttribute('data-bs-target') || c.getAttribute('href')) === target) {
bootstrap.Tab.getOrCreateInstance(c).show();
return;
}
}
}
function on(id, event, handler) {
var element = document.getElementById(id);
if (element) element.addEventListener(event, handler);
}
// the VM's screen: read now and every 10 s while the page is in view, one read at a time; a frame
// replaces the shown one once it has fully come, and the screen is hidden while there is none
var screen = document.getElementById("vm-screen");
if (screen) {
var shown = screen.querySelector("img");
var reading = false;
var read_screen = function () {
if (reading || document.hidden) return;
reading = true;
var frame = new Image();
frame.alt = shown.alt;
frame.onload = function () { shown.replaceWith(frame); shown = frame; screen.hidden = false; reading = false; };
frame.onerror = function () { screen.hidden = true; reading = false; };
frame.src = screen.dataset.screenUrl + "?t=" + Date.now();
};
read_screen();
setInterval(read_screen, 10000);
}
// the guest OS details, read through the guest agent
function load_osinfo() {
var error = $('#osinfo-error');
var silent = error[0].dataset.silent;
$.getJSON(summary.dataset.osinfoUrl, function (data) {
error.addClass('d-none');
$('#oshostname').text(data['host-name'] || '');
$('#osname').text(data.id || '');
$('#osprettyname').text(data['pretty-name'] || '');
$('#oskernelrelease').text(data['kernel-release'] || '');
$('#oskernelversion').text(data['kernel-version'] || '');
$('#osversion').text(data.version || '');
$('#ostimezone').text(data.zone ? data.zone + " / " + data.offset : '');
show_filesystems(data.filesystems || []);
document.getElementById('osfilesystems-unread').hidden = (data.filesystems || []).length > 0;
}).fail(function (xhr) {
var message = xhr.responseJSON && xhr.responseJSON.error;
error.text(message || silent).removeClass('d-none');
show_filesystems([]);
document.getElementById('osfilesystems-unread').hidden = true; // the error says why
});
}
// each file system: its mount point, type, and how full it is (a bar, amber from 85 %)
function show_filesystems(filesystems) {
var table = document.getElementById('osfilesystems');
var body = table.tBodies[0];
body.replaceChildren();
filesystems.forEach(function (fs) {
var row = body.insertRow();
row.insertCell().textContent = fs.mountpoint;
row.insertCell().textContent = fs.type;
var used = row.insertCell();
used.textContent = fs.used_text + " / " + fs.total_text;
var bar = document.createElement('div');
bar.className = 'progress mt-1';
bar.style.setProperty('--bs-progress-height', '0.3rem');
bar.setAttribute('role', 'progressbar');
bar.setAttribute('aria-valuenow', fs.percent);
bar.setAttribute('aria-valuemin', 0);
bar.setAttribute('aria-valuemax', 100);
bar.setAttribute('aria-label', fs.mountpoint);
var fill = document.createElement('div');
fill.className = 'progress-bar' + (fs.percent >= 85 ? ' bg-warning' : '');
fill.style.width = fs.percent + '%';
bar.appendChild(fill);
used.appendChild(bar);
});
table.hidden = filesystems.length === 0;
}
function get_osinfo() {
show_tab('#settings');
show_tab('#osinfo');
load_osinfo();
}
on("os-details-button", "click", get_osinfo);
on("osinfo-tab", "click", get_osinfo);
on("clone-button", "click", function () {
show_tab('#settings');
show_tab('#clone');
});
if (summary.dataset.vdiUrl) {
$.get(summary.dataset.vdiUrl, function (data) {
$("#vdi_url_input").attr("value", data);
$("#vdi_url").attr("href", data);
});
}
// a switch whose form is sent as soon as it is flipped
document.querySelectorAll("[data-submit-on-change]").forEach(function (input) {
input.addEventListener("change", function () { input.form.submit(); });
});
// a memory snapshot freezes the guest by itself: the freeze option waits
// until memory is unchecked, then is what it was
on("snapshot-memory", "change", function () {
var quiesce = document.getElementById('snapshot-quiesce');
if (!quiesce.dataset.offered) return;
if (this.checked) quiesce.dataset.was = quiesce.checked;
quiesce.disabled = this.checked;
quiesce.checked = !this.checked && quiesce.dataset.was !== 'false';
});
// the Add disk dialog: the form goes where the chosen source is made,
// and an existing volume is chosen from the volumes of its pool
var addvol = document.getElementById('addvol');
if (addvol) {
var addvolForm = document.getElementById('addvol-form');
choiceDialog(addvol, 'disk_source', function (source) {
addvolForm.action = addvolForm.dataset[source];
});
}
var pool = document.getElementById("selected_storage");
if (pool) {
pool.addEventListener("change", function () {
$.getJSON(pool.dataset.volumesUrl.replace("__pool__", encodeURIComponent(pool.value)), function (data) {
var vols = $("#vols").empty();
$.each(data['vols'], function (i, item) {
vols.append($("