re-enable buttons if editing fails or is cancelled

This commit is contained in:
joshuaboud 2021-05-31 10:15:54 -03:00
parent 5955a677d4
commit 3cf7725787
No known key found for this signature in database
GPG Key ID: 17EFB59E2A8BF50E
1 changed files with 54 additions and 19 deletions

View File

@ -366,19 +366,29 @@ class NavFile extends NavEntry {
} }
async show_edit_file_contents() { async show_edit_file_contents() {
for (let button of document.getElementsByTagName("button")) { this.nav_window_ref.disable_buttons_for_editing();
if (!button.classList.contains("editor-btn"))
button.disabled = true;
}
document.getElementById("pwd").disabled = true;
var proc_output = await cockpit.spawn(["file", "--mime-type", this.path_str()], {superuser: "try"}); var proc_output = await cockpit.spawn(["file", "--mime-type", this.path_str()], {superuser: "try"});
var fields = proc_output.split(':'); var fields = proc_output.split(':');
var type = fields[1].trim(); var type = fields[1].trim();
if (!(type.match(/^text/) || type.match(/^inode\/x-empty$/) || this.stat["size"] === 0)) { if (!(type.match(/^text/) || type.match(/^inode\/x-empty$/) || this.stat["size"] === 0)) {
if(!window.confirm("File is of type `" + type + "`. Are you sure you want to edit it?")) if (!window.confirm("File is of type `" + type + "`. Are you sure you want to edit it?")) {
this.nav_window_ref.enable_buttons();
return;
}
}
var contents = "";
try {
contents = await cockpit.spawn(["cat", this.path_str()], {superuser: "try"});
} catch (e) {
this.nav_window_ref.enable_buttons();
var message = "";
if (e.message === "protocol-error")
message = "Error reading file.";
else
message = e.message;
window.alert(message);
return; return;
} }
var contents = await cockpit.spawn(["cat", this.path_str()], {superuser: "try"});
document.getElementById("nav-edit-contents-textarea").value = contents; document.getElementById("nav-edit-contents-textarea").value = contents;
document.getElementById("nav-cancel-edit-contents-btn").onclick = this.hide_edit_file_contents.bind(this); document.getElementById("nav-cancel-edit-contents-btn").onclick = this.hide_edit_file_contents.bind(this);
document.getElementById("nav-continue-edit-contents-btn").onclick = this.write_to_file.bind(this); document.getElementById("nav-continue-edit-contents-btn").onclick = this.write_to_file.bind(this);
@ -397,10 +407,7 @@ class NavFile extends NavEntry {
hide_edit_file_contents() { hide_edit_file_contents() {
document.getElementById("nav-edit-contents-view").style.display = "none"; document.getElementById("nav-edit-contents-view").style.display = "none";
document.getElementById("nav-contents-view").style.display = "flex"; document.getElementById("nav-contents-view").style.display = "flex";
for (let button of document.getElementsByTagName("button")) { this.nav_window_ref.enable_buttons();
button.disabled = false;
}
document.getElementById("pwd").disabled = false;
} }
} }
@ -443,20 +450,31 @@ class NavFileLink extends NavFile{
} }
async show_edit_file_contents() { async show_edit_file_contents() {
for (let button of document.getElementsByTagName("button")) { this.nav_window_ref.disable_buttons_for_editing();
if (!button.classList.contains("editor-btn"))
button.disabled = true;
}
document.getElementById("pwd").disabled = true; document.getElementById("pwd").disabled = true;
var target_path = this.get_link_target_path(); var target_path = this.get_link_target_path();
var proc_output = await cockpit.spawn(["file", "--mime-type", target_path], {superuser: "try"}); var proc_output = await cockpit.spawn(["file", "--mime-type", target_path], {superuser: "try"});
var fields = proc_output.split(':'); var fields = proc_output.split(':');
var type = fields[1].trim(); var type = fields[1].trim();
if (!(type.match(/^text/) || type.match(/^inode\/x-empty$/) || this.stat["size"] === 0)) { if (!(type.match(/^text/) || type.match(/^inode\/x-empty$/) || this.stat["size"] === 0)) {
if(!window.confirm("File is of type `" + type + "`. Are you sure you want to edit it?")) if (!window.confirm("File is of type `" + type + "`. Are you sure you want to edit it?")) {
this.nav_window_ref.enable_buttons();
return;
}
}
var contents = "";
try {
contents = await cockpit.spawn(["cat", target_path], {superuser: "try"});
} catch(e) {
this.nav_window_ref.enable_buttons();
var message = "";
if (e.message === "protocol-error")
message = "Error reading file.";
else
message = e.message;
window.alert(message);
return; return;
} }
var contents = await cockpit.spawn(["cat", target_path], {superuser: "try"});
document.getElementById("nav-edit-contents-textarea").value = contents; document.getElementById("nav-edit-contents-textarea").value = contents;
document.getElementById("nav-cancel-edit-contents-btn").onclick = this.hide_edit_file_contents.bind(this); document.getElementById("nav-cancel-edit-contents-btn").onclick = this.hide_edit_file_contents.bind(this);
document.getElementById("nav-continue-edit-contents-btn").onclick = this.write_to_file.bind(this); document.getElementById("nav-continue-edit-contents-btn").onclick = this.write_to_file.bind(this);
@ -993,6 +1011,7 @@ class NavWindow {
} }
this.refresh(); this.refresh();
} }
async get_system_users() { async get_system_users() {
var proc = cockpit.spawn(["getent", "passwd"], {err: "ignore", superuser: "try"}); var proc = cockpit.spawn(["getent", "passwd"], {err: "ignore", superuser: "try"});
var list = document.getElementById("possible-owners"); var list = document.getElementById("possible-owners");
@ -1009,6 +1028,7 @@ class NavWindow {
list.appendChild(option); list.appendChild(option);
} }
} }
async get_system_groups() { async get_system_groups() {
var proc = cockpit.spawn(["getent", "group"], {err: "ignore", superuser: "try"}); var proc = cockpit.spawn(["getent", "group"], {err: "ignore", superuser: "try"});
var list = document.getElementById("possible-groups"); var list = document.getElementById("possible-groups");
@ -1025,6 +1045,21 @@ class NavWindow {
list.appendChild(option); list.appendChild(option);
} }
} }
disable_buttons_for_editing() {
for (let button of document.getElementsByTagName("button")) {
if (!button.classList.contains("editor-btn"))
button.disabled = true;
}
document.getElementById("pwd").disabled = true;
}
enable_buttons() {
for (let button of document.getElementsByTagName("button")) {
button.disabled = false;
}
document.getElementById("pwd").disabled = false;
}
} }
let nav_window = new NavWindow(); let nav_window = new NavWindow();