From 0255b5f7f31c4c6caefc736c59ba6959d671a92d Mon Sep 17 00:00:00 2001 From: Alex Vitkov Date: Sat, 20 Mar 2021 10:58:25 +0200 Subject: Broke up loggedin.js into modules --- front/upload_form.ts | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 front/upload_form.ts (limited to 'front/upload_form.ts') diff --git a/front/upload_form.ts b/front/upload_form.ts new file mode 100644 index 0000000..a2f402e --- /dev/null +++ b/front/upload_form.ts @@ -0,0 +1,66 @@ +import { focused_window } from './window' +import { ExplorerWindow } from './explorer' + + +const upload_form: HTMLFormElement = document.getElementById("upload_form") as any; +const filename_input: HTMLInputElement = document.getElementById("filename") as any; +const override_input: HTMLInputElement = document.getElementById("override_input") as any; +const upload_parent_directory: HTMLInputElement = document.getElementById("upload_parent_directory") as any; +const the_file: HTMLInputElement = document.getElementById("the_file") as any; + +// If this is set to true, requests to uploads.php will be sent with the "override" flag +// which will override existing files with the same name +var override_file = false; +var override_file_filename = ""; +var override_file_path = ""; + +export function show_upload_dialog() { + override_file = false; + the_file.click(); +} + +export function show_upload_dialog_replace_file(folder, filename) { + override_file = true; + override_file_path = folder; + override_file_filename = filename; + the_file.click(); +} + + +// This is called whenever the 's value changes +function on_file_added(_e) { + if (the_file.files.length >= 1) { + + if (override_file) { + filename_input.value = override_file_filename; + override_input.value = "1"; + upload_parent_directory.value = override_file_path; + console.log(filename_input.value, override_input.value, upload_parent_directory.value); + } else { + filename_input.value = the_file.files[0].name; + override_input.value = "0"; + upload_parent_directory.value = focused_window.get_path(); + } + + // Send the form asynchronously through the fetch api + fetch(upload_form.action, { + method: upload_form.method, + body: new FormData(upload_form) + }).then((resp) => { + if (resp.status == 200) { + // TODO the focused window may have changed + // Reload the directory so the user can see the newly uploaded file + (focused_window as ExplorerWindow).openfile(true); + } else { + alert("Upload failed"); + } + }, () => { + alert("Upload failed") + }); + } + else { + alert("No files selected"); + } +} + +the_file.onchange = (e) => { on_file_added(e); }; \ No newline at end of file -- cgit v1.2.3