Implemented frontend logic for 'step' and 'answer'.

parent feb84e0e
...@@ -108,13 +108,14 @@ ...@@ -108,13 +108,14 @@
})(math_lines, real_lines)); })(math_lines, real_lines));
} }
setInterval(function() { window.update_math = function() {
if (trigger_update) { if (trigger_update) {
trigger_update = false; trigger_update = false;
//update_math(input_box.getValue());
update_math(input_textarea.val()); update_math(input_textarea.val());
} }
}, 100); };
setInterval(window.update_math, 100);
}); });
var loader = $('#loader'); var loader = $('#loader');
...@@ -127,23 +128,96 @@ ...@@ -127,23 +128,96 @@
loader.hide(); loader.hide();
}; };
window.append_hint = function(hint) {
$('#math div').last().filter('.hint').remove();
var elem = $('<div class=hint/>');
elem.text(hint);
$('#math').append(elem);
};
window.append_input = function(input) {
input_textarea.val(input_textarea.val() + '\n' + input);
};
window.answer_input = function() {
show_loader();
// TODO: disable input box and enable it when this ajax request is done
// (on failure and success).
$.post('/answer', {data: input_textarea.val()}, function(response) {
if (!response)
return;
if ('steps' in response) {
for(i = 0; i < response.steps.length; i++) {
cur = response.steps[i];
if ('step' in cur)
window.append_input(cur.step);
if('hint' in cur)
window.append_hint(cur.hint);
trigger_update = true;
window.update_math();
}
}
if('hint' in response)
window.append_hint(response.hint);
input_textarea.focus();
hide_loader();
}, 'json').error(function(e) {
console.log('error:', e);
hide_loader();
});
};
window.hint_input = function() { window.hint_input = function() {
show_loader(); show_loader();
// TODO: disable input box and enable it when this ajax request is done // TODO: disable input box and enable it when this ajax request is done
// (on failure and success). // (on failure and success).
//$.post('/hint', {data: input_box.getValue()}, function(response) {
$.post('/hint', {data: input_textarea.val()}, function(response) { $.post('/hint', {data: input_textarea.val()}, function(response) {
if (!response) if (!response)
return; return;
window.append_hint(response.hint);
input_textarea.focus();
hide_loader();
}, 'json').error(function(e) {
console.log('error:', e);
hide_loader();
});
};
window.step_input = function() {
show_loader();
// TODO: disable input box and enable it when this ajax request is done
// (on failure and success).
$.post('/step', {data: input_textarea.val()}, function(response) {
if (!response)
return;
console.log(response); console.log(response);
$('#math div').last().filter('.hint').remove(); if ('step' in response) {
window.append_input(response.step);
trigger_update = true;
}
if('hint' in response)
window.append_hint(response.hint);
var elem = $('<div class=hint/>'); input_textarea.focus();
elem.text(response.hint);
$('#math').append(elem);
hide_loader(); hide_loader();
}, 'json').error(function(e) { }, 'json').error(function(e) {
...@@ -158,7 +232,6 @@ ...@@ -158,7 +232,6 @@
// TODO: disable input box and enable it when this ajax request is done // TODO: disable input box and enable it when this ajax request is done
// (on failure and success). // (on failure and success).
//$.post('/validate', {data: input_box.getValue()}, function(response) {
$.post('/validate', {data: input_textarea.val()}, function(response) { $.post('/validate', {data: input_textarea.val()}, function(response) {
if (!response) if (!response)
return; return;
...@@ -166,8 +239,6 @@ ...@@ -166,8 +239,6 @@
var math_container = $('#math'), var math_container = $('#math'),
math_lines = math_container.find('div.box'); math_lines = math_container.find('div.box');
console.log(response.validated);
var i = 0; var i = 0;
// Mark every line as correct (and remove previous class names). // Mark every line as correct (and remove previous class names).
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment