From 9b29bb3deb3943dd101d7ab7b01f42ee49951e1b Mon Sep 17 00:00:00 2001 From: Alex Vitkov Date: Fri, 12 Feb 2021 14:33:42 +0200 Subject: icons for different mimetypes --- loggedin.js | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) (limited to 'loggedin.js') diff --git a/loggedin.js b/loggedin.js index d774e16..b275c4a 100644 --- a/loggedin.js +++ b/loggedin.js @@ -8,12 +8,22 @@ const current_directory = document.getElementById("current_directory"); the_file.onchange = on_file_added; -var files = []; const pwd = "/"; const pending_uploads = []; +class FileView { + constructor(filename, visuals, mimetype, is_directory) { + this.filename = filename; + this.visuals = visuals; + this.mimetype = mimetype; + this.is_directory = is_directory; + } +} + +var files = []; + function on_file_added(_e) { if (the_file.files.length >= 1) { filename_input.value = the_file.files[0].name; @@ -54,24 +64,30 @@ function load_dir(pwd) { xhr.open('POST', '/php/readdir.php', true); xhr.onload = function () { for (const f of files) - f[1].remove(); + f.visuals.remove(); files = []; var json = JSON.parse(this.responseText); for (const f of json) { - add_file_visuals(f.name, f.mimetype); + add_file_visuals(f.name, f.is_directory, f.mimetype); } }; xhr.send(data); } -function add_file_visuals(name, mimetype) { +function add_file_visuals(name, is_directory, mimetype) { var fileDiv = document.createElement('div'); var img = document.createElement('img'); var filename = document.createElement('div'); - img.src="/mimeicons/application-pdf.png"; + if (is_directory) { + img.src="/mimeicons/directory.png"; + } + else { + img.src=`/mimeicons/${mimetype.replace("/", "-")}.png`; + } + fileDiv.classList.add('file'); filename.classList.add('filename'); filename.innerText = name; @@ -81,9 +97,9 @@ function add_file_visuals(name, mimetype) { current_directory.appendChild(fileDiv); - files.push([name, fileDiv]); - - return fileDiv; + var file = new FileView(name, fileDiv, mimetype, is_directory); + files.push(file); + return file; } function begin_upload() { -- cgit v1.2.3 From 34ba723e18a7ba0f785e456e38a9234d6efda9b7 Mon Sep 17 00:00:00 2001 From: Alex Vitkov Date: Fri, 12 Feb 2021 15:36:38 +0200 Subject: css --- loggedin.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'loggedin.js') diff --git a/loggedin.js b/loggedin.js index b275c4a..d4f56ae 100644 --- a/loggedin.js +++ b/loggedin.js @@ -1,4 +1,4 @@ -var FORM_ASYNC = false; +var FORM_ASYNC = true; const upload_form = document.getElementById("upload_form"); const the_file = document.getElementById("the_file"); @@ -22,6 +22,12 @@ class FileView { } } +class PendingUpload { + constructor(fileview) { + this.fileview = fileview; + } +} + var files = []; function on_file_added(_e) { @@ -33,13 +39,15 @@ function on_file_added(_e) { return; } + var fileview = add_file_visuals(filename_input.value, false, "pending"); + // 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) { - add_file_visuals(filename_input.value, true); + done_upload(fileview); } else { alert("Upload failed"); @@ -47,13 +55,19 @@ function on_file_added(_e) { }, () => { alert("Upload failed") }); - - + + pending_uploads.push(fileview); } else { alert("No files selected"); } +} +function done_upload(fileview) { + var index = pending_uploads.indexOf(fileview); + if (index >= 0) + pending_uploads.splice(index, 1); + load_dir(pwd); } function load_dir(pwd) { @@ -92,6 +106,9 @@ function add_file_visuals(name, is_directory, mimetype) { filename.classList.add('filename'); filename.innerText = name; + if (mimetype == "pending") + fileDiv.classList.add('pending'); + fileDiv.appendChild(img); fileDiv.appendChild(filename); -- cgit v1.2.3 From d682faeaeea940ab8ab6bcb59245a2116bcc80b1 Mon Sep 17 00:00:00 2001 From: Alex Vitkov Date: Fri, 12 Feb 2021 16:03:14 +0200 Subject: path selector --- loggedin.js | 48 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) (limited to 'loggedin.js') diff --git a/loggedin.js b/loggedin.js index d4f56ae..1affb09 100644 --- a/loggedin.js +++ b/loggedin.js @@ -4,13 +4,12 @@ const upload_form = document.getElementById("upload_form"); const the_file = document.getElementById("the_file"); const filename_input = document.getElementById("filename"); const upload_btn = document.getElementById("upload_btn"); +const the_path = document.getElementById("the_path"); const current_directory = document.getElementById("current_directory"); the_file.onchange = on_file_added; - -const pwd = "/"; - +var pwd = []; const pending_uploads = []; class FileView { @@ -48,8 +47,7 @@ function on_file_added(_e) { }).then((resp) => { if (resp.status == 200) { done_upload(fileview); - } - else { + } else { alert("Upload failed"); } }, () => { @@ -67,12 +65,40 @@ function done_upload(fileview) { var index = pending_uploads.indexOf(fileview); if (index >= 0) pending_uploads.splice(index, 1); - load_dir(pwd); + + load_dir(); } -function load_dir(pwd) { +function load_dir() { var data = new FormData(); - data.append('path', '/'); + + var path = "/"; + for (const d of pwd) + path += d + "/"; + + while (the_path.children.length > 1) + the_path.removeChild(the_path.lastChild); + + for (let i = 0; i < pwd.length; i++) { + var d = pwd[i]; + + var separator_div = document.createElement('div'); + separator_div.classList.add('separator'); + the_path.appendChild(separator_div); + separator_div.innerText = "ยป"; + + var entry = document.createElement('button'); + entry.classList.add('pathentry'); + entry.innerText = d; + the_path.appendChild(entry); + + entry.onclick = () => { + pwd.length = i + 1; + load_dir(); + } + } + + data.append('path', path); var xhr = new XMLHttpRequest(); xhr.open('POST', '/php/readdir.php', true); @@ -97,6 +123,10 @@ function add_file_visuals(name, is_directory, mimetype) { if (is_directory) { img.src="/mimeicons/directory.png"; + fileDiv.onclick = () => { + pwd.push(name); + load_dir(); + } } else { img.src=`/mimeicons/${mimetype.replace("/", "-")}.png`; @@ -123,4 +153,4 @@ function begin_upload() { the_file.click(); } -load_dir("/"); +load_dir(); -- cgit v1.2.3 From e9d94ce6144746fe69a3d9f26e12a8fab9d4a534 Mon Sep 17 00:00:00 2001 From: Alex Vitkov Date: Fri, 12 Feb 2021 16:27:47 +0200 Subject: right click context menu on files --- loggedin.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) (limited to 'loggedin.js') diff --git a/loggedin.js b/loggedin.js index 1affb09..d80c734 100644 --- a/loggedin.js +++ b/loggedin.js @@ -12,6 +12,8 @@ the_file.onchange = on_file_added; var pwd = []; const pending_uploads = []; +var context_menu = null; + class FileView { constructor(filename, visuals, mimetype, is_directory) { this.filename = filename; @@ -127,11 +129,27 @@ function add_file_visuals(name, is_directory, mimetype) { pwd.push(name); load_dir(); } - } - else { + } else { img.src=`/mimeicons/${mimetype.replace("/", "-")}.png`; } + fileDiv.oncontextmenu = (e) => { + context(e, [ + ['Open', () => { + if (is_directory) { + pwd.push(name); + load_dir(); + } else { + alert('not implemented'); + } + }], + ['Rename', () => {alert('not implemented')}], + ['Share', () => {alert('not implemented')}], + ['Delete', () => {alert('not implemented')}], + ]); + e.preventDefault(); + } + fileDiv.classList.add('file'); filename.classList.add('filename'); filename.innerText = name; @@ -153,4 +171,30 @@ function begin_upload() { the_file.click(); } +function context(e, entries) { + if (context_menu) + context_menu.remove(); + + context_menu = document.createElement('ul'); + context_menu.classList.add('context'); + + context_menu.style.left = e.clientX + "px"; + context_menu.style.top = e.clientY + "px"; + + + for (const e of entries) { + const li = document.createElement('li'); + li.innerText = e[0]; + li.onclick = e[1]; + context_menu.appendChild(li); + } + + document.body.appendChild(context_menu); +} + +document.body.onclick = () => { + if (context_menu) + context_menu.remove(); +} + load_dir(); -- cgit v1.2.3 From 6992510faf7de95b37d151da3d5e6f7026af343e Mon Sep 17 00:00:00 2001 From: Alex Vitkov Date: Fri, 12 Feb 2021 16:35:22 +0200 Subject: front end code for rename/delete --- loggedin.js | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) (limited to 'loggedin.js') diff --git a/loggedin.js b/loggedin.js index d80c734..78a7099 100644 --- a/loggedin.js +++ b/loggedin.js @@ -72,11 +72,6 @@ function done_upload(fileview) { } function load_dir() { - var data = new FormData(); - - var path = "/"; - for (const d of pwd) - path += d + "/"; while (the_path.children.length > 1) the_path.removeChild(the_path.lastChild); @@ -100,7 +95,8 @@ function load_dir() { } } - data.append('path', path); + var data = new FormData(); + data.append('path', get_path()); var xhr = new XMLHttpRequest(); xhr.open('POST', '/php/readdir.php', true); @@ -117,6 +113,39 @@ function load_dir() { xhr.send(data); } +function delete_file(filename) { + var file_full_path = get_path() + filename; + + var data = new FormData(); + data.append('path', file_full_path); + + var xhr = new XMLHttpRequest(); + xhr.open('POST', '/php/delete.php', true); + xhr.onload = function () { + load_dir(); + }; + xhr.send(data); +} + +function rename_file(filename) { + var file_full_path = get_path() + filename; + + var new_name = prompt(`Rename ${filename} to`, filename); + if (!new_name) + return; + + var data = new FormData(); + data.append('path', file_full_path); + data.append('new_name', new_name); + + var xhr = new XMLHttpRequest(); + xhr.open('POST', '/php/rename.php', true); + xhr.onload = function () { + load_dir(); + }; + xhr.send(data); +} + function add_file_visuals(name, is_directory, mimetype) { var fileDiv = document.createElement('div'); @@ -143,9 +172,9 @@ function add_file_visuals(name, is_directory, mimetype) { alert('not implemented'); } }], - ['Rename', () => {alert('not implemented')}], + ['Rename', () => { rename_file(name); }], ['Share', () => {alert('not implemented')}], - ['Delete', () => {alert('not implemented')}], + ['Delete', () => { delete_file(name); }], ]); e.preventDefault(); } @@ -192,6 +221,13 @@ function context(e, entries) { document.body.appendChild(context_menu); } +function get_path() { + var path = "/"; + for (const d of pwd) + path += d + "/"; + return path; +} + document.body.onclick = () => { if (context_menu) context_menu.remove(); -- cgit v1.2.3