topic.js
3.87 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
$.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(type, question, tries, progress) {
console.log("new_question " + type);
$("#question_div").html(question);
$("#comments").html("");
$("#solution").html("");
if (type == "info") {
$("#submit").html("Continuar");
}
else {
$("#submit").html("Responder");
}
$("#submit").off();
$("#submit").click(postAnswer);
$("#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
// $("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){
console.log('updateQuestion '+response["method"]);
var method = response["method"];
var params = response["params"];
switch (method) {
case "new_question":
console.log(params["type"]);
new_question(params["type"], params["question"], params["tries"], params["progress"]);
break;
case "try_again":
$('#question_div').animateCSS('shake');
$('#topic_progress').css('width', (100*params["progress"])+'%').attr('aria-valuenow', 100*params["progress"]);
$("#tries").html(params["tries"]);
$('#comments').html(params['comments']);
MathJax.Hub.Queue(["Typeset",MathJax.Hub,"#comments"]);
break;
case "wrong":
$('#question_div').animateCSS('shake');
$('#topic_progress').css('width', (100*params["progress"])+'%').attr('aria-valuenow', 100*params["progress"]);
$("#tries").html(params["tries"]);
$('#comments').html(params['comments']);
MathJax.Hub.Queue(["Typeset", MathJax.Hub, "#comments"]);
$('#solution').html(params['solution']);
MathJax.Hub.Queue(["Typeset", MathJax.Hub, "#solution"]);
$("fieldset").attr("disabled", "disabled");
$("#submit").html("Continuar");
$("#submit").off();
$("#submit").click(getQuestion);
break;
case "finished_topic":
$('#submit').hide(); //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() {
console.log("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 postAnswer() {
console.log("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: updateQuestion,
error: function() {alert("O servidor não responde.");}
});
}
$(document).ready(function() {
getQuestion();
$("#submit").click(postAnswer);
});