filebrowser/_embed/public/js/common.js

468 lines
13 KiB
JavaScript
Raw Normal View History

2016-12-30 11:15:10 +00:00
'use strict';
var tempID = "_fm_internal_temporary_id",
2016-12-30 11:37:17 +00:00
buttons = {},
2016-12-31 15:29:36 +00:00
templates = {},
2017-01-01 22:40:12 +00:00
selectedItems = [],
overlay, clickOverlay;
2016-12-30 11:15:10 +00:00
// Removes an element, if exists, from an array
Array.prototype.removeElement = function(element) {
var i = this.indexOf(element);
2016-12-30 11:30:33 +00:00
if (i != -1) this.splice(i, 1);
2016-12-30 11:15:10 +00:00
}
// Replaces an element inside an array by another
2016-12-30 11:30:33 +00:00
Array.prototype.replaceElement = function(oldElement, newElement) {
var i = this.indexOf(oldElement);
if (i != -1) this[i] = newElement;
2016-12-30 11:15:10 +00:00
}
// Sends a costum event to itself
Document.prototype.sendCostumEvent = function(text) {
this.dispatchEvent(new CustomEvent(text));
}
// Gets the content of a cookie
Document.prototype.getCookie = function(name) {
var re = new RegExp("(?:(?:^|.*;\\s*)" + name + "\\s*\\=\\s*([^;]*).*$)|^.*$");
return document.cookie.replace(re, "$1");
}
// Changes a button to the loading animation
Element.prototype.changeToLoading = function() {
2016-12-30 11:30:33 +00:00
let element = this,
originalText = element.innerHTML;
2016-12-30 11:15:10 +00:00
element.style.opacity = 0;
setTimeout(function() {
2017-01-01 22:40:12 +00:00
element.classList.add('spin');
element.innerHTML = 'autorenew';
2016-12-30 11:15:10 +00:00
element.style.opacity = 1;
}, 200);
return originalText;
}
// Changes an element to done animation
Element.prototype.changeToDone = function(error, html) {
this.style.opacity = 0;
let thirdStep = () => {
this.innerHTML = html;
this.style.opacity = null;
2016-12-30 11:30:33 +00:00
if (selectedItems.length == 0 && document.getElementById('listing')) document.sendCostumEvent('changed-selected');
2016-12-30 11:15:10 +00:00
}
let secondStep = () => {
this.style.opacity = 0;
setTimeout(thirdStep, 200);
}
let firstStep = () => {
2017-01-01 22:40:12 +00:00
this.classList.remove('spin');
this.innerHTML = error ? 'close' : 'done';
2016-12-30 11:15:10 +00:00
this.style.opacity = 1;
setTimeout(secondStep, 1000);
}
setTimeout(firstStep, 200);
return false;
}
function toWebDavURL(url) {
return window.location.origin + url.replace(baseURL + "/", webdavURL + "/");
}
// Remove the last directory of an url
var removeLastDirectoryPartOf = function(url) {
var arr = url.split('/');
2016-12-31 15:29:36 +00:00
if (arr.pop() === "") {
arr.pop();
}
2016-12-30 11:15:10 +00:00
return (arr.join('/'));
}
2017-01-02 12:20:20 +00:00
function getCSSRule(rules) {
for (let i = 0; i < rules.length; i++) {
rules[i] = rules[i].toLowerCase();
}
var result = null,
find = Array.prototype.find;
find.call(document.styleSheets, styleSheet => {
result = find.call(styleSheet.cssRules, cssRule => {
let found = false;
if (cssRule instanceof CSSStyleRule) {
for (let i = 0; i < rules.length; i++) {
if (cssRule.selectorText.toLowerCase() == rules[i]) found = true;
}
}
return found;
});
return result != null;
});
return result;
}
2016-12-30 11:15:10 +00:00
/* * * * * * * * * * * * * * * *
* *
* EVENTS *
* *
* * * * * * * * * * * * * * * */
2016-12-31 15:29:36 +00:00
function closePrompt(event) {
2016-12-31 21:04:47 +00:00
let prompt = document.querySelector('.prompt');
2017-01-01 22:40:12 +00:00
2016-12-31 21:04:47 +00:00
if (!prompt) return;
2016-12-31 15:29:36 +00:00
event.preventDefault();
document.querySelector('.overlay').classList.remove('active');
prompt.classList.remove('active');
setTimeout(() => {
prompt.remove();
}, 100);
}
function notImplemented(event) {
event.preventDefault();
2017-01-01 22:40:12 +00:00
clickOverlay.click();
2016-12-31 15:29:36 +00:00
let clone = document.importNode(templates.info.content, true);
clone.querySelector('h3').innerHTML = 'Not implemented';
clone.querySelector('p').innerHTML = "Sorry, but this feature wasn't implemented yet.";
document.querySelector('body').appendChild(clone)
document.querySelector('.overlay').classList.add('active');
document.querySelector('.prompt').classList.add('active');
}
2016-12-30 11:15:10 +00:00
// Prevent Default event
var preventDefault = function(event) {
event.preventDefault();
}
function logoutEvent(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 = "/";
}
}
}
function openEvent(event) {
if (event.currentTarget.classList.contains('disabled')) {
return false;
}
let link = '?raw=true';
if (selectedItems.length) {
link = document.getElementById(selectedItems[0]).dataset.url + link;
} else {
link = window.location + link;
}
window.open(link);
return false;
}
2016-12-31 15:29:36 +00:00
function deleteSelected(single) {
return function(event) {
event.preventDefault();
Array.from(selectedItems).forEach(id => {
let request = new XMLHttpRequest(),
2017-01-01 22:40:12 +00:00
html = buttons.delete.querySelector('i').changeToLoading(),
2016-12-31 15:29:36 +00:00
el, url;
if (single) {
url = window.location.pathname;
} else {
el = document.getElementById(id);
url = el.dataset.url;
}
request.open('DELETE', toWebDavURL(url));
request.onreadystatechange = function() {
if (request.readyState == 4) {
if (request.status == 204) {
if (single) {
window.location.pathname = removeLastDirectoryPartOf(window.location.pathname);
} else {
el.remove();
selectedItems.removeElement(id);
}
}
2017-01-01 22:40:12 +00:00
buttons.delete.querySelector('i').changeToDone(request.status != 204, html);
2016-12-31 15:29:36 +00:00
}
}
request.send();
});
closePrompt(event);
}
}
2016-12-30 11:15:10 +00:00
// Handles the delete button event
function deleteEvent(event) {
let single = false;
if (!selectedItems.length) {
2016-12-30 11:45:09 +00:00
selectedItems = ["placeholder"];
2016-12-30 11:15:10 +00:00
single = true;
}
2016-12-31 15:29:36 +00:00
let clone = document.importNode(templates.question.content, true);
clone.querySelector('h3').innerHTML = 'Delete files';
2017-01-01 22:40:12 +00:00
2016-12-31 21:04:47 +00:00
if (single) {
2017-01-01 22:40:12 +00:00
clone.querySelector('p').innerHTML = `Are you sure you want to delete this file/folder?`;
2016-12-31 21:04:47 +00:00
} else {
2017-01-01 22:40:12 +00:00
clone.querySelector('p').innerHTML = `Are you sure you want to delete ${selectedItems.length} file(s)?`;
2016-12-31 21:04:47 +00:00
}
2017-01-01 22:40:12 +00:00
2016-12-31 15:29:36 +00:00
clone.querySelector('input').remove();
clone.querySelector('.ok').innerHTML = 'Delete';
clone.querySelector('form').addEventListener('submit', deleteSelected(single));
2016-12-30 11:53:46 +00:00
2016-12-31 15:29:36 +00:00
document.querySelector('body').appendChild(clone)
document.querySelector('.overlay').classList.add('active');
document.querySelector('.prompt').classList.add('active');
2016-12-30 11:15:10 +00:00
return false;
}
2016-12-30 11:32:54 +00:00
function resetSearchText() {
let box = document.querySelector('#search > div div');
if (user.AllowCommands) {
2016-12-30 16:22:26 +00:00
box.innerHTML = `Search or use one of your supported commands: ${user.Commands.join(", ")}.`;
2016-12-30 11:32:54 +00:00
} else {
box.innerHTML = "Type and press enter to search.";
}
}
2016-12-30 11:15:10 +00:00
2016-12-30 11:32:54 +00:00
function searchEvent(event) {
if (this.value.length == 0) {
resetSearchText();
2016-12-30 11:15:10 +00:00
return;
}
2016-12-30 11:32:54 +00:00
let value = this.value,
search = document.getElementById('search'),
scrollable = document.querySelector('#search > div'),
box = document.querySelector('#search > div div'),
pieces = value.split(' '),
supported = false;
2016-12-30 11:15:10 +00:00
user.Commands.forEach(function(cmd) {
if (cmd == pieces[0]) {
supported = true;
}
});
2016-12-30 11:32:54 +00:00
if (!supported || !user.AllowCommands) {
2016-12-30 11:15:10 +00:00
box.innerHTML = "Press enter to search."
} else {
box.innerHTML = "Press enter to execute."
}
if (event.keyCode == 13) {
box.innerHTML = '';
search.classList.add('ongoing');
2016-12-30 11:53:46 +00:00
2016-12-30 11:36:20 +00:00
let url = window.location.host + window.location.pathname;
2016-12-30 11:53:46 +00:00
2016-12-30 11:36:20 +00:00
if (document.getElementById("editor")) {
url = removeLastDirectoryPartOf(url);
}
2016-12-30 11:15:10 +00:00
2016-12-30 11:32:54 +00:00
if (supported && user.AllowCommands) {
2016-12-30 11:36:20 +00:00
let conn = new WebSocket(`ws://${url}?command=true`);
2016-12-30 11:32:54 +00:00
2016-12-30 11:15:10 +00:00
conn.onopen = function() {
conn.send(value);
};
conn.onmessage = function(event) {
box.innerHTML = event.data;
scrollable.scrollTop = scrollable.scrollHeight;
}
conn.onclose = function(event) {
search.classList.remove('ongoing');
reloadListing();
}
2016-12-30 11:32:54 +00:00
return;
}
2016-12-30 11:15:10 +00:00
2016-12-30 11:32:54 +00:00
box.innerHTML = '<ul></ul>';
2016-12-30 11:15:10 +00:00
2016-12-30 11:32:54 +00:00
let ul = box.querySelector('ul'),
2016-12-30 11:36:20 +00:00
conn = new WebSocket(`ws://${url}?search=true`);
2016-12-30 11:32:54 +00:00
conn.onopen = function() {
conn.send(value);
};
conn.onmessage = function(event) {
ul.innerHTML += '<li><a href="' + event.data + '">' + event.data + '</a></li>';
scrollable.scrollTop = scrollable.scrollHeight;
}
conn.onclose = function(event) {
search.classList.remove('ongoing');
2016-12-30 11:15:10 +00:00
}
}
}
2016-12-30 11:32:54 +00:00
function setupSearch() {
let search = document.getElementById("search"),
searchInput = search.querySelector("input"),
searchDiv = search.querySelector("div"),
hover = false,
focus = false;
resetSearchText();
searchInput.addEventListener('focus', event => {
focus = true;
search.classList.add('active');
});
searchDiv.addEventListener('mouseover', event => {
hover = true;
search.classList.add('active');
});
searchInput.addEventListener('blur', event => {
focus = false;
if (hover) return;
search.classList.remove('active');
});
search.addEventListener('mouseleave', event => {
hover = false;
if (focus) return;
search.classList.remove('active');
});
search.addEventListener("click", event => {
search.classList.add("active");
search.querySelector("input").focus();
});
searchInput.addEventListener('keyup', searchEvent);
}
2016-12-31 21:04:47 +00:00
function closeHelp(event) {
event.preventDefault();
2017-01-01 22:40:12 +00:00
2016-12-31 19:13:35 +00:00
document.querySelector('.help').classList.remove('active');
document.querySelector('.overlay').classList.remove('active');
}
2016-12-31 21:04:47 +00:00
function openHelp(event) {
closePrompt(event);
2017-01-01 22:40:12 +00:00
2016-12-31 21:04:47 +00:00
document.querySelector('.help').classList.add('active');
document.querySelector('.overlay').classList.add('active');
}
window.addEventListener('keydown', (event) => {
2017-01-01 22:40:12 +00:00
if (event.keyCode == 27) {
2016-12-31 21:04:47 +00:00
if (document.querySelector('.help.active')) {
closeHelp(event);
}
}
2017-01-01 22:40:12 +00:00
2016-12-31 21:04:47 +00:00
if (event.keyCode == 46) {
deleteEvent(event);
}
2017-01-01 22:40:12 +00:00
2016-12-31 19:13:35 +00:00
if (event.keyCode == 112) {
event.preventDefault();
2016-12-31 21:04:47 +00:00
openHelp(event);
2016-12-31 19:13:35 +00:00
}
});
2016-12-30 11:15:10 +00:00
/* * * * * * * * * * * * * * * *
* *
* BOOTSTRAP *
* *
* * * * * * * * * * * * * * * */
document.addEventListener("DOMContentLoaded", function(event) {
2017-01-01 22:40:12 +00:00
overlay = document.querySelector('.overlay');
clickOverlay = document.querySelector('#click-overlay');
2017-01-01 23:38:11 +00:00
2016-12-30 11:15:10 +00:00
buttons.logout = document.getElementById("logout");
buttons.open = document.getElementById("open");
buttons.delete = document.getElementById("delete");
2017-01-01 22:40:12 +00:00
buttons.previous = document.getElementById("previous");
2016-12-30 11:15:10 +00:00
// Attach event listeners
buttons.logout.addEventListener("click", logoutEvent);
buttons.open.addEventListener("click", openEvent);
2016-12-31 15:29:36 +00:00
templates.question = document.querySelector('#question-template');
templates.info = document.querySelector('#info-template');
2016-12-30 11:15:10 +00:00
if (user.AllowEdit) {
buttons.delete.addEventListener("click", deleteEvent);
}
2016-12-31 15:29:36 +00:00
2017-01-01 23:38:11 +00:00
let dropdownButtons = document.querySelectorAll('.action[data-dropdown]')
Array.from(dropdownButtons).forEach(button => {
button.addEventListener("click", event => {
button.querySelector('ul').classList.toggle("active");
2017-01-01 22:40:12 +00:00
clickOverlay.classList.add('active');
2017-01-01 23:38:11 +00:00
clickOverlay.addEventListener('click', event => {
button.querySelector('ul').classList.remove("active");
2017-01-01 22:40:12 +00:00
clickOverlay.classList.remove('active');
})
2016-12-31 18:35:27 +00:00
});
2017-01-01 23:38:11 +00:00
});
2016-12-31 18:35:27 +00:00
2017-01-01 22:40:12 +00:00
overlay.addEventListener('click', event => {
2016-12-31 21:04:47 +00:00
if (document.querySelector('.help.active')) {
closeHelp(event);
return;
}
2017-01-01 22:40:12 +00:00
2016-12-31 18:35:27 +00:00
closePrompt(event);
})
2017-01-01 23:38:11 +00:00
2017-01-01 22:40:12 +00:00
let mainActions = document.getElementById('main-actions');
2017-01-01 23:38:11 +00:00
2017-01-01 22:40:12 +00:00
document.getElementById('more').addEventListener('click', event => {
event.preventDefault();
event.stopPropagation();
2017-01-01 23:38:11 +00:00
2017-01-01 22:40:12 +00:00
clickOverlay.classList.add('active');
mainActions.classList.add('active');
2017-01-01 23:38:11 +00:00
clickOverlay.addEventListener('click', event => {
2017-01-01 22:40:12 +00:00
mainActions.classList.remove('active');
clickOverlay.classList.remove('active');
})
2017-01-01 23:38:11 +00:00
})
2016-12-30 11:15:10 +00:00
2016-12-30 11:32:54 +00:00
setupSearch();
2016-12-30 11:15:10 +00:00
return false;
2016-12-30 11:53:46 +00:00
});