course_survey.js 3.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
$(function() {

    // adding js class for styling with accessibility in mind
    $('body').addClass('js');

    // form field label styling on focus
    $("form :input").focus(function() {
        $("label[for='" + this.id + "']").parent().addClass("is-focused");
    }).blur(function() {
        $("label").parent().removeClass("is-focused");
    });

    $('.status.message.submission-error').addClass("is-hidden");

    toggleSubmitButton(true);

    $('#survey-form').on('submit', function() {
      /* validate required fields */

        var $inputs = $('#survey-form :input');

        $('.status.message.submission-error .message-copy').empty();

        var cancel_submit = false;

        $inputs.each(function() {
            /* see if it is a required field and - if so - make sure user presented all information */
            if (typeof $(this).attr("required") !== typeof undefined) {
                var val = $(this).val();
                if (typeof(val) === "string") {
                    if (val.trim().length === 0) {
                        var field_label = $(this).parent().find("label");
                        $(this).parent().addClass('field-error');
                        $('.status.message.submission-error .message-copy').append("<li class='error-item'>"+field_label.text()+"</li>");
                        cancel_submit = true;
                    }
                } else if (typeof(val) === "object") {
                    /* for SELECT statements */
                    if (val === null || val.length === 0 || val[0] === "") {
                        var field_label = $(this).parent().find("label");
                        $(this).parent().addClass('field-error');
                        $('.status.message.submission-error .message-copy').append("<li class='error-item'>"+field_label.text()+"</li>");
                        cancel_submit = true;
                    }
                }
            }
        });

        if (cancel_submit) {
          $('.status.message.submission-error').
            removeClass("is-hidden").
            focus();
          $("html, body").animate({ scrollTop: 0 }, "fast");
          return false;
        }

        toggleSubmitButton(false);
    });

    $('#survey-form').on('ajax:error', function() {
        toggleSubmitButton(true);
    });

    $('#survey-form').on('ajax:success', function(event, json, xhr) {
        var url = json.redirect_url;
        location.href = url;
    });

    $('#survey-form').on('ajax:error', function(event, jqXHR, textStatus) {
        toggleSubmitButton(true);
        json = $.parseJSON(jqXHR.responseText);
        $('.status.message.submission-error').addClass('is-shown').focus();
        $('.status.message.submission-error .message-copy').
            html(gettext("There has been an error processing your survey.")).
            stop().
            css("display", "block");
    });
});

function toggleSubmitButton(enable) {
    var $submitButton = $('form .form-actions #submit');

    if(enable) {
        $submitButton.
            removeClass('is-disabled').
            removeProp('disabled');
    }
    else {
        $submitButton.
            addClass('is-disabled').
            prop('disabled', true);
    }
}