latin/app/static/js/cloze.js

83 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-01-07 22:33:50 +01:00
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");
}
});
}