try usng a promise

pull/144/head
Henrique Dias 2017-01-03 15:58:45 +00:00
parent 35d375188e
commit b39126fdd9
2 changed files with 25 additions and 23 deletions

View File

@ -4,7 +4,7 @@ var tempID = "_fm_internal_temporary_id",
buttons = {}, buttons = {},
templates = {}, templates = {},
selectedItems = [], selectedItems = [],
overlay, clickOverlay, overlay, clickOverlay,
webdav = {}; webdav = {};
// Removes an element, if exists, from an array // 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. // TODO: here, we should create an abstraction layer from the webdav.
// We must create functions that do the requests to the webdav backend. // 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. // 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)); webdav.move = function(oldLink, newLink) {
request.setRequestHeader('Destination', toWebDavURL(newLink)); return new Promise((resolve, reject) => {
request.send(); let request = new XMLHttpRequest();
request.onreadystatechange = function() { request.open('MOVE', toWebDavURL(oldLink), true);
if (request.readyState == 4) { request.setRequestHeader('Destination', toWebDavURL(newLink));
if (typeof callback == 'function') { request.onload = () => {
// This callback argument is a 'success' if (request.status == 201 || request.status == 204) {
callback(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) { function moveSelected(event) {
event.preventDefault(); event.preventDefault();
// TODO: this only works for ONE file. What if there are more files selected? // TODO: this only works for ONE file. What if there are more files selected?
// TODO: use webdav.rename // TODO: use webdav.rename

View File

@ -130,23 +130,24 @@ listing.rename = function(event) {
let newName = event.currentTarget.querySelector('input').value, let newName = event.currentTarget.querySelector('input').value,
newLink = removeLastDirectoryPartOf(link) + "/" + newName, newLink = removeLastDirectoryPartOf(link) + "/" + newName,
html = buttons.rename.querySelector('i').changeToLoading(); html = buttons.rename.querySelector('i').changeToLoading();
closePrompt(event);
webdav.rename(link, newLink, success => { webdav.move(link, newLink)
if (success) { .then(data => {
listing.reload(() => { listing.reload(() => {
newName = btoa(newName); newName = btoa(newName);
selectedItems = [newName]; selectedItems = [newName];
document.getElementById(newName).setAttribute("aria-selected", true); document.getElementById(newName).setAttribute("aria-selected", true);
listing.handleSelectionChange(); 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; return false;
} }