acoeur/themes/acoeur/assets/js/author.js

58 lines
2.4 KiB
JavaScript

// Add the proper absolute url to invitation link
document.querySelectorAll('aside#actions #invitation')
.forEach(elt => elt.href += document.location.href)
// Generate QR Code to ease URL transmission
document.querySelectorAll('aside#actions #barcode')
.forEach(elt => new QRCode(elt, document.location.href))
document.querySelectorAll('aside#actions #create')
.forEach(elt => elt.onclick = () => {
const url = `${topSection}/${uuid()}`
fetch(`/edit/${url}.create`, { credentials: 'include', method: 'PUT', body: topSection })
.then(() => sleep(1))
.then(() => window.location.assign('/' + url))
.catch(error => console.error(`Error creating #{topSection}`, error))
})
document.querySelectorAll('aside#actions #delete')
.forEach(elt => elt.onclick = () => {
if (confirm(elt.dataset.confirm))
fetch(document.body.dataset.editUrl.replace('/_index', '.delete'), { method: 'PUT' })
.then(() => sleep(1))
.then(() => window.location.assign('/' + topSection))
.catch(error => console.error(`Error deleting #{topSection}`, error))
})
document.querySelectorAll('aside#actions input#cover')
.forEach(elt => elt.onchange = () => {
const header = document.querySelector('body > header')
const url = `/assets/users/${uuid()}.${elt.files[0].name.split('.').reverse()[0]}`
const body = `featured_image: ../..${url}`
fetch(`/edit${url}`, { method: 'PUT', body: elt.files[0] })
.then(() => fetch(`${document.body.dataset.editUrl}.prop`, { method: 'PUT', body: body }))
.then(() => header.style.backgroundImage = null)
.then(() => sleep(1))
.then(() => header.style.backgroundImage = `url("../..${url}")`)
.catch(error => console.error(`Error uploading ${elt.files[0].name}`, error))
})
document.querySelectorAll('aside#actions input#file')
.forEach(elt => elt.onchange = () =>
[...elt.files].forEach(file =>
fetch(document.body.dataset.editUrl.replace('_index', file.name), { method: 'PUT', body: file })
.then(() => sleep(1))
.then(() => window.location.reload())
.catch(error => console.error(`Error uploading ${elt.files[0].name}`, error))))
document.querySelectorAll('.delete_image')
.forEach(elt => elt.onclick = () => {
if (confirm(elt.dataset.confirm))
fetch('/edit/' + elt.dataset.target, { method: 'DELETE' })
.then(() => elt.parentNode.remove())
.catch(error => console.error(error))
})