-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
211 lines (189 loc) · 5.88 KB
/
script.js
File metadata and controls
211 lines (189 loc) · 5.88 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
let count = 0;
let alphabetIndex = 0;
let alphabetMode = false;
const countDisplay = document.getElementById("count");
const incBtn = document.getElementById("inc");
const decBtn = document.getElementById("dec");
const resetBtn = document.getElementById("reset");
const stepSelect = document.getElementById("step");
const alphabetBtn = document.getElementById("alp");
const controlDiv = document.querySelector(".control");
const tabCounter = document.getElementById("tab-counter");
const tabColors = document.getElementById("tab-colors");
const screenCounter = document.getElementById("screen-counter");
const screenColors = document.getElementById("screen-colors");
const colorTiles = document.querySelectorAll(".color-tile");
const languageSelect = document.getElementById("language");
const stepLabel = document.getElementById("label-step");
const languageLabel = document.getElementById("label-language");
const colorsTitle = document.getElementById("title-colors");
const translations = {
en: {
tabCounter: "Counter",
tabColors: "Colors",
stepLabel: "Increment amount:",
decrease: "Decrease",
increase: "Increase",
reset: "Reset",
alphabet: "Alphabet",
colorsTitle: "Colors",
languageLabel: "Language",
colors: {
red: "Red",
orange: "Orange",
yellow: "Yellow",
green: "Green",
blue: "Blue",
indigo: "Indigo",
violet: "Violet",
},
},
// no: {
// tabCounter: "Teller",
// tabColors: "Farger",
// stepLabel: "Øk med:",
// decrease: "Mindre",
// increase: "Mer",
// reset: "Nullstill",
// alphabet: "Alfabet",
// colorsTitle: "Farger",
// languageLabel: "Språk",
// colors: {
// red: "Rød",
// orange: "Oransje",
// yellow: "Gul",
// green: "Grønn",
// blue: "Blå",
// indigo: "Indigo",
// violet: "Fiolett",
// },
// },
};
let currentLang = "en";
function applyLanguage(lang) {
const t = translations[lang] || translations.en;
currentLang = lang;
if (tabCounter) tabCounter.textContent = t.tabCounter;
if (tabColors) tabColors.textContent = t.tabColors;
if (stepLabel) stepLabel.textContent = t.stepLabel;
if (decBtn) decBtn.textContent = t.decrease;
if (incBtn) incBtn.textContent = t.increase;
if (resetBtn) resetBtn.textContent = t.reset;
if (alphabetBtn) alphabetBtn.textContent = t.alphabet;
if (colorsTitle) colorsTitle.textContent = t.colorsTitle;
if (languageLabel) languageLabel.textContent = t.languageLabel;
colorTiles.forEach((tile) => {
const key = tile.dataset.colorKey;
if (t.colors[key]) {
tile.textContent = t.colors[key];
}
});
}
function setActiveTab(tabName) {
const isCounter = tabName === "counter";
tabCounter.classList.toggle("active", isCounter);
tabCounter.setAttribute("aria-selected", String(isCounter));
tabColors.classList.toggle("active", !isCounter);
tabColors.setAttribute("aria-selected", String(!isCounter));
screenCounter.classList.toggle("active", isCounter);
screenCounter.setAttribute("aria-hidden", String(!isCounter));
screenColors.classList.toggle("active", !isCounter);
screenColors.setAttribute("aria-hidden", String(isCounter));
}
function speak(text) {
window.speechSynthesis.cancel();
const utterance = new SpeechSynthesisUtterance(String(text).toLowerCase()); // 👈 lowercases before speaking
utterance.lang = currentLang === "no" ? "nb-NO" : "en-US";
window.speechSynthesis.speak(utterance);
}
function updateDisplay() {
if (!countDisplay) return;
countDisplay.textContent = count;
}
function getStep() {
if (!stepSelect) return 1;
const step = parseInt(stepSelect.value, 10);
return Number.isNaN(step) ? 1 : step;
}
function toLetter(index) {
// Wraps around A-Z
return String.fromCharCode(65 + (index % 26 + 26) % 26);
}
tabCounter.addEventListener("click", () => setActiveTab("counter"));
tabColors.addEventListener("click", () => setActiveTab("colors"));
colorTiles.forEach((tile) => {
tile.addEventListener("click", () => {
const key = tile.dataset.colorKey;
const name = translations[currentLang].colors[key] || tile.textContent;
speak(name);
});
});
if (languageSelect) {
languageSelect.addEventListener("change", (event) => {
applyLanguage(event.target.value);
});
applyLanguage(languageSelect.value);
} else {
applyLanguage("en");
}
if (incBtn) {
incBtn.addEventListener("click", () => {
if (alphabetMode) {
alphabetIndex = (alphabetIndex + 1) % 26;
const letter = toLetter(alphabetIndex);
if (countDisplay) countDisplay.textContent = letter;
speak(letter);
} else {
count += getStep();
updateDisplay();
speak(count);
}
});
}
if (decBtn) {
decBtn.addEventListener("click", () => {
if (alphabetMode) {
alphabetIndex = (alphabetIndex - 1 + 26) % 26;
const letter = toLetter(alphabetIndex);
if (countDisplay) countDisplay.textContent = letter;
speak(letter);
} else {
count -= getStep();
updateDisplay();
speak(count);
}
});
}
if (resetBtn) {
resetBtn.addEventListener("click", () => {
if (alphabetMode) {
alphabetIndex = 0;
if (countDisplay) countDisplay.textContent = "A";
speak("A");
} else {
count = 0;
updateDisplay();
speak("zero");
}
});
}
if (alphabetBtn) {
alphabetBtn.addEventListener("click", () => {
alphabetMode = !alphabetMode;
if (alphabetMode) {
// Switch to alphabet mode
if (countDisplay) countDisplay.textContent = "A";
alphabetIndex = 0;
if (controlDiv) controlDiv.style.display = "none";
count = 0;
alphabetBtn.textContent = "Number Mode";
speak("A");
} else {
// Switch back to number mode
if (controlDiv) controlDiv.style.display = "flex";
if (countDisplay) countDisplay.textContent = count;
alphabetBtn.textContent = "Alphabet Mode";
speak(count);
}
});
}