topic.js 3.43 KB
$.fn.extend({
    animateCSS: function (animation) {
        var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
        this.addClass('animated ' + animation).one(animationEnd, function() {
            $(this).removeClass('animated ' + animation);
        });
    }
});


function new_question(question, tries, progress) {
    $("#question_div").html(question);
    $("#comments").html("");
    $("#tries").html(tries);
    $('#topic_progress').css('width', (100*progress)+'%').attr('aria-valuenow', 100*progress);
    MathJax.Hub.Queue(["Typeset",MathJax.Hub,"question_div"]);
    $('#question_div').animateCSS('bounceInDown');

    // enable shift+enter to submit and tab to spaces conversion
    $("input:text, input:radio, input:checkbox").keydown(function (e) {
        if (e.keyCode == 13) {
            e.preventDefault();
            if (e.shiftKey) postQuestion();
            return false;
        }});
}

// updates question according to the response given by the server
function updateQuestion(response){

    switch (response["method"]) {
    case "new_question":
        params = response["params"];
        new_question(params["question"], params["tries"], params["progress"]);
        break;

    case "try_again":
        $('#topic_progress').css('width', (100*response["params"]["progress"])+'%').attr('aria-valuenow', 100*response["params"]["progress"]);
        $('#question_div').animateCSS('shake');
        $('#comments').html(response['params']['comments']);
        $("#tries").html(response["params"]["tries"]);
        MathJax.Hub.Queue(["Typeset",MathJax.Hub,"#comments"]);
        break;

    // case "wrong":
    //     $('#topic_progress').css('width', (100*response["params"]["progress"])+'%').attr('aria-valuenow', 100*response["params"]["progress"]);
    //     $('#question_div').animateCSS('shake');
    //     $('#comments').html(response['params']['comments']);
    //     $("#tries").html(response["params"]["tries"]);
    //     MathJax.Hub.Queue(["Typeset",MathJax.Hub,"#comments"]);

    //     // setTimeout(function(){
    //         new_question(response["params"]["question"], response["params"]["tries"], response["params"]["progress"]);
    //     // }, 5000);
    //     break;



    case "finished_topic":
        $('#submit').css("visibility", "hidden");
        $("#content").html(response["params"]["question"]);
        $('#topic_progress').css('width', '100%').attr('aria-valuenow', 100);
        $("#content").animateCSS('tada');
        setTimeout(function(){ window.location.replace('/'); }, 2000);
        break;
    }
}

// Get current question
function getQuestion() {
    $.ajax({
        type: "GET",
        url: "/question",
        dataType: "json", // expected from server
        success: updateQuestion,
        error: function() {alert("O servidor não responde.");}
    });
}

// Send answer and receive a response.
// The response can be a new_question or a shake if the answer is wrong, which
// is then passed to updateQuestion()
function postQuestion() {
    if (typeof editor === 'object')
        editor.save();

    $.ajax({
        type: "POST",
        url: "/question",
        data: $("#question_form").serialize(), // {'a':10,'b':20},
        dataType: "json", // expected from server
        success: updateQuestion,
        error: function() {alert("O servidor não responde.");}
    });
}

$(document).ready(function() {
    getQuestion();
    $("#submit").click(postQuestion);
});