'use strict'; var tempID = "_fm_internal_temporary_id" var selectedItems = []; /* * * * * * * * * * * * * * * * * * * GENERAL FUNCTIONS * * * * * * * * * * * * * * * * * * */ // Removes an element, if exists, from an array Array.prototype.removeElement = function(element) { var i = this.indexOf(element); if (i != -1) { this.splice(i, 1); } } // Replaces an element inside an array by another Array.prototype.replaceElement = function(begin, end) { var i = this.indexOf(begin); if (i != -1) { this[i] = end; } } // 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() { let element = this; let originalText = element.innerHTML; element.style.opacity = 0; setTimeout(function() { element.innerHTML = 'autorenew'; 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; if (selectedItems.length == 0 && document.getElementById('listing')) { document.sendCostumEvent('changed-selected'); } } let secondStep = () => { this.style.opacity = 0; setTimeout(thirdStep, 200); } let firstStep = () => { if (error) { this.innerHTML = 'close'; } else { this.innerHTML = 'done'; } this.style.opacity = 1; setTimeout(secondStep, 1000); } setTimeout(firstStep, 200); return false; } var toWebDavURL = function(url) { url = url.replace(baseURL + "/", webdavURL + "/"); return window.location.origin + url } // Handles the open file button event var openEvent = function(event) { if (this.classList.contains('disabled')) { return false; } if (selectedItems.length) { window.open(selectedItems[0] + '?raw=true'); return false; } window.open(window.location + '?raw=true'); return false; } // Handles the delete button event var deleteEvent = function(event) { let single = false; if (!selectedItems.length) { selectedItems = [window.location.pathname]; single = true; } if (selectedItems.length) { Array.from(selectedItems).forEach(link => { let html = document.getElementById("delete").changeToLoading(); let request = new XMLHttpRequest(); request.open('DELETE', toWebDavURL(link)); request.send(); request.onreadystatechange = function() { if (request.readyState == 4) { if (request.status == 204) { if (single) { window.location.pathname = RemoveLastDirectoryPartOf(window.location.pathname); } else { document.getElementById(link).remove(); selectedItems.removeElement(link); } } document.getElementById('delete').changeToDone((request.status != 204), html); } } }); return false; } return false; } // Prevent Default event var preventDefault = function(event) { event.preventDefault(); } // Remove the last directory of an url var RemoveLastDirectoryPartOf = function(url) { var arr = url.split('/'); arr.pop(); return (arr.join('/')); } /* * * * * * * * * * * * * * * * * * * LISTING SPECIFIC FUNCTIONS * * * * * * * * * * * * * * * * * * */ var reloadListing = function(callback) { 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; // Handle date times let timeList = document.getElementsByTagName("time"); Array.from(timeList).forEach(localizeDatetime); // Add action to checkboxes let checkboxes = document.getElementsByClassName('checkbox'); Array.from(checkboxes).forEach(link => { link.addEventListener('click', itemClickEvent); }); if (typeof callback == 'function') { callback(); } } } } } // Rename file event var renameEvent = function(event) { if (this.classList.contains('disabled') || !selectedItems.length) { return false; } // This mustn't happen if (selectedItems.length > 1) { alert("Something went wrong. Please refresh the page."); location.refresh(); } let link = selectedItems[0]; 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 newLink = toWebDavURL(link).replace(name, newName) let html = document.getElementById('rename').changeToLoading(); let request = new XMLHttpRequest(); request.open('MOVE', toWebDavURL(link)); request.setRequestHeader('Destination', newLink); request.send(); request.onreadystatechange = function() { // TODO: redirect if it's moved to another folder if (request.readyState == 4) { if (request.status != 201 && request.status != 204) { span.innerHTML = name; } else { let newLink = encodeURI(link.replace(name, newName)); console.log(request.body) reloadListing(() => { let newLink = encodeURI(link.replace(name, newName)); selectedItems = [newLink]; document.getElementById(newLink).classList.add("selected") var event = new CustomEvent('changed-selected'); document.dispatchEvent(event); }); } document.getElementById('rename').changeToDone((request.status != 201 && request.status != 204), 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; } 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; } // Upload files var handleFiles = function(files, base) { let button = document.getElementById("upload"); let html = button.changeToLoading(); for (let i = 0; i < files.length; i++) { let request = new XMLHttpRequest(); request.open('PUT', toWebDavURL(window.location.pathname + base + files[i].name)); request.send(files[i]); request.onreadystatechange = function() { if (request.readyState == 4) { if (request.status == 201) { reloadListing(); } button.changeToDone((request.status != 201), html); } } } return false; } // Handles the back button event 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; } // Handles the click event var itemClickEvent = function(event) { var url = this.dataset.href; var el = document.getElementById(url); if (selectedItems.length != 0) event.preventDefault(); if (selectedItems.indexOf(url) == -1) { el.classList.add('selected'); selectedItems.push(url); } else { el.classList.remove('selected'); selectedItems.removeElement(url); } var event = new CustomEvent('changed-selected'); document.dispatchEvent(event); return false; } // Handles the datetimes present on the document 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(); } // Toggles the view mode 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; } // Handles the view mode change var handleViewType = function(viewList) { let listing = document.getElementById('listing'); let button = document.getElementById('view'); if (viewList == "true") { listing.classList.add('list'); button.innerHTML = 'view_module Switch view'; return false; } button.innerHTML = 'view_list Switch view'; listing.classList.remove('list'); return false; } var addNewDirEvents = function() { document.getElementById('new').addEventListener('click', event => { let newdir = document.getElementById('newdir'); newdir.classList.add('enabled'); newdir.focus(); }); document.getElementById('newdir').addEventListener('blur', event => { document.getElementById('newdir').classList.remove('enabled'); }); document.getElementById('newdir').addEventListener('keydown', newDirEvent); } // Handles the new directory event var newDirEvent = function(event) { // TODO: create new dir button and new file button if (event.keyCode == 27) { document.getElementById('newdir').classList.toggle('enabled'); setTimeout(() => { document.getElementById('newdir').value = ''; }, 200); } if (event.keyCode == 13) { event.preventDefault(); let button = document.getElementById('new'); let html = button.changeToLoading(); let request = new XMLHttpRequest(); let name = document.getElementById('newdir').value; request.open((name.endsWith("/") ? "MKCOL" : "PUT"), toWebDavURL(window.location.pathname + name)); request.send(); request.onreadystatechange = function() { if (request.readyState == 4) { button.changeToDone((request.status != 201), html); reloadListing(() => { addNewDirEvents(); }); } } } return false; } // Handles the event when there is change on selected elements 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"); } redefineDownloadURLs(); return false; } toolbar.classList.remove("enabled"); return false; }); var redefineDownloadURLs = function() { let files = ""; for (let i = 0; i < selectedItems.length; i++) { files += selectedItems[i].replace(window.location.pathname, "") + ","; } files = files.substring(0, files.length - 1); files = encodeURIComponent(files); let links = document.querySelectorAll("#download ul a"); Array.from(links).forEach(link => { link.href = "?download=" + link.dataset.format + "&files=" + files; }); } var searchEvent = function(event) { let value = this.value; let search = document.getElementById('search'); let scrollable = document.querySelector('#search > div'); let box = document.querySelector('#search > div div'); if (value.length == 0) { box.innerHTML = "Search or use one of your supported commands: " + user.Commands.join(", ") + "."; return; } let pieces = value.split(' '); let supported = false; user.Commands.forEach(function(cmd) { if (cmd == pieces[0]) { supported = true; } }); if (!supported) { box.innerHTML = "Press enter to search." } else { box.innerHTML = "Press enter to execute." } if (event.keyCode == 13) { box.innerHTML = ''; search.classList.add('ongoing'); if (supported) { var conn = new WebSocket('ws://' + window.location.host + window.location.pathname + '?command=true'); 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(); } } else { box.innerHTML = '