diff --git a/crowdsourcer/static/js/questions.esm.js b/crowdsourcer/static/js/questions.esm.js new file mode 100644 index 0000000..3376ded --- /dev/null +++ b/crowdsourcer/static/js/questions.esm.js @@ -0,0 +1,75 @@ +const form_regex = /form-\d-/i +function get_name_from_input(input) { + return input.name.replace(form_regex, ""); +} + +function disable_submit_if_invalid() { + if ($.find('.invalid-feedback').length > 0) { + $('#save_all_answers').prop("disabled", true); + } else { + $('#save_all_answers').prop("disabled", false); + } +} + +$(function(){ + $('.form-select, .form-control, .form-check-input').on('blur', function(e){ + var $d = $(this); + var $fs = $d.parents('fieldset'); + + var csrf = $('input[name="csrfmiddlewaretoken"]').val() + let data = {"csrfmiddlewaretoken": csrf}; + let has_values = false; + $fs.find('.form-select, .form-control, input[type="hidden"]').each(function($i, $field) { + let name = get_name_from_input($field); + let $f = $( $field ); + let val = $( $field ).val(); + if (name != "question" && name != "authority" && val) { + has_values = true; + } + data[name] = val; + }); + if ( $fs.find('.form-check-input') ) { + let $f = $fs.find('.form-check-input').get(0); + let name = get_name_from_input($f); + data[name] = $( $f.name ).val(); + } + + if (!has_values) { + $fs.find('.form-select, .form-control, .form-check-input, input[type="hidden"]').each(function($i, $field) { + let $f = $($field); + $f.removeClass("is-invalid").removeClass("is-valid"); + $f.next('.invalid-feedback').remove(); + }); + disable_submit_if_invalid() + return; + } + + url = window.location + data["question"] + "/"; + + $.post(url, data, function(r_data) { + if (r_data["success"] != 1) { + $fs.find('.form-select, .form-control, .form-check-input, input[type="hidden"]').each(function($i, $field) { + let name = get_name_from_input($field); + let $f = $($field); + $f.next('.invalid-feedback').remove(); + if (r_data["errors"].hasOwnProperty(name)) { + $f.addClass("is-invalid").removeClass("is-valid"); + $f.after('
' + r_data["errors"][name] + '
'); + } else { + $f.addClass("is-valid").removeClass("is-invalid"); + } + }); + $('#save_all_answers').prop("disabled", true); + } else { + $fs.find('.form-select, .form-control, .form-check-input, input[type="hidden"]').each(function($i, $field) { + let name = get_name_from_input($field); + $f = $($field); + $f.next('.invalid-feedback').remove(); + $f.addClass("is-valid").removeClass("is-invalid"); + }); + + disable_submit_if_invalid() + } + }); + }); +}); diff --git a/crowdsourcer/templates/crowdsourcer/authority_questions.html b/crowdsourcer/templates/crowdsourcer/authority_questions.html index 106a989..63a60f6 100644 --- a/crowdsourcer/templates/crowdsourcer/authority_questions.html +++ b/crowdsourcer/templates/crowdsourcer/authority_questions.html @@ -1,6 +1,7 @@ {% extends 'crowdsourcer/base.html' %} {% load django_bootstrap5 %} +{% load static %} {% block content %}

@@ -82,8 +83,12 @@

{% endfor %}
- +
{% endblock %} + +{% block script %} + +{% endblock %} diff --git a/crowdsourcer/templates/crowdsourcer/authority_questions_with_previous.html b/crowdsourcer/templates/crowdsourcer/authority_questions_with_previous.html index 77bda6f..4989ebb 100644 --- a/crowdsourcer/templates/crowdsourcer/authority_questions_with_previous.html +++ b/crowdsourcer/templates/crowdsourcer/authority_questions_with_previous.html @@ -2,6 +2,7 @@ {% load django_bootstrap5 %} {% load neighbourhood_filters %} +{% load static %} {% block content %}

@@ -173,7 +174,7 @@

New response

{% endfor %}
- +
@@ -206,3 +207,7 @@

New response

{% endblock %} + +{% block script %} + +{% endblock %}