mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-10 04:06:47 +00:00
feat(instances): the XML editor shows a change before saving it
Change used to define the edited XML at once. It now opens a dialog with the lines the edit changes in the VM's definition, and Save there defines it. A definition libvirt refuses comes back to the dialog with the reason, and the edit stays in the editor; before, the page reloaded with the current XML and the edit was lost. A definition that changed since the editor was opened (another tab, a disk added) is not overwritten: the dialog asks to reload. An unchanged XML has nothing to save. change_xml answers the dialog with JSON; the editor's script moves to static/js/xml_editor.js.
This commit is contained in:
parent
7cf12b997b
commit
3f329b50dd
7 changed files with 198 additions and 26 deletions
82
static/js/xml_editor.js
Normal file
82
static/js/xml_editor.js
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
// The VM page's XML editor. Change shows the lines the edit changes in the
|
||||
// VM's definition (instances.views.change_xml), and Save in that dialog
|
||||
// defines it. A refusal shows in the dialog, and the edit stays in the editor.
|
||||
(function () {
|
||||
var form = document.getElementById("xml-form");
|
||||
if (!form) {
|
||||
return; // the editor exists only for superusers
|
||||
}
|
||||
var editor = ace.edit("editor");
|
||||
editor.getSession().setMode("ace/mode/xml");
|
||||
var base = editor.getValue(); // the definition the editor opened with
|
||||
var dialog = document.getElementById("xml-review");
|
||||
if (!dialog) {
|
||||
return; // a running VM's editor only shows its XML
|
||||
}
|
||||
var modal = bootstrap.Modal.getOrCreateInstance(dialog);
|
||||
var error = document.getElementById("xml-review-error");
|
||||
var same = document.getElementById("xml-review-same");
|
||||
var diff = document.getElementById("xml-review-diff");
|
||||
var save = document.getElementById("xml-review-save");
|
||||
var reviewed = ""; // Save defines the XML the dialog shows, not later edits
|
||||
|
||||
// null after a redirect (the VM is busy or moved): the page says why
|
||||
function post(xml, confirm) {
|
||||
var data = new FormData(form);
|
||||
data.set("inst_xml", xml);
|
||||
data.set("base", base);
|
||||
if (confirm) {
|
||||
data.set("confirm", "1");
|
||||
}
|
||||
return fetch(form.action, {method: "POST", body: data, redirect: "manual"}).then(function (response) {
|
||||
if (response.type === "opaqueredirect") {
|
||||
location.reload();
|
||||
return null;
|
||||
}
|
||||
return response.json();
|
||||
}).catch(function () {
|
||||
return {error: dialog.dataset.failed};
|
||||
});
|
||||
}
|
||||
|
||||
function show(answer) {
|
||||
error.textContent = answer.error || "";
|
||||
error.classList.toggle("d-none", !answer.error);
|
||||
var lines = answer.diff || [];
|
||||
same.classList.toggle("d-none", Boolean(answer.error) || lines.length > 0);
|
||||
save.classList.toggle("d-none", lines.length === 0);
|
||||
save.disabled = false;
|
||||
diff.replaceChildren();
|
||||
// skip the two file header lines; + added, - removed, @@ where
|
||||
lines.slice(2).forEach(function (line) {
|
||||
var row = document.createElement("div");
|
||||
row.textContent = line;
|
||||
row.className = {"+": "text-success", "-": "text-danger", "@": "text-body-secondary"}[line[0]] || "";
|
||||
diff.appendChild(row);
|
||||
});
|
||||
diff.classList.toggle("d-none", lines.length === 0);
|
||||
}
|
||||
|
||||
form.addEventListener("submit", function (event) {
|
||||
event.preventDefault();
|
||||
reviewed = editor.getValue();
|
||||
post(reviewed, false).then(function (answer) {
|
||||
if (answer) {
|
||||
show(answer);
|
||||
modal.show();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
save.addEventListener("click", function () {
|
||||
save.disabled = true;
|
||||
post(reviewed, true).then(function (answer) {
|
||||
if (answer && answer.saved) {
|
||||
location.hash = "xmledit";
|
||||
location.reload();
|
||||
} else if (answer) {
|
||||
show({error: answer.error, diff: []});
|
||||
}
|
||||
});
|
||||
});
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue