wanikaniburned/static/quiz_item.coffee

76 lines
2.9 KiB
CoffeeScript
Raw Normal View History

refreshQuestion = () ->
if(not sessionStorage.getItem('user_items'))
$.ajax
url: "/user_items",
dataType: 'json',
success: (e) ->
sessionStorage.setItem('user_items', JSON.stringify(e['item_list']))
sessionStorage.setItem('radical_count', e['radical_count'])
sessionStorage.setItem('kanji_count', e['kanji_count'])
sessionStorage.setItem('vocabulary_count', e['vocabulary_count'])
refreshQuestion()
if($('#radical-num').text() == '')
$('#radical-num').text(sessionStorage.getItem('radical_count'));
$('#kanji-num').text(sessionStorage.getItem('kanji_count'));
$('#vocab-num').text(sessionStorage.getItem('vocabulary_count'));
if(sessionStorage.getItem('user_items'))
items = JSON.parse(sessionStorage.getItem('user_items'))
selection = items[Math.floor(Math.random() * items.length)]
$("#help-area").text('');
$("#kana").removeClass("correct").val('');
$("#question-area").text('');
$("#question-image").attr("src", '');
$('#answer').addClass('hidden');
$('#answer').text(selection['answer']);
input_element = document.getElementById('kana');
if(selection['item_type'] == 'radical')
$("#kana").attr("placeholder", "Meaning")
document.body.style.backgroundColor = "deepskyblue";
wanakana.unbind(input_element);
if(selection['question'].indexOf('http') >= 0)
$("#question-image").attr("src", selection['question']);
else
$("#question-area").text(selection['question']);
else
if(selection['item_type'] == 'kanji')
document.body.style.backgroundColor = "deeppink";
else
document.body.style.backgroundColor = "darkviolet";
$("#question-area").text(selection['question']);
if(Math.random() >= 0.5) # Random chance of asking for the kana or meaning
$("#kana").attr("placeholder", "Meaning")
$('#answer').text(selection['answer_meaning']);
wanakana.unbind(input_element);
else
$("#kana").attr("placeholder", "かな")
wanakana.bind(input_element);
$(document).keypress (e) ->
if(e.which == 13)
$("#submit-answer").click();
$("#submit-answer").click (e) ->
e.preventDefault();
answers = $('#answer').text().split(/[,\.]/g);
answers = $.map(answers, (x) -> )
input_answer = document.getElementById('kana').value;
if($("#kana").hasClass("correct"))
refreshQuestion();
else if($('#answer').text().split(/[,\.]/g).map((x) -> x.trim()).indexOf(input_answer) != -1)
$("#kana").addClass("correct").removeClass("wrong");
else
$("#kana").addClass("wrong").removeClass("correct").val('')
$("#get-help").click (e) ->
e.preventDefault();
$("#answer").removeClass('hidden');
$("#api-refresh").click (e) ->
e.preventDefault();
$.post("/refresh");
2015-10-05 13:26:13 +01:00
refreshQuestion()
$ ->
$(document).ready ->
$('[data-toggle="tooltip"]').tooltip();
refreshQuestion();