Added filtering for item types (radical, kanji & vocab)

This commit is contained in:
neviyn 2015-12-10 15:57:52 +00:00
parent c5fb8db98c
commit a9e48abd96
4 changed files with 67 additions and 37 deletions

View File

@ -41,12 +41,16 @@ getSrsTypeFilters = () ->
srs_levels = ['burnedcheck','enlightencheck','mastercheck','gurucheck','apprenticecheck'] srs_levels = ['burnedcheck','enlightencheck','mastercheck','gurucheck','apprenticecheck']
return (document.getElementById(s).value for s in srs_levels when document.getElementById(s).checked).toString() return (document.getElementById(s).value for s in srs_levels when document.getElementById(s).checked).toString()
getItemTypeFilters = () ->
item_types = ['radicalcheck', 'kanjicheck', 'vocabcheck']
return (document.getElementById(s).value for s in item_types when document.getElementById(s).checked).toString()
filterQuestions = () -> filterQuestions = () ->
sessionStorage.removeItem('user_items') sessionStorage.removeItem('user_items')
sessionStorage.removeItem('radical_count') sessionStorage.removeItem('radical_count')
sessionStorage.removeItem('kanji_count') sessionStorage.removeItem('kanji_count')
sessionStorage.removeItem('vocabulary_count') sessionStorage.removeItem('vocabulary_count')
target_url = "/user_items?level_range=" + $('#filter-input').val() + "&item_state=" + getSrsTypeFilters() target_url = "/user_items?level_range=" + $('#filter-input').val() + "&item_state=" + getSrsTypeFilters() + "&item_types=" + getItemTypeFilters()
$.ajax $.ajax
url: target_url, url: target_url,
dataType: 'json', dataType: 'json',
@ -55,7 +59,6 @@ filterQuestions = () ->
document.getElementById('filter-input').value = '' document.getElementById('filter-input').value = ''
$('#error-text').text(e['error']) $('#error-text').text(e['error'])
$('#error-area').show(); $('#error-area').show();
filterQuestions()
else else
sessionStorage.setItem('user_items', JSON.stringify(e['item_list'])) sessionStorage.setItem('user_items', JSON.stringify(e['item_list']))
sessionStorage.setItem('radical_count', e['radical_count']) sessionStorage.setItem('radical_count', e['radical_count'])

View File

