From db65a9aa482c183b3c481ff34a9129d9f5d6b32f Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Mon, 5 Oct 2015 12:25:11 +0100 Subject: [PATCH] Moved random selection to client side. Server dumps all possibilities to client rather than a single random item. --- static/quiz_item.coffee | 61 +++++++++++++++++++++++++++++++++++ static/style.css | 4 +++ templates/quiz.html | 71 +++-------------------------------------- wanikaniburned.py | 48 +++++++++++----------------- 4 files changed, 88 insertions(+), 96 deletions(-) create mode 100644 static/quiz_item.coffee diff --git a/static/quiz_item.coffee b/static/quiz_item.coffee new file mode 100644 index 0000000..122280b --- /dev/null +++ b/static/quiz_item.coffee @@ -0,0 +1,61 @@ +refreshQuestion = () -> + if(not sessionStorage.getItem('user_items')) + $.ajax + url: "/user_items", + dataType: 'json', + success: (e) -> + sessionStorage.setItem('user_items', JSON.stringify(e['item_list'])) + 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 + $("#kana").attr("placeholder", "かな") + if(selection['item_type'] == 'kanji') + document.body.style.backgroundColor = "deeppink"; + else + document.body.style.backgroundColor = "darkviolet"; + wanakana.bind(input_element); + $("#question-area").text(selection['question']); + +$(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(); + else if($('#answer').text().replace(/\s/g, '').split(/[,\.]/g).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("{{ url_for('refresh_api') }}"); + +$ -> + $(document).ready -> + $('[data-toggle="tooltip"]').tooltip(); + refreshQuestion(); \ No newline at end of file diff --git a/static/style.css b/static/style.css index 11e830e..1ae8da8 100644 --- a/static/style.css +++ b/static/style.css @@ -37,4 +37,8 @@ body{ .help-size{ font-size: 200%; color: white; +} + +.hidden{ + display: none; } \ No newline at end of file diff --git a/templates/quiz.html b/templates/quiz.html index 155f2b4..6dfaad3 100644 --- a/templates/quiz.html +++ b/templates/quiz.html @@ -18,7 +18,7 @@