filebrowser/assets/public/js/application.js

187 lines
5.2 KiB
JavaScript
Raw Normal View History

2016-06-14 15:13:38 +00:00
"use strict";
var selectedItems = [];
2016-06-14 19:33:59 +00:00
Array.prototype.removeElement = function(element) {
var i = this.indexOf(element);
if (i != -1) {
this.splice(i, 1);
}
}
2016-06-14 15:13:38 +00:00
document.addEventListener("DOMContentLoaded", function(event) {
2016-06-14 17:19:10 +00:00
var items = document.getElementsByClassName('item');
2016-06-14 15:13:38 +00:00
Array.from(items).forEach(link => {
link.addEventListener('click', function(event) {
var url = link.getElementsByTagName('a')[0].getAttribute('href');
if (selectedItems.indexOf(url) == -1) {
link.classList.add('selected');
selectedItems.push(url);
} else {
link.classList.remove('selected');
2016-06-14 19:33:59 +00:00
selectedItems.removeElement(url);
2016-06-14 15:13:38 +00:00
}
var event = new CustomEvent('changed-selected');
document.dispatchEvent(event);
return false;
});
});
2016-06-14 19:33:59 +00:00
document.getElementById("open").addEventListener("click", openEvent);
if (document.getElementById("back")) {
document.getElementById("back").addEventListener("click", backEvent)
};
2016-06-14 15:13:38 +00:00
document.getElementById("delete").addEventListener("click", deleteEvent);
document.getElementById("download").addEventListener("click", downloadEvent);
return false;
});
2016-06-14 20:23:54 +00:00
var changeView = function(event) {
return false;
}
2016-06-14 20:07:44 +00:00
var changeToLoading = function(element) {
var originalText = element.innerHTML;
element.style.opacity = 0;
setTimeout(function() {
element.innerHTML = "<i class=\"material-icons spin\">autorenew</i>";
element.style.opacity = 1;
}, 200);
return originalText;
}
var changeToDone = function(element, error, html) {
element.style.opacity = 0;
setTimeout(function() {
if (error) {
element.innerHTML = "<i class=\"material-icons\">close</i>";
} else {
element.innerHTML = "<i class=\"material-icons\">done</i>";
}
element.style.opacity = 1;
setTimeout(function() {
element.style.opacity = 0;
2016-06-14 19:33:59 +00:00
2016-06-14 20:07:44 +00:00
setTimeout(function() {
element.innerHTML = html;
element.style.opacity = 1;
2016-06-14 19:33:59 +00:00
2016-06-14 20:07:44 +00:00
if (selectedItems.length == 0) {
var event = new CustomEvent('changed-selected');
document.dispatchEvent(event);
}
}, 200);
}, 1000);
}, 200);
return false;
}
var openEvent = function(event) {
if (selectedItems.length) {
window.open(selectedItems[0] + "?raw=true");
2016-06-14 19:33:59 +00:00
return false;
}
window.open(window.location + "?raw=true");
return false;
}
2016-06-14 15:13:38 +00:00
var backEvent = function(event) {
2016-06-14 19:33:59 +00:00
var items = document.getElementsByClassName('item');
Array.from(items).forEach(link => {
link.classList.remove('selected');
});
selectedItems = [];
var event = new CustomEvent('changed-selected');
document.dispatchEvent(event);
return false;
2016-06-14 15:13:38 +00:00
}
var deleteEvent = function(event) {
2016-06-14 20:23:54 +00:00
if (selectedItems.length) {
2016-06-14 19:33:59 +00:00
Array.from(selectedItems).forEach(item => {
2016-06-14 20:07:44 +00:00
var html = changeToLoading(document.getElementById("delete"));
2016-06-14 19:33:59 +00:00
var request = new XMLHttpRequest();
request.open("DELETE", item);
request.send();
request.onreadystatechange = function() {
if (request.readyState == 4) {
2016-06-14 20:07:44 +00:00
if (request.status == 200) {
document.getElementById(item).remove();
selectedItems.removeElement(item);
2016-06-14 19:33:59 +00:00
}
2016-06-14 15:13:38 +00:00
2016-06-14 20:07:44 +00:00
changeToDone(document.getElementById("delete"), (request.status != 200), html);
2016-06-14 19:33:59 +00:00
}
}
});
2016-06-14 20:07:44 +00:00
2016-06-14 19:33:59 +00:00
return false;
2016-06-14 20:23:54 +00:00
}
var request = new XMLHttpRequest();
request.open("DELETE", window.location);
request.send();
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 200) {
window.location.pathname = RemoveLastDirectoryPartOf(window.location.pathname);
}
changeToDone(document.getElementById("delete"), (request.status != 200), html);
}
}
return false;
2016-06-14 15:13:38 +00:00
}
var downloadEvent = function(event) {
2016-06-14 19:33:59 +00:00
if (selectedItems.length) {
Array.from(selectedItems).forEach(item => {
window.open(item + "?download=true");
});
return false;
}
window.open(window.location + "?download=true");
return false;
2016-06-14 15:13:38 +00:00
}
2016-06-14 20:23:54 +00:00
var RemoveLastDirectoryPartOf = function(url) {
var arr = url.split('/');
arr.pop();
return( arr.join('/') );
}
2016-06-14 15:13:38 +00:00
document.addEventListener("changed-selected", function(event) {
var toolbar = document.getElementById("toolbar");
var selectedNumber = selectedItems.length;
document.getElementById("selected-number").innerHTML = selectedNumber;
if (selectedNumber) {
toolbar.classList.add("enabled");
if (selectedNumber > 1) {
document.getElementById("open").classList.add("disabled");
document.getElementById("rename").classList.add("disabled");
}
if (selectedNumber == 1) {
document.getElementById("open").classList.remove("disabled");
document.getElementById("rename").classList.remove("disabled");
}
return false;
}
toolbar.classList.remove("enabled");
return false;
});