diff --git a/static/quiz_item.coffee b/static/quiz_item.coffee index 6de90f3..78f1211 100644 --- a/static/quiz_item.coffee +++ b/static/quiz_item.coffee @@ -1,18 +1,4 @@ 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)] @@ -45,6 +31,58 @@ refreshQuestion = () -> $("#kana").attr("placeholder", "かな") wanakana.bind(input_element); +updateQuizItemStats = () -> + $('#radical-num').text(sessionStorage.getItem('radical_count')); + $('#kanji-num').text(sessionStorage.getItem('kanji_count')); + $('#vocab-num').text(sessionStorage.getItem('vocabulary_count')); + +filterQuestions = () -> + 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/" + input_data + $.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 + 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() + else + sessionStorage.removeItem('user_items') + sessionStorage.removeItem('radical_count') + sessionStorage.removeItem('kanji_count') + sessionStorage.removeItem('vocabulary_count') + $.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']) + updateQuizItemStats() + refreshQuestion() + +$('#filter-form').submit( (e) -> + e.preventDefault() + filterQuestions() + refreshQuestion() + $('#modal-filter').modal('hide') +) + $(document).keypress (e) -> if(e.which == 13) $("#submit-answer").click(); @@ -72,6 +110,7 @@ $("#api-refresh").click (e) -> $('#success-area').show(); ).fail(() -> $('#error-area').show(); + $('#error-text').text("Cannot refresh API. Try again later.") $("#api-refresh").find('span').removeClass('glyphicon-spin'); ); @@ -84,4 +123,4 @@ $('#success-area').find('button').click () -> $ -> $(document).ready -> $('[data-toggle="tooltip"]').tooltip(); - refreshQuestion(); + filterQuestions() diff --git a/static/quiz_item.js b/static/quiz_item.js index a0f3590..c1fb596 100644 --- a/static/quiz_item.js +++ b/static/quiz_item.js @@ -1,27 +1,9 @@ // Generated by CoffeeScript 1.10.0 (function() { - var refreshQuestion; + var filterQuestions, refreshQuestion, updateQuizItemStats; refreshQuestion = function() { var input_element, items, selection; - if (!sessionStorage.getItem('user_items')) { - $.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']); - return 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)]; @@ -60,6 +42,67 @@ } }; + updateQuizItemStats = function() { + $('#radical-num').text(sessionStorage.getItem('radical_count')); + $('#kanji-num').text(sessionStorage.getItem('kanji_count')); + return $('#vocab-num').text(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/" + 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(); @@ -95,6 +138,7 @@ 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'); }); }); @@ -110,7 +154,7 @@ $(function() { return $(document).ready(function() { $('[data-toggle="tooltip"]').tooltip(); - return refreshQuestion(); + return filterQuestions(); }); }); diff --git a/templates/quiz.html b/templates/quiz.html index ea028e5..120ae93 100644 --- a/templates/quiz.html +++ b/templates/quiz.html @@ -1,5 +1,24 @@ {% extends "layout.html" %} {% block content %} +