-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
370 lines (316 loc) · 12 KB
/
Copy pathapp.js
File metadata and controls
370 lines (316 loc) · 12 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
const WORD_CATEGORIES = ["Ah", "Um", "Er", "Well", "So", "Like", "But", "Repeats", "Other"];
let currentCounts = {};
let sessionHistory = [];
let theme = 'dark';
let isSorted = false;
// DOM Elements
const themeSelect = document.getElementById('theme-select');
const speakerInput = document.getElementById('speaker-name');
const roleInput = document.getElementById('speaker-role');
const counterBtns = document.querySelectorAll('.counter-btn');
const saveSpeakerBtn = document.getElementById('save-speaker-btn');
const resetCurrentBtn = document.getElementById('reset-current-btn');
const generateReportBtn = document.getElementById('generate-report-btn');
const exportBtn = document.getElementById('export-btn');
const importFile = document.getElementById('import-file');
const clearHistoryBtn = document.getElementById('clear-history-btn');
const historyList = document.getElementById('history-list');
const sortHistoryBtn = document.getElementById('sort-history-btn');
const reportModal = document.getElementById('report-modal');
const closeModalBtn = document.getElementById('close-modal-btn');
const reportBody = document.getElementById('report-body');
// Initialize
function init() {
// Load local storage
const storedHistory = localStorage.getItem('ahCounterHistory');
if (storedHistory) {
sessionHistory = JSON.parse(storedHistory);
}
const storedTheme = localStorage.getItem('ahCounterTheme');
if (storedTheme) {
theme = storedTheme;
document.body.setAttribute('data-theme', theme);
themeSelect.value = theme;
} else {
// match media
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches) {
theme = 'light';
document.body.setAttribute('data-theme', 'light');
themeSelect.value = 'light';
}
}
const storedCurrent = localStorage.getItem('ahCounterCurrent');
if (storedCurrent) {
const parsed = JSON.parse(storedCurrent);
currentCounts = parsed.counts;
speakerInput.value = parsed.speaker || "";
roleInput.value = parsed.role || "";
} else {
resetCurrentCounts(false);
}
updateCounterUI();
renderHistory();
attachListeners();
}
function attachListeners() {
themeSelect.addEventListener('change', (e) => {
theme = e.target.value;
document.body.setAttribute('data-theme', theme);
localStorage.setItem('ahCounterTheme', theme);
// Update manifest meta theme color if needed
const metaThemeColor = document.querySelector('meta[name="theme-color"]');
if (metaThemeColor) {
if (theme === 'light') metaThemeColor.setAttribute('content', '#f8fafc');
else if (theme === 'tm') metaThemeColor.setAttribute('content', '#f5f5f5');
else metaThemeColor.setAttribute('content', '#121212');
}
});
counterBtns.forEach(btn => {
if (btn.getAttribute('data-has-listeners')) return;
btn.setAttribute('data-has-listeners', 'true');
let pressTimer;
let isLongPress = false;
const startPress = (e) => {
// Prevent context menu on mobile
if (e.type === 'touchstart') e.preventDefault();
isLongPress = false;
pressTimer = setTimeout(() => {
isLongPress = true;
const word = btn.getAttribute('data-word');
if (currentCounts[word] > 0) {
currentCounts[word]--;
saveCurrentState();
updateCounterUI();
if (navigator.vibrate) navigator.vibrate([100, 50, 100]);
}
}, 600);
};
const endPress = (e) => {
clearTimeout(pressTimer);
if (!isLongPress) {
const word = btn.getAttribute('data-word');
currentCounts[word]++;
saveCurrentState();
updateCounterUI();
if (navigator.vibrate) navigator.vibrate(50);
}
isLongPress = false;
};
const cancelPress = () => {
clearTimeout(pressTimer);
isLongPress = false;
};
// Touch events
btn.addEventListener('touchstart', startPress, { passive: false });
btn.addEventListener('touchend', endPress);
btn.addEventListener('touchcancel', cancelPress);
// Mouse events (for desktop)
btn.addEventListener('mousedown', startPress);
btn.addEventListener('mouseup', endPress);
btn.addEventListener('mouseleave', cancelPress);
// Prevent context menu
btn.addEventListener('contextmenu', (e) => e.preventDefault());
});
speakerInput.addEventListener('input', saveCurrentState);
roleInput.addEventListener('input', saveCurrentState);
sortHistoryBtn.addEventListener('click', () => {
isSorted = !isSorted;
sortHistoryBtn.textContent = isSorted ? 'Unsort' : 'Sort';
renderHistory();
});
resetCurrentBtn.addEventListener('click', () => {
if(confirm("Are you sure you want to reset the current counts?")) {
resetCurrentCounts(true);
}
});
saveSpeakerBtn.addEventListener('click', () => {
const name = speakerInput.value.trim();
const role = roleInput.value.trim();
if (!name) {
alert('Please enter a speaker name.');
return;
}
// Check if any counts exist
const total = Object.values(currentCounts).reduce((a, b) => a + b, 0);
if (total === 0) {
if(!confirm("No filler words recorded. Save anyway?")) return;
}
const record = {
id: Date.now(),
speaker: name,
role: role,
counts: { ...currentCounts },
total: total
};
sessionHistory.push(record);
saveHistory();
renderHistory();
resetCurrentCounts(true);
speakerInput.focus();
});
clearHistoryBtn.addEventListener('click', () => {
if(confirm('Are you sure you want to clear all session history?')) {
sessionHistory = [];
saveHistory();
renderHistory();
}
});
exportBtn.addEventListener('click', () => {
const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(sessionHistory, null, 2));
const downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", `ah-counter-session-${new Date().toISOString().split('T')[0]}.json`);
document.body.appendChild(downloadAnchorNode); // required for firefox
downloadAnchorNode.click();
downloadAnchorNode.remove();
});
importFile.addEventListener('change', (e) => {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (event) => {
try {
const importedData = JSON.parse(event.target.result);
if (Array.isArray(importedData)) {
sessionHistory = importedData;
saveHistory();
renderHistory();
alert("Session history imported successfully.");
} else {
alert("Invalid file format.");
}
} catch (err) {
alert("Error reading JSON file.");
}
};
reader.readAsText(file);
// reset input
e.target.value = '';
});
generateReportBtn.addEventListener('click', () => {
generateSummaryReport();
reportModal.classList.remove('hidden');
});
closeModalBtn.addEventListener('click', () => {
reportModal.classList.add('hidden');
});
reportModal.addEventListener('click', (e) => {
if(e.target === reportModal) {
reportModal.classList.add('hidden');
}
});
}
function resetCurrentCounts(clearName = true) {
WORD_CATEGORIES.forEach(w => currentCounts[w] = 0);
if (clearName) {
speakerInput.value = '';
roleInput.value = '';
}
saveCurrentState();
updateCounterUI();
}
function saveCurrentState() {
localStorage.setItem('ahCounterCurrent', JSON.stringify({
speaker: speakerInput.value,
role: roleInput.value,
counts: currentCounts
}));
}
function deleteRecord(id) {
if(confirm("Are you sure you want to delete this speaker entry?")) {
sessionHistory = sessionHistory.filter(r => r.id !== id);
saveHistory();
renderHistory();
}
}
window.deleteRecord = deleteRecord;
function saveHistory() {
localStorage.setItem('ahCounterHistory', JSON.stringify(sessionHistory));
}
function updateCounterUI() {
counterBtns.forEach(btn => {
const word = btn.getAttribute('data-word');
btn.querySelector('.count').textContent = currentCounts[word];
});
}
function renderHistory() {
historyList.innerHTML = '';
if (sessionHistory.length === 0) {
historyList.innerHTML = '<li class="history-item" style="text-align:center; color: var(--text-secondary);">No speakers saved yet.</li>';
return;
}
let displayHistory = sessionHistory.slice();
if (isSorted) {
displayHistory.sort((a, b) => b.total - a.total);
} else {
displayHistory.reverse();
}
displayHistory.forEach(record => {
const li = document.createElement('li');
li.className = 'history-item';
let detailsHtml = '';
let countsEntries = Object.entries(record.counts).filter(([_, count]) => count > 0);
if (isSorted) {
countsEntries.sort((a, b) => b[1] - a[1]);
}
countsEntries.forEach(([word, count]) => {
detailsHtml += `<div class="detail-badge">${word}: <span>${count}</span></div>`;
});
li.innerHTML = `
<div class="history-item-header">
<span class="history-item-name">${record.speaker}</span>
${record.role ? `<span class="history-item-role">(${record.role})</span>` : ''}
<span class="history-item-total">Total: ${record.total}</span>
<button class="delete-entry-btn" onclick="deleteRecord(${record.id})" aria-label="Delete Entry">×</button>
</div>
<div class="history-item-details">
${detailsHtml || '<span style="color:var(--text-secondary)">Perfect! No filler words.</span>'}
</div>
`;
historyList.appendChild(li);
});
}
function generateSummaryReport() {
if (sessionHistory.length === 0) {
reportBody.innerHTML = '<p>No data available for report.</p>';
return;
}
let overallCounts = {};
WORD_CATEGORIES.forEach(w => overallCounts[w] = 0);
let totalFillers = 0;
sessionHistory.forEach(record => {
for(let word in record.counts) {
overallCounts[word] += record.counts[word];
totalFillers += record.counts[word];
}
});
let html = `
<div style="margin-bottom: 1.5rem;">
<strong>Total Speakers:</strong> ${sessionHistory.length} <br>
<strong>Total Filler Words:</strong> ${totalFillers}
</div>
<h3>Overall Breakdown</h3>
<table class="report-table">
<thead>
<tr>
<th>Filler Word</th>
<th>Count</th>
</tr>
</thead>
<tbody>
`;
// Sort categories by count descending
const sortedCategories = WORD_CATEGORIES.map(w => ({word: w, count: overallCounts[w]})).sort((a,b) => b.count - a.count);
sortedCategories.forEach(item => {
html += `
<tr>
<td>${item.word}</td>
<td>${item.count}</td>
</tr>
`;
});
html += `</tbody></table>`;
reportBody.innerHTML = html;
}
// Run init
init();