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

277 lines
8.2 KiB
JavaScript
Raw Permalink Normal View History

2022-06-15 11:43:04 +01:00
// Menu Highlight Scripts
const menuItems = document.getElementsByClassName('nav-link')
2022-06-15 11:43:04 +01:00
for(let i = 0; i < menuItems.length; i++) {
if(menuItems[i].pathname == window.location.pathname) {
menuItems[i].classList.add('active')
2022-06-15 11:43:04 +01:00
}
}
const dropdownItems = document.getElementsByClassName('dropdown-item')
2022-06-15 11:43:04 +01:00
for(let i = 0; i< dropdownItems.length; i++) {
if(dropdownItems[i].pathname == window.location.pathname) {
dropdownItems[i].classList.add('active')
$( "#" + dropdownItems[i].id ).closest( '.dropdown' ).find('.dropdown-toggle').addClass('active')
2022-06-15 11:43:04 +01:00
}
}
// General Post Method Form Processing Script
$('form.form-post').submit(function(event) {
var $form = $(this)
var data = $form.serialize()
var url = $(this).prop('action')
var rel_success = $(this).data('rel-success')
2022-06-15 11:43:04 +01:00
$.ajax({
url: url,
type: 'POST',
data: data,
dataType: 'json',
success: function(response) {
if (response.redirect_to) {
window.location.href = response.redirect_to
2022-06-15 11:43:04 +01:00
}
else {
window.location.href = rel_success
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
// Form Upload Questions - Special case, needs to handle files.
$('form[name=form-upload-questions]').submit(function(event) {
var $form = $(this)
var data = new FormData($form[0])
2022-06-15 11:43:04 +01:00
var file = $('input[name=data_file]')[0].files[0]
data.append('file', file)
$.ajax({
url: window.location.pathname,
type: 'POST',
data: data,
processData: false,
contentType: false,
success: function(response) {
window.location.reload()
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
// Edit and Delete Test Button Handlers
$('.test-action').click(function(event) {
let id = $(this).data('id')
let action = $(this).data('action')
2022-06-15 11:43:04 +01:00
2022-06-15 23:54:44 +01:00
if (action == 'delete' || action == 'start' || action == 'end') {
2022-06-15 11:43:04 +01:00
$.ajax({
2022-06-15 23:54:44 +01:00
url: `/admin/tests/edit/`,
2022-06-15 11:43:04 +01:00
type: 'POST',
2022-06-19 11:17:21 +01:00
data: JSON.stringify({'id': id, 'action': action}),
2022-06-15 11:43:04 +01:00
contentType: 'application/json',
success: function(response) {
window.location.href = '/admin/tests/'
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
} else if (action == 'edit') {
2022-06-15 23:54:44 +01:00
window.location.href = `/admin/test/${id}/`
2023-03-05 00:28:54 +00:00
} else if (action == 'analyse') {
$.ajax({
url: `/admin/analysis/`,
type: 'POST',
data: JSON.stringify({'id': id, 'class': 'test'}),
contentType: 'application/json',
success: function(response) {
window.location.href = response
},
error: function(response){
error_response(response)
},
})
2022-06-15 11:43:04 +01:00
}
event.preventDefault()
})
2022-06-15 11:43:04 +01:00
// Edit Dataset Button Handlers
$('.edit-question-dataset').click(function(event) {
var id = $(this).data('id')
var action = $(this).data('action')
var disabled = $(this).hasClass('disabled')
2022-06-15 11:43:04 +01:00
if ( !disabled ) {
2022-06-22 01:54:40 +01:00
if (action == 'delete') {
$.ajax({
url: `/admin/settings/questions/${action}/`,
type: 'POST',
data: JSON.stringify({
'id': id,
'action': action,
}),
contentType: 'application/json',
success: function(response) {
window.location.reload()
2022-06-22 01:54:40 +01:00
},
error: function(response){
error_response(response)
2022-06-22 01:54:40 +01:00
},
})
2022-06-22 01:54:40 +01:00
} else if (action == 'edit') {
window.location.href = `/admin/editor/${id}/`
} else if (action == 'view') {
window.location.href = `/admin/view/${id}`
2022-06-22 01:54:40 +01:00
} else if (action == 'download') {
window.location.href = `/admin/settings/questions/download/${id}/`
2023-03-05 00:28:54 +00:00
} else if (action == 'analyse') {
$.ajax({
url: `/admin/analysis/`,
type: 'POST',
data: JSON.stringify({'id': id, 'class': 'dataset'}),
contentType: 'application/json',
success: function(response) {
window.location.href = response
},
error: function(response){
error_response(response)
},
})
2022-06-22 01:54:40 +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
}
}
$alert.focus()
}
// 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
})
// Script for Result Actions
$('.result-action-buttons').click(function(event){
var id = $(this).data('id')
2022-06-15 11:43:04 +01:00
if ($(this).data('result-action') == 'generate') {
$.ajax({
url: '/admin/certificate/',
type: 'POST',
2022-06-15 23:54:44 +01:00
data: JSON.stringify({'id': id}),
2022-06-15 11:43:04 +01:00
contentType: 'application/json',
dataType: 'html',
success: function(response) {
var display_window = window.open()
display_window.document.write(response)
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
} else {
var action = $(this).data('result-action')
$.ajax({
url: window.location.href,
type: 'POST',
2022-06-15 23:54:44 +01:00
data: JSON.stringify({'id': id, 'action': action}),
2022-06-15 11:43:04 +01:00
contentType: 'application/json',
success: function(response) {
if (action == 'delete') {
window.location.href = '/admin/results/'
} else window.location.reload()
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
// Script for Deleting Time Adjustment
$('.adjustment-delete').click(function(event){
var user_code = $(this).data('user_code')
var location = window.location.href
2022-06-15 11:43:04 +01:00
location = location.replace('#', '')
$.ajax({
url: location + 'delete-adjustment/',
type: 'POST',
data: JSON.stringify({'user_code': user_code}),
contentType: 'application/json',
success: function(response) {
window.location.reload()
2022-06-15 11:43:04 +01:00
},
error: function(response){
error_response(response)
2022-06-15 11:43:04 +01:00
},
})
event.preventDefault()
})
// Detailed Results view questions
$('.view-full-questions').click(function(event) {
var dataset = $(this).data('dataset')
window.open(`/admin/view/${dataset}`, '_blank')
event.preventDefault()
})