-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
170 lines (145 loc) · 6.79 KB
/
renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
document.addEventListener('DOMContentLoaded', async () => {
let words = await window.api.readWords();
let testStats = {
total: 0,
correct: 0,
incorrect: 0,
wrongWords: {}
};
let currentWordIndex = null;
const updateWordCount = () => {
const wordCount = words.length;
document.getElementById('wordCount').textContent = `V databáze je ${wordCount} slovíčok.`;
};
const playPronunciation = (word) => {
const audio = new Audio(`https://translate.google.com/translate_tts?ie=UTF-8&q=${encodeURIComponent(word)}&tl=en&client=tw-ob`);
audio.play();
};
const refreshWordList = (filteredWords = words) => {
const wordListContent = document.getElementById('wordListContent');
wordListContent.innerHTML = '';
filteredWords.sort((a, b) => a.english.localeCompare(b.english, 'sk'));
filteredWords.forEach((word, index) => {
const li = document.createElement('li');
li.innerHTML = `${word.english} - ${word.slovak}`;
// Tlačidlo na prehratie výslovnosti
const playBtn = document.createElement('span');
playBtn.innerHTML = ' <span style="cursor:pointer; color:blue;">🔊</span>';
playBtn.onclick = () => playPronunciation(word.english);
// Tlačidlo na vymazanie
const deleteBtn = document.createElement('span');
deleteBtn.innerHTML = ' <span style="color:red; font-weight:bold; cursor:pointer;">Vymazať</span>';
deleteBtn.onclick = async () => {
words.splice(index, 1);
await window.api.writeWords(words);
refreshWordList();
updateWordCount();
};
li.appendChild(playBtn);
li.appendChild(deleteBtn);
wordListContent.appendChild(li);
});
};
const displayMessage = (message, type = 'success') => {
const messageBox = document.getElementById('addMessageBox');
messageBox.innerHTML = message;
messageBox.style.color = type === 'success' ? 'green' : 'red';
setTimeout(() => messageBox.innerHTML = '', 3000);
};
document.getElementById('addWordButton').addEventListener('click', async () => {
const english = document.getElementById('newEnglish').value.trim();
const slovak = document.getElementById('newSlovak').value.trim();
if (english && slovak) {
words.push({ english, slovak });
await window.api.writeWords(words);
document.getElementById('newEnglish').value = '';
document.getElementById('newSlovak').value = '';
displayMessage('Slovíčko bolo pridané!');
refreshWordList();
updateWordCount();
} else {
displayMessage('Vyplňte obe polia!', 'error');
}
});
const askNextWord = () => {
if (words.length === 0) return;
currentWordIndex = Math.floor(Math.random() * words.length);
document.getElementById('currentWord').textContent = words[currentWordIndex].slovak;
document.getElementById('answer').value = '';
document.getElementById('answer').focus();
};
const checkAnswer = () => {
const userAnswer = document.getElementById('answer').value.trim().toLowerCase();
const correctAnswer = words[currentWordIndex].english.toLowerCase();
testStats.total++;
const resultBox = document.getElementById('result');
if (userAnswer === correctAnswer) {
testStats.correct++;
resultBox.innerHTML = '<span style="color: green;">Správne!</span>';
} else {
testStats.incorrect++;
testStats.wrongWords[words[currentWordIndex].slovak] =
(testStats.wrongWords[words[currentWordIndex].slovak] || 0) + 1;
resultBox.innerHTML = `<span style="color: red;">Nesprávne! Správna odpoveď: <b>${correctAnswer}</b></span>`;
}
// Prehrať výslovnosť správneho slovíčka
playPronunciation(words[currentWordIndex].english);
setTimeout(() => {
resultBox.innerHTML = '';
askNextWord();
}, 2000);
};
document.getElementById('answer').addEventListener('keypress', (e) => {
if (e.key === 'Enter') checkAnswer();
});
document.getElementById('startTest').addEventListener('click', () => {
if (words.length === 0) {
displayMessage('Najprv pridajte slovíčka!', 'error');
return;
}
testStats = { total: 0, correct: 0, incorrect: 0, wrongWords: {} };
document.getElementById('statsBox').innerHTML = ''; // Schovať štatistiku
askNextWord();
});
document.getElementById('stopTest').addEventListener('click', () => {
const statsBox = document.getElementById('statsBox');
// TOP 5 nesprávnych slovíčok
const sortedWrongWords = Object.entries(testStats.wrongWords)
.sort(([, a], [, b]) => b - a)
.slice(0, 5);
let wrongWordsList = '';
sortedWrongWords.forEach(([slovak, count]) => {
const correctAnswer = words.find(word => word.slovak === slovak)?.english || 'neznámy';
wrongWordsList += `<li>${slovak} - správne: ${correctAnswer} (${count}x)</li>`;
});
statsBox.innerHTML = `
<p>Test ukončený!</p>
<p>Celkový počet otázok: ${testStats.total}</p>
<p>Správne: ${testStats.correct}</p>
<p>Nesprávne: ${testStats.incorrect}</p>
${wrongWordsList ? `<ul>TOP 5 Nesprávne zodpovedané:<br>${wrongWordsList}</ul>` : ''}
`;
});
document.getElementById('exitProgram').addEventListener('click', () => {
window.close();
});
// Opravená funkcia vyhľadávania
document.getElementById('searchWord').addEventListener('input', (e) => {
const query = e.target.value.trim().toLowerCase();
const filteredWords = words.filter(word =>
word.english.toLowerCase().includes(query) || word.slovak.toLowerCase().includes(query)
);
refreshWordList(filteredWords);
});
const toggleWordList = () => {
const wordList = document.getElementById('wordList');
const toggleButtons = document.querySelectorAll('#toggleWords, #toggleWordsBottom');
const isHidden = wordList.style.display === 'none';
wordList.style.display = isHidden ? 'block' : 'none';
toggleButtons.forEach(btn => btn.textContent = isHidden ? 'Skryť slovíčka' : 'Zobraziť slovíčka');
};
document.getElementById('toggleWords').addEventListener('click', toggleWordList);
document.getElementById('toggleWordsBottom').addEventListener('click', toggleWordList);
updateWordCount();
refreshWordList();
});