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

44 lines
1.7 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`, { 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#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))
})