From f1f248e4d6f8b05250edf0f144e8551346eb5d15 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 3 Jan 2017 15:10:33 +0000 Subject: [PATCH] Updates Former-commit-id: 35d375188e7c700484d32a27eb48b30192e20537 --- .editorconfig | 2 +- _embed/public/js/common.js | 143 ++++++++++++++++++++++++------------ _embed/public/js/listing.js | 44 +++++------ _embed/templates/base.tmpl | 9 ++- file/info.go | 25 +++++-- file/listing.go | 20 +++-- filemanager.go | 6 +- handlers/download.go | 6 +- handlers/editor.go | 2 +- handlers/single.go | 2 +- 10 files changed, 160 insertions(+), 99 deletions(-) diff --git a/.editorconfig b/.editorconfig index 8965ca77..c9dfe8a4 100644 --- a/.editorconfig +++ b/.editorconfig @@ -15,7 +15,7 @@ charset = utf-8 # 4 space indentation [*.go] -indent_style = space +indent_style = tab indent_size = 2 # Indentation override for all JS under lib directory diff --git a/_embed/public/js/common.js b/_embed/public/js/common.js index 2f8363af..38a6e1fe 100644 --- a/_embed/public/js/common.js +++ b/_embed/public/js/common.js @@ -4,7 +4,8 @@ var tempID = "_fm_internal_temporary_id", buttons = {}, templates = {}, selectedItems = [], - overlay, clickOverlay; + overlay, clickOverlay, + webdav = {}; // Removes an element, if exists, from an array Array.prototype.removeElement = function(element) { @@ -112,6 +113,33 @@ function getCSSRule(rules) { return result; } + +/* * * * * * * * * * * * * * * * + * * + * WEBDAV * + * * + * * * * * * * * * * * * * * * */ +// TODO: here, we should create an abstraction layer from the webdav. +// We must create functions that do the requests to the webdav backend. +// this functions will contain a 'callback' to be used withing the other function. + +webdav.rename = function(oldLink, newLink, callback) { + let request = new XMLHttpRequest(); + + request.open('MOVE', toWebDavURL(oldLink)); + request.setRequestHeader('Destination', toWebDavURL(newLink)); + request.send(); + request.onreadystatechange = function() { + if (request.readyState == 4) { + if (typeof callback == 'function') { + // This callback argument is a 'success' + callback(request.status == 201 || request.status == 204); + } + } + } +} + + /* * * * * * * * * * * * * * * * * * * EVENTS * @@ -176,11 +204,11 @@ function openEvent(event) { } function selectMoveFolder(event) { - if(event.target.getAttribute("aria-selected") === "true") { + if (event.target.getAttribute("aria-selected") === "true") { event.target.setAttribute("aria-selected", false); return; } else { - if(document.querySelector(".file-list li[aria-selected=true]")) { + if (document.querySelector(".file-list li[aria-selected=true]")) { document.querySelector(".file-list li[aria-selected=true]").setAttribute("aria-selected", false); } event.target.setAttribute("aria-selected", true); @@ -189,71 +217,89 @@ function selectMoveFolder(event) { } function loadNextFolder(event) { - let request = new XMLHttpRequest(), - prompt = document.querySelector("form.prompt.active"); - prompt.addEventListener("submit", moveSelected); + let request = new XMLHttpRequest(), + prompt = document.querySelector("form.prompt.active"); - request.open("GET", "/" + event.target.innerHTML); - request.setRequestHeader("Accept", "application/json"); - request.send(); - request.onreadystatechange = function() { - if(request.readyState == 4 && request.status == 200) { - prompt.querySelector("ul").innerHTML = ""; - for(let f of JSON.parse(request.response)) { - if(f.URL.substr(f.URL.length - 1) == "/") { - if(selectedItems.includes(btoa(f.URL.split("/")[1]))) continue; - let newNode = document.createElement("li"); - newNode.innerHTML = (f.URL.replace("/" + event.target.innerHTML, "").split("/").join("")); - newNode.setAttribute("aria-selected", false); + prompt.addEventListener("submit", moveSelected); - newNode.addEventListener("dblclick", loadNextFolder); - newNode.addEventListener("click", selectMoveFolder); + request.open("GET", event.target.dataset.url); + request.setRequestHeader("Accept", "application/json"); + request.send(); + request.onreadystatechange = function() { + if (request.readyState == 4 && request.status == 200) { + let dirs = 0; - prompt.querySelector("ul").appendChild(newNode); + prompt.querySelector("ul").innerHTML = ""; + prompt.querySelector('code').innerHTML = event.target.dataset.url; + + for (let f of JSON.parse(request.response)) { + if (f.IsDir === true) { + dirs++; + + let newNode = document.createElement("li"); + newNode.dataset.url = f.URL; + newNode.innerHTML = f.Name; + newNode.setAttribute("aria-selected", false); + + newNode.addEventListener("dblclick", loadNextFolder); + newNode.addEventListener("click", selectMoveFolder); + + prompt.querySelector("div.file-list ul").appendChild(newNode); + } + } + + if (dirs === 0) { + prompt.querySelector("p").innerHTML = `There aren't any folders in this directory.`; + } } - } } - } } function moveSelected(event) { - event.preventDefault(); - let request = new XMLHttpRequest(), - oldLink = toWebDavURL(window.location.pathname), - newLink = toWebDavURL(event.srcElement.querySelector("li[aria-selected=true]").innerHTML + "/"); - request.open("MOVE", oldLink); - request.setRequestHeader("Destination", newLink); - request.send(); - request.onreadystatechange = function() { - if(request.readyState == 4) { - if(request.status == 200 || request.status == 204) { - window.reload(); - } + event.preventDefault(); + + // TODO: this only works for ONE file. What if there are more files selected? + // TODO: use webdav.rename + + let request = new XMLHttpRequest(), + oldLink = toWebDavURL(window.location.pathname), + newLink = toWebDavURL(event.srcElement.querySelector("li[aria-selected=true]").innerHTML + "/"); + + request.open("MOVE", oldLink); + request.setRequestHeader("Destination", newLink); + request.send(); + request.onreadystatechange = function() { + if (request.readyState == 4) { + if (request.status == 200 || request.status == 204) { + window.reload(); + } + } } - } } function moveEvent(event) { - if(event.currentTarget.classList.contains("disabled")) return; + if (event.currentTarget.classList.contains("disabled")) return; let request = new XMLHttpRequest(); - request.open("GET", window.location.pathname, true); request.setRequestHeader("Accept", "application/json"); request.send(); request.onreadystatechange = function() { - if(request.readyState == 4) { - if(request.status == 200) { - let prompt = document.importNode(templates.move.content, true); + if (request.readyState == 4) { + if (request.status == 200) { + let prompt = document.importNode(templates.move.content, true), + dirs = 0; - prompt.querySelector("p").innerHTML = `Choose new house for your file(s)/folder(s):`; prompt.querySelector("form").addEventListener("submit", moveSelected); + prompt.querySelector('code').innerHTML = window.location.pathname; + + for (let f of JSON.parse(request.response)) { + if (f.IsDir === true) { + dirs++; - for(let f of JSON.parse(request.response)) { - if(f.URL.split("/").length == 3) { - if(selectedItems.includes(btoa(f.URL.split("/")[1]))) continue; let newNode = document.createElement("li"); - newNode.innerHTML = f.URL.split("/")[1]; + newNode.dataset.url = f.URL; + newNode.innerHTML = f.Name; newNode.setAttribute("aria-selected", false); newNode.addEventListener("dblclick", loadNextFolder); @@ -263,6 +309,10 @@ function moveEvent(event) { } } + if (dirs === 0) { + prompt.querySelector("p").innerHTML = `There aren't any folders in this directory.`; + } + document.body.appendChild(prompt); document.querySelector(".overlay").classList.add("active"); document.querySelector(".prompt").classList.add("active"); @@ -424,6 +474,7 @@ function searchEvent(event) { } } + function setupSearch() { let search = document.getElementById("search"), searchInput = search.querySelector("input"), diff --git a/_embed/public/js/listing.js b/_embed/public/js/listing.js index 712f56ec..0cd7e308 100644 --- a/_embed/public/js/listing.js +++ b/_embed/public/js/listing.js @@ -15,7 +15,7 @@ listing.reload = function(callback) { if (request.status == 200) { document.querySelector('body main').innerHTML = request.responseText; listing.addDoubleTapEvent(); - + if (typeof callback == 'function') { callback(); } @@ -128,31 +128,24 @@ listing.rename = function(event) { event.preventDefault(); let newName = event.currentTarget.querySelector('input').value, - newLink = removeLastDirectoryPartOf(toWebDavURL(link)) + "/" + newName, - html = buttons.rename.querySelector('i').changeToLoading(), - request = new XMLHttpRequest(); + newLink = removeLastDirectoryPartOf(link) + "/" + newName, + html = buttons.rename.querySelector('i').changeToLoading(); - request.open('MOVE', toWebDavURL(link)); - request.setRequestHeader('Destination', newLink); - request.send(); - request.onreadystatechange = function() { - if (request.readyState == 4) { - if (request.status != 201 && request.status != 204) { - span.innerHTML = name; - } else { - closePrompt(event); - - listing.reload(() => { - newName = btoa(newName); - selectedItems = [newName]; - document.getElementById(newName).setAttribute("aria-selected", true); - listing.handleSelectionChange(); - }); - } - - buttons.rename.querySelector('i').changeToDone((request.status != 201 && request.status != 204), html); + webdav.rename(link, newLink, success => { + if (success) { + listing.reload(() => { + newName = btoa(newName); + selectedItems = [newName]; + document.getElementById(newName).setAttribute("aria-selected", true); + listing.handleSelectionChange(); + }); + } else { + item.querySelector('.name').innerHTML = name; } - } + + closePrompt(event); + buttons.rename.querySelector('i').changeToDone(!success, html); + }); return false; } @@ -317,7 +310,6 @@ listing.updateColumns = function(event) { items.style.width = `calc(${100/columns}% - 1em)`; } - listing.addDoubleTapEvent = function() { let items = document.getElementsByClassName('item'), touches = { @@ -434,4 +426,4 @@ document.addEventListener('DOMContentLoaded', event => { } -}); \ No newline at end of file +}); diff --git a/_embed/templates/base.tmpl b/_embed/templates/base.tmpl index a716b8db..f17580fd 100644 --- a/_embed/templates/base.tmpl +++ b/_embed/templates/base.tmpl @@ -193,17 +193,20 @@