Jonas Havelka
2 years ago
5 changed files with 131 additions and 115 deletions
@ -0,0 +1,22 @@ |
|||||
|
// Kontrola, že org neposílá nějakou blbost v detail.html
|
||||
|
|
||||
|
function zkontroluj_hodnoceni() { |
||||
|
const pocet = $('.hodnoceni').length; |
||||
|
if (pocet === 1) { // vydím pouze plusko
|
||||
|
const vysledek = confirm("Odstranil jsi všechny problémy tohoto řešení. Nepůjde tedy dohledat přes problémy, co řeší, tj. například v došlých řešeních. Přesto odeslat?"); |
||||
|
if (!vysledek) { |
||||
|
event.preventDefault(); |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function problem_is_empty(elem, index, array) {return elem.firstElementChild.children.length !== 1 && elem.firstElementChild.children[1].textContent === "";} |
||||
|
|
||||
|
if ($('.hodnoceni').toArray().some(problem_is_empty)) { |
||||
|
alert("Neuloženo! Nezadal jsi problém, ke kterému posíláš hodnocení. Pokud je toto hodnocení navíc, smaž ho prosím křížkem a znovu odešli.") |
||||
|
event.preventDefault() |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
@ -0,0 +1,56 @@ |
|||||
|
// FIXME: Necopypastovat! Tohle je zkopírované ze static/odevzdavatko/dynamic_formsets.js
|
||||
|
|
||||
|
|
||||
|
// Credit https://medium.com/all-about-django/adding-forms-dynamically-to-a-django-formset-375f1090c2b0
|
||||
|
function updateElementIndex(el, prefix, ndx) { |
||||
|
var id_regex = new RegExp('(' + prefix + '-\\d+)'); |
||||
|
var replacement = prefix + '-' + ndx; |
||||
|
if ($(el).attr("for")) { |
||||
|
$(el).attr("for", $(el).attr("for").replace(id_regex, replacement)); |
||||
|
} |
||||
|
if (el.id) { |
||||
|
el.id = el.id.replace(id_regex, replacement); |
||||
|
} |
||||
|
if (el.name) { |
||||
|
el.name = el.name.replace(id_regex, replacement); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Credit https://medium.com/all-about-django/adding-forms-dynamically-to-a-django-formset-375f1090c2b0
|
||||
|
function deleteForm(prefix, btn) { |
||||
|
var total = parseInt($('#id_' + prefix + '-TOTAL_FORMS').val()); |
||||
|
if (total >= 1){ |
||||
|
btn.closest('tr').remove(); |
||||
|
var forms = $('.hodnoceni'); |
||||
|
var formCount = forms.length - 1; // There is one extra such form hidden as template!
|
||||
|
$('#id_' + prefix + '-TOTAL_FORMS').val(formCount); |
||||
|
for (var i=0; i<formCount; i++) { |
||||
|
$(forms.get(i)).find(':input').each(function() { |
||||
|
updateElementIndex(this, prefix, i); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
// Credit: https://simpleit.rocks/python/django/dynamic-add-form-with-add-button-in-django-modelformset-template/
|
||||
|
$(document).ready(function(){ |
||||
|
$('#pridat_hodnoceni').click(function() { |
||||
|
var form_idx = $('#id_form-TOTAL_FORMS').val(); |
||||
|
var new_form = $('#empty_form').html().replace(/__prefix__/g, form_idx); |
||||
|
$('#form_set').append(new_form); |
||||
|
// Newly created form has not the binding between remove button and remove function
|
||||
|
// We need to add it manually
|
||||
|
$('.smazat_hodnoceni').click(function(){ |
||||
|
deleteForm("form",this); |
||||
|
}); |
||||
|
// Copy deadline
|
||||
|
if (form_idx !== "0") { |
||||
|
$('#id_form-' + form_idx + '-deadline_body')[0].value = $('#id_form-' + (form_idx - 1) + '-deadline_body')[0].value |
||||
|
} |
||||
|
$('#id_form-TOTAL_FORMS').val(parseInt(form_idx) + 1); |
||||
|
}); |
||||
|
$('.smazat_hodnoceni').click(function(){ |
||||
|
deleteForm("form",this); |
||||
|
}); |
||||
|
}); |
Loading…
Reference in new issue