98 lines
3.4 KiB
JavaScript
98 lines
3.4 KiB
JavaScript
const form = document.querySelector('form[data-media-upload]')
|
|
const fields = document.getElementById('uploadFields')
|
|
const fileInput = form.elements.namedItem('files[]')
|
|
const nameInput = form.elements.namedItem('name')
|
|
const kind = form.dataset.mediaKind
|
|
const sizeError = `The ${kind} must be no larger than ${form.dataset.limitLabel}.`
|
|
const progress = document.getElementById('uploadProgress')
|
|
const bar = document.getElementById('uploadProgressBar')
|
|
const statusText = document.getElementById('uploadStatus')
|
|
const errorBox = document.getElementById('uploadError')
|
|
let uploading = false
|
|
|
|
function warnBeforeLeaving(event) {
|
|
event.preventDefault()
|
|
event.returnValue = ''
|
|
}
|
|
|
|
function setProgress(percent) {
|
|
bar.style.width = `${percent}%`
|
|
bar.textContent = `${percent}%`
|
|
progress.setAttribute('aria-valuenow', percent)
|
|
}
|
|
|
|
function failUpload(message) {
|
|
uploading = false
|
|
fields.disabled = false
|
|
window.removeEventListener('beforeunload', warnBeforeLeaving)
|
|
errorBox.textContent = message
|
|
errorBox.hidden = false
|
|
statusText.textContent = 'Upload not completed. Please try again.'
|
|
}
|
|
|
|
form.addEventListener('submit', function(event) {
|
|
event.preventDefault()
|
|
if (uploading) return
|
|
errorBox.hidden = true
|
|
const file = fileInput.files[0]
|
|
if (!nameInput.value.trim()) {
|
|
failUpload(`Please enter a name for the ${kind}.`)
|
|
return
|
|
}
|
|
if (!file || file.size === 0) {
|
|
failUpload(`Please select a non-empty ${kind} file.`)
|
|
return
|
|
}
|
|
if (file.size > Number(form.dataset.maxSize)) {
|
|
failUpload(sizeError)
|
|
return
|
|
}
|
|
|
|
const data = new FormData(form)
|
|
data.set('name', nameInput.value.trim())
|
|
uploading = true
|
|
fields.disabled = true
|
|
setProgress(0)
|
|
statusText.textContent = 'Uploading… Do not leave this page.'
|
|
window.addEventListener('beforeunload', warnBeforeLeaving)
|
|
|
|
const xhr = new XMLHttpRequest()
|
|
xhr.open('POST', form.action)
|
|
xhr.responseType = 'json'
|
|
xhr.upload.onprogress = function(event) {
|
|
if (event.lengthComputable) {
|
|
setProgress(Math.min(100, Math.floor(event.loaded / event.total * 100)))
|
|
} else {
|
|
statusText.textContent = 'Uploading… Please wait and do not leave this page.'
|
|
}
|
|
}
|
|
xhr.upload.onload = function() {
|
|
setProgress(100)
|
|
statusText.textContent = `Transfer complete. Waiting for the server to save the ${kind}…`
|
|
}
|
|
xhr.onload = function() {
|
|
if (xhr.status === 200 && xhr.response && xhr.response.status === 'SUCCESS') {
|
|
uploading = false
|
|
window.removeEventListener('beforeunload', warnBeforeLeaving)
|
|
statusText.textContent = 'Saved. Returning to the homepage…'
|
|
window.location.href = form.dataset.homeUrl
|
|
} else {
|
|
const message = xhr.response && xhr.response.error
|
|
failUpload(message || (xhr.status === 413
|
|
? sizeError
|
|
: 'The server could not save the upload. Please try again.'))
|
|
}
|
|
}
|
|
xhr.onerror = function() {
|
|
failUpload('The upload connection failed. Check your connection before trying again.')
|
|
}
|
|
xhr.onabort = function() {
|
|
failUpload('The upload was cancelled.')
|
|
}
|
|
try {
|
|
xhr.send(data)
|
|
} catch (error) {
|
|
failUpload('Unable to start the upload. Please try again.')
|
|
}
|
|
})
|