-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
101 lines (89 loc) · 2.96 KB
/
main.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
var children = [
'Miklós',
'Kornél',
'Vince',
'Fruzsi'
]
var commands = [
'😶 Csukott szájjal egyél!',
'🥘 Egyél!',
'🙃 Fordulj előre!',
'🚶 Gyere vissza!',
'🍽 Hajolj a tányér fölé!',
'😴 Kelj fel az asztalról!',
'🔇 Maradj csöndben!',
'🛑 Ne csináld már!',
'📢 Ne kiabálj!',
'🤏 Ne piszkáld a testvéred!',
'🍞 Nem repül a kenyér!',
'🥤 Tedd le a poharat!',
'🦶 Tedd le a lábad!',
'🪑 Ülj le!'
]
function speak(text) {
var utter = new SpeechSynthesisUtterance(text);
utter.lang = preferredLanguage();
window.speechSynthesis.speak(utter);
}
function fetchData(key) {
var stored = window.localStorage.getItem(`dinnercommander/${key}`);
return stored && JSON.parse(stored);
}
function storeData(key, value) {
window.localStorage.setItem(`dinnercommander/${key}`,
JSON.stringify(value));
}
function populateButtons() {
var labels = children.map(
name => `<label><input type="radio" name="child" ` +
`value="${name}"><span>${name}</span></label>`
).join('');
document.getElementById('labels').innerHTML = labels;
document.querySelector('#labels input').checked = true;
var buttons = commands.map(cmd => `<button>${cmd}</button>`).join('');
document.getElementById('buttons').innerHTML = buttons;
document.querySelectorAll('#buttons button').forEach(
function (btn) {
var command = btn.innerText.
replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, '');
btn.addEventListener('click', function () {
var child =
document.querySelector('#labels input:checked').value;
speak(child + ', ' + command);
});
}
);
}
function configure() {
labelsEditor.value = children.join(' ');
buttonsEditor.value = commands.join('\n');
configWindow.style.visibility = 'visible';
}
function cancelConfig() {
configWindow.style.visibility = 'hidden';
}
function acceptConfig() {
var lang = languageSelector.selectedOptions[0].value;
children = labelsEditor.value.trim().split(/\s+/);
commands = buttonsEditor.value.split('\n').
map(s => s.trim()).filter(s => s != '');
storeData('lang', lang);
storeData('children', children);
storeData('commands', commands);
configWindow.style.visibility = 'hidden';
translatePage();
populateButtons();
}
function help() {
window.location = `${translate('readme')}.html`;
}
document.addEventListener('DOMContentLoaded', function() {
configWindow = document.getElementById('config');
languageSelector = document.getElementById('lang');
labelsEditor = document.getElementById('labels-editor');
buttonsEditor = document.getElementById('buttons-editor');
children = fetchData('children') || children;
commands = fetchData('commands') || commands;
translatePage();
populateButtons();
})