From b17c9ee6046735433e4ba037c9b60c5f592bd15e Mon Sep 17 00:00:00 2001 From: Alex Vitkov Date: Sat, 13 Feb 2021 09:12:28 +0200 Subject: /php/move.php --- loggedin.js | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'loggedin.js') diff --git a/loggedin.js b/loggedin.js index a703849..0f48746 100644 --- a/loggedin.js +++ b/loggedin.js @@ -123,7 +123,7 @@ function load_dir() { } function delete_file(filename) { - var file_full_path = get_path() + "/" + filename; + var file_full_path = path_combine(get_path(), filename); var data = new FormData(); data.append('path', file_full_path); @@ -137,7 +137,7 @@ function delete_file(filename) { } function rename_file(filename) { - var file_full_path = get_path() + filename; + var file_full_path = path_combine(get_path(), filename); var new_name = prompt(`Rename ${filename} to`, filename); if (!new_name) @@ -155,6 +155,20 @@ function rename_file(filename) { xhr.send(data); } +function move_file(new_folder, filename) { + var data = new FormData(); + data.append('old_folder', get_path()); + data.append('new_folder', new_folder); + data.append('filename', filename); + + var xhr = new XMLHttpRequest(); + xhr.open('POST', '/php/move.php', true); + xhr.onload = function () { + load_dir(); + }; + xhr.send(data); +} + function new_folder() { var dirname = prompt(`Directory name`, "New Folder"); if (!dirname) @@ -210,7 +224,11 @@ function end_drag(e) { } function drop_handler(dst, src) { - alert(`Dropped ${dst.filename} on ${src.filename}`); + if (dst.is_directory) { + move_file(path_combine(get_path(), dst.filename), src.filename); + } else { + alert(`Dropped ${dst.filename} on ${src.filename}`); + } } function add_file_visuals(fileview) { @@ -256,7 +274,7 @@ function add_file_visuals(fileview) { visuals.onmouseup = (e) => { if (dragging) { - drop_handler(dragging_fileview, fileview); + drop_handler(fileview, dragging_fileview); end_drag(); } e.preventDefault(); @@ -308,6 +326,14 @@ function get_path() { return path; } +function path_combine(a, b) { + const last_char = a.slice(-1); + if (last_char == "/") + return a + b; + else + return a + "/" + b; +} + document.body.onclick = () => { if (context_menu) context_menu.remove(); -- cgit v1.2.3