All basic question types

This commit is contained in:
Charlotte Van Petegem 2025-04-27 00:13:20 +02:00
parent 3e1e5618ca
commit 4fe218166f
Signed by: chvp
SSH key fingerprint: SHA256:s9rb8jBVfdahqWHuBAcHCBP1wmj4eYQXZfqgz4H3E9E
51 changed files with 760 additions and 92 deletions

View file

@ -1 +1,8 @@
// Entry point for the build script in your package.json
import initSimpleQuestions from './simple_input.js'
import initImageQuestions from './image_input.js'
addEventListener("DOMContentLoaded", () => {
initSimpleQuestions()
initImageQuestions()
})

View file

@ -0,0 +1,35 @@
const csrfToken = document.querySelector("[name='csrf-token']").content
export default function initImageQuestions() {
document.querySelectorAll('[data-behaviour="question_image_input"]').forEach((input) => {
const submitUrl = input.dataset.submitUrl
input.addEventListener('change', () => {
const file = input.files[0];
const fileReader = new FileReader();
fileReader.onload = (ev) => {
fetch(submitUrl, {
method: 'PUT',
headers: {
"x-csrf-token": csrfToken,
"content-type": "application/json",
},
body: JSON.stringify({
filename: file.name,
mimetype: file.type,
data: ev.target.result.replace(/^data:[a-zA-Z0-9!#$%^&\\*_\-+{}|'.`~]+\/[a-zA-Z0-9!#$%^&\\*_\-+{}|'.`~]+;base64,/, ""),
})
})
}
fileReader.readAsDataURL(file);
const preview = document.querySelector(`#${input.id}_preview`)
while (preview.firstChild) {
preview.removeChild(preview.firstChild)
}
const previewImage = document.createElement("img")
previewImage.src = URL.createObjectURL(file)
previewImage.alt = previewImage.title = file.name
preview.appendChild(previewImage)
})
})
}

View file

@ -0,0 +1,23 @@
import debounce from 'debounce'
const csrfToken = document.querySelector("[name='csrf-token']").content
export default function initSimpleQuestions() {
document.querySelectorAll('[data-behaviour="question_simple_input"]').forEach((input) => {
const submitUrl = input.dataset.submitUrl
const handleInput = debounce(() => {
fetch(submitUrl, {
method: 'PUT',
headers: {
"x-csrf-token": csrfToken,
"content-type": "application/json",
},
body: JSON.stringify({ value: input.value }),
})
}, 300)
input.addEventListener('change', handleInput)
input.addEventListener('input', handleInput)
})
}