From ce9405341a3e08d92ce75d73ddce117661632a0f Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 3 Jan 2017 17:10:30 +0000 Subject: [PATCH] travis --- .travis.yml | 4 +- _embed/public/js/common.js | 868 ++++++++++++++++++------------------ _embed/public/js/editor.js | 402 ++++++++--------- _embed/public/js/listing.js | 64 +-- 4 files changed, 669 insertions(+), 669 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3744e57a..863e0567 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,6 @@ go: - tip install: - - go generate - go get -u -v $(go list -f '{{join .Imports "\n"}}' ./... | sort | uniq | grep -v caddy-filemanager) - go get -u -v github.com/mholt/caddy/caddyhttp - go get -u -v github.com/caddyserver/caddydev @@ -12,7 +11,8 @@ install: - go get github.com/gordonklaus/ineffassign script: + - diff <(echo -n) <(gofmt -s -d .) + - go generate - sed -i 's/\_ \"github.com\/mholt\/caddy\/caddyhttp\"/\_ \"github.com\/mholt\/caddy\/caddyhttp\"\n\_ \"github.com\/hacdias\/caddy-filemanager\"/g' $GOPATH/src/github.com/mholt/caddy/caddy/caddymain/run.go - go build github.com/mholt/caddy/caddy - - diff <(echo -n) <(gofmt -s -d .) - ineffassign . diff --git a/_embed/public/js/common.js b/_embed/public/js/common.js index ae550b94..57e32b0e 100644 --- a/_embed/public/js/common.js +++ b/_embed/public/js/common.js @@ -1,632 +1,632 @@ 'use strict'; var tempID = "_fm_internal_temporary_id", - buttons = {}, - templates = {}, - selectedItems = [], - overlay, clickOverlay, - webdav = {}; + buttons = {}, + templates = {}, + selectedItems = [], + overlay, clickOverlay, + webdav = {}; // 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); + var i = this.indexOf(element); + if (i != -1) this.splice(i, 1); } // Replaces an element inside an array by another Array.prototype.replaceElement = function(oldElement, newElement) { - var i = this.indexOf(oldElement); - if (i != -1) this[i] = newElement; + var i = this.indexOf(oldElement); + if (i != -1) this[i] = newElement; } // Sends a costum event to itself Document.prototype.sendCostumEvent = function(text) { - this.dispatchEvent(new CustomEvent(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"); + 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, - originalText = element.innerHTML; + let element = this, + originalText = element.innerHTML; - element.style.opacity = 0; + element.style.opacity = 0; - setTimeout(function() { - element.classList.add('spin'); - element.innerHTML = 'autorenew'; - element.style.opacity = 1; - }, 200); + setTimeout(function() { + element.classList.add('spin'); + element.innerHTML = 'autorenew'; + element.style.opacity = 1; + }, 200); - return originalText; + 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 thirdStep = () => { - this.innerHTML = html; - this.style.opacity = null; + let firstStep = () => { + this.classList.remove('spin'); + this.innerHTML = error ? 'close' : 'done'; + this.style.opacity = 1; + setTimeout(secondStep, 1000); + } - if (selectedItems.length == 0 && document.getElementById('listing')) document.sendCostumEvent('changed-selected'); - } - - let secondStep = () => { - this.style.opacity = 0; - setTimeout(thirdStep, 200); - } - - let firstStep = () => { - this.classList.remove('spin'); - this.innerHTML = error ? 'close' : 'done'; - this.style.opacity = 1; - setTimeout(secondStep, 1000); - } - - setTimeout(firstStep, 200); - return false; + setTimeout(firstStep, 200); + return false; } function toWebDavURL(url) { - return window.location.origin + url.replace(baseURL + "/", webdavURL + "/"); + return window.location.origin + url.replace(baseURL + "/", webdavURL + "/"); } // Remove the last directory of an url var removeLastDirectoryPartOf = function(url) { - var arr = url.split('/'); - if (arr.pop() === "") { - arr.pop(); - } - return (arr.join('/')); + var arr = url.split('/'); + if (arr.pop() === "") { + arr.pop(); + } + return (arr.join('/')); } function getCSSRule(rules) { - for (let i = 0; i < rules.length; i++) { - rules[i] = rules[i].toLowerCase(); - } + for (let i = 0; i < rules.length; i++) { + rules[i] = rules[i].toLowerCase(); + } - var result = null, - find = Array.prototype.find; + var result = null, + find = Array.prototype.find; - find.call(document.styleSheets, styleSheet => { - result = find.call(styleSheet.cssRules, cssRule => { - let found = false; + 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; - } - } + 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 found; }); - return result; + return result != null; + }); + + return result; } /* * * * * * * * * * * * * * * * - * * - * WEBDAV * - * * - * * * * * * * * * * * * * * * */ -// TODO: here, we should create an abstraction layer from the webdav. +* * +* WEBDAV * +* * +* * * * * * * * * * * * * * * */ +// TODO: here, we should create an abstraction layer from the webdav. // We must create functions that do the requests to the webdav backend. webdav.move = function(oldLink, newLink) { - return new Promise((resolve, reject) => { - let request = new XMLHttpRequest(); - request.open('MOVE', toWebDavURL(oldLink), true); - request.setRequestHeader('Destination', toWebDavURL(newLink)); - request.onload = () => { - if (request.status == 201 || request.status == 204) { - resolve(request.response); - } else { - reject(request.statusText); - } - } - request.onerror = () => reject(request.statusText); - request.send(); - }); + return new Promise((resolve, reject) => { + let request = new XMLHttpRequest(); + request.open('MOVE', toWebDavURL(oldLink), true); + request.setRequestHeader('Destination', toWebDavURL(newLink)); + request.onload = () => { + if (request.status == 201 || request.status == 204) { + resolve(request.response); + } else { + reject(request.statusText); + } + } + request.onerror = () => reject(request.statusText); + request.send(); + }); } webdav.put = function(link, body) { - return new Promise((resolve, reject) => { - let request = new XMLHttpRequest(); - request.open('PUT', toWebDavURL(link), true); - request.onload = () => { - if (request.status == 201) { - resolve(request.response); - } else { - reject(request.statusText); - } - } - request.onerror = () => reject(request.statusText); - request.send(body); - }); + return new Promise((resolve, reject) => { + let request = new XMLHttpRequest(); + request.open('PUT', toWebDavURL(link), true); + request.onload = () => { + if (request.status == 201) { + resolve(request.response); + } else { + reject(request.statusText); + } + } + request.onerror = () => reject(request.statusText); + request.send(body); + }); } /* * * * * * * * * * * * * * * * - * * - * EVENTS * - * * - * * * * * * * * * * * * * * * */ +* * +* EVENTS * +* * +* * * * * * * * * * * * * * * */ function closePrompt(event) { - let prompt = document.querySelector('.prompt'); + let prompt = document.querySelector('.prompt'); - if (!prompt) return; + if (!prompt) return; - event.preventDefault(); - document.querySelector('.overlay').classList.remove('active'); - prompt.classList.remove('active'); + event.preventDefault(); + document.querySelector('.overlay').classList.remove('active'); + prompt.classList.remove('active'); - setTimeout(() => { - prompt.remove(); - }, 100); + setTimeout(() => { + prompt.remove(); + }, 100); } function notImplemented(event) { - event.preventDefault(); - clickOverlay.click(); + event.preventDefault(); + clickOverlay.click(); - 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."; + 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'); + document.querySelector('body').appendChild(clone) + document.querySelector('.overlay').classList.add('active'); + document.querySelector('.prompt').classList.add('active'); } // Prevent Default event var preventDefault = function(event) { - event.preventDefault(); + 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 = "/"; - } + 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; + if (event.currentTarget.classList.contains('disabled')) return false; - let link = '?raw=true'; + let link = '?raw=true'; - if (selectedItems.length) { - link = document.getElementById(selectedItems[0]).dataset.url + link; - } else { - link = window.location + link; - } + if (selectedItems.length) { + link = document.getElementById(selectedItems[0]).dataset.url + link; + } else { + link = window.location + link; + } - window.open(link); - return false; + window.open(link); + return false; } function selectMoveFolder(event) { - if (event.target.getAttribute("aria-selected") === "true") { - event.target.setAttribute("aria-selected", false); - return; - } else { - if (document.querySelector(".file-list li[aria-selected=true]")) { - document.querySelector(".file-list li[aria-selected=true]").setAttribute("aria-selected", false); - } - event.target.setAttribute("aria-selected", true); - return; + if (event.target.getAttribute("aria-selected") === "true") { + event.target.setAttribute("aria-selected", false); + return; + } else { + if (document.querySelector(".file-list li[aria-selected=true]")) { + document.querySelector(".file-list li[aria-selected=true]").setAttribute("aria-selected", false); } + event.target.setAttribute("aria-selected", true); + return; + } } function loadNextFolder(event) { - let request = new XMLHttpRequest(), - prompt = document.querySelector("form.prompt.active"); + let request = new XMLHttpRequest(), + prompt = document.querySelector("form.prompt.active"); - prompt.addEventListener("submit", moveSelected); + prompt.addEventListener("submit", moveSelected); - request.open("GET", event.target.dataset.url); - request.setRequestHeader("Accept", "application/json"); - request.send(); - request.onreadystatechange = function() { - if (request.readyState == 4 && request.status == 200) { - let dirs = 0; + request.open("GET", event.target.dataset.url); + request.setRequestHeader("Accept", "application/json"); + request.send(); + request.onreadystatechange = function() { + if (request.readyState == 4 && request.status == 200) { + let dirs = 0; - prompt.querySelector("ul").innerHTML = ""; - prompt.querySelector('code').innerHTML = event.target.dataset.url; + prompt.querySelector("ul").innerHTML = ""; + prompt.querySelector('code').innerHTML = event.target.dataset.url; - for (let f of JSON.parse(request.response)) { - if (f.IsDir === true) { - dirs++; + for (let f of JSON.parse(request.response)) { + if (f.IsDir === true) { + dirs++; - let newNode = document.createElement("li"); - newNode.dataset.url = f.URL; - newNode.innerHTML = f.Name; - newNode.setAttribute("aria-selected", false); + let newNode = document.createElement("li"); + newNode.dataset.url = f.URL; + newNode.innerHTML = f.Name; + newNode.setAttribute("aria-selected", false); - newNode.addEventListener("dblclick", loadNextFolder); - newNode.addEventListener("click", selectMoveFolder); + newNode.addEventListener("dblclick", loadNextFolder); + newNode.addEventListener("click", selectMoveFolder); - prompt.querySelector("div.file-list ul").appendChild(newNode); - } - } - - if (dirs === 0) { - prompt.querySelector("p").innerHTML = `There aren't any folders in this directory.`; - } + prompt.querySelector("div.file-list ul").appendChild(newNode); } + } + + if (dirs === 0) { + prompt.querySelector("p").innerHTML = `There aren't any folders in this directory.`; + } } + } } function moveSelected(event) { - event.preventDefault(); + event.preventDefault(); - // TODO: this only works for ONE file. What if there are more files selected? - // TODO: use webdav.rename + // TODO: this only works for ONE file. What if there are more files selected? + // TODO: use webdav.rename - let request = new XMLHttpRequest(), - oldLink = toWebDavURL(window.location.pathname), - newLink = toWebDavURL(event.srcElement.querySelector("li[aria-selected=true]").innerHTML + "/"); + let request = new XMLHttpRequest(), + oldLink = toWebDavURL(window.location.pathname), + newLink = toWebDavURL(event.srcElement.querySelector("li[aria-selected=true]").innerHTML + "/"); - request.open("MOVE", oldLink); - request.setRequestHeader("Destination", newLink); - request.send(); - request.onreadystatechange = function() { - if (request.readyState == 4) { - if (request.status == 200 || request.status == 204) { - window.reload(); - } - } + request.open("MOVE", oldLink); + request.setRequestHeader("Destination", newLink); + request.send(); + request.onreadystatechange = function() { + if (request.readyState == 4) { + if (request.status == 200 || request.status == 204) { + window.reload(); + } } + } } function moveEvent(event) { - if (event.currentTarget.classList.contains("disabled")) return; + if (event.currentTarget.classList.contains("disabled")) return; - let request = new XMLHttpRequest(); - request.open("GET", window.location.pathname, true); - request.setRequestHeader("Accept", "application/json"); - request.send(); - request.onreadystatechange = function() { - if (request.readyState == 4) { - if (request.status == 200) { - let prompt = document.importNode(templates.move.content, true), - dirs = 0; + let request = new XMLHttpRequest(); + request.open("GET", window.location.pathname, true); + request.setRequestHeader("Accept", "application/json"); + request.send(); + request.onreadystatechange = function() { + if (request.readyState == 4) { + if (request.status == 200) { + let prompt = document.importNode(templates.move.content, true), + dirs = 0; - prompt.querySelector("form").addEventListener("submit", moveSelected); - prompt.querySelector('code').innerHTML = window.location.pathname; + prompt.querySelector("form").addEventListener("submit", moveSelected); + prompt.querySelector('code').innerHTML = window.location.pathname; - for (let f of JSON.parse(request.response)) { - if (f.IsDir === true) { - dirs++; + for (let f of JSON.parse(request.response)) { + if (f.IsDir === true) { + dirs++; - let newNode = document.createElement("li"); - newNode.dataset.url = f.URL; - newNode.innerHTML = f.Name; - newNode.setAttribute("aria-selected", false); + let newNode = document.createElement("li"); + newNode.dataset.url = f.URL; + newNode.innerHTML = f.Name; + newNode.setAttribute("aria-selected", false); - newNode.addEventListener("dblclick", loadNextFolder); - newNode.addEventListener("click", selectMoveFolder); + newNode.addEventListener("dblclick", loadNextFolder); + newNode.addEventListener("click", selectMoveFolder); - prompt.querySelector("div.file-list ul").appendChild(newNode); - } - } - - if (dirs === 0) { - prompt.querySelector("p").innerHTML = `There aren't any folders in this directory.`; - } - - document.body.appendChild(prompt); - document.querySelector(".overlay").classList.add("active"); - document.querySelector(".prompt").classList.add("active"); - } + prompt.querySelector("div.file-list ul").appendChild(newNode); + } } + + if (dirs === 0) { + prompt.querySelector("p").innerHTML = `There aren't any folders in this directory.`; + } + + document.body.appendChild(prompt); + document.querySelector(".overlay").classList.add("active"); + document.querySelector(".prompt").classList.add("active"); + } } + } } function deleteSelected(single) { - return function(event) { - event.preventDefault(); + return function(event) { + event.preventDefault(); - Array.from(selectedItems).forEach(id => { - let request = new XMLHttpRequest(), - html = buttons.delete.querySelector('i').changeToLoading(), - el, url; + Array.from(selectedItems).forEach(id => { + let request = new XMLHttpRequest(), + html = buttons.delete.querySelector('i').changeToLoading(), + 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) { - url = window.location.pathname; + window.location.pathname = removeLastDirectoryPartOf(window.location.pathname); } else { - el = document.getElementById(id); - url = el.dataset.url; + el.remove(); + selectedItems.removeElement(id); } + } - 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); - } - } + buttons.delete.querySelector('i').changeToDone(request.status != 204, html); + } + } - buttons.delete.querySelector('i').changeToDone(request.status != 204, html); - } - } + request.send(); + }); - request.send(); - }); - - closePrompt(event); - } + closePrompt(event); + } } // Handles the delete button event function deleteEvent(event) { - let single = false; + let single = false; - if (!selectedItems.length) { - selectedItems = ["placeholder"]; - single = true; - } + if (!selectedItems.length) { + selectedItems = ["placeholder"]; + single = true; + } - let clone = document.importNode(templates.question.content, true); - clone.querySelector('h3').innerHTML = 'Delete files'; + let clone = document.importNode(templates.question.content, true); + clone.querySelector('h3').innerHTML = 'Delete files'; - if (single) { - clone.querySelector('p').innerHTML = `Are you sure you want to delete this file/folder?`; - } else { - clone.querySelector('p').innerHTML = `Are you sure you want to delete ${selectedItems.length} file(s)?`; - } + if (single) { + clone.querySelector('p').innerHTML = `Are you sure you want to delete this file/folder?`; + } else { + clone.querySelector('p').innerHTML = `Are you sure you want to delete ${selectedItems.length} file(s)?`; + } - clone.querySelector('input').remove(); - clone.querySelector('.ok').innerHTML = 'Delete'; - clone.querySelector('form').addEventListener('submit', deleteSelected(single)); + clone.querySelector('input').remove(); + clone.querySelector('.ok').innerHTML = 'Delete'; + clone.querySelector('form').addEventListener('submit', deleteSelected(single)); - document.body.appendChild(clone); - document.querySelector('.overlay').classList.add('active'); - document.querySelector('.prompt').classList.add('active'); + document.body.appendChild(clone); + document.querySelector('.overlay').classList.add('active'); + document.querySelector('.prompt').classList.add('active'); - return false; + return false; } function resetSearchText() { - let box = document.querySelector('#search > div div'); + let box = document.querySelector('#search > div div'); - 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."; - } + 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."; + } } function searchEvent(event) { - if (this.value.length == 0) { - resetSearchText(); - return; + if (this.value.length == 0) { + resetSearchText(); + return; + } + + let value = this.value, + search = document.getElementById('search'), + scrollable = document.querySelector('#search > div'), + box = document.querySelector('#search > div div'), + pieces = value.split(' '), + supported = false; + + user.Commands.forEach(function(cmd) { + if (cmd == pieces[0]) { + supported = true; + } + }); + + if (!supported || !user.AllowCommands) { + box.innerHTML = "Press enter to search." + } else { + box.innerHTML = "Press enter to execute." + } + + if (event.keyCode == 13) { + box.innerHTML = ''; + search.classList.add('ongoing'); + + let url = window.location.host + window.location.pathname; + + if (document.getElementById("editor")) { + url = removeLastDirectoryPartOf(url); } - let value = this.value, - search = document.getElementById('search'), - scrollable = document.querySelector('#search > div'), - box = document.querySelector('#search > div div'), - pieces = value.split(' '), - supported = false; + if (supported && user.AllowCommands) { + let conn = new WebSocket(`ws://${url}?command=true`); - user.Commands.forEach(function(cmd) { - if (cmd == pieces[0]) { - supported = true; - } - }); + conn.onopen = function() { + conn.send(value); + }; - if (!supported || !user.AllowCommands) { - box.innerHTML = "Press enter to search." - } else { - box.innerHTML = "Press enter to execute." + conn.onmessage = function(event) { + box.innerHTML = event.data; + scrollable.scrollTop = scrollable.scrollHeight; + } + + conn.onclose = function(event) { + search.classList.remove('ongoing'); + reloadListing(); + } + + return; } - if (event.keyCode == 13) { - box.innerHTML = ''; - search.classList.add('ongoing'); + box.innerHTML = ''; - let url = window.location.host + window.location.pathname; + let ul = box.querySelector('ul'), + conn = new WebSocket(`ws://${url}?search=true`); - if (document.getElementById("editor")) { - url = removeLastDirectoryPartOf(url); - } + conn.onopen = function() { + conn.send(value); + }; - if (supported && user.AllowCommands) { - let conn = new WebSocket(`ws://${url}?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(); - } - - return; - } - - box.innerHTML = ''; - - let ul = box.querySelector('ul'), - conn = new WebSocket(`ws://${url}?search=true`); - - conn.onopen = function() { - conn.send(value); - }; - - conn.onmessage = function(event) { - ul.innerHTML += '
  • ' + event.data + '
  • '; - scrollable.scrollTop = scrollable.scrollHeight; - } - - conn.onclose = function(event) { - search.classList.remove('ongoing'); - } + conn.onmessage = function(event) { + ul.innerHTML += '
  • ' + event.data + '
  • '; + scrollable.scrollTop = scrollable.scrollHeight; } + + conn.onclose = function(event) { + search.classList.remove('ongoing'); + } + } } function setupSearch() { - let search = document.getElementById("search"), - searchInput = search.querySelector("input"), - searchDiv = search.querySelector("div"), - hover = false, - focus = false; + let search = document.getElementById("search"), + searchInput = search.querySelector("input"), + searchDiv = search.querySelector("div"), + hover = false, + focus = false; - resetSearchText(); + resetSearchText(); - searchInput.addEventListener('focus', event => { - focus = true; - search.classList.add('active'); - }); + searchInput.addEventListener('focus', event => { + focus = true; + search.classList.add('active'); + }); - searchDiv.addEventListener('mouseover', event => { - hover = 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'); - }); + 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('mouseleave', event => { + hover = false; + if (focus) return; + search.classList.remove('active'); + }); - search.addEventListener("click", event => { - search.classList.add("active"); - search.querySelector("input").focus(); - }); + search.addEventListener("click", event => { + search.classList.add("active"); + search.querySelector("input").focus(); + }); - searchInput.addEventListener('keyup', searchEvent); + searchInput.addEventListener('keyup', searchEvent); } function closeHelp(event) { - event.preventDefault(); + event.preventDefault(); - document.querySelector('.help').classList.remove('active'); - document.querySelector('.overlay').classList.remove('active'); + document.querySelector('.help').classList.remove('active'); + document.querySelector('.overlay').classList.remove('active'); } function openHelp(event) { - closePrompt(event); + closePrompt(event); - document.querySelector('.help').classList.add('active'); - document.querySelector('.overlay').classList.add('active'); + document.querySelector('.help').classList.add('active'); + document.querySelector('.overlay').classList.add('active'); } window.addEventListener('keydown', (event) => { - if (event.keyCode == 27) { - if (document.querySelector('.help.active')) { - closeHelp(event); - } + if (event.keyCode == 27) { + if (document.querySelector('.help.active')) { + closeHelp(event); } + } - if (event.keyCode == 46) { - deleteEvent(event); - } + if (event.keyCode == 46) { + deleteEvent(event); + } - if (event.keyCode == 112) { - event.preventDefault(); - openHelp(event); - } + if (event.keyCode == 112) { + event.preventDefault(); + openHelp(event); + } }); /* * * * * * * * * * * * * * * * - * * - * BOOTSTRAP * - * * - * * * * * * * * * * * * * * * */ +* * +* BOOTSTRAP * +* * +* * * * * * * * * * * * * * * */ document.addEventListener("DOMContentLoaded", function(event) { - overlay = document.querySelector('.overlay'); - clickOverlay = document.querySelector('#click-overlay'); + 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"); - buttons.move = document.getElementById("move"); + buttons.logout = document.getElementById("logout"); + buttons.open = document.getElementById("open"); + buttons.delete = document.getElementById("delete"); + buttons.previous = document.getElementById("previous"); + buttons.move = document.getElementById("move"); - // Attach event listeners - buttons.logout.addEventListener("click", logoutEvent); - buttons.open.addEventListener("click", openEvent); - buttons.move.addEventListener("click", moveEvent); + // Attach event listeners + buttons.logout.addEventListener("click", logoutEvent); + buttons.open.addEventListener("click", openEvent); + buttons.move.addEventListener("click", moveEvent); - templates.question = document.querySelector('#question-template'); - templates.info = document.querySelector('#info-template'); - templates.move = document.querySelector("#move-template"); + 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); + 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'); + }) + }); + }); + + overlay.addEventListener('click', event => { + if (document.querySelector('.help.active')) { + closeHelp(event); + return; } - 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'); + closePrompt(event); + }) - clickOverlay.addEventListener('click', event => { - button.querySelector('ul').classList.remove("active"); - clickOverlay.classList.remove('active'); - }) - }); - }); + let mainActions = document.getElementById('main-actions'); - overlay.addEventListener('click', event => { - if (document.querySelector('.help.active')) { - closeHelp(event); - return; - } + document.getElementById('more').addEventListener('click', event => { + event.preventDefault(); + event.stopPropagation(); - closePrompt(event); + clickOverlay.classList.add('active'); + mainActions.classList.add('active'); + + clickOverlay.addEventListener('click', event => { + mainActions.classList.remove('active'); + clickOverlay.classList.remove('active'); }) + }) - let mainActions = document.getElementById('main-actions'); - - document.getElementById('more').addEventListener('click', event => { - event.preventDefault(); - event.stopPropagation(); - - clickOverlay.classList.add('active'); - mainActions.classList.add('active'); - - clickOverlay.addEventListener('click', event => { - mainActions.classList.remove('active'); - clickOverlay.classList.remove('active'); - }) - }) - - setupSearch(); - return false; -}); \ No newline at end of file + setupSearch(); + return false; +}); diff --git a/_embed/public/js/editor.js b/_embed/public/js/editor.js index 07ba76b9..045c40fb 100644 --- a/_embed/public/js/editor.js +++ b/_embed/public/js/editor.js @@ -3,253 +3,253 @@ var editor = {}; editor.textareaAutoGrow = function() { - let autogrow = function() { - console.log(this.style.height) - this.style.height = 'auto'; - this.style.height = (this.scrollHeight) + 'px'; - } + let autogrow = function() { + console.log(this.style.height) + this.style.height = 'auto'; + this.style.height = (this.scrollHeight) + 'px'; + } - let textareas = document.getElementsByTagName('textarea'); + let textareas = document.getElementsByTagName('textarea'); - let addAutoGrow = () => { - Array.from(textareas).forEach(textarea => { - autogrow.bind(textarea)(); - textarea.addEventListener('keyup', autogrow); - }); - } + let addAutoGrow = () => { + Array.from(textareas).forEach(textarea => { + autogrow.bind(textarea)(); + textarea.addEventListener('keyup', autogrow); + }); + } - addAutoGrow(); - window.addEventListener('resize', addAutoGrow) + addAutoGrow(); + window.addEventListener('resize', addAutoGrow) } function deleteFrontMatterItem(event) { - event.preventDefault(); - document.getElementById(this.dataset.delete).remove(); + event.preventDefault(); + document.getElementById(this.dataset.delete).remove(); } function makeFromBaseTemplate(id, type, name, parent) { - let clone = document.importNode(templates.base.content, true); - clone.querySelector('fieldset').id = id; - clone.querySelector('fieldset').dataset.type = type; - clone.querySelector('h3').innerHTML = name; - clone.querySelector('.delete').dataset.delete = id; - clone.querySelector('.delete').addEventListener('click', deleteFrontMatterItem); - clone.querySelector('.add').addEventListener('click', addFrontMatterItem); + let clone = document.importNode(templates.base.content, true); + clone.querySelector('fieldset').id = id; + clone.querySelector('fieldset').dataset.type = type; + clone.querySelector('h3').innerHTML = name; + clone.querySelector('.delete').dataset.delete = id; + clone.querySelector('.delete').addEventListener('click', deleteFrontMatterItem); + clone.querySelector('.add').addEventListener('click', addFrontMatterItem); - if (parent.classList.contains("frontmatter")) { - parent.insertBefore(clone, document.querySelector('div.button.add')); - return - } + if (parent.classList.contains("frontmatter")) { + parent.insertBefore(clone, document.querySelector('div.button.add')); + return + } - parent.appendChild(clone); + parent.appendChild(clone); } function makeFromArrayItemTemplate(id, number, parent) { - let clone = document.importNode(templates.arrayItem.content, true); - clone.querySelector('[data-type="array-item"]').id = `${id}-${number}`; - clone.querySelector('input').name = id; - clone.querySelector('input').id = id; - clone.querySelector('div.action').dataset.delete = `${id}-${number}`; - clone.querySelector('div.action').addEventListener('click', deleteFrontMatterItem); - parent.querySelector('.group').appendChild(clone) - document.getElementById(`${id}-${number}`).querySelector('input').focus(); + let clone = document.importNode(templates.arrayItem.content, true); + clone.querySelector('[data-type="array-item"]').id = `${id}-${number}`; + clone.querySelector('input').name = id; + clone.querySelector('input').id = id; + clone.querySelector('div.action').dataset.delete = `${id}-${number}`; + clone.querySelector('div.action').addEventListener('click', deleteFrontMatterItem); + parent.querySelector('.group').appendChild(clone) + document.getElementById(`${id}-${number}`).querySelector('input').focus(); } function makeFromObjectItemTemplate(id, name, parent) { - let clone = document.importNode(templates.objectItem.content, true); - clone.querySelector('.block').id = `block-${id}`; - clone.querySelector('.block').dataset.content = id; - clone.querySelector('label').for = id; - clone.querySelector('label').innerHTML = name; - clone.querySelector('input').name = id; - clone.querySelector('input').id = id; - clone.querySelector('.action').dataset.delete = `block-${id}`; - clone.querySelector('.action').addEventListener('click', deleteFrontMatterItem); + let clone = document.importNode(templates.objectItem.content, true); + clone.querySelector('.block').id = `block-${id}`; + clone.querySelector('.block').dataset.content = id; + clone.querySelector('label').for = id; + clone.querySelector('label').innerHTML = name; + clone.querySelector('input').name = id; + clone.querySelector('input').id = id; + clone.querySelector('.action').dataset.delete = `block-${id}`; + clone.querySelector('.action').addEventListener('click', deleteFrontMatterItem); - parent.appendChild(clone) - document.getElementById(id).focus(); + parent.appendChild(clone) + document.getElementById(id).focus(); } function addFrontMatterItemPrompt(parent) { - return function(event) { - event.preventDefault(); + return function(event) { + event.preventDefault(); - let value = event.currentTarget.querySelector('input').value; - if (value === '') { - return true; - } - - closePrompt(event); - - let name = value.substring(0, value.lastIndexOf(':')), - type = value.substring(value.lastIndexOf(':') + 1, value.length); - - if (type !== "" && type !== "array" && type !== "object") { - name = value; - } - - name = name.replace(' ', '_'); - - let id = name; - - if (parent.id != '') { - id = parent.id + "." + id; - } - - if (type == "array" || type == "object") { - if (parent.dataset.type == "parent") { - makeFromBaseTemplate(id, type, name, document.querySelector('.frontmatter')); - return; - } - - makeFromBaseTemplate(id, type, name, block); - return; - } - - let group = parent.querySelector('.group'); - - if (group == null) { - parent.insertAdjacentHTML('afterbegin', '
    '); - group = parent.querySelector('.group'); - } - - makeFromObjectItemTemplate(id, name, group); + let value = event.currentTarget.querySelector('input').value; + if (value === '') { + return true; } + + closePrompt(event); + + let name = value.substring(0, value.lastIndexOf(':')), + type = value.substring(value.lastIndexOf(':') + 1, value.length); + + if (type !== "" && type !== "array" && type !== "object") { + name = value; + } + + name = name.replace(' ', '_'); + + let id = name; + + if (parent.id != '') { + id = parent.id + "." + id; + } + + if (type == "array" || type == "object") { + if (parent.dataset.type == "parent") { + makeFromBaseTemplate(id, type, name, document.querySelector('.frontmatter')); + return; + } + + makeFromBaseTemplate(id, type, name, block); + return; + } + + let group = parent.querySelector('.group'); + + if (group == null) { + parent.insertAdjacentHTML('afterbegin', '
    '); + group = parent.querySelector('.group'); + } + + makeFromObjectItemTemplate(id, name, group); + } } function addFrontMatterItem(event) { - event.preventDefault(); + event.preventDefault(); - let parent = event.currentTarget.parentNode, - type = parent.dataset.type; + let parent = event.currentTarget.parentNode, + type = parent.dataset.type; - // If the block is an array - if (type === "array") { - let id = parent.id + "[]", - count = parent.querySelectorAll('.group > div').length, - fieldsets = parent.getElementsByTagName("fieldset"); + // If the block is an array + if (type === "array") { + let id = parent.id + "[]", + count = parent.querySelectorAll('.group > div').length, + fieldsets = parent.getElementsByTagName("fieldset"); - if (fieldsets.length > 0) { - let itemType = fieldsets[0].dataset.type, - itemID = parent.id + "[" + fieldsets.length + "]", - itemName = fieldsets.length; + if (fieldsets.length > 0) { + let itemType = fieldsets[0].dataset.type, + itemID = parent.id + "[" + fieldsets.length + "]", + itemName = fieldsets.length; - makeFromBaseTemplate(itemID, itemType, itemName, parent); - } else { - makeFromArrayItemTemplate(id, count, parent); - } - - return; + makeFromBaseTemplate(itemID, itemType, itemName, parent); + } else { + makeFromArrayItemTemplate(id, count, parent); } - if (type == "object" || type == "parent") { - let clone = document.importNode(templates.question.content, true); - clone.querySelector('form').id = tempID; - clone.querySelector('h3').innerHTML = 'New field'; - clone.querySelector('p').innerHTML = 'Write the field name and then press enter. If you want to create an array or an object, end the name with :array or :object.'; - clone.querySelector('.ok').innerHTML = 'Create'; - clone.querySelector('form').addEventListener('submit', addFrontMatterItemPrompt(parent)); - clone.querySelector('form').classList.add('active') - document.querySelector('body').appendChild(clone); + return; + } - document.querySelector('.overlay').classList.add('active'); - document.getElementById(tempID).classList.add('active'); - } + if (type == "object" || type == "parent") { + let clone = document.importNode(templates.question.content, true); + clone.querySelector('form').id = tempID; + clone.querySelector('h3').innerHTML = 'New field'; + clone.querySelector('p').innerHTML = 'Write the field name and then press enter. If you want to create an array or an object, end the name with :array or :object.'; + clone.querySelector('.ok').innerHTML = 'Create'; + clone.querySelector('form').addEventListener('submit', addFrontMatterItemPrompt(parent)); + clone.querySelector('form').classList.add('active') + document.querySelector('body').appendChild(clone); - return false; + document.querySelector('.overlay').classList.add('active'); + document.getElementById(tempID).classList.add('active'); + } + + return false; } document.addEventListener("DOMContentLoaded", (event) => { - if (!document.getElementById('editor')) return; + if (!document.getElementById('editor')) return; - editor.textareaAutoGrow(); - templates.arrayItem = document.getElementById("array-item-template"); - templates.base = document.getElementById('base-template'); - templates.objectItem = document.getElementById("object-item-template"); - templates.temporary = document.getElementById('temporary-template'); - buttons.save = document.querySelector('#save'); + editor.textareaAutoGrow(); + templates.arrayItem = document.getElementById("array-item-template"); + templates.base = document.getElementById('base-template'); + templates.objectItem = document.getElementById("object-item-template"); + templates.temporary = document.getElementById('temporary-template'); + buttons.save = document.querySelector('#save'); - let container = document.getElementById('editor'), - kind = container.dataset.kind; + let container = document.getElementById('editor'), + kind = container.dataset.kind; - if (kind != 'frontmatter-only') { - let editor = document.querySelector('.content #ace'), - mode = editor.dataset.mode, - textarea = document.querySelector('textarea[name="content"]'), - aceEditor = ace.edit('ace'), - options = { - wrap: true, - maxLines: Infinity, - theme: "ace/theme/github", - showPrintMargin: false, - fontSize: "1em", - minLines: 20 - }; + if (kind != 'frontmatter-only') { + let editor = document.querySelector('.content #ace'), + mode = editor.dataset.mode, + textarea = document.querySelector('textarea[name="content"]'), + aceEditor = ace.edit('ace'), + options = { + wrap: true, + maxLines: Infinity, + theme: "ace/theme/github", + showPrintMargin: false, + fontSize: "1em", + minLines: 20 + }; - aceEditor.getSession().setMode("ace/mode/" + mode); - aceEditor.getSession().setValue(textarea.value); - aceEditor.getSession().on('change', function() { - textarea.value = aceEditor.getSession().getValue(); - }); + aceEditor.getSession().setMode("ace/mode/" + mode); + aceEditor.getSession().setValue(textarea.value); + aceEditor.getSession().on('change', function() { + textarea.value = aceEditor.getSession().getValue(); + }); - if (mode == "markdown") options.showGutter = false; - aceEditor.setOptions(options); + if (mode == "markdown") options.showGutter = false; + aceEditor.setOptions(options); + } + + let deleteFrontMatterItemButtons = document.getElementsByClassName('delete'); + Array.from(deleteFrontMatterItemButtons).forEach(button => { + button.addEventListener('click', deleteFrontMatterItem); + }); + + let addFrontMatterItemButtons = document.getElementsByClassName('add'); + Array.from(addFrontMatterItemButtons).forEach(button => { + button.addEventListener('click', addFrontMatterItem); + }); + + let saveContent = function() { + let data = form2js(document.querySelector('form')); + + if (typeof data.content === "undefined" && kind != 'frontmatter-only') { + data.content = ""; } - let deleteFrontMatterItemButtons = document.getElementsByClassName('delete'); - Array.from(deleteFrontMatterItemButtons).forEach(button => { - button.addEventListener('click', deleteFrontMatterItem); - }); - - let addFrontMatterItemButtons = document.getElementsByClassName('add'); - Array.from(addFrontMatterItemButtons).forEach(button => { - button.addEventListener('click', addFrontMatterItem); - }); - - let saveContent = function() { - let data = form2js(document.querySelector('form')); - - if (typeof data.content === "undefined" && kind != 'frontmatter-only') { - data.content = ""; - } - - if (typeof data.content === "number") { - data.content = data.content.toString(); - } - - let html = buttons.save.querySelector('i').changeToLoading(), - request = new XMLHttpRequest(); - - request.open("PUT", toWebDavURL(window.location.pathname)); - request.setRequestHeader('Kind', kind); - request.send(JSON.stringify(data)); - request.onreadystatechange = function() { - if (request.readyState == 4) { - buttons.save.querySelector('i').changeToDone((request.status != 201), html); - } - } + if (typeof data.content === "number") { + data.content = data.content.toString(); } - document.querySelector('#save').addEventListener('click', event => { + let html = buttons.save.querySelector('i').changeToLoading(), + request = new XMLHttpRequest(); + + request.open("PUT", toWebDavURL(window.location.pathname)); + request.setRequestHeader('Kind', kind); + request.send(JSON.stringify(data)); + request.onreadystatechange = function() { + if (request.readyState == 4) { + buttons.save.querySelector('i').changeToDone((request.status != 201), html); + } + } + } + + document.querySelector('#save').addEventListener('click', event => { + event.preventDefault(); + saveContent(); + }); + + document.querySelector('form').addEventListener('submit', (event) => { + event.preventDefault(); + saveContent(); + }); + + window.addEventListener('keydown', (event) => { + if (event.ctrlKey || event.metaKey) { + switch (String.fromCharCode(event.which).toLowerCase()) { + case 's': event.preventDefault(); saveContent(); - }); + break; + } + } + }); - document.querySelector('form').addEventListener('submit', (event) => { - event.preventDefault(); - saveContent(); - }); - - window.addEventListener('keydown', (event) => { - if (event.ctrlKey || event.metaKey) { - switch (String.fromCharCode(event.which).toLowerCase()) { - case 's': - event.preventDefault(); - saveContent(); - break; - } - } - }); - - return false; -}); \ No newline at end of file + return false; +}); diff --git a/_embed/public/js/listing.js b/_embed/public/js/listing.js index 09ba2974..d5d3ad4c 100644 --- a/_embed/public/js/listing.js +++ b/_embed/public/js/listing.js @@ -54,8 +54,8 @@ listing.itemDrop = function(e) { e.preventDefault(); let el = e.target, - id = e.dataTransfer.getData("id"), - name = e.dataTransfer.getData("name"); + id = e.dataTransfer.getData("id"), + name = e.dataTransfer.getData("name"); if (id == "" || name == "") return; @@ -68,19 +68,19 @@ listing.itemDrop = function(e) { if (el.id === id) return; let oldLink = document.getElementById(id).dataset.url, - newLink = el.dataset.url + name; + newLink = el.dataset.url + name; webdav.move(oldLink, newLink) - .then(() => listing.reload()) - .catch(e => console.log(e)); + .then(() => listing.reload()) + .catch(e => console.log(e)); } listing.documentDrop = function(event) { event.preventDefault(); let dt = event.dataTransfer, - files = dt.files, - el = event.target, - items = document.getElementsByClassName('item'); + files = dt.files, + el = event.target, + items = document.getElementsByClassName('item'); for (let i = 0; i < 5; i++) { if (el != null && !el.classList.contains('item')) { @@ -114,15 +114,15 @@ listing.rename = function(event) { } let link = item.dataset.url, - field = item.querySelector('.name'), - name = field.innerHTML; + field = item.querySelector('.name'), + name = field.innerHTML; let submit = (event) => { event.preventDefault(); let newName = event.currentTarget.querySelector('input').value, - newLink = removeLastDirectoryPartOf(link) + "/" + newName, - html = buttons.rename.querySelector('i').changeToLoading(); + newLink = removeLastDirectoryPartOf(link) + "/" + newName, + html = buttons.rename.querySelector('i').changeToLoading(); closePrompt(event); webdav.move(link, newLink).then(() => { @@ -159,21 +159,21 @@ listing.rename = function(event) { listing.handleFiles = function(files, base) { let html = buttons.upload.querySelector('i').changeToLoading(), - promises = []; + promises = []; 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); - }) + .then(() => { + listing.reload(); + buttons.upload.querySelector('i').changeToDone(false, html); + }) + .catch(e => { + console.log(e); + buttons.upload.querySelector('i').changeToDone(true, html); + }) return false; } @@ -194,7 +194,7 @@ listing.handleSelectionChange = function(event) { listing.redefineDownloadURLs(); let selectedNumber = selectedItems.length, - fileAction = document.getElementById("file-only"); + fileAction = document.getElementById("file-only"); if (selectedNumber) { fileAction.classList.remove("disabled"); @@ -278,9 +278,9 @@ listing.newFilePrompt = function(event) { event.preventDefault(); let button = document.getElementById('new'), - html = button.querySelector('i').changeToLoading(), - request = new XMLHttpRequest(), - name = event.currentTarget.querySelector('input').value; + 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(); @@ -297,17 +297,17 @@ listing.newFilePrompt = function(event) { listing.updateColumns = function(event) { let columns = Math.floor(document.getElementById('listing').offsetWidth / 300), - items = getCSSRule(['#listing.mosaic .item', '.mosaic#listing .item']); + items = getCSSRule(['#listing.mosaic .item', '.mosaic#listing .item']); items.style.width = `calc(${100/columns}% - 1em)`; } listing.addDoubleTapEvent = function() { let items = document.getElementsByClassName('item'), - touches = { - id: '', - count: 0 - }; + touches = { + id: '', + count: 0 + }; Array.from(items).forEach(file => { file.addEventListener('touchstart', event => { @@ -349,8 +349,8 @@ window.addEventListener('keydown', (event) => { console.log("hey") switch (String.fromCharCode(event.which).toLowerCase()) { case 's': - event.preventDefault(); - window.location = "?download=true" + event.preventDefault(); + window.location = "?download=true" } } });