From c5fb8db98cd368b256985d7fca6bf32b2a0f5e3e Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Sat, 21 Nov 2015 03:00:17 +0000 Subject: [PATCH] Added and setup frontend for filtering by srs state. --- static/quiz_item.coffee | 52 +++++++++++----------------- static/quiz_item.js | 75 +++++++++++++++++++---------------------- templates/quiz.html | 7 ++++ wanikaniburned.py | 46 ++++++++++++------------- 4 files changed, 84 insertions(+), 96 deletions(-) diff --git a/static/quiz_item.coffee b/static/quiz_item.coffee index e340672..8a2e037 100644 --- a/static/quiz_item.coffee +++ b/static/quiz_item.coffee @@ -37,39 +37,26 @@ updateQuizItemStats = () -> $('#vocab-num').text(sessionStorage.getItem('vocabulary_count')); $('#total-num').text(parseInt(sessionStorage.getItem('radical_count')) + parseInt(sessionStorage.getItem('kanji_count'))+ parseInt(sessionStorage.getItem('vocabulary_count'))) +getSrsTypeFilters = () -> + srs_levels = ['burnedcheck','enlightencheck','mastercheck','gurucheck','apprenticecheck'] + return (document.getElementById(s).value for s in srs_levels when document.getElementById(s).checked).toString() + 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?level_range=" + 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.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 sessionStorage.setItem('user_items', JSON.stringify(e['item_list'])) sessionStorage.setItem('radical_count', e['radical_count']) sessionStorage.setItem('kanji_count', e['kanji_count']) @@ -80,7 +67,6 @@ filterQuestions = () -> $('#filter-form').submit( (e) -> e.preventDefault() filterQuestions() - refreshQuestion() $('#modal-filter').modal('hide') ) diff --git a/static/quiz_item.js b/static/quiz_item.js index 00d8444..4013ff7 100644 --- a/static/quiz_item.js +++ b/static/quiz_item.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript 1.10.0 (function() { - var filterQuestions, refreshQuestion, updateQuizItemStats; + var filterQuestions, getSrsTypeFilters, refreshQuestion, updateQuizItemStats; refreshQuestion = function() { var input_element, items, selection; @@ -49,43 +49,39 @@ 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(); - } + getSrsTypeFilters = function() { + var s, srs_levels; + srs_levels = ['burnedcheck', 'enlightencheck', 'mastercheck', 'gurucheck', 'apprenticecheck']; + return ((function() { + var i, len, results; + results = []; + for (i = 0, len = srs_levels.length; i < len; i++) { + s = srs_levels[i]; + if (document.getElementById(s).checked) { + results.push(document.getElementById(s).value); } - }); - } 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) { + } + return results; + })()).toString(); + }; + + filterQuestions = function() { + var target_url; + 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(); + 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']); @@ -93,14 +89,13 @@ updateQuizItemStats(); return refreshQuestion(); } - }); - } + } + }); }; $('#filter-form').submit(function(e) { e.preventDefault(); filterQuestions(); - refreshQuestion(); return $('#modal-filter').modal('hide'); }); diff --git a/templates/quiz.html b/templates/quiz.html index 7ef93fb..d14d395 100644 --- a/templates/quiz.html +++ b/templates/quiz.html @@ -1,5 +1,7 @@ {% extends "layout.html" %} {% block content %} +{% set srs_levels = [ ('burned', 'Burned'), ('enlighten', 'Enlightened'), ('master', 'Master'), +('guru', 'Guru'), ('apprentice', 'Apprentice') ] -%}