diff --git a/app/static/js/cloze.js b/app/static/js/cloze.js new file mode 100644 index 0000000..de8dc98 --- /dev/null +++ b/app/static/js/cloze.js @@ -0,0 +1,83 @@ +function adjust_size(divclass) { + const cloze = document.querySelector(divclass); + const inputs = cloze.querySelectorAll('input'); + inputs.forEach((input) => { + if (input.type=="text") { + input.setAttribute("value", ""); + input.setAttribute("style", "width: " + input.dataset.expected.length + "em"); + } + }); +} + +function nomacron_to_macron(char) { + var mapper = { 'A': 'Ā', + 'a': 'ā', + 'E': 'Ē', + 'e': 'ē', + 'I': 'Ī', + 'i': 'ī', + 'O': 'Ō', + 'o': 'ō', + 'U': 'Ū', + 'u': 'ū'}; + return mapper[char[0]]; +} + +function macron_to_nomacron(char) { + var mapper = { 'Ā': 'A', + 'ā': 'a', + 'Ē': 'E', + 'ē': 'e', + 'Ī': 'I', + 'ī': 'i', + 'Ō': 'O', + 'ō': 'o', + 'Ū': 'U', + 'ū': 'u'}; + return mapper[char]; +} + +function to_nonmacron(input) { + return input.replace(/Ā|ā|Ē|ē|Ī|ī/, macron_to_nomacron); +} + +function to_macron(input) { + return input.replace(/(\w)\1/, nomacron_to_macron); +} + + +function validate(divclass) { + const pensum = document.querySelector(divclass); + const inputs = pensum.querySelectorAll('input'); + + inputs.forEach((input) => { + if (input.type == "text") { + is_nonmacron_ok = false; + is_macron_ok = false; + if (document.getElementById('vowel_length').checked == false) { + is_nonmacron_ok = (to_nonmacron(input.value) == to_nonmacron(input.dataset.expected)); + } else { + is_macron_ok = (input.value == input.dataset.expected); + } + if (is_nonmacron_ok || is_macron_ok) { + input.setAttribute("class", "correct"); + } else { + input.setAttribute("class", "incorrect"); + } + } + }); + + return false; +} + +function show_answers(divclass) { + const cloze = document.querySelector(divclass); + const inputs = cloze.querySelectorAll('input'); + + inputs.forEach((input) => { + if (input.type=="text") { + input.value = input.dataset.expected; + input.removeAttribute("class"); + } + }); +} \ No newline at end of file diff --git a/app/templates/Exercitium_cloze.html b/app/templates/Exercitium_cloze.html index 178435f..a41b0c6 100644 --- a/app/templates/Exercitium_cloze.html +++ b/app/templates/Exercitium_cloze.html @@ -5,91 +5,7 @@
{{exercitium_content|safe}}