165 lines
5.7 KiB
JavaScript
165 lines
5.7 KiB
JavaScript
// Generated by CoffeeScript 1.10.0
|
|
(function() {
|
|
var filterQuestions, refreshQuestion, updateQuizItemStats;
|
|
|
|
refreshQuestion = function() {
|
|
var input_element, items, selection;
|
|
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) {
|
|
return $("#question-image").attr("src", selection['question']);
|
|
} else {
|
|
return $("#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) {
|
|
$("#kana").attr("placeholder", "Meaning");
|
|
$('#answer').text(selection['answer_meaning']);
|
|
return wanakana.unbind(input_element);
|
|
} else {
|
|
$("#kana").attr("placeholder", "かな");
|
|
return wanakana.bind(input_element);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
updateQuizItemStats = function() {
|
|
$('#radical-num').text(sessionStorage.getItem('radical_count'));
|
|
$('#kanji-num').text(sessionStorage.getItem('kanji_count'));
|
|
$('#vocab-num').text(sessionStorage.getItem('vocabulary_count'));
|
|
return $('#total-num').text(parseInt(sessionStorage.getItem('radical_count')) + parseInt(sessionStorage.getItem('kanji_count')) + parseInt(sessionStorage.getItem('vocabulary_count')));
|
|
};
|
|
|
|
filterQuestions = function() {
|
|
var input_data, target_url;
|
|
input_data = $('#filter-input').val();
|
|
if (input_data) {
|
|
sessionStorage.removeItem('user_items');
|
|
sessionStorage.removeItem('radical_count');
|
|
sessionStorage.removeItem('kanji_count');
|
|
sessionStorage.removeItem('vocabulary_count');
|
|
target_url = "/user_items?level_range=" + input_data;
|
|
return $.ajax({
|
|
url: target_url,
|
|
dataType: 'json',
|
|
success: function(e) {
|
|
if (e.hasOwnProperty('error')) {
|
|
document.getElementById('filter-input').value = '';
|
|
$('#error-text').text(e['error']);
|
|
$('#error-area').show();
|
|
return filterQuestions();
|
|
} else {
|
|
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();
|
|
return refreshQuestion();
|
|
}
|
|
}
|
|
});
|
|
} else {
|
|
sessionStorage.removeItem('user_items');
|
|
sessionStorage.removeItem('radical_count');
|
|
sessionStorage.removeItem('kanji_count');
|
|
sessionStorage.removeItem('vocabulary_count');
|
|
return $.ajax({
|
|
url: "/user_items",
|
|
dataType: 'json',
|
|
success: function(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']);
|
|
updateQuizItemStats();
|
|
return refreshQuestion();
|
|
}
|
|
});
|
|
}
|
|
};
|
|
|
|
$('#filter-form').submit(function(e) {
|
|
e.preventDefault();
|
|
filterQuestions();
|
|
refreshQuestion();
|
|
return $('#modal-filter').modal('hide');
|
|
});
|
|
|
|
$(document).keypress(function(e) {
|
|
if (e.which === 13) {
|
|
return $("#submit-answer").click();
|
|
}
|
|
});
|
|
|
|
$("#submit-answer").click(function(e) {
|
|
var input_answer;
|
|
e.preventDefault();
|
|
input_answer = document.getElementById('kana').value;
|
|
if ($("#kana").hasClass("correct")) {
|
|
return refreshQuestion();
|
|
} else if ($('#answer').text().split(/[,\.]/g).map(function(x) {
|
|
return x.trim();
|
|
}).indexOf(input_answer) !== -1) {
|
|
return $("#kana").addClass("correct").removeClass("wrong");
|
|
} else {
|
|
return $("#kana").addClass("wrong").removeClass("correct").val('');
|
|
}
|
|
});
|
|
|
|
$("#get-help").click(function(e) {
|
|
e.preventDefault();
|
|
return $("#answer").removeClass('hidden');
|
|
});
|
|
|
|
$("#api-refresh").click(function(e) {
|
|
e.preventDefault();
|
|
$("#api-refresh").find('span').addClass('glyphicon-spin');
|
|
return $.post("/refresh").success(function() {
|
|
refreshQuestion();
|
|
$("#api-refresh").find('span').removeClass('glyphicon-spin');
|
|
return $('#success-area').show();
|
|
}).fail(function() {
|
|
$('#error-area').show();
|
|
$('#error-text').text("Cannot refresh API. Try again later.");
|
|
return $("#api-refresh").find('span').removeClass('glyphicon-spin');
|
|
});
|
|
});
|
|
|
|
$('#error-area').find('button').click(function() {
|
|
return $('#error-area').hide();
|
|
});
|
|
|
|
$('#success-area').find('button').click(function() {
|
|
return $('#success-area').hide();
|
|
});
|
|
|
|
$(function() {
|
|
return $(document).ready(function() {
|
|
$('[data-toggle="tooltip"]').tooltip();
|
|
return filterQuestions();
|
|
});
|
|
});
|
|
|
|
}).call(this);
|
|
|
|
//# sourceMappingURL=quiz_item.js.map
|