choicetextinput.js 2.98 KB
Newer Older
1 2
(function() {
    var update = function() {
3
        // Whenever a value changes create a new serialized version of this
4 5
        // problem's inputs and set the hidden input field's value to equal it.
        var parent = $(this).closest('section.choicetextinput');
6 7 8 9
        // find the closest parent problems-wrapper and use that as the problem
        // grab the input id from the input
        // real_input is the hidden input field
        var real_input = $('input.choicetextvalue', parent);
10
        var all_inputs = $('input.ctinput', parent);
11
        var user_inputs = {};
12
        $(all_inputs).each(function(index, elt) {
13 14 15 16 17 18
            var node = $(elt);
            var name = node.attr('id');
            var val = node.val();
            var radio_value = node.attr('value');
            var type = node.attr('type');
            var is_checked = node.attr('checked');
19 20
            if (type === 'radio' || type === 'checkbox') {
                if (is_checked === 'checked' || is_checked === 'true') {
21 22 23 24 25 26 27
                    user_inputs[name] = radio_value;
                }
            } else {
                user_inputs[name] = val;
            }
        });
        var val_string = JSON.stringify(user_inputs);
28
        // this is what gets submitted as the answer, we deserialize it later
29 30 31
        real_input.val(val_string);
    };

32
    var check_parent = function(event) {
33 34 35 36 37
        // This looks for the containing choice of a textinput
        // and sets it to be checked.
        var elt = $(event.target);
        var parent_container = elt.closest('section[id^="forinput"]');
        var choice = parent_container.find("input[type='checkbox'], input[type='radio']");
38
        choice.attr('checked', 'checked');
39
        choice.change();
40
        // need to check it then trigger the change event
41 42
    };

43
    var imitate_label = function(event) {
44 45 46 47 48 49
        // This causes a section to check and uncheck
        // a radiobutton/checkbox whenever a user clicks on it
        // If the button/checkbox is disabled, nothing happens
        var elt = $(event.target);
        var parent_container = elt.closest('section[id^="forinput"]');
        var choice = parent_container.find("input[type='checkbox'], input[type='radio']");
50 51 52 53 54
        if (choice.attr('type') === 'radio') {
            choice.attr('checked', 'checked');
        } else {
            if (choice.attr('checked')) {
                choice.prop('checked', false);
55
            } else {
56
                choice.prop('checked', true);
57
            }
58 59 60
        }
        choice.change();
        update();
61 62 63 64 65 66 67 68 69 70
    };
    var choices = $('.mock_label');
    var inputs = $('.choicetextinput .ctinput');
    var text_inputs = $('.choicetextinput .ctinput[type="text"]');
    // update on load
    inputs.each(update);
    // and on every change
    // This allows text inside of choices to behave as if they were part of
    // a label for the choice's button/checkbox
    choices.click(imitate_label);
71
    inputs.bind('change', update);
72 73
    text_inputs.click(check_parent);
}).call(this);