Web M&M
https://mam.matfyz.cz
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
2.1 KiB
56 lines
2.1 KiB
// 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);
|
|
});
|
|
});
|
|
|