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

function showTriesLeft(tries) {
    var tries_msg;

    switch (tries) {
    case 0:
        tries_msg = "";
        break;
    case 1:
        tries_msg = "(1 tentativa)";
        break;
    default:
        tries_msg = "(" + tries + " tentativas)";
    }

    $("#tries").html(tries_msg);
}


// Get current question
function getQuestion() {
    $("#submit").addClass("disabled");

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


// updates question according to the response given by the server
function updateQuestion(response) {
    var method = response["method"];
    var params = response["params"];

    $("#right, #wrong").hide();

    switch (method) {
    case "new_question":
        new_question(params["type"], params["question"], params["tries"], params["progress"]);
        break;
    case "finished_topic":
        $('#submit, #comments, #solution').remove();
        $("#content").html(response["params"]["question"]).animateCSS('tada');
        $('#topic_progress').css('width', '100%').attr('aria-valuenow', 100);
        setTimeout(function(){window.location.replace('/');}, 2000);
        break;
    }
}


function new_question(type, question, tries, progress) {
    window.scrollTo(0, 0);
    $("#question_div").html(question).animateCSS('bounceInDown');
    showTriesLeft(tries);
    $("#comments, #solution").html("");
    var btntext = (type == "info") ? "Continuar" : "Responder";
    $("#submit").html(btntext).off().click(postAnswer);
    $('#topic_progress').css('width', (100*progress)+'%').attr('aria-valuenow', 100*progress);
    MathJax.Hub.Queue(["Typeset",MathJax.Hub,"question_div"]);

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


// ===========================================================================
// Send answer and receive a response with the result of the correction.
// The response can be right, try_again or wrong.
// A new question is not loaded. To load questions a GET is required.
function postAnswer() {
    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: getFeedback,
        error: function() {alert("O servidor não responde.");}
    });
}

function getFeedback(response) {
    var method = response["method"];
    var params = response["params"];

    if (params['type'] == "info") {
        getQuestion();
        return;
    }

    switch (method) {
    case "right":
        $('#right').show();
        $('#wrong').hide();
        $('#comments').html(params['comments']);
        MathJax.Hub.Queue(["Typeset", MathJax.Hub, "#comments"]);
        $("#submit").html("Continuar").off().click(getQuestion);
        $("#link_solution_on_right").click(function(){
            $("#right").hide();
            $('#solution').html(params['solution']).animateCSS('flipInX');
            MathJax.Hub.Queue(["Typeset", MathJax.Hub, "#solution"]);
        });
        break;

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

    case "wrong":
        $('#right').hide();
        $('#wrong').show();
        $('#topic_progress').css('width', (100*params["progress"])+'%').attr('aria-valuenow', 100*params["progress"]);
        showTriesLeft(params["tries"]);
        $('#comments').html(params['comments']);
        MathJax.Hub.Queue(["Typeset", MathJax.Hub, "#comments"]);
        $("#link_solution_on_wrong").click(function () {
            $("#wrong").hide();
            $('#solution').html(params['solution']).animateCSS('flipInX');
            MathJax.Hub.Queue(["Typeset", MathJax.Hub, "#solution"]);
        });
        $("fieldset").attr("disabled", "disabled");
        $("#submit").html("Continuar").off().click(getQuestion);
        break;
    }
}

// ===========================================================================
$(document).ready(function() {
    getQuestion();
    $("#submit").click(postAnswer);
});