linearMode = false linearCount = 0 selection = null itemMax = 1 refreshQuestion = () -> if(sessionStorage.getItem('user_items')) items = JSON.parse(sessionStorage.getItem('user_items')) itemMax = items.length if linearMode selection = items[linearCount] linearCount++ refreshQuizCounter() if linearCount >= items.length # Wrap around linearCount = 0 else 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'); $("#question-image").hide() 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']); $("#question-image").show() else $("#question-area").text(selection['question']); document.getElementById('view-on-wk').href ="https://www.wanikani.com/radicals/" + selection['answer'] else if(selection['item_type'] == 'kanji') document.body.style.backgroundColor = "deeppink"; else document.body.style.backgroundColor = "darkviolet"; $("#question-area").text(selection['question']); if(selection['answer_type'] == 'eng') $("#kana").attr("placeholder", "Meaning") wanakana.unbind(input_element); else $("#kana").attr("placeholder", "かな") wanakana.bind(input_element); document.getElementById('view-on-wk').href ="https://www.wanikani.com/" + selection['item_type'] + "/" + selection['question'] updateQuizItemStats = () -> $('#radical-num').text(sessionStorage.getItem('radical_count')); $('#kanji-num').text(sessionStorage.getItem('kanji_count')); $('#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() getItemTypeFilters = () -> item_types = ['radicalcheck', 'kanjicheck', 'vocabcheck'] return (document.getElementById(s).value for s in item_types when document.getElementById(s).checked).toString() filterQuestions = () -> 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() + "&item_types=" + getItemTypeFilters() $.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(); 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() $('#filter-form').submit( (e) -> e.preventDefault() $('#error-area').hide() filterQuestions() $('#modal-filter').modal('hide') linearCount = 0 ) toggleQuizMode = () -> linearMode = !linearMode if linearMode document.getElementById('quiz-counter').style.visibility = "visible" $('#mode-toggle').text("Linear Mode") else linearCount = 0 document.getElementById('quiz-counter').style.visibility = "hidden" $('#mode-toggle').text("Random Mode") refreshQuestion() refreshQuizCounter = () -> document.getElementById('current-quiz').value = linearCount document.getElementById('total-quiz').textContent = "/" + itemMax $(document).keypress (e) -> if(e.which == 13) $("#submit-answer").click(); else if($("#kana").hasClass("wrong")) $("#kana").removeClass("wrong") $("#submit-answer").click (e) -> e.preventDefault(); if($("#kana").hasClass("correct")) refreshQuestion(); else input_answer = document.getElementById('kana').value; expected_answer = selection['answer'].split(/[,\.]/g).map((x) -> x.trim()) if(selection["item_type"] == "radical") expected_answer = expected_answer.map((x) -> x.replace("-", " ")) if(expected_answer.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(); $("#api-refresh").find('span').addClass('glyphicon-spin'); $.post("/refresh").success(() -> refreshQuestion(); $("#api-refresh").find('span').removeClass('glyphicon-spin'); $('#success-area').show(); ).fail(() -> $('#error-area').show(); $('#error-text').text("Cannot refresh API. Try again later.") $("#api-refresh").find('span').removeClass('glyphicon-spin'); ); $('#error-area').find('button').click () -> $('#error-area').hide() $('#success-area').find('button').click () -> $('#success-area').hide() $('#skip-to-item').click (e) -> e.preventDefault() targetValue = document.getElementById('current-quiz').value - 1 if(targetValue >= 0 && targetValue < itemMax) linearCount = targetValue refreshQuestion() else refreshQuizCounter() $ -> $(document).ready -> $('[data-toggle="tooltip"]').tooltip(); document.getElementById('mode-toggle').onclick = toggleQuizMode filterQuestions()