2015-10-05 12:25:11 +01:00
|
|
|
refreshQuestion = () ->
|
|
|
|
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']);
|
2015-10-05 12:41:55 +01:00
|
|
|
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);
|
2015-10-05 12:25:11 +01:00
|
|
|
|
2015-10-19 23:33:26 +01:00
|
|
|
updateQuizItemStats = () ->
|
|
|
|
$('#radical-num').text(sessionStorage.getItem('radical_count'));
|
|
|
|
$('#kanji-num').text(sessionStorage.getItem('kanji_count'));
|
|
|
|
$('#vocab-num').text(sessionStorage.getItem('vocabulary_count'));
|
2015-11-17 18:47:00 +00:00
|
|
|
$('#total-num').text(parseInt(sessionStorage.getItem('radical_count')) + parseInt(sessionStorage.getItem('kanji_count'))+ parseInt(sessionStorage.getItem('vocabulary_count')))
|
2015-10-19 23:33:26 +01:00
|
|
|
|
2015-11-21 03:00:17 +00:00
|
|
|
getSrsTypeFilters = () ->
|
|
|
|
srs_levels = ['burnedcheck','enlightencheck','mastercheck','gurucheck','apprenticecheck']
|
|
|
|
return (document.getElementById(s).value for s in srs_levels when document.getElementById(s).checked).toString()
|
|
|
|
|
2015-10-19 23:33:26 +01:00
|
|
|
filterQuestions = () ->
|
2015-11-21 03:00:17 +00:00
|
|
|
sessionStorage.removeItem('user_items')
|
|
|
|
sessionStorage.removeItem('radical_count')
|
|
|
|
sessionStorage.removeItem('kanji_count')
|
|
|
|
sessionStorage.removeItem('vocabulary_count')
|
|
|
|
target_url = "/user_items?level_range=" + $('#filter-input').val() + "&item_state=" + getSrsTypeFilters()
|
|
|
|
$.ajax
|
|
|
|
url: target_url,
|
|
|
|
dataType: 'json',
|
|
|
|
success: (e) ->
|
|
|
|
if(e.hasOwnProperty('error'))
|
|
|
|
document.getElementById('filter-input').value = ''
|
|
|
|
$('#error-text').text(e['error'])
|
|
|
|
$('#error-area').show();
|
|
|
|
filterQuestions()
|
|
|
|
else
|
2015-10-19 23:33:26 +01:00
|
|
|
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'])
|
|
|
|
updateQuizItemStats()
|
|
|
|
refreshQuestion()
|
|
|
|
|
|
|
|
$('#filter-form').submit( (e) ->
|
|
|
|
e.preventDefault()
|
|
|
|
filterQuestions()
|
|
|
|
$('#modal-filter').modal('hide')
|
|
|
|
)
|
|
|
|
|
2015-10-05 12:25:11 +01:00
|
|
|
$(document).keypress (e) ->
|
|
|
|
if(e.which == 13)
|
|
|
|
$("#submit-answer").click();
|
|
|
|
|
|
|
|
$("#submit-answer").click (e) ->
|
|
|
|
e.preventDefault();
|
|
|
|
input_answer = document.getElementById('kana').value;
|
|
|
|
if($("#kana").hasClass("correct"))
|
|
|
|
refreshQuestion();
|
2015-10-05 15:48:57 +01:00
|
|
|
else if($('#answer').text().split(/[,\.]/g).map((x) -> x.trim()).indexOf(input_answer) != -1)
|
2015-10-05 12:25:11 +01:00
|
|
|
$("#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();
|
2015-10-07 18:39:36 +01:00
|
|
|
$("#api-refresh").find('span').addClass('glyphicon-spin');
|
2015-10-06 00:03:07 +01:00
|
|
|
$.post("/refresh").success(() ->
|
2015-10-07 18:39:36 +01:00
|
|
|
refreshQuestion();
|
|
|
|
$("#api-refresh").find('span').removeClass('glyphicon-spin');
|
|
|
|
$('#success-area').show();
|
|
|
|
).fail(() ->
|
|
|
|
$('#error-area').show();
|
2015-10-19 23:33:26 +01:00
|
|
|
$('#error-text').text("Cannot refresh API. Try again later.")
|
2015-10-07 18:39:36 +01:00
|
|
|
$("#api-refresh").find('span').removeClass('glyphicon-spin');
|
|
|
|
);
|
2015-10-05 23:49:59 +01:00
|
|
|
|
|
|
|
$('#error-area').find('button').click () ->
|
|
|
|
$('#error-area').hide()
|
2015-10-05 12:25:11 +01:00
|
|
|
|
2015-10-06 00:03:07 +01:00
|
|
|
$('#success-area').find('button').click () ->
|
|
|
|
$('#success-area').hide()
|
|
|
|
|
2015-10-05 12:25:11 +01:00
|
|
|
$ ->
|
|
|
|
$(document).ready ->
|
|
|
|
$('[data-toggle="tooltip"]').tooltip();
|
2015-10-19 23:33:26 +01:00
|
|
|
filterQuestions()
|