filebrowser/_embed/public/js/common.js

573 lines
14 KiB
JavaScript
Raw Normal View History

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