filebrowser/_embed/public/js/listing.js

421 lines
11 KiB
JavaScript
Raw Normal View History

2016-12-30 11:15:10 +00:00
'use strict';
2017-01-01 22:40:12 +00:00
var listing = {
2017-01-03 17:06:04 +00:00
selectMultiple: false
2017-01-01 22:40:12 +00:00
};
2016-12-31 15:29:36 +00:00
listing.reload = function(callback) {
2017-01-03 17:06:04 +00:00
let request = new XMLHttpRequest();
request.open('GET', window.location);
request.setRequestHeader('Minimal', 'true');
request.send();
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 200) {
document.querySelector('body main').innerHTML = request.responseText;
listing.addDoubleTapEvent();
if (typeof callback == 'function') {
callback();
2016-12-30 11:15:10 +00:00
}
2017-01-03 17:06:04 +00:00
}
2016-12-30 11:15:10 +00:00
}
2017-01-03 17:06:04 +00:00
}
2016-12-30 11:15:10 +00:00
}
2016-12-31 15:29:36 +00:00
listing.itemDragStart = function(event) {
2017-01-03 17:06:04 +00:00
let el = event.target;
2016-12-31 15:29:36 +00:00
2017-01-03 17:06:04 +00:00
for (let i = 0; i < 5; i++) {
if (!el.classList.contains('item')) {
el = el.parentElement;
2016-12-31 15:29:36 +00:00
}
2017-01-03 17:06:04 +00:00
}
2016-12-31 15:29:36 +00:00
2017-01-03 17:06:04 +00:00
event.dataTransfer.setData("id", el.id);
event.dataTransfer.setData("name", el.querySelector('.name').innerHTML);
2016-12-31 15:29:36 +00:00
}
listing.itemDragOver = function(event) {
2017-01-03 17:06:04 +00:00
event.preventDefault();
let el = event.target;
2016-12-31 15:29:36 +00:00
2017-01-03 17:06:04 +00:00
for (let i = 0; i < 5; i++) {
if (!el.classList.contains('item')) {
el = el.parentElement;
2016-12-31 15:29:36 +00:00
}
2017-01-03 17:06:04 +00:00
}
2016-12-31 15:29:36 +00:00
2017-01-03 17:06:04 +00:00
el.style.opacity = 1;
2016-12-31 15:29:36 +00:00
}
listing.itemDrop = function(e) {
2017-01-03 17:06:04 +00:00
e.preventDefault();
2016-12-31 15:29:36 +00:00
2017-01-03 17:06:04 +00:00
let el = e.target,
id = e.dataTransfer.getData("id"),
name = e.dataTransfer.getData("name");
2016-12-31 15:29:36 +00:00
2017-01-03 17:06:04 +00:00
if (id == "" || name == "") return;
2016-12-31 15:29:36 +00:00
2017-01-03 17:06:04 +00:00
for (let i = 0; i < 5; i++) {
if (!el.classList.contains('item')) {
el = el.parentElement;
2016-12-31 15:29:36 +00:00
}
2017-01-03 17:06:04 +00:00
}
2016-12-31 15:29:36 +00:00
2017-01-03 17:06:04 +00:00
if (el.id === id) return;
2016-12-31 15:29:36 +00:00
2017-01-03 17:06:04 +00:00
let oldLink = document.getElementById(id).dataset.url,
newLink = el.dataset.url + name;
2016-12-31 15:29:36 +00:00
2017-01-03 17:06:04 +00:00
webdav.move(oldLink, newLink)
.then(() => listing.reload())
.catch(e => console.log(e));
2016-12-31 15:29:36 +00:00
}
listing.documentDrop = function(event) {
2017-01-03 17:06:04 +00:00
event.preventDefault();
let dt = event.dataTransfer,
files = dt.files,
el = event.target,
items = document.getElementsByClassName('item');
for (let i = 0; i < 5; i++) {
if (el != null && !el.classList.contains('item')) {
el = el.parentElement;
2016-12-31 15:29:36 +00:00
}
2017-01-03 17:06:04 +00:00
}
2016-12-31 15:29:36 +00:00
2017-01-03 17:06:04 +00:00
if (files.length > 0) {
if (el != null && el.classList.contains('item') && el.dataset.dir == "true") {
listing.handleFiles(files, el.querySelector('.name').innerHTML + "/");
return;
2016-12-31 15:29:36 +00:00
}
2017-01-03 17:06:04 +00:00
listing.handleFiles(files, "");
} else {
Array.from(items).forEach(file => {
file.style.opacity = 1;
});
}
2016-12-31 15:29:36 +00:00
}
listing.rename = function(event) {
2017-01-03 17:06:04 +00:00
if (!selectedItems.length || selectedItems.length > 1) {
return false;
}
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
let item = document.getElementById(selectedItems[0]);
2016-12-31 18:35:27 +00:00
2017-01-03 17:06:04 +00:00
if (item.classList.contains('disabled')) {
return false;
}
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
let link = item.dataset.url,
field = item.querySelector('.name'),
name = field.innerHTML;
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
let submit = (event) => {
event.preventDefault();
2016-12-31 18:35:27 +00:00
2017-01-03 17:06:04 +00:00
let newName = event.currentTarget.querySelector('input').value,
newLink = removeLastDirectoryPartOf(link) + "/" + newName,
html = buttons.rename.querySelector('i').changeToLoading();
closePrompt(event);
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
webdav.move(link, newLink).then(() => {
listing.reload(() => {
newName = btoa(newName);
selectedItems = [newName];
document.getElementById(newName).setAttribute("aria-selected", true);
listing.handleSelectionChange();
});
2016-12-31 18:35:27 +00:00
2017-01-03 17:06:04 +00:00
buttons.rename.querySelector('i').changeToDone(false, html);
}).catch(error => {
field.innerHTML = name;
buttons.rename.querySelector('i').changeToDone(true, html);
console.log(error);
});
2016-12-30 11:15:10 +00:00
return false;
2017-01-03 17:06:04 +00:00
}
let clone = document.importNode(templates.question.content, true);
clone.querySelector('h3').innerHTML = 'Rename';
clone.querySelector('input').value = name;
clone.querySelector('.ok').innerHTML = 'Rename';
clone.querySelector('form').addEventListener('submit', submit);
document.querySelector('body').appendChild(clone)
document.querySelector('.overlay').classList.add('active');
document.querySelector('.prompt').classList.add('active');
return false;
2016-12-30 11:15:10 +00:00
}
2016-12-31 15:29:36 +00:00
listing.handleFiles = function(files, base) {
2017-01-03 17:06:04 +00:00
let html = buttons.upload.querySelector('i').changeToLoading(),
promises = [];
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
for (let file of files) {
promises.push(webdav.put(window.location.pathname + base + file.name, file));
}
Promise.all(promises)
.then(() => {
listing.reload();
buttons.upload.querySelector('i').changeToDone(false, html);
})
.catch(e => {
console.log(e);
buttons.upload.querySelector('i').changeToDone(true, html);
})
return false;
2016-12-30 11:15:10 +00:00
}
2016-12-31 15:29:36 +00:00
listing.unselectAll = function() {
2017-01-03 17:06:04 +00:00
let items = document.getElementsByClassName('item');
Array.from(items).forEach(link => {
link.setAttribute("aria-selected", false);
});
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
selectedItems = [];
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
listing.handleSelectionChange();
return false;
2016-12-30 11:15:10 +00:00
}
2016-12-31 15:29:36 +00:00
listing.handleSelectionChange = function(event) {
2017-01-03 17:06:04 +00:00
listing.redefineDownloadURLs();
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
let selectedNumber = selectedItems.length,
fileAction = document.getElementById("file-only");
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
if (selectedNumber) {
fileAction.classList.remove("disabled");
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
if (selectedNumber > 1) {
buttons.open.classList.add("disabled");
buttons.rename.classList.add("disabled");
}
2017-01-01 22:40:12 +00:00
2017-01-03 17:06:04 +00:00
if (selectedNumber == 1) {
if (document.getElementById(selectedItems[0]).dataset.dir == "true") {
buttons.open.classList.add("disabled");
} else {
buttons.open.classList.remove("disabled");
}
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
buttons.rename.classList.remove("disabled");
2016-12-30 11:15:10 +00:00
}
return false;
2017-01-03 17:06:04 +00:00
}
fileAction.classList.add("disabled");
return false;
2016-12-31 15:29:36 +00:00
}
2016-12-30 11:15:10 +00:00
2016-12-31 15:29:36 +00:00
listing.redefineDownloadURLs = function() {
2017-01-03 17:06:04 +00:00
let files = "";
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
for (let i = 0; i < selectedItems.length; i++) {
let url = document.getElementById(selectedItems[i]).dataset.url;
files += url.replace(window.location.pathname, "") + ",";
}
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
files = files.substring(0, files.length - 1);
files = encodeURIComponent(files);
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
let links = document.querySelectorAll("#download ul a");
Array.from(links).forEach(link => {
link.href = "?download=" + link.dataset.format + "&files=" + files;
});
2016-12-30 11:15:10 +00:00
}
2016-12-31 15:29:36 +00:00
listing.openItem = function(event) {
2017-01-03 17:06:04 +00:00
window.location = event.currentTarget.dataset.url;
2016-12-31 15:29:36 +00:00
}
listing.selectItem = function(event) {
2017-01-03 17:06:04 +00:00
let el = event.currentTarget;
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
if (selectedItems.length != 0) event.preventDefault();
if (selectedItems.indexOf(el.id) == -1) {
if (!event.ctrlKey && !listing.selectMultiple) listing.unselectAll();
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
el.setAttribute("aria-selected", true);
selectedItems.push(el.id);
} else {
el.setAttribute("aria-selected", false);
selectedItems.removeElement(el.id);
}
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
listing.handleSelectionChange();
return false;
2016-12-31 15:29:36 +00:00
}
2016-12-30 11:15:10 +00:00
2016-12-31 15:29:36 +00:00
listing.newFileButton = function(event) {
2017-01-03 17:06:04 +00:00
event.preventDefault();
2016-12-31 15:29:36 +00:00
2017-01-03 17:06:04 +00:00
let clone = document.importNode(templates.question.content, true);
clone.querySelector('h3').innerHTML = 'New file';
clone.querySelector('p').innerHTML = 'End with a trailing slash to create a dir.';
clone.querySelector('.ok').innerHTML = 'Create';
clone.querySelector('form').addEventListener('submit', listing.newFilePrompt);
2016-12-31 15:29:36 +00:00
2017-01-03 17:06:04 +00:00
document.querySelector('body').appendChild(clone)
document.querySelector('.overlay').classList.add('active');
document.querySelector('.prompt').classList.add('active');
2016-12-31 15:29:36 +00:00
}
listing.newFilePrompt = function(event) {
2017-01-03 17:06:04 +00:00
event.preventDefault();
let button = document.getElementById('new'),
html = button.querySelector('i').changeToLoading(),
request = new XMLHttpRequest(),
name = event.currentTarget.querySelector('input').value;
request.open((name.endsWith("/") ? "MKCOL" : "PUT"), toWebDavURL(window.location.pathname + name));
request.send();
request.onreadystatechange = function() {
if (request.readyState == 4) {
button.querySelector('i').changeToDone((request.status != 201), html);
listing.reload();
2016-12-31 15:29:36 +00:00
}
2017-01-03 17:06:04 +00:00
}
2016-12-31 15:29:36 +00:00
2017-01-03 17:06:04 +00:00
closePrompt(event);
return false;
2016-12-31 15:29:36 +00:00
}
listing.updateColumns = function(event) {
2017-01-03 17:06:04 +00:00
let columns = Math.floor(document.getElementById('listing').offsetWidth / 300),
items = getCSSRule(['#listing.mosaic .item', '.mosaic#listing .item']);
2016-12-31 15:29:36 +00:00
2017-01-03 17:06:04 +00:00
items.style.width = `calc(${100/columns}% - 1em)`;
2016-12-31 15:29:36 +00:00
}
2017-01-02 12:20:20 +00:00
listing.addDoubleTapEvent = function() {
2017-01-03 17:06:04 +00:00
let items = document.getElementsByClassName('item'),
touches = {
id: '',
count: 0
};
Array.from(items).forEach(file => {
file.addEventListener('touchstart', event => {
if (touches.id != file.id) {
touches.id = file.id;
touches.count = 1;
setTimeout(() => {
touches.count = 0;
}, 500)
return;
}
touches.count++;
if (touches.count > 1) {
window.location = file.dataset.url;
}
2017-01-02 12:20:20 +00:00
});
2017-01-03 17:06:04 +00:00
});
2017-01-02 12:20:20 +00:00
}
2016-12-31 15:29:36 +00:00
// Keydown events
2016-12-31 21:04:47 +00:00
window.addEventListener('keydown', (event) => {
2017-01-03 17:06:04 +00:00
if (event.keyCode == 27) {
listing.unselectAll();
2016-12-31 18:35:27 +00:00
2017-01-03 17:06:04 +00:00
if (document.querySelectorAll('.prompt').length) {
closePrompt(event);
2016-12-31 18:35:27 +00:00
}
2017-01-03 17:06:04 +00:00
}
2016-12-31 18:35:27 +00:00
2017-01-03 17:06:04 +00:00
if (event.keyCode == 113) {
listing.rename();
}
2017-01-01 22:40:12 +00:00
2017-01-03 17:06:04 +00:00
if (event.ctrlKey || event.metaKey) {
console.log("hey")
switch (String.fromCharCode(event.which).toLowerCase()) {
case 's':
event.preventDefault();
window.location = "?download=true"
2016-12-31 21:04:47 +00:00
}
2017-01-03 17:06:04 +00:00
}
2016-12-31 15:29:36 +00:00
});
window.addEventListener("resize", () => {
2017-01-03 17:06:04 +00:00
listing.updateColumns();
2016-12-31 15:29:36 +00:00
});
document.addEventListener('DOMContentLoaded', event => {
2017-01-03 17:06:04 +00:00
listing.updateColumns();
listing.addDoubleTapEvent();
2016-12-31 15:29:36 +00:00
2017-01-03 17:06:04 +00:00
buttons.rename = document.getElementById("rename");
buttons.upload = document.getElementById("upload");
buttons.new = document.getElementById('new');
buttons.download = document.getElementById('download');
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
document.getElementById('multiple-selection-activate').addEventListener('click', event => {
listing.selectMultiple = true;
clickOverlay.click();
2017-01-01 23:38:11 +00:00
2017-01-03 17:06:04 +00:00
document.getElementById('multiple-selection').classList.add('active');
document.querySelector('body').style.paddingBottom = "4em";
})
2017-01-01 22:40:12 +00:00
2017-01-03 17:06:04 +00:00
document.getElementById('multiple-selection-cancel').addEventListener('click', event => {
listing.selectMultiple = false;
2017-01-01 22:40:12 +00:00
2017-01-03 17:06:04 +00:00
document.querySelector('body').style.paddingBottom = "0";
document.getElementById('multiple-selection').classList.remove('active');
})
2017-01-01 22:40:12 +00:00
2017-01-03 17:06:04 +00:00
if (user.AllowEdit) {
buttons.rename.addEventListener("click", listing.rename);
}
2017-01-01 23:38:11 +00:00
2017-01-03 17:06:04 +00:00
let items = document.getElementsByClassName('item');
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
if (user.AllowNew) {
buttons.upload.addEventListener("click", (event) => {
document.getElementById("upload-input").click();
});
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
buttons.new.addEventListener('click', listing.newFileButton);
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
// Drag and Drop
document.addEventListener("dragover", function(event) {
event.preventDefault();
}, false);
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
document.addEventListener("dragenter", (event) => {
Array.from(items).forEach(file => {
file.style.opacity = 0.5;
});
}, false);
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
document.addEventListener("dragend", (event) => {
Array.from(items).forEach(file => {
file.style.opacity = 1;
});
}, false);
2016-12-30 11:15:10 +00:00
2017-01-03 17:06:04 +00:00
document.addEventListener("drop", listing.documentDrop, false);
}
2017-01-01 23:38:11 +00:00
2017-01-03 15:10:33 +00:00
});