ska-referee-test/ref-test/app/quiz/static/js/script.js

86 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-06-15 11:43:04 +01:00
$(document).ready(function() {
$("#od-font-test").click(function(){
$("body").css("font-family", "opendyslexic3regular")
})
2022-06-15 11:43:04 +01:00
$('.test-code-input').keyup(function() {
var input = $(this).val().split("-").join("").split("—").join("")
2022-06-15 11:43:04 +01:00
if (input.length > 0) {
input = input.match(new RegExp('.{1,4}', 'g')).join("—")
2022-06-15 11:43:04 +01:00
}
$(this).val(input)
})
})
2022-06-15 11:43:04 +01:00
$('form[name=form-quiz-start]').submit(function(event) {
var $form = $(this)
var data = $form.serialize()
2022-06-15 11:43:04 +01:00
$.ajax({
url: window.location.pathname,
type: 'POST',
data: data,
dataType: 'json',
success: function(response) {
2022-06-15 23:54:44 +01:00
var id = response.id
window.localStorage.setItem('id', id)
window.location.href = `/quiz/`
2022-06-15 11:43:04 +01:00
},
error: function(response) {
error_response(response)
2022-06-15 11:43:04 +01:00
}
})
2022-06-15 11:43:04 +01:00
event.preventDefault()
})
2022-06-15 11:43:04 +01:00
function error_response(response) {
const $alert = $("#alert-box")
$alert.html('')
2022-06-15 11:43:04 +01:00
if (typeof response.responseJSON.error === 'string' || response.responseJSON.error instanceof String) {
$alert.html(`
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="bi bi-exclamation-triangle-fill" title="Danger"></i>
${response.responseJSON.error}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
`)
2022-06-15 11:43:04 +01:00
} else if (response.responseJSON.error instanceof Array) {
var output = ''
for (let i = 0; i < response.responseJSON.error.length; i ++) {
2022-06-15 11:43:04 +01:00
output += `
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="bi bi-exclamation-triangle-fill" title="Danger"></i>
${response.responseJSON.error[i]}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
`
$alert.html(output)
2022-06-15 11:43:04 +01:00
}
}
}
// Dismiss Cookie Alert
$('#dismiss-cookie-alert').click(function(event){
$.ajax({
url: '/cookies/',
2022-06-16 13:22:06 +01:00
type: 'POST',
2022-06-15 11:43:04 +01:00
data: {
time: Date.now()
},
dataType: 'json',
success: function(response){
console.log(response)
2022-06-15 11:43:04 +01:00
},
error: function(response){
console.log(response)
2022-06-15 11:43:04 +01:00
}
})
event.preventDefault()
2022-06-15 11:43:04 +01:00
})