filebrowser/assets/public/js/application.js

421 lines
10 KiB
JavaScript
Raw Normal View History

2016-06-14 15:13:38 +00:00
"use strict";
var selectedItems = [];
2016-06-21 13:46:59 +00:00
// Array prototype function to remove an element
2016-06-22 20:03:48 +00:00
Array.prototype.removeElement = function (element) {
var i = this.indexOf(element);
if (i != -1) {
this.splice(i, 1);
}
2016-06-14 19:33:59 +00:00
}
2016-06-21 13:46:59 +00:00
// Array prototype function to replace an element
2016-06-22 20:03:48 +00:00
Array.prototype.replaceElement = function (oldEl, newEl) {
var i = this.indexOf(oldEl);
if (i != -1) {
this[i] = newEl;
}
2016-06-21 13:46:59 +00:00
}
// Document prototype function to send a costum event to itself
2016-06-22 20:03:48 +00:00
Document.prototype.sendCostumEvent = function (text) {
document.dispatchEvent(new CustomEvent(text));
}
2016-06-21 13:46:59 +00:00
// Document prototype to get a cookie content
2016-06-22 20:03:48 +00:00
Document.prototype.getCookie = function (name) {
var re = new RegExp("(?:(?:^|.*;\\s*)" + name + "\\s*\\=\\s*([^;]*).*$)|^.*$");
return document.cookie.replace(re, "$1");
2016-06-21 13:46:59 +00:00
}
// Changes a button to the loading animation
2016-06-22 20:03:48 +00:00
Element.prototype.changeToLoading = function () {
let element = this;
let originalText = element.innerHTML;
2016-06-21 13:46:59 +00:00
2016-06-22 20:03:48 +00:00
element.style.opacity = 0;
2016-06-21 13:46:59 +00:00
2016-06-22 20:03:48 +00:00
setTimeout(function () {
element.innerHTML = '<i class="material-icons spin">autorenew</i>';
element.style.opacity = 1;
}, 200);
2016-06-21 13:46:59 +00:00
2016-06-22 20:03:48 +00:00
return originalText;
2016-06-21 13:46:59 +00:00
}
// Changes an element to done animation
2016-06-22 20:03:48 +00:00
Element.prototype.changeToDone = function (error, html) {
this.style.opacity = 0;
2016-06-21 13:46:59 +00:00
2016-06-22 20:03:48 +00:00
let thirdStep = () => {
this.innerHTML = html;
this.style.opacity = 1;
2016-06-21 13:46:59 +00:00
2016-06-22 20:03:48 +00:00
if (selectedItems.length == 0) {
document.sendCostumEvent('changed-selected');
2016-06-21 13:46:59 +00:00
}
2016-06-22 20:03:48 +00:00
}
2016-06-21 13:46:59 +00:00
2016-06-22 20:03:48 +00:00
let secondStep = () => {
this.style.opacity = 0;
setTimeout(thirdStep, 200);
}
let firstStep = () => {
if (error) {
this.innerHTML = '<i class="material-icons">close</i>';
} else {
this.innerHTML = '<i class="material-icons">done</i>';
2016-06-21 13:46:59 +00:00
}
2016-06-22 20:03:48 +00:00
this.style.opacity = 1;
2016-06-21 13:46:59 +00:00
2016-06-22 20:03:48 +00:00
setTimeout(secondStep, 1000);
}
2016-06-21 13:46:59 +00:00
2016-06-22 20:03:48 +00:00
setTimeout(firstStep, 200);
return false;
2016-06-21 13:46:59 +00:00
}
// Event for toggling the mode of view
2016-06-22 20:03:48 +00:00
var viewEvent = function (event) {
let cookie = document.getCookie('view-list');
let listing = document.getElementById('listing');
if (cookie != 'true') {
document.cookie = 'view-list=true';
} else {
document.cookie = 'view-list=false';
}
handleViewType(document.getCookie('view-list'));
return false;
2016-06-15 07:47:33 +00:00
}
2016-06-14 15:13:38 +00:00
2016-06-21 13:46:59 +00:00
// Handles the view type change
2016-06-22 20:03:48 +00:00
var handleViewType = function (viewList) {
let listing = document.getElementById('listing');
let button = document.getElementById('view');
2016-06-14 20:23:54 +00:00
2016-06-22 20:03:48 +00:00
if (viewList == "true") {
listing.classList.add('list');
button.innerHTML = '<i class="material-icons">view_module</i>';
return false;
}
button.innerHTML = '<i class="material-icons">view_list</i>';
listing.classList.remove('list');
return false;
2016-06-14 20:23:54 +00:00
}
2016-06-21 13:46:59 +00:00
// Handles the open file button event
2016-06-22 20:03:48 +00:00
var openEvent = function (event) {
if (selectedItems.length) {
window.open(selectedItems[0] + '?raw=true');
return false;
}
2016-06-14 19:33:59 +00:00
2016-06-22 20:03:48 +00:00
window.open(window.location + '?raw=true');
return false;
2016-06-14 19:33:59 +00:00
}
2016-06-21 13:46:59 +00:00
// Handles the back button event
2016-06-22 20:03:48 +00:00
var backEvent = function (event) {
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
}
2016-06-21 13:46:59 +00:00
// Handles the delete button event
2016-06-22 20:03:48 +00:00
var deleteEvent = function (event) {
if (selectedItems.length) {
Array.from(selectedItems).forEach(link => {
let html = document.getElementById("delete").changeToLoading();
let request = new XMLHttpRequest();
request.open('DELETE', link);
request.send();
request.onreadystatechange = function () {
if (request.readyState == 4) {
if (request.status == 200) {
document.getElementById(link).remove();
console.log(selectedItems);
selectedItems.removeElement(link);
}
document.getElementById('delete').changeToDone((request.status != 200), html);
}
2016-06-22 20:03:48 +00:00
}
});
2016-06-14 15:13:38 +00:00
2016-06-22 20:03:48 +00:00
return false;
}
let 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);
}
document.getElementById('delete').changeToDone((request.status != 200), html);
}
}
2016-06-14 20:07:44 +00:00
2016-06-22 20:03:48 +00:00
return false;
}
2016-06-14 20:23:54 +00:00
2016-06-21 13:46:59 +00:00
// Prevent Default event
2016-06-22 20:03:48 +00:00
var preventDefault = function (event) {
event.preventDefault();
}
2016-06-14 20:23:54 +00:00
2016-06-21 13:46:59 +00:00
// Rename file event
2016-06-22 20:03:48 +00:00
var renameEvent = function (event) {
if (selectedItems.length) {
Array.from(selectedItems).forEach(link => {
let item = document.getElementById(link);
let span = item.getElementsByTagName('span')[0];
let name = span.innerHTML;
item.addEventListener('click', preventDefault);
item.removeEventListener('click', itemClickEvent);
span.setAttribute('contenteditable', 'true');
span.focus();
let keyDownEvent = (event) => {
if (event.keyCode == 13) {
let newName = span.innerHTML;
let html = document.getElementById('rename').changeToLoading();
let request = new XMLHttpRequest();
request.open('PATCH', link);
request.setRequestHeader('Rename-To', newName);
request.send();
request.onreadystatechange = function () {
if (request.readyState == 4) {
if (request.status != 200) {
span.innerHTML = name;
} else {
let newLink = link.replace(name, newName);
item.id = newLink;
selectedItems.replaceElement(link, newLink);
span.innerHTML = newName;
}
document.getElementById('rename').changeToDone((request.status != 200), html);
}
}
}
if (event.KeyCode == 27) {
span.innerHTML = name;
}
if (event.keyCode == 13 || event.keyCode == 27) {
span.setAttribute('contenteditable', 'false');
span.removeEventListener('keydown', keyDownEvent);
item.removeEventListener('click', preventDefault);
item.addEventListener('click', itemClickEvent);
event.preventDefault();
}
return false;
2016-06-22 20:03:48 +00:00
}
span.addEventListener('keydown', keyDownEvent);
span.addEventListener('blur', (event) => {
span.innerHTML = name;
span.setAttribute('contenteditable', 'false');
span.removeEventListener('keydown', keyDownEvent);
item.removeEventListener('click', preventDefault);
});
});
return false;
}
return false;
2016-06-14 15:13:38 +00:00
}
2016-06-21 13:46:59 +00:00
// Download file event
2016-06-22 20:03:48 +00:00
var downloadEvent = function (event) {
if (selectedItems.length) {
Array.from(selectedItems).forEach(item => {
window.open(item + "?download=true");
});
return false;
}
2016-06-14 19:33:59 +00:00
2016-06-22 20:03:48 +00:00
window.open(window.location + "?download=true");
return false;
2016-06-14 15:13:38 +00:00
}
2016-06-23 14:05:51 +00:00
var handleFiles = function (files) {
let button = document.getElementById("upload");
let html = button.changeToLoading();
let data = new FormData();
2016-06-23 07:22:39 +00:00
2016-06-23 14:05:51 +00:00
for (let i = 0; i < files.length; i++) {
data.append(files[i].name, files[i]);
}
2016-06-23 07:22:39 +00:00
2016-06-23 14:05:51 +00:00
let request = new XMLHttpRequest();
request.open('POST', window.location.pathname);
request.setRequestHeader("Upload", "true");
request.send(data);
request.onreadystatechange = function () {
if (request.readyState == 4) {
if (request.status == 200) {
location.reload();
}
2016-06-23 08:15:00 +00:00
2016-06-23 14:05:51 +00:00
button.changeToDone((request.status != 200), html);
}
}
2016-06-23 07:26:44 +00:00
2016-06-23 14:05:51 +00:00
return false;
2016-06-23 07:22:39 +00:00
}
2016-06-22 20:03:48 +00:00
var RemoveLastDirectoryPartOf = function (url) {
var arr = url.split('/');
arr.pop();
return (arr.join('/'));
2016-06-14 20:23:54 +00:00
}
2016-06-22 20:03:48 +00:00
document.addEventListener("changed-selected", function (event) {
var toolbar = document.getElementById("toolbar");
var selectedNumber = selectedItems.length;
2016-06-14 15:13:38 +00:00
2016-06-22 20:03:48 +00:00
document.getElementById("selected-number").innerHTML = selectedNumber;
2016-06-14 15:13:38 +00:00
2016-06-22 20:03:48 +00:00
if (selectedNumber) {
toolbar.classList.add("enabled");
2016-06-14 15:13:38 +00:00
2016-06-22 20:03:48 +00:00
if (selectedNumber > 1) {
document.getElementById("open").classList.add("disabled");
document.getElementById("rename").classList.add("disabled");
}
2016-06-14 15:13:38 +00:00
2016-06-22 20:03:48 +00:00
if (selectedNumber == 1) {
document.getElementById("open").classList.remove("disabled");
document.getElementById("rename").classList.remove("disabled");
}
2016-06-14 15:13:38 +00:00
2016-06-22 20:03:48 +00:00
return false;
}
2016-06-14 15:13:38 +00:00
2016-06-22 20:03:48 +00:00
toolbar.classList.remove("enabled");
return false;
2016-06-14 15:13:38 +00:00
});
2016-06-15 07:47:33 +00:00
2016-06-22 20:03:48 +00:00
var itemClickEvent = function (event) {
var url = this.getElementsByTagName('a')[0].getAttribute('href');
if (selectedItems.length != 0) event.preventDefault();
if (selectedItems.indexOf(url) == -1) {
this.classList.add('selected');
selectedItems.push(url);
} else {
this.classList.remove('selected');
selectedItems.removeElement(url);
}
var event = new CustomEvent('changed-selected');
document.dispatchEvent(event);
return false;
}
2016-06-22 20:03:48 +00:00
var localizeDatetime = function (e, index, ar) {
if (e.textContent === undefined) {
return;
}
var d = new Date(e.getAttribute('datetime'));
if (isNaN(d)) {
d = new Date(e.textContent);
if (isNaN(d)) {
return;
}
}
e.textContent = d.toLocaleString();
}
2016-06-19 22:34:16 +00:00
2016-06-22 20:03:48 +00:00
document.addEventListener("DOMContentLoaded", function (event) {
2016-06-23 21:27:13 +00:00
document.getElementById("logout").addEventListener("click", event => {
let request = new XMLHttpRequest();
request.open('GET', window.location.pathname, true, "username", "password");
request.send();
request.onreadystatechange = function () {
if (request.readyState == 4) {
window.location = "/";
}
}
});
2016-06-22 20:03:48 +00:00
var timeList = Array.prototype.slice.call(document.getElementsByTagName("time"));
timeList.forEach(localizeDatetime);
var items = document.getElementsByClassName('item');
Array.from(items).forEach(link => {
link.addEventListener('click', itemClickEvent);
});
document.getElementById("open").addEventListener("click", openEvent);
if (document.getElementById("back")) {
document.getElementById("back").addEventListener("click", backEvent)
};
if (document.getElementById("view")) {
handleViewType(document.getCookie("view-list"));
document.getElementById("view").addEventListener("click", viewEvent)
};
2016-06-23 07:22:39 +00:00
if (document.getElementById("upload")) {
2016-06-23 14:05:51 +00:00
document.getElementById("upload").addEventListener("click", (event) => {
document.getElementById("upload-input").click();
});
2016-06-23 07:22:39 +00:00
}
2016-06-22 20:03:48 +00:00
document.getElementById("delete").addEventListener("click", deleteEvent);
document.getElementById("download").addEventListener("click", downloadEvent);
2016-06-23 14:13:22 +00:00
let rename = document.getElementById("rename");
if (rename) {
2016-06-23 21:27:13 +00:00
rename.addEventListener("click", renameEvent);
2016-06-23 14:13:22 +00:00
}
2016-06-23 14:05:51 +00:00
2016-06-23 14:13:22 +00:00
if (document.getElementById("listing")) {
document.addEventListener("dragover", function (event) {
event.preventDefault();
}, false);
2016-06-23 08:15:00 +00:00
2016-06-23 14:13:22 +00:00
document.addEventListener("dragover", (event) => {
Array.from(items).forEach(file => {
file.style.opacity = 0.5;
});
}, false);
2016-06-23 08:15:00 +00:00
2016-06-23 14:13:22 +00:00
document.addEventListener("dragleave", (event) => {
Array.from(items).forEach(file => {
file.style.opacity = 1;
});
}, false);
2016-06-23 08:15:00 +00:00
2016-06-23 14:13:22 +00:00
document.addEventListener("drop", function (event) {
event.preventDefault();
var dt = event.dataTransfer;
var files = dt.files;
2016-06-23 08:15:00 +00:00
2016-06-23 14:13:22 +00:00
handleFiles(files);
}, false);
}
2016-06-22 20:03:48 +00:00
return false;
2016-06-15 07:47:33 +00:00
});