filebrowser/_embed/public/js/common.js

569 lines
14 KiB
JavaScript
Raw Normal View History

2016-12-30 11:15:10 +00:00
'use strict';
var tempID = "_fm_internal_temporary_id",
2017-01-03 19:19:29 +00:00
ssl = (location.protocol === 'https:'),
2017-01-03 17:35:13 +00:00
templates = {},
selectedItems = [],
overlay,
2017-01-03 18:13:08 +00:00
clickOverlay;
2016-12-30 11:15:10 +00:00
// Removes an element, if exists, from an array
2017-01-03 17:14:28 +00:00
Array.prototype.removeElement = function (element) {
2017-01-03 17:35:13 +00:00
var i = this.indexOf(element);
if(i != -1)
this.splice(i, 1);
2016-12-30 11:15:10 +00:00
}
// Replaces an element inside an array by another
2017-01-03 17:14:28 +00:00
Array.prototype.replaceElement = function (oldElement, newElement) {
2017-01-03 17:35:13 +00:00
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
2017-01-03 17:14:28 +00:00
Document.prototype.sendCostumEvent = function (text) {
2017-01-03 17:35:13 +00:00
this.dispatchEvent(new CustomEvent(text));
2016-12-30 11:15:10 +00:00
}
// Gets the content of a cookie
2017-01-03 17:14:28 +00:00
Document.prototype.getCookie = function (name) {
2017-01-03 17:35:13 +00:00
var re = new RegExp("(?:(?:^|.*;\\s*)" + name + "\\s*\\=\\s*([^;]*).*$)|^.*$");
return document.cookie.replace(re, "$1");
2016-12-30 11:15:10 +00:00
}
// Remove the last directory of an url
2017-01-03 17:14:28 +00:00
var removeLastDirectoryPartOf = function (url) {
2017-01-03 17:35:13 +00:00
var arr = url.split('/');
if(arr.pop() === "") {
arr.pop();
}
return(arr.join('/'));
2016-12-30 11:15:10 +00:00
}
2017-01-02 12:20:20 +00:00
function getCSSRule(rules) {
2017-01-03 17:35:13 +00:00
for(let i = 0; i < rules.length; i++) {
rules[i] = rules[i].toLowerCase();
}
2017-01-02 12:20:20 +00:00
2017-01-03 17:35:13 +00:00
var result = null,
find = Array.prototype.find;
2017-01-02 12:20:20 +00:00
2017-01-03 17:35:13 +00:00
find.call(document.styleSheets, styleSheet => {
result = find.call(styleSheet.cssRules, cssRule => {
let found = false;
2017-01-02 12:20:20 +00:00
2017-01-03 17:35:13 +00:00
if(cssRule instanceof CSSStyleRule) {
for(let i = 0; i < rules.length; i++) {
if(cssRule.selectorText.toLowerCase() == rules[i])
found = true;
}
}
2017-01-02 12:20:20 +00:00
2017-01-03 17:35:13 +00:00
return found;
});
2017-01-03 17:10:30 +00:00
2017-01-03 17:35:13 +00:00
return result != null;
});
2017-01-02 12:20:20 +00:00
2017-01-03 17:35:13 +00:00
return result;
2017-01-03 17:14:28 +00:00
}
2017-01-03 15:10:33 +00:00
2017-01-03 18:13:08 +00:00
/* * * * * * * * * * * * * * * *
* *
* BUTTONS *
* *
* * * * * * * * * * * * * * * */
var buttons = {
previousState: {},
};
buttons.setLoading = function (name) {
if(typeof this[name] === 'undefined') return;
let i = this[name].querySelector('i');
this.previousState[name] = i.innerHTML;
i.style.opacity = 0;
setTimeout(function () {
i.classList.add('spin');
i.innerHTML = 'autorenew';
i.style.opacity = 1;
}, 200);
}
// Changes an element to done animation
buttons.setDone = function (name, success = true) {
let i = this[name].querySelector('i');
i.style.opacity = 0;
let thirdStep = () => {
i.innerHTML = this.previousState[name];
i.style.opacity = null;
if(selectedItems.length == 0 && document.getElementById('listing'))
document.sendCostumEvent('changed-selected');
}
let secondStep = () => {
i.style.opacity = 0;
setTimeout(thirdStep, 200);
}
let firstStep = () => {
i.classList.remove('spin');
i.innerHTML = success ?
'done' :
'close';
i.style.opacity = 1;
setTimeout(secondStep, 1000);
}
setTimeout(firstStep, 200);
return false;
}
2017-01-03 15:10:33 +00:00
/* * * * * * * * * * * * * * * *
2017-01-03 17:14:28 +00:00
* *
* WEBDAV *
* *
* * * * * * * * * * * * * * * */
2017-01-03 18:13:08 +00:00
var webdav = {};
webdav.convertURL = function (url) {
return window.location.origin + url.replace(baseURL + "/", webdavURL + "/");
}
2017-01-03 15:10:33 +00:00
2017-01-03 17:14:28 +00:00
webdav.move = function (oldLink, newLink) {
2017-01-03 17:35:13 +00:00
return new Promise((resolve, reject) => {
let request = new XMLHttpRequest();
2017-01-04 17:58:14 +00:00
2017-01-03 18:13:08 +00:00
request.open('MOVE', webdav.convertURL(oldLink), true);
request.setRequestHeader('Destination', webdav.convertURL(newLink));
2017-01-03 17:35:13 +00:00
request.onload = () => {
if(request.status == 201 || request.status == 204) {
2017-01-03 18:13:08 +00:00
resolve();
2017-01-03 17:35:13 +00:00
} else {
reject(request.statusText);
}
}
request.onerror = () => reject(request.statusText);
request.send();
});
2017-01-03 15:10:33 +00:00
}
2017-01-03 18:13:08 +00:00
webdav.put = function (link, body, headers = {}) {
2017-01-03 17:35:13 +00:00
return new Promise((resolve, reject) => {
let request = new XMLHttpRequest();
2017-01-03 18:13:08 +00:00
request.open('PUT', webdav.convertURL(link), true);
for(let key in headers) {
request.setRequestHeader(key, headers[key]);
}
2017-01-03 17:35:13 +00:00
request.onload = () => {
if(request.status == 201) {
2017-01-03 18:13:08 +00:00
resolve();
2017-01-03 17:35:13 +00:00
} else {
reject(request.statusText);
}
}
request.onerror = () => reject(request.statusText);
request.send(body);
});
2017-01-03 17:06:04 +00:00
}
2017-01-03 18:13:08 +00:00
webdav.delete = function (link) {
return new Promise((resolve, reject) => {
let request = new XMLHttpRequest();
request.open('DELETE', webdav.convertURL(link), true);
request.onload = () => {
if(request.status == 204) {
resolve();
} else {
reject(request.statusText);
}
}
request.onerror = () => reject(request.statusText);
request.send();
});
request.send();
}
webdav.new = function (link) {
return new Promise((resolve, reject) => {
let request = new XMLHttpRequest();
request.open((link.endsWith("/") ? "MKCOL" : "PUT"), webdav.convertURL(link), true);
request.onload = () => {
if(request.status == 201) {
resolve();
} else {
reject(request.statusText);
}
}
request.onerror = () => reject(request.statusText);
request.send();
});
request.send();
}
2016-12-30 11:15:10 +00:00
/* * * * * * * * * * * * * * * *
2017-01-03 17:14:28 +00:00
* *
* EVENTS *
* *
* * * * * * * * * * * * * * * */
2016-12-31 15:29:36 +00:00
function closePrompt(event) {
2017-01-03 17:35:13 +00:00
let prompt = document.querySelector('.prompt');
2017-01-01 22:40:12 +00:00
2017-01-03 17:35:13 +00:00
if(!prompt)
return;
2016-12-31 15:29:36 +00:00
2017-01-03 17:35:13 +00:00
event.preventDefault();
document.querySelector('.overlay').classList.remove('active');
prompt.classList.remove('active');
2016-12-31 15:29:36 +00:00
2017-01-03 17:35:13 +00:00
setTimeout(() => {
prompt.remove();
}, 100);
2016-12-31 15:29:36 +00:00
}
function notImplemented(event) {
2017-01-03 17:35:13 +00:00
event.preventDefault();
clickOverlay.click();
2016-12-31 15:29:36 +00:00
2017-01-03 17:35:13 +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.";
2016-12-31 15:29:36 +00:00
2017-01-03 17:35:13 +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
}
2016-12-30 11:15:10 +00:00
// Prevent Default event
2017-01-03 17:14:28 +00:00
var preventDefault = function (event) {
2017-01-03 17:35:13 +00:00
event.preventDefault();
2016-12-30 11:15:10 +00:00
}
function logoutEvent(event) {
2017-01-03 17:35:13 +00:00
let request = new XMLHttpRequest();
request.open('GET', window.location.pathname, true, "username", "password");
request.send();
request.onreadystatechange = function () {
if(request.readyState == 4) {
window.location = "/";
}
2017-01-03 17:10:30 +00:00
}
2016-12-30 11:15:10 +00:00
}
function openEvent(event) {
2017-01-03 17:35:13 +00:00
if(event.currentTarget.classList.contains('disabled'))
return false;
2016-12-30 11:15:10 +00:00
2017-01-03 17:35:13 +00:00
let link = '?raw=true';
2016-12-30 11:15:10 +00:00
2017-01-03 17:35:13 +00:00
if(selectedItems.length) {
link = document.getElementById(selectedItems[0]).dataset.url + link;
} else {
link = window.location + link;
}
2016-12-30 11:15:10 +00:00
2017-01-03 17:35:13 +00:00
window.open(link);
return false;
2016-12-30 11:15:10 +00:00
}
2017-01-03 18:13:08 +00:00
function deleteOnSingleFile() {
closePrompt(event);
buttons.setLoading('delete');
2017-01-03 17:14:28 +00:00
2017-01-03 18:13:08 +00:00
let promises = [];
2016-12-31 15:29:36 +00:00
2017-01-03 18:13:08 +00:00
webdav.delete(window.location.pathname)
.then(() => {
window.location.pathname = removeLastDirectoryPartOf(window.location.pathname);
})
.catch(e => {
buttons.setDone('delete', false);
console.log(e)
})
}
2017-01-03 17:35:13 +00:00
2017-01-03 18:13:08 +00:00
function deleteOnListing() {
closePrompt(event);
buttons.setLoading('delete');
2016-12-31 15:29:36 +00:00
2017-01-03 18:13:08 +00:00
let promises = [];
2016-12-31 15:29:36 +00:00
2017-01-03 18:13:08 +00:00
for(let id of selectedItems) {
promises.push(webdav.delete(document.getElementById(id).dataset.url));
2017-01-03 17:35:13 +00:00
}
2017-01-03 18:13:08 +00:00
Promise.all(promises)
.then(() => {
listing.reload();
buttons.setDone('delete');
})
.catch(e => {
console.log(e);
buttons.setDone('delete', false);
})
2016-12-31 15:29:36 +00:00
}
2016-12-30 11:15:10 +00:00
// Handles the delete button event
function deleteEvent(event) {
2017-01-03 17:35:13 +00:00
let single = false;
2016-12-30 11:15:10 +00:00
2017-01-03 17:35:13 +00:00
if(!selectedItems.length) {
selectedItems = ["placeholder"];
single = true;
}
2016-12-30 11:15:10 +00:00
2017-01-03 17:35:13 +00:00
let clone = document.importNode(templates.question.content, true);
clone.querySelector('h3').innerHTML = 'Delete files';
2017-01-01 22:40:12 +00:00
2017-01-03 17:35:13 +00:00
if(single) {
2017-01-03 18:13:08 +00:00
clone.querySelector('form').addEventListener('submit', deleteOnSingleFile);
2017-01-03 17:35:13 +00:00
clone.querySelector('p').innerHTML = `Are you sure you want to delete this file/folder?`;
} else {
2017-01-03 18:13:08 +00:00
clone.querySelector('form').addEventListener('submit', deleteOnListing);
2017-01-03 17:35:13 +00:00
clone.querySelector('p').innerHTML = `Are you sure you want to delete ${selectedItems.length} file(s)?`;
}
2017-01-01 22:40:12 +00:00
2017-01-03 17:35:13 +00:00
clone.querySelector('input').remove();
clone.querySelector('.ok').innerHTML = 'Delete';
2016-12-30 11:53:46 +00:00
2017-01-03 17:35:13 +00:00
document.body.appendChild(clone);
document.querySelector('.overlay').classList.add('active');
document.querySelector('.prompt').classList.add('active');
2016-12-30 11:15:10 +00:00
2017-01-03 17:35:13 +00:00
return false;
2016-12-30 11:15:10 +00:00
}
2016-12-30 11:32:54 +00:00
function resetSearchText() {
2017-01-03 17:35:13 +00:00
let box = document.querySelector('#search > div div');
2016-12-30 11:32:54 +00:00
2017-01-03 17:35:13 +00:00
if(user.AllowCommands) {
box.innerHTML = `Search or use one of your supported commands: ${user.Commands.join(", ")}.`;
} else {
box.innerHTML = "Type and press enter to search.";
}
2016-12-30 11:32:54 +00:00
}
2016-12-30 11:15:10 +00:00
2016-12-30 11:32:54 +00:00
function searchEvent(event) {
2017-01-03 17:35:13 +00:00
if(this.value.length == 0) {
resetSearchText();
return;
2017-01-03 17:10:30 +00:00
}
2016-12-30 11:15:10 +00:00
2017-01-03 17:35:13 +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
2017-01-03 17:35:13 +00:00
user.Commands.forEach(function (cmd) {
if(cmd == pieces[0]) {
supported = true;
}
});
2016-12-30 11:15:10 +00:00
2017-01-03 17:35:13 +00:00
if(!supported || !user.AllowCommands) {
box.innerHTML = "Press enter to search."
} else {
box.innerHTML = "Press enter to execute."
2017-01-03 17:14:28 +00:00
}
2016-12-30 11:32:54 +00:00
2017-01-03 17:35:13 +00:00
if(event.keyCode == 13) {
box.innerHTML = '';
search.classList.add('ongoing');
2016-12-30 11:15:10 +00:00
2017-01-03 17:35:13 +00:00
let url = window.location.host + window.location.pathname;
2016-12-30 11:15:10 +00:00
2017-01-03 17:35:13 +00:00
if(document.getElementById("editor")) {
url = removeLastDirectoryPartOf(url);
}
2016-12-30 11:15:10 +00:00
2017-01-03 22:33:59 +00:00
let protocol = ssl ? 'wss:' : 'ws:';
2017-01-03 19:19:29 +00:00
2017-01-03 17:35:13 +00:00
if(supported && user.AllowCommands) {
2017-01-03 19:19:29 +00:00
let conn = new WebSocket(`${protocol}//${url}?command=true`);
2016-12-30 11:15:10 +00:00
2017-01-03 17:35:13 +00:00
conn.onopen = function () {
conn.send(value);
};
2016-12-30 11:15:10 +00:00
2017-01-03 17:35:13 +00:00
conn.onmessage = function (event) {
box.innerHTML = event.data;
scrollable.scrollTop = scrollable.scrollHeight;
}
2016-12-30 11:32:54 +00:00
2017-01-03 17:35:13 +00:00
conn.onclose = function (event) {
search.classList.remove('ongoing');
2017-01-03 19:19:29 +00:00
listing.reload();
2017-01-03 17:35:13 +00:00
}
2016-12-30 11:32:54 +00:00
2017-01-03 17:35:13 +00:00
return;
}
2016-12-30 11:32:54 +00:00
2017-01-03 17:35:13 +00:00
box.innerHTML = '<ul></ul>';
2016-12-30 11:15:10 +00:00
2017-01-03 17:35:13 +00:00
let ul = box.querySelector('ul'),
2017-01-03 19:19:29 +00:00
conn = new WebSocket(`${protocol}//${url}?search=true`);
2017-01-03 17:35:13 +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');
}
2017-01-03 17:14:28 +00:00
}
}
2017-01-03 15:10:33 +00:00
2016-12-30 11:32:54 +00:00
function setupSearch() {
2017-01-03 17:35:13 +00:00
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-30 11:32:54 +00:00
}
2016-12-31 21:04:47 +00:00
function closeHelp(event) {
2017-01-03 17:35:13 +00:00
event.preventDefault();
2017-01-01 22:40:12 +00:00
2017-01-03 17:35:13 +00:00
document.querySelector('.help').classList.remove('active');
document.querySelector('.overlay').classList.remove('active');
2016-12-31 19:13:35 +00:00
}
2016-12-31 21:04:47 +00:00
function openHelp(event) {
2017-01-03 17:35:13 +00:00
closePrompt(event);
2017-01-01 22:40:12 +00:00
2017-01-03 17:35:13 +00:00
document.querySelector('.help').classList.add('active');
document.querySelector('.overlay').classList.add('active');
2016-12-31 21:04:47 +00:00
}
window.addEventListener('keydown', (event) => {
2017-01-03 17:35:13 +00:00
if(event.keyCode == 27) {
if(document.querySelector('.help.active')) {
closeHelp(event);
}
2017-01-03 17:10:30 +00:00
}
2017-01-01 22:40:12 +00:00
2017-01-03 17:35:13 +00:00
if(event.keyCode == 46) {
deleteEvent(event);
}
2017-01-01 22:40:12 +00:00
2017-01-03 17:35:13 +00:00
if(event.keyCode == 112) {
event.preventDefault();
openHelp(event);
}
2016-12-31 19:13:35 +00:00
});
2016-12-30 11:15:10 +00:00
/* * * * * * * * * * * * * * * *
2017-01-03 17:14:28 +00:00
* *
* BOOTSTRAP *
* *
* * * * * * * * * * * * * * * */
document.addEventListener("DOMContentLoaded", function (event) {
2017-01-03 17:35:13 +00:00
overlay = document.querySelector('.overlay');
clickOverlay = document.querySelector('#click-overlay');
buttons.logout = document.getElementById("logout");
buttons.open = document.getElementById("open");
buttons.delete = document.getElementById("delete");
buttons.previous = document.getElementById("previous");
// Attach event listeners
buttons.logout.addEventListener("click", logoutEvent);
buttons.open.addEventListener("click", openEvent);
templates.question = document.querySelector('#question-template');
templates.info = document.querySelector('#info-template');
templates.move = document.querySelector("#move-template");
if(user.AllowEdit) {
buttons.delete.addEventListener("click", deleteEvent);
}
let dropdownButtons = document.querySelectorAll('.action[data-dropdown]')
Array.from(dropdownButtons).forEach(button => {
button.addEventListener("click", event => {
button.querySelector('ul').classList.toggle("active");
clickOverlay.classList.add('active');
clickOverlay.addEventListener('click', event => {
button.querySelector('ul').classList.remove("active");
clickOverlay.classList.remove('active');
})
});
2017-01-03 17:10:30 +00:00
});
2016-12-31 18:35:27 +00:00
2017-01-03 17:35:13 +00:00
overlay.addEventListener('click', event => {
if(document.querySelector('.help.active')) {
closeHelp(event);
return;
}
2017-01-01 22:40:12 +00:00
2017-01-03 17:35:13 +00:00
closePrompt(event);
})
2017-01-01 23:38:11 +00:00
2017-01-03 17:35:13 +00:00
let mainActions = document.getElementById('main-actions');
2017-01-01 23:38:11 +00:00
2017-01-03 17:35:13 +00:00
document.getElementById('more').addEventListener('click', event => {
event.preventDefault();
event.stopPropagation();
2017-01-01 23:38:11 +00:00
2017-01-03 17:35:13 +00:00
clickOverlay.classList.add('active');
mainActions.classList.add('active');
2017-01-01 23:38:11 +00:00
2017-01-03 17:35:13 +00:00
clickOverlay.addEventListener('click', event => {
mainActions.classList.remove('active');
clickOverlay.classList.remove('active');
})
2017-01-03 17:10:30 +00:00
})
2016-12-30 11:15:10 +00:00
2017-01-03 17:35:13 +00:00
setupSearch();
return false;
2017-01-03 18:13:08 +00:00
});