use modal style prompts

This commit is contained in:
joshuaboud 2021-07-14 14:57:58 -03:00
parent 65695633c9
commit 8e55152c69
No known key found for this signature in database
GPG Key ID: 17EFB59E2A8BF50E
6 changed files with 58 additions and 40 deletions

View File

@ -92,7 +92,7 @@ export class FileUpload {
async upload() { async upload() {
if (await this.check_if_exists()) { if (await this.check_if_exists()) {
if (!window.confirm(this.filename + ": File exists. Replace?")) if (!await this.nav_window_ref.modal_prompt.confirm(this.filename + ": File exists. Replace?"))
return; return;
} }
this.make_html_element(); this.make_html_element();
@ -100,7 +100,7 @@ export class FileUpload {
this.proc.fail((e, data) => { this.proc.fail((e, data) => {
this.reader.onload = () => {} this.reader.onload = () => {}
this.done(); this.done();
window.alert(data); this.nav_window_ref.modal_prompt.alert(data);
}) })
this.proc.done((data) => { this.proc.done((data) => {
this.nav_window_ref.refresh(); this.nav_window_ref.refresh();

View File

@ -75,13 +75,13 @@ export class NavContextMenu {
if (new_name === null) if (new_name === null)
return; return;
if (new_name.includes("/")) { if (new_name.includes("/")) {
window.alert("File name can't contain `/`."); this.nav_window_ref.modal_prompt.alert("File name can't contain `/`.");
return; return;
} }
try { try {
await this.target.mv(new_name); await this.target.mv(new_name);
} catch(e) { } catch(e) {
window.alert(e); this.nav_window_ref.modal_prompt.alert(e);
return; return;
} }
this.nav_window_ref.refresh(); this.nav_window_ref.refresh();
@ -117,7 +117,7 @@ export class NavContextMenu {
result = await this.zip_for_download(); result = await this.zip_for_download();
} catch(e) { } catch(e) {
this.nav_window_ref.stop_load(); this.nav_window_ref.stop_load();
window.alert(e.message); this.nav_window_ref.modal_prompt.alert(e.message);
return; return;
} }
this.nav_window_ref.stop_load(); this.nav_window_ref.stop_load();

View File

@ -102,9 +102,9 @@ export class NavDir extends NavEntry {
proc.done((data) => { proc.done((data) => {
resolve(); resolve();
}); });
proc.fail((e, data) => { proc.fail(async (e, data) => {
if (/^rmdir: failed to remove .*: Directory not empty\n?$/.test(data)) { if (/^rmdir: failed to remove .*: Directory not empty\n?$/.test(data)) {
if (window.confirm("WARNING: '" + this.path_str() + "' is not empty. Delete recursively? This can NOT be undone.")) { if (await this.nav_window_ref.modal_prompt.confirm("WARNING: '" + this.path_str() + "' is not empty.", "Delete recursively? This can NOT be undone.")) {
this.rm_recursive(resolve, reject); this.rm_recursive(resolve, reject);
} }
} else { } else {

View File

@ -32,14 +32,27 @@ export class NavDragDrop {
for (let item of e.dataTransfer.items) { for (let item of e.dataTransfer.items) {
if (item.kind === 'file') { if (item.kind === 'file') {
var file = item.getAsFile(); var file = item.getAsFile();
if (file.type === "") { if (file.type === "" && file.size !== 0) {
window.alert(file.name + ": Cannot upload folders."); this.nav_window_ref.modal_prompt.alert(file.name + ": Cannot upload folders.");
continue; continue;
} }
if (file.size === 0) {
var proc = cockpit.spawn(
["/usr/share/cockpit/navigator/scripts/touch.py", this.nav_window_ref.pwd().path_str() + "/" + file.name],
{superuser: "try", err: "out"}
);
proc.done(() => {
this.nav_window_ref.refresh();
});
proc.fail((e, data) => {
this.nav_window_ref.modal_prompt.alert(data);
});
} else {
var uploader = new FileUpload(file, this.nav_window_ref); var uploader = new FileUpload(file, this.nav_window_ref);
uploader.upload(); uploader.upload();
} }
} }
}
} else { } else {
for (let file of ev.dataTransfer.files) { for (let file of ev.dataTransfer.files) {
if (file.type === "") if (file.type === "")

View File

@ -74,7 +74,7 @@ export class NavFile extends NavEntry {
this.show_edit_file_contents(); this.show_edit_file_contents();
} else { } else {
console.log("Unknown mimetype: " + type); console.log("Unknown mimetype: " + type);
if (window.confirm("Can't open " + this.filename() + " for editing. Download?")) { if (await this.nav_window_ref.modal_prompt.confirm("Can't open " + this.filename() + " for editing.", "Download it instead?")) {
var download = new NavDownloader(this); var download = new NavDownloader(this);
download.download(); download.download();
} }
@ -89,7 +89,7 @@ export class NavFile extends NavEntry {
contents = await cockpit.file(this.path_str(), {superuser: "try"}).read(); contents = await cockpit.file(this.path_str(), {superuser: "try"}).read();
} catch (e) { } catch (e) {
this.nav_window_ref.enable_buttons(); this.nav_window_ref.enable_buttons();
window.alert(e.message); this.nav_window_ref.modal_prompt.alert(e.message);
return; return;
} }
var text_area = document.getElementById("nav-edit-contents-textarea"); var text_area = document.getElementById("nav-edit-contents-textarea");
@ -110,7 +110,7 @@ export class NavFile extends NavEntry {
else else
await cockpit.script("echo -n > $1", [this.path_str()], {superuser: "try"}); await cockpit.script("echo -n > $1", [this.path_str()], {superuser: "try"});
} catch (e) { } catch (e) {
window.alert(e.message); this.nav_window_ref.modal_prompt.alert(e.message);
} }
this.nav_window_ref.refresh(); this.nav_window_ref.refresh();
this.hide_edit_file_contents(); this.hide_edit_file_contents();
@ -175,7 +175,7 @@ export class NavFileLink extends NavFile{
this.show_edit_file_contents(); this.show_edit_file_contents();
} else { } else {
console.log("Unknown mimetype: " + type); console.log("Unknown mimetype: " + type);
window.alert("Can't open " + this.filename() + " for editing."); this.nav_window_ref.modal_prompt.alert("Can't open " + this.filename() + " for editing.");
} }
} }
@ -189,7 +189,7 @@ export class NavFileLink extends NavFile{
contents = await cockpit.file(target_path, {superuser: "try"}).read(); contents = await cockpit.file(target_path, {superuser: "try"}).read();
} catch(e) { } catch(e) {
this.nav_window_ref.enable_buttons(); this.nav_window_ref.enable_buttons();
window.alert(e.message); this.nav_window_ref.modal_prompt.alert(e.message);
return; return;
} }
var text_area = document.getElementById("nav-edit-contents-textarea"); var text_area = document.getElementById("nav-edit-contents-textarea");
@ -208,7 +208,7 @@ export class NavFileLink extends NavFile{
try { try {
await cockpit.file(target_path, {superuser: "try"}).replace(new_contents); await cockpit.file(target_path, {superuser: "try"}).replace(new_contents);
} catch (e) { } catch (e) {
window.alert(e.message); this.nav_window_ref.modal_prompt.alert(e.message);
} }
this.nav_window_ref.refresh(); this.nav_window_ref.refresh();
this.hide_edit_file_contents(); this.hide_edit_file_contents();

View File

@ -3,6 +3,7 @@ import {NavDir} from "./NavDir.js";
import {NavContextMenu} from "./NavContextMenu.js"; import {NavContextMenu} from "./NavContextMenu.js";
import {NavDragDrop} from "./NavDragDrop.js"; import {NavDragDrop} from "./NavDragDrop.js";
import {SortFunctions} from "./SortFunctions.js"; import {SortFunctions} from "./SortFunctions.js";
import {ModalPrompt} from "./ModalPrompt.js";
import {format_bytes} from "../functions.js"; import {format_bytes} from "../functions.js";
export class NavWindow { export class NavWindow {
@ -27,6 +28,8 @@ export class NavWindow {
this.uploader = new NavDragDrop(this.window, this); this.uploader = new NavDragDrop(this.window, this);
this.sort_function = new SortFunctions(); this.sort_function = new SortFunctions();
this.modal_prompt = new ModalPrompt();
} }
/** /**
@ -74,7 +77,7 @@ export class NavWindow {
var files = await this.pwd().get_children(this); var files = await this.pwd().get_children(this);
} catch(e) { } catch(e) {
this.up(); this.up();
window.alert(e); this.modal_prompt.alert(e);
return; return;
} }
while (this.entries.length) { while (this.entries.length) {
@ -227,7 +230,7 @@ export class NavWindow {
this.selected_entry().show_properties(); this.selected_entry().show_properties();
} }
show_edit_selected() { async show_edit_selected() {
var dangerous_dirs = [ var dangerous_dirs = [
"/", "/",
"/usr", "/usr",
@ -261,18 +264,20 @@ export class NavWindow {
} else { } else {
dangerous_selected_str = dangerous_selected[0]; dangerous_selected_str = dangerous_selected[0];
} }
if (!window.confirm( if (!await this.modal_prompt.confirm(
"Warning: editing " + "Warning: editing " +
dangerous_selected_str + dangerous_selected_str +
" can be dangerous. Are you sure?" " can be dangerous.",
"Are you sure?"
)) { )) {
return; return;
} }
} else if (this.selected_entries.size > 1) { } else if (this.selected_entries.size > 1) {
if (!window.confirm( if (!await this.modal_prompt.confirm(
"Warning: are you sure you want to edit permissions for " + "Warning: editing permissions for " +
this.selected_entries.size + this.selected_entries.size +
" files?" " files.",
"Are you sure?"
)) { )) {
return; return;
} }
@ -351,14 +356,14 @@ export class NavWindow {
try { try {
await entry.chown(new_owner, new_group); await entry.chown(new_owner, new_group);
} catch(e) { } catch(e) {
window.alert(e); this.modal_prompt.alert(e);
} }
} }
if (this.changed_mode && (new_perms & 0o777) !== (entry.stat["mode"] & 0o777)) { if (this.changed_mode && (new_perms & 0o777) !== (entry.stat["mode"] & 0o777)) {
try { try {
await entry.chmod(new_perms); await entry.chmod(new_perms);
} catch(e) { } catch(e) {
window.alert(e); this.modal_prompt.alert(e);
} }
} }
} }
@ -369,18 +374,18 @@ export class NavWindow {
async delete_selected() { async delete_selected() {
var prompt = ""; var prompt = "";
if (this.selected_entries.size > 1) { if (this.selected_entries.size > 1) {
prompt = "Deleting " + this.selected_entries.size + " files. This cannot be undone. Are you sure?"; prompt = "Deleting " + this.selected_entries.size + " files.";
} else { } else {
prompt = "Deleting `" + this.selected_entry().path_str() + "` cannot be undone. Are you sure?"; prompt = "Deleting `" + this.selected_entry().path_str() + "`.";
} }
if (!window.confirm(prompt)) { if (!await this.modal_prompt.confirm(prompt, "This cannot be undone. Are you sure?")) {
return; return;
} }
for (let target of this.selected_entries) { for (let target of this.selected_entries) {
try { try {
await target.rm(); await target.rm();
} catch(e) { } catch(e) {
window.alert(e); this.modal_prompt.alert(e);
} }
} }
this.refresh(); this.refresh();
@ -391,7 +396,7 @@ export class NavWindow {
if (new_dir_name === null) if (new_dir_name === null)
return; return;
if (new_dir_name.includes("/")) { if (new_dir_name.includes("/")) {
window.alert("Directory name can't contain `/`."); this.modal_prompt.alert("Directory name can't contain `/`.");
return; return;
} }
var promise = new Promise((resolve, reject) => { var promise = new Promise((resolve, reject) => {
@ -409,7 +414,7 @@ export class NavWindow {
try { try {
await promise; await promise;
} catch(e) { } catch(e) {
window.alert(e); this.modal_prompt.alert(e);
} }
this.refresh(); this.refresh();
} }
@ -419,7 +424,7 @@ export class NavWindow {
if (new_file_name === null) if (new_file_name === null)
return; return;
if (new_file_name.includes("/")) { if (new_file_name.includes("/")) {
window.alert("File name can't contain `/`."); this.modal_prompt.alert("File name can't contain `/`.");
return; return;
} }
var promise = new Promise((resolve, reject) => { var promise = new Promise((resolve, reject) => {
@ -437,7 +442,7 @@ export class NavWindow {
try { try {
await promise; await promise;
} catch(e) { } catch(e) {
window.alert(e); this.modal_prompt.alert(e);
} }
this.refresh(); this.refresh();
} }
@ -450,7 +455,7 @@ export class NavWindow {
if (link_name === null) if (link_name === null)
return; return;
if (link_name.includes("/")) { if (link_name.includes("/")) {
window.alert("Link name can't contain `/`."); this.modal_prompt.alert("Link name can't contain `/`.");
return; return;
} }
var link_path = this.pwd().path_str() + "/" + link_name; var link_path = this.pwd().path_str() + "/" + link_name;
@ -469,7 +474,7 @@ export class NavWindow {
try { try {
await promise; await promise;
} catch(e) { } catch(e) {
window.alert(e); this.modal_prompt.alert(e);
} }
this.refresh(); this.refresh();
} }
@ -511,13 +516,13 @@ export class NavWindow {
cmd, cmd,
{superuser: "try", err: "ignore"} {superuser: "try", err: "ignore"}
); );
proc.stream((data) => { proc.stream(async (data) => {
var payload = JSON.parse(data); var payload = JSON.parse(data);
if (payload["wants-response"]) { if (payload["wants-response"]) {
var user_response = window.confirm(payload["message"]); var user_response = await this.modal_prompt.confirm(payload["message"]);
proc.input(JSON.stringify(user_response) + "\n", true); proc.input(JSON.stringify(user_response) + "\n", true);
} else { } else {
window.alert(payload["message"]); await this.modal_prompt.alert(payload["message"]);
} }
}); });
proc.done((data) => { proc.done((data) => {
@ -530,7 +535,7 @@ export class NavWindow {
try { try {
await promise; await promise;
} catch(e) { } catch(e) {
window.alert(e); this.modal_prompt.alert(e);
} }
this.stop_load(); this.stop_load();
this.refresh(); this.refresh();