@ -1,6 +1,6 @@
// Generated by CoffeeScript 1.10.0 // Generated by CoffeeScript 1.10.0
(function() { (function() {
var filterQuestions, getSrsTypeFilters, refreshQuestion, updateQuizItemStats; var filterQuestions, getItemTypeFilters, getSrsTypeFilters, refreshQuestion, updateQuizItemStats;
refreshQuestion = function() { refreshQuestion = function() {
var input_element, items, selection; var input_element, items, selection;
@ -65,13 +65,29 @@
})()).toString(); })()).toString();
}; };
getItemTypeFilters = function() {
var item_types, s;
item_types = ['radicalcheck', 'kanjicheck', 'vocabcheck'];
return ((function() {
var i, len, results;
results = [];
for (i = 0, len = item_types.length; i < len; i++) {
s = item_types[i];
if (document.getElementById(s).checked) {
results.push(document.getElementById(s).value);
}
}
return results;
})()).toString();
};
filterQuestions = function() { filterQuestions = function() {
var target_url; var target_url;
sessionStorage.removeItem('user_items'); sessionStorage.removeItem('user_items');
sessionStorage.removeItem('radical_count'); sessionStorage.removeItem('radical_count');
sessionStorage.removeItem('kanji_count'); sessionStorage.removeItem('kanji_count');
sessionStorage.removeItem('vocabulary_count'); sessionStorage.removeItem('vocabulary_count');
target_url = "/user_items?level_range=" + $('#filter-input').val() + "&item_state=" + getSrsTypeFilters(); target_url = "/user_items?level_range=" + $('#filter-input').val() + "&item_state=" + getSrsTypeFilters() + "&item_types=" + getItemTypeFilters();
return $.ajax({ return $.ajax({
url: target_url, url: target_url,
dataType: 'json', dataType: 'json',
@ -79,8 +95,7 @@
if (e.hasOwnProperty('error')) { if (e.hasOwnProperty('error')) {
document.getElementById('filter-input').value = ''; document.getElementById('filter-input').value = '';
$('#error-text').text(e['error']); $('#error-text').text(e['error']);
$('#error-area').show(); return $('#error-area').show();
return filterQuestions();
} else { } else {
sessionStorage.setItem('user_items', JSON.stringify(e['item_list'])); sessionStorage.setItem('user_items', JSON.stringify(e['item_list']));
sessionStorage.setItem('radical_count', e['radical_count']); sessionStorage.setItem('radical_count', e['radical_count']);

View File

@ -1,7 +1,7 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% block content %} {% block content %}
{% set srs_levels = [ ('burned', 'Burned'), ('enlighten', 'Enlightened'), ('master', 'Master'), {% set srs_levels = ['burned', 'enlighten', 'master', 'guru', 'apprentice'] -%}
('guru', 'Guru'), ('apprentice', 'Apprentice') ] -%} {% set item_types = ['radical', 'kanji', 'vocab'] %}
<div class="modal fade" id="modal-filter" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel"> <div class="modal fade" id="modal-filter" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
<div class="modal-dialog modal-sm"> <div class="modal-dialog modal-sm">
<div class="modal-content"> <div class="modal-content">
@ -11,10 +11,16 @@
</div> </div>
<form id="filter-form"> <form id="filter-form">
<div class="modal-body"> <div class="modal-body">
<input type="text" pattern="\d[\d-,]*\d*" class="form-control" id="filter-input" placeholder="e.g. 1-3,5,8"> <label for=filter-input">Level</label><input type="text" pattern="\d[\d-,]*\d*" class="form-control" id="filter-input" placeholder="e.g. 1-3,5,8">
<br /> <br />
{% for srs_tag, srs_text in srs_levels %} <p><strong>Progress</strong></p>
<input type="checkbox" id="{{ srs_tag }}check" name="{{ srs_tag }}" value="{{ srs_tag }}" {% if srs_tag == "burned" %}checked{% endif %}><label for="{{ srs_tag }}check">{{ srs_text }}</label> {% for srs_tag in srs_levels %}
<input type="checkbox" id="{{ srs_tag }}check" name="{{ srs_tag }}" value="{{ srs_tag }}" {% if srs_tag == "burned" %}checked{% endif %}><label for="{{ srs_tag }}check">{{ srs_tag|capitalize }}</label>
<br />
{% endfor %}
<p><strong>Type</strong></p>
{% for item in item_types %}
<input type="checkbox" id="{{ item }}check" name="{{ item }}" value="{{ item }}" checked><label for="{{ item }}check">{{ item|capitalize }}</label>
<br /> <br />
{% endfor %} {% endfor %}
</div> </div>

View File

@ -104,35 +104,38 @@ def parse_range(input_range):
return result return result
def get_items_with_level_restriction(level_range, item_state): def get_items_with_level_restriction(level_range, item_state, item_types):
items = [] items = []
radical_count = 0 radical_count = 0
for item in json.loads(current_user.radicals): if 'radical' in item_types:
if item['user_specific'] and item['user_specific']['srs'] in item_state and item['level'] in level_range: for item in json.loads(current_user.radicals):
radical_count += 1 if item['user_specific'] and item['user_specific']['srs'] in item_state and item['level'] in level_range:
if item['image']: radical_count += 1
items.append({'item_type': 'radical', 'question': item['image'], 'answer': item['meaning']}) if item['image']:
else: items.append({'item_type': 'radical', 'question': item['image'], 'answer': item['meaning']})
items.append({'item_type': 'radical', 'question': item['character'], 'answer': item['meaning']}) else:
items.append({'item_type': 'radical', 'question': item['character'], 'answer': item['meaning']})
kanji_count = 0 kanji_count = 0
for item in json.loads(current_user.kanji): if 'kanji' in item_types:
made_answer = "" for item in json.loads(current_user.kanji):
if item['user_specific'] and item['user_specific']['srs'] in item_state and item['level'] in level_range: made_answer = ""
kanji_count += 1 if item['user_specific'] and item['user_specific']['srs'] in item_state and item['level'] in level_range:
if item['onyomi'] and item['kunyomi']: kanji_count += 1
made_answer = item['onyomi'] + ',' + item['kunyomi'].replace('.*', '') if item['onyomi'] and item['kunyomi']:
elif item['onyomi']: made_answer = item['onyomi'] + ',' + item['kunyomi'].replace('.*', '')
made_answer = item['onyomi'] elif item['onyomi']:
elif item['kunyomi']: made_answer = item['onyomi']
made_answer = item['kunyomi'].replace('.*', '') elif item['kunyomi']:
items.append({'item_type': 'kanji', 'question': item['character'], 'answer': made_answer, made_answer = item['kunyomi'].replace('.*', '')
'answer_meaning': item['meaning']}) items.append({'item_type': 'kanji', 'question': item['character'], 'answer': made_answer,
'answer_meaning': item['meaning']})
vocabulary_count = 0 vocabulary_count = 0
for item in json.loads(current_user.vocabulary): if 'vocab' in item_types:
if item['user_specific'] and item['user_specific']['srs'] in item_state and item['level'] in level_range: for item in json.loads(current_user.vocabulary):
vocabulary_count += 1 if item['user_specific'] and item['user_specific']['srs'] in item_state and item['level'] in level_range:
items.append({'item_type': 'vocabulary', 'question': item['character'], 'answer': item['kana'], vocabulary_count += 1
'answer_meaning': item['meaning']}) items.append({'item_type': 'vocabulary', 'question': item['character'], 'answer': item['kana'],
'answer_meaning': item['meaning']})
if not items: if not items:
return flask.jsonify(error="No items within these filter parameters") return flask.jsonify(error="No items within these filter parameters")
else: else:
@ -187,7 +190,10 @@ def get_items():
item_state = ['burned'] item_state = ['burned']
if flask.request.args.get('item_state') is not '': if flask.request.args.get('item_state') is not '':
item_state = flask.request.args.get('item_state').split(',') item_state = flask.request.args.get('item_state').split(',')
return get_items_with_level_restriction(level_range, item_state) item_types = ['radical', 'kanji', 'vocab']
if flask.request.args.get('item_types') is not '':
item_types = flask.request.args.get('item_types').split(',')
return get_items_with_level_restriction(level_range, item_state, item_types)
@app.route('/refresh', methods=['POST']) @app.route('/refresh', methods=['POST'])