From 4c4d80166cd04c049cc33f15f873945c1c7a3748 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 3 Jan 2017 15:58:45 +0000 Subject: [PATCH] try usng a promise Former-commit-id: b39126fdd9a4d600741661e85e73e36c3b86bf8e --- _embed/public/js/common.js | 29 +++++++++++++++-------------- _embed/public/js/listing.js | 19 ++++++++++--------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/_embed/public/js/common.js b/_embed/public/js/common.js index 38a6e1fe..65fea17d 100644 --- a/_embed/public/js/common.js +++ b/_embed/public/js/common.js @@ -4,7 +4,7 @@ var tempID = "_fm_internal_temporary_id", buttons = {}, templates = {}, selectedItems = [], - overlay, clickOverlay, + overlay, clickOverlay, webdav = {}; // Removes an element, if exists, from an array @@ -122,21 +122,22 @@ function getCSSRule(rules) { // 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); +webdav.move = function(oldLink, newLink) { + return new Promise((resolve, reject) => { + let request = new XMLHttpRequest(); + request.open('MOVE', toWebDavURL(oldLink), true); + request.setRequestHeader('Destination', toWebDavURL(newLink)); + request.onload = () => { + if (request.status == 201 || request.status == 204) { + resolve(request.response); + } else { + reject(request.statusText); } } - } + request.onerror = () => reject(request.statusText); + request.send(); + }); } @@ -257,7 +258,7 @@ function loadNextFolder(event) { function moveSelected(event) { event.preventDefault(); - + // TODO: this only works for ONE file. What if there are more files selected? // TODO: use webdav.rename diff --git a/_embed/public/js/listing.js b/_embed/public/js/listing.js index 0cd7e308..a52acab2 100644 --- a/_embed/public/js/listing.js +++ b/_embed/public/js/listing.js @@ -130,23 +130,24 @@ listing.rename = function(event) { let newName = event.currentTarget.querySelector('input').value, newLink = removeLastDirectoryPartOf(link) + "/" + newName, html = buttons.rename.querySelector('i').changeToLoading(); + closePrompt(event); - webdav.rename(link, newLink, success => { - if (success) { + webdav.move(link, newLink) + .then(data => { 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); - }); + buttons.rename.querySelector('i').changeToDone(false, html); + }) + .catch(error => { + item.querySelector('.name').innerHTML = name; + buttons.rename.querySelector('i').changeToDone(true, html); + }); + return false; }