From 7bf3c5919738767628f874854368988ee49d232b Mon Sep 17 00:00:00 2001 From: wralto-user-1 Date: Sat, 21 Nov 2026 23:00:15 +0100 Subject: [PATCH] Browser-kompatible Version ohne Optional Chaining - Alle ?.-Operatoren durch klassische Null-Checks ersetzt - ES5/ES6 kompatibel - Funktioniert in allen modernen Browsern Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe --- script.js | 970 +++++++++++++++++++++++++++++------------------------- 1 file changed, 518 insertions(+), 452 deletions(-) diff --git a/script.js b/script.js index b2b9695..0d33734 100644 --- a/script.js +++ b/script.js @@ -1,7 +1,7 @@ -// TeeTracker Application mit Firebase -// ====================================== +// TeeTracker Application mit Firebase (Browser-kompatibel) +// ===================================================== -const App = { +var App = { // Initialer Zustand state: { teaTypes: [], @@ -12,235 +12,235 @@ const App = { }, // Initialisierung - init() { + init: function() { this.bindEvents(); this.setupFirebaseAuth(); this.updateDate(); }, // Firebase Auth einrichten - setupFirebaseAuth() { - firebase.auth().onAuthStateChanged((user) => { + setupFirebaseAuth: function() { + var self = this; + firebase.auth().onAuthStateChanged(function(user) { if (user) { - this.state.currentUser = user; - this.loadUserData(); - this.hideAuthSection(); - this.showSignedInUI(); + self.state.currentUser = user; + self.loadUserData(); + self.hideAuthSection(); + self.showSignedInUI(); } else { - this.state.currentUser = null; - this.state.teaTypes = []; - this.state.consumption = []; - this.showAuthSection(); - this.showSignedOutUI(); - this.renderAll(); + self.state.currentUser = null; + self.state.teaTypes = []; + self.state.consumption = []; + self.showAuthSection(); + self.showSignedOutUI(); + self.renderAll(); } }); }, // Benutzerdaten laden - async loadUserData() { + loadUserData: function() { + var self = this; if (!this.state.currentUser) return; - try { - this.state.loading = true; - - // Teesorten laden - const teaTypesSnapshot = await db - .collection(`users/${this.state.currentUser.uid}/teaTypes`) - .orderBy('name') - .get(); - - this.state.teaTypes = teaTypesSnapshot.docs.map(doc => ({ - id: doc.id, - ...doc.data() - })); + this.state.loading = true; - // Konsumdaten laden - const consumptionSnapshot = await db - .collection(`users/${this.state.currentUser.uid}/consumption`) - .orderBy('date', 'desc') - .get(); - - this.state.consumption = consumptionSnapshot.docs.map(doc => ({ - id: doc.id, - ...doc.data() - })); - - this.state.loading = false; - this.renderAll(); - - // Echtzeit-Updates abonnieren - this.setupRealtimeUpdates(); - - } catch (error) { - console.error('Fehler beim Laden der Daten:', error); - this.state.loading = false; - this.showToast('Fehler beim Laden der Daten', 'error'); - } + // Teesorten laden + db.collection('users/' + this.state.currentUser.uid + '/teaTypes') + .orderBy('name') + .get() + .then(function(snapshot) { + self.state.teaTypes = snapshot.docs.map(function(doc) { + return { id: doc.id, name: doc.data().name, color: doc.data().color }; + }); + return db.collection('users/' + self.state.currentUser.uid + '/consumption') + .orderBy('date', 'desc') + .get(); + }) + .then(function(snapshot) { + self.state.consumption = snapshot.docs.map(function(doc) { + return { + id: doc.id, + date: doc.data().date, + teaId: doc.data().teaId, + teaName: doc.data().teaName, + teaColor: doc.data().teaColor, + brewTime: doc.data().brewTime, + amount: doc.data().amount, + notes: doc.data().notes + }; + }); + self.state.loading = false; + self.renderAll(); + self.setupRealtimeUpdates(); + }) + .catch(function(error) { + console.error('Fehler beim Laden der Daten:', error); + self.state.loading = false; + self.showToast('Fehler beim Laden der Daten', 'error'); + }); }, // Echtzeit-Updates einrichten - setupRealtimeUpdates() { + setupRealtimeUpdates: function() { + var self = this; if (!this.state.currentUser) return; // Teesorten-Updates - db.collection(`users/${this.state.currentUser.uid}/teaTypes`) + db.collection('users/' + this.state.currentUser.uid + '/teaTypes') .orderBy('name') - .onSnapshot((snapshot) => { - this.state.teaTypes = snapshot.docs.map(doc => ({ - id: doc.id, - ...doc.data() - })); - this.renderTeaTypes(); - this.renderTrackForm(); + .onSnapshot(function(snapshot) { + self.state.teaTypes = snapshot.docs.map(function(doc) { + return { id: doc.id, name: doc.data().name, color: doc.data().color }; + }); + self.renderTeaTypes(); + self.renderTrackForm(); }); // Konsum-Updates - db.collection(`users/${this.state.currentUser.uid}/consumption`) + db.collection('users/' + this.state.currentUser.uid + '/consumption') .orderBy('date', 'desc') - .onSnapshot((snapshot) => { - this.state.consumption = snapshot.docs.map(doc => ({ - id: doc.id, - ...doc.data() - })); - this.renderHistory(); - this.renderStats(); - this.renderCharts(); + .onSnapshot(function(snapshot) { + self.state.consumption = snapshot.docs.map(function(doc) { + return { + id: doc.id, + date: doc.data().date, + teaId: doc.data().teaId, + teaName: doc.data().teaName, + teaColor: doc.data().teaColor, + brewTime: doc.data().brewTime, + amount: doc.data().amount, + notes: doc.data().notes + }; + }); + self.renderHistory(); + self.renderStats(); + self.renderCharts(); }); }, // Events binden - bindEvents() { + bindEvents: function() { + var self = this; + // Google Sign-In - document.getElementById('googleSignIn')?.addEventListener('click', () => { - this.signInWithGoogle(); - }); + var googleSignIn = document.getElementById('googleSignIn'); + if (googleSignIn) { + googleSignIn.addEventListener('click', function() { + self.signInWithGoogle(); + }); + } // Sign Out - document.getElementById('signOut')?.addEventListener('click', () => { - this.signOut(); - }); + var signOut = document.getElementById('signOut'); + if (signOut) { + signOut.addEventListener('click', function() { + self.signOut(); + }); + } // Formular für neue Teesorte - document.getElementById('addTeaForm')?.addEventListener('submit', (e) => { - e.preventDefault(); - this.addTeaType(); - }); + var addTeaForm = document.getElementById('addTeaForm'); + if (addTeaForm) { + addTeaForm.addEventListener('submit', function(e) { + e.preventDefault(); + self.addTeaType(); + }); + } // Formular für Tracking - document.getElementById('trackForm')?.addEventListener('submit', (e) => { - e.preventDefault(); - this.trackConsumption(); - }); + var trackForm = document.getElementById('trackForm'); + if (trackForm) { + trackForm.addEventListener('submit', function(e) { + e.preventDefault(); + self.trackConsumption(); + }); + } // Filter-Buttons - document.getElementById('filterToday')?.addEventListener('click', () => { - this.setFilter('today'); - }); + var filterToday = document.getElementById('filterToday'); + if (filterToday) { + filterToday.addEventListener('click', function() { + self.setFilter('today'); + }); + } - document.getElementById('filterWeek')?.addEventListener('click', () => { - this.setFilter('week'); - }); + var filterWeek = document.getElementById('filterWeek'); + if (filterWeek) { + filterWeek.addEventListener('click', function() { + self.setFilter('week'); + }); + } - document.getElementById('filterMonth')?.addEventListener('click', () => { - this.setFilter('month'); - }); + var filterMonth = document.getElementById('filterMonth'); + if (filterMonth) { + filterMonth.addEventListener('click', function() { + self.setFilter('month'); + }); + } - document.getElementById('filterAll')?.addEventListener('click', () => { - this.setFilter('all'); - }); + var filterAll = document.getElementById('filterAll'); + if (filterAll) { + filterAll.addEventListener('click', function() { + self.setFilter('all'); + }); + } // Datum auf heute setzen - document.getElementById('trackDate')?.setAttribute('value', new Date().toISOString().split('T')[0]); + var trackDate = document.getElementById('trackDate'); + if (trackDate) { + trackDate.value = new Date().toISOString().split('T')[0]; + } }, // Google Sign-In - signInWithGoogle() { - const provider = new firebase.auth.GoogleAuthProvider(); + signInWithGoogle: function() { + var self = this; + var provider = new firebase.auth.GoogleAuthProvider(); firebase.auth().signInWithPopup(provider) - .then((result) => { - this.showToast('Erfolgreich angemeldet!', 'success'); + .then(function() { + self.showToast('Erfolgreich angemeldet!', 'success'); }) - .catch((error) => { + .catch(function(error) { console.error('Anmeldung fehlgeschlagen:', error); - this.showToast('Anmeldung fehlgeschlagen: ' + error.message, 'error'); + self.showToast('Anmeldung fehlgeschlagen: ' + error.message, 'error'); }); }, // Sign Out - signOut() { + signOut: function() { + var self = this; firebase.auth().signOut() - .then(() => { - this.showToast('Erfolgreich abgemeldet', 'success'); + .then(function() { + self.showToast('Erfolgreich abgemeldet', 'success'); }) - .catch((error) => { + .catch(function(error) { console.error('Abmeldung fehlgeschlagen:', error); - this.showToast('Abmeldung fehlgeschlagen', 'error'); + self.showToast('Abmeldung fehlgeschlagen', 'error'); }); }, - // Auth-Bereich verstecken - hideAuthSection() { - const authSection = document.getElementById('auth-section'); - if (authSection) { - authSection.classList.add('hidden'); - } - }, - - // Auth-Bereich anzeigen - showAuthSection() { - const authSection = document.getElementById('auth-section'); - if (authSection) { - authSection.classList.remove('hidden'); - } - }, - - // Angemeldete UI anzeigen - showSignedInUI() { - const signInBtn = document.getElementById('googleSignIn'); - const signOutBtn = document.getElementById('signOut'); - const authStatus = document.getElementById('auth-status'); - - if (signInBtn) signInBtn.classList.add('hidden'); - if (signOutBtn) signOutBtn.classList.remove('hidden'); - if (authStatus) { - authStatus.innerHTML = `Angemeldet als: ${this.state.currentUser?.displayName || this.state.currentUser?.email}`; - } - }, - - // Abgemeldete UI anzeigen - showSignedOutUI() { - const signInBtn = document.getElementById('googleSignIn'); - const signOutBtn = document.getElementById('signOut'); - const authStatus = document.getElementById('auth-status'); - - if (signInBtn) signInBtn.classList.remove('hidden'); - if (signOutBtn) signOutBtn.classList.add('hidden'); - if (authStatus) { - authStatus.innerHTML = 'Nicht angemeldet'; - } - }, - // Filter setzen - setFilter(filter) { + setFilter: function(filter) { this.state.currentFilter = filter; this.renderHistory(); this.updateFilterButtons(); }, // Filter-Buttons aktualisieren - updateFilterButtons() { - const buttons = [ + updateFilterButtons: function() { + var buttons = [ { id: 'filterToday', filter: 'today' }, { id: 'filterWeek', filter: 'week' }, { id: 'filterMonth', filter: 'month' }, { id: 'filterAll', filter: 'all' } ]; - buttons.forEach(btn => { - const element = document.getElementById(btn.id); + var self = this; + buttons.forEach(function(btn) { + var element = document.getElementById(btn.id); if (element) { - if (this.state.currentFilter === btn.filter) { + if (self.state.currentFilter === btn.filter) { element.classList.add('active'); } else { element.classList.remove('active'); @@ -250,17 +250,17 @@ const App = { }, // Neue Teesorte hinzufügen - async addTeaType() { + addTeaType: function() { if (!this.state.currentUser) { this.showToast('Bitte melden Sie sich zuerst an', 'error'); return; } - const nameInput = document.getElementById('teaName'); - const colorInput = document.getElementById('teaColor'); + var nameInput = document.getElementById('teaName'); + var colorInput = document.getElementById('teaColor'); - const name = nameInput?.value?.trim(); - const color = colorInput?.value || '#4CAF50'; + var name = nameInput ? nameInput.value.trim() : ''; + var color = colorInput ? colorInput.value : '#4CAF50'; if (!name) { this.showToast('Bitte geben Sie einen Namen für die Teesorte ein', 'error'); @@ -268,98 +268,117 @@ const App = { } // Prüfen, ob die Sorte bereits existiert - const exists = this.state.teaTypes.some(t => t.name.toLowerCase() === name.toLowerCase()); + var exists = false; + for (var i = 0; i < this.state.teaTypes.length; i++) { + if (this.state.teaTypes[i].name.toLowerCase() === name.toLowerCase()) { + exists = true; + break; + } + } if (exists) { this.showToast('Diese Teesorte existiert bereits', 'error'); return; } - try { - const docRef = await db - .collection(`users/${this.state.currentUser.uid}/teaTypes`) - .add({ - name: name, - color: color, - createdAt: firebase.firestore.FieldValue.serverTimestamp() - }); - - nameInput.value = ''; - colorInput.value = '#4CAF50'; - this.showToast('Teesorte erfolgreich hinzugefügt', 'success'); - - } catch (error) { - console.error('Fehler beim Hinzufügen der Teesorte:', error); - this.showToast('Fehler beim Hinzufügen der Teesorte', 'error'); - } + var self = this; + db.collection('users/' + this.state.currentUser.uid + '/teaTypes') + .add({ + name: name, + color: color, + createdAt: firebase.firestore.FieldValue.serverTimestamp() + }) + .then(function() { + if (nameInput) nameInput.value = ''; + if (colorInput) colorInput.value = '#4CAF50'; + self.showToast('Teesorte erfolgreich hinzugefügt', 'success'); + }) + .catch(function(error) { + console.error('Fehler beim Hinzufügen der Teesorte:', error); + self.showToast('Fehler beim Hinzufügen der Teesorte', 'error'); + }); }, // Teesorte entfernen - async removeTeaType(teaId) { + removeTeaType: function(teaId) { if (!this.state.currentUser) { this.showToast('Bitte melden Sie sich zuerst an', 'error'); return; } - const teaType = this.state.teaTypes.find(t => t.id === teaId); + var teaType = null; + for (var i = 0; i < this.state.teaTypes.length; i++) { + if (this.state.teaTypes[i].id === teaId) { + teaType = this.state.teaTypes[i]; + break; + } + } if (!teaType) return; // Prüfen, ob die Sorte in Konsumdaten verwendet wird - const isUsed = this.state.consumption.some(c => c.teaId === teaId); - + var isUsed = false; + for (var i = 0; i < this.state.consumption.length; i++) { + if (this.state.consumption[i].teaId === teaId) { + isUsed = true; + break; + } + } + if (isUsed) { if (!confirm('Diese Teesorte wird in Konsumdaten verwendet. Möchten Sie sie trotzdem entfernen?')) { return; } } - try { - // Konsumdaten bereinigen - const consumptionRefs = await db - .collection(`users/${this.state.currentUser.uid}/consumption`) - .where('teaId', '==', teaId) - .get(); + var self = this; + var batch = db.batch(); - const batch = db.batch(); - consumptionRefs.forEach(doc => { - batch.update(doc.ref, { - teaName: '[Gelöschte Sorte]', - teaId: null + // Konsumdaten bereinigen + db.collection('users/' + this.state.currentUser.uid + '/consumption') + .where('teaId', '==', teaId) + .get() + .then(function(snapshot) { + snapshot.forEach(function(doc) { + batch.update(doc.ref, { + teaName: '[Gelöschte Sorte]', + teaId: null + }); }); + + // Teesorte löschen + var teaRef = db.collection('users/' + self.state.currentUser.uid + '/teaTypes').doc(teaId); + batch.delete(teaRef); + + return batch.commit(); + }) + .then(function() { + self.showToast('Teesorte entfernt', 'success'); + }) + .catch(function(error) { + console.error('Fehler beim Entfernen der Teesorte:', error); + self.showToast('Fehler beim Entfernen der Teesorte', 'error'); }); - - // Teesorte löschen - const teaRef = db.collection(`users/${this.state.currentUser.uid}/teaTypes`).doc(teaId); - batch.delete(teaRef); - - await batch.commit(); - this.showToast('Teesorte entfernt', 'success'); - - } catch (error) { - console.error('Fehler beim Entfernen der Teesorte:', error); - this.showToast('Fehler beim Entfernen der Teesorte', 'error'); - } }, // Konsum tracken - async trackConsumption() { + trackConsumption: function() { if (!this.state.currentUser) { this.showToast('Bitte melden Sie sich zuerst an', 'error'); return; } - const dateInput = document.getElementById('trackDate'); - const teaSelect = document.getElementById('trackTea'); - const amountInput = document.getElementById('trackAmount'); - const notesInput = document.getElementById('trackNotes'); + var dateInput = document.getElementById('trackDate'); + var teaSelect = document.getElementById('trackTea'); + var amountInput = document.getElementById('trackAmount'); + var notesInput = document.getElementById('trackNotes'); - const date = dateInput?.value; - const teaId = teaSelect?.value; - const amount = parseInt(amountInput?.value) || 1; - const notes = notesInput?.value?.trim() || ''; + var date = dateInput ? dateInput.value : ''; + var teaId = teaSelect ? teaSelect.value : ''; + var amount = amountInput ? parseInt(amountInput.value) || 1 : 1; + var notes = notesInput ? notesInput.value.trim() : ''; // Brühzeit - const brewTimeRadio = document.querySelector('input[name="brewTime"]:checked'); - const brewTime = brewTimeRadio?.value || 'normal'; + var brewTimeRadio = document.querySelector('input[name="brewTime"]:checked'); + var brewTime = brewTimeRadio ? brewTimeRadio.value : 'normal'; if (!date) { this.showToast('Bitte wählen Sie ein Datum aus', 'error'); @@ -371,43 +390,46 @@ const App = { return; } - const teaType = this.state.teaTypes.find(t => t.id === teaId); + var teaType = null; + for (var i = 0; i < this.state.teaTypes.length; i++) { + if (this.state.teaTypes[i].id === teaId) { + teaType = this.state.teaTypes[i]; + break; + } + } if (!teaType) { this.showToast('Ungültige Teesorte', 'error'); return; } - try { - await db - .collection(`users/${this.state.currentUser.uid}/consumption`) - .add({ - date: date, - teaId: teaId, - teaName: teaType.name, - teaColor: teaType.color, - brewTime: brewTime, - amount: amount, - notes: notes, - createdAt: firebase.firestore.FieldValue.serverTimestamp() - }); - - // Formular zurücksetzen - dateInput.valueAsDate = new Date(); - teaSelect.selectedIndex = 0; - amountInput.value = 1; - notesInput.value = ''; - brewTimeRadio.checked = true; - - this.showToast('Tee-Konsum erfolgreich getrackt!', 'success'); - - } catch (error) { - console.error('Fehler beim Tracken:', error); - this.showToast('Fehler beim Tracken', 'error'); - } + var self = this; + db.collection('users/' + this.state.currentUser.uid + '/consumption') + .add({ + date: date, + teaId: teaId, + teaName: teaType.name, + teaColor: teaType.color, + brewTime: brewTime, + amount: amount, + notes: notes, + createdAt: firebase.firestore.FieldValue.serverTimestamp() + }) + .then(function() { + if (dateInput) dateInput.valueAsDate = new Date(); + if (teaSelect) teaSelect.selectedIndex = 0; + if (amountInput) amountInput.value = 1; + if (notesInput) notesInput.value = ''; + if (brewTimeRadio) brewTimeRadio.checked = true; + self.showToast('Tee-Konsum erfolgreich getrackt!', 'success'); + }) + .catch(function(error) { + console.error('Fehler beim Tracken:', error); + self.showToast('Fehler beim Tracken', 'error'); + }); }, // Konsum-Eintrag entfernen - async removeConsumption(entryId) { + removeConsumption: function(entryId) { if (!this.state.currentUser) { this.showToast('Bitte melden Sie sich zuerst an', 'error'); return; @@ -417,242 +439,230 @@ const App = { return; } - try { - await db - .collection(`users/${this.state.currentUser.uid}/consumption`) - .doc(entryId) - .delete(); - - this.showToast('Eintrag entfernt', 'success'); - - } catch (error) { - console.error('Fehler beim Entfernen des Eintrags:', error); - this.showToast('Fehler beim Entfernen des Eintrags', 'error'); - } - }, - - // Alle Render-Funktionen - renderAll() { - this.renderTeaTypes(); - this.renderTrackForm(); - this.renderHistory(); - this.renderStats(); - this.renderCharts(); + var self = this; + db.collection('users/' + this.state.currentUser.uid + '/consumption') + .doc(entryId) + .delete() + .then(function() { + self.showToast('Eintrag entfernt', 'success'); + }) + .catch(function(error) { + console.error('Fehler beim Entfernen des Eintrags:', error); + self.showToast('Fehler beim Entfernen des Eintrags', 'error'); + }); }, // Teetypen rendern - renderTeaTypes() { - const teaList = document.getElementById('teaList'); + renderTeaTypes: function() { + var teaList = document.getElementById('teaList'); if (!teaList) return; if (this.state.loading) { - teaList.innerHTML = '
⏳

Daten werden geladen...

'; + teaList.innerHTML = '
Loading...

Daten werden geladen...

'; return; } if (this.state.teaTypes.length === 0 && this.state.currentUser) { - teaList.innerHTML = ` -
-
🍃
-

Keine Teesorten hinterlegt

-

Fügen Sie oben eine neue Teesorte hinzu

-
- `; + teaList.innerHTML = '
No Tea

Keine Teesorten hinterlegt

Add a new tea type above

'; return; } if (!this.state.currentUser) { - teaList.innerHTML = ` -
-
🔒
-

Bitte melden Sie sich an, um Ihre Teesorten zu sehen

-
- `; + teaList.innerHTML = '
Lock

Please log in to see your tea types

'; return; } - teaList.innerHTML = this.state.teaTypes.map(tea => ` -
-
-
- ${this.escapeHtml(tea.name)} -
-
- -
-
- `).join(''); + var html = ''; + for (var i = 0; i < this.state.teaTypes.length; i++) { + var tea = this.state.teaTypes[i]; + html += '
' + + '
' + + '
' + + '' + this.escapeHtml(tea.name) + '' + + '
' + + '
' + + '' + + '
' + + '
'; + } + teaList.innerHTML = html; }, // Track-Formular rendern - renderTrackForm() { - const teaSelect = document.getElementById('trackTea'); + renderTrackForm: function() { + var teaSelect = document.getElementById('trackTea'); if (!teaSelect) return; if (!this.state.currentUser) { - teaSelect.innerHTML = ''; + teaSelect.innerHTML = ''; return; } if (this.state.teaTypes.length === 0) { - teaSelect.innerHTML = ''; + teaSelect.innerHTML = ''; return; } - const options = this.state.teaTypes.map(tea => - `` - ); - - // Standard-Option - options.unshift(''); - - teaSelect.innerHTML = options.join(''); + var options = ''; + for (var i = 0; i < this.state.teaTypes.length; i++) { + var tea = this.state.teaTypes[i]; + options += ''; + } + + options = '' + options; + teaSelect.innerHTML = options; }, // Konsum-Historie rendern - renderHistory() { - const historyList = document.getElementById('historyList'); + renderHistory: function() { + var historyList = document.getElementById('historyList'); if (!historyList) return; if (!this.state.currentUser) { - historyList.innerHTML = ` -
-
🔒
-

Bitte melden Sie sich an, um Ihre Historie zu sehen

-
- `; + historyList.innerHTML = '
Lock

Please log in to see your history

'; return; } - let filteredConsumption = [...this.state.consumption]; + var filteredConsumption = this.state.consumption.slice(); // Filter anwenden - const today = new Date().toISOString().split('T')[0]; - const weekAgo = new Date(); + var today = new Date().toISOString().split('T')[0]; + var weekAgo = new Date(); weekAgo.setDate(weekAgo.getDate() - 7); - const weekAgoStr = weekAgo.toISOString().split('T')[0]; - const monthAgo = new Date(); + var weekAgoStr = weekAgo.toISOString().split('T')[0]; + var monthAgo = new Date(); monthAgo.setMonth(monthAgo.getMonth() - 1); - const monthAgoStr = monthAgo.toISOString().split('T')[0]; + var monthAgoStr = monthAgo.toISOString().split('T')[0]; switch (this.state.currentFilter) { case 'today': - filteredConsumption = filteredConsumption.filter(c => c.date === today); + filteredConsumption = filteredConsumption.filter(function(c) { + return c.date === today; + }); break; case 'week': - filteredConsumption = filteredConsumption.filter(c => c.date >= weekAgoStr); + filteredConsumption = filteredConsumption.filter(function(c) { + return c.date >= weekAgoStr; + }); break; case 'month': - filteredConsumption = filteredConsumption.filter(c => c.date >= monthAgoStr); + filteredConsumption = filteredConsumption.filter(function(c) { + return c.date >= monthAgoStr; + }); break; } // Sortieren nach Datum (neueste zuerst) - filteredConsumption.sort((a, b) => new Date(b.date) - new Date(a.date)); + filteredConsumption.sort(function(a, b) { + return new Date(b.date) - new Date(a.date); + }); if (filteredConsumption.length === 0) { - historyList.innerHTML = ` -
-
☕
-

Keine Einträge gefunden

-

Tracken Sie Ihren ersten Tee!

-
- `; + historyList.innerHTML = '
Coffee

No entries found

Track your first tea!

'; return; } - historyList.innerHTML = filteredConsumption.map(entry => { - const date = new Date(entry.date); - const dateStr = date.toLocaleDateString('de-DE', { + var html = ''; + for (var i = 0; i < filteredConsumption.length; i++) { + var entry = filteredConsumption[i]; + var date = new Date(entry.date); + var dateStr = date.toLocaleDateString('de-DE', { weekday: 'short', day: '2-digit', month: '2-digit', year: 'numeric' }); - const brewTimeLabel = this.getBrewTimeLabel(entry.brewTime); - const brewTimeClass = entry.brewTime; + var brewTimeLabel = this.getBrewTimeLabel(entry.brewTime); + var brewTimeClass = entry.brewTime; - return ` -
- -
- -
-
- `; - }).join(''); + html += '
' + + '' + + '
' + + '' + + '
' + + '
'; + } + historyList.innerHTML = html; }, // Statistiken rendern - renderStats() { + renderStats: function() { if (!this.state.currentUser) { - document.getElementById('today-count')?.textContent = '?'; - document.getElementById('month-count')?.textContent = '?'; - document.getElementById('year-count')?.textContent = '?'; - document.getElementById('favorite-tea')?.textContent = '?'; - document.getElementById('favorite-count')?.textContent = '?'; + if (document.getElementById('today-count')) document.getElementById('today-count').textContent = '?'; + if (document.getElementById('month-count')) document.getElementById('month-count').textContent = '?'; + if (document.getElementById('year-count')) document.getElementById('year-count').textContent = '?'; + if (document.getElementById('favorite-tea')) document.getElementById('favorite-tea').textContent = '?'; + if (document.getElementById('favorite-count')) document.getElementById('favorite-count').textContent = '?'; return; } - const today = new Date().toISOString().split('T')[0]; - const currentMonth = new Date().getMonth(); - const currentYear = new Date().getFullYear(); + var today = new Date().toISOString().split('T')[0]; + var currentMonth = new Date().getMonth(); + var currentYear = new Date().getFullYear(); // Heute - const todayConsumption = this.state.consumption.filter(c => c.date === today); - const todayCount = todayConsumption.reduce((sum, c) => sum + c.amount, 0); - document.getElementById('today-count')?.textContent = todayCount; + var todayConsumption = this.state.consumption.filter(function(c) { + return c.date === today; + }); + var todayCount = todayConsumption.reduce(function(sum, c) { + return sum + c.amount; + }, 0); + if (document.getElementById('today-count')) document.getElementById('today-count').textContent = todayCount; // Dieser Monat - const monthConsumption = this.state.consumption.filter(c => { - const entryDate = new Date(c.date); + var monthConsumption = this.state.consumption.filter(function(c) { + var entryDate = new Date(c.date); return entryDate.getMonth() === currentMonth && entryDate.getFullYear() === currentYear; }); - const monthCount = monthConsumption.reduce((sum, c) => sum + c.amount, 0); - document.getElementById('month-count')?.textContent = monthCount; + var monthCount = monthConsumption.reduce(function(sum, c) { + return sum + c.amount; + }, 0); + if (document.getElementById('month-count')) document.getElementById('month-count').textContent = monthCount; // Dieses Jahr - const yearConsumption = this.state.consumption.filter(c => { - const entryDate = new Date(c.date); + var yearConsumption = this.state.consumption.filter(function(c) { + var entryDate = new Date(c.date); return entryDate.getFullYear() === currentYear; }); - const yearCount = yearConsumption.reduce((sum, c) => sum + c.amount, 0); - document.getElementById('year-count')?.textContent = yearCount; + var yearCount = yearConsumption.reduce(function(sum, c) { + return sum + c.amount; + }, 0); + if (document.getElementById('year-count')) document.getElementById('year-count').textContent = yearCount; - // Lieblingssorte (meist konsumiert in diesem Jahr) - const yearTeaCounts = {}; - yearConsumption.forEach(c => { - const teaName = c.teaName || 'Unbekannt'; + // Lieblingssorte + var yearTeaCounts = {}; + yearConsumption.forEach(function(c) { + var teaName = c.teaName || 'Unknown'; yearTeaCounts[teaName] = (yearTeaCounts[teaName] || 0) + c.amount; }); - const sortedTeas = Object.entries(yearTeaCounts).sort((a, b) => b[1] - a[1]); + var sortedTeas = Object.entries(yearTeaCounts).sort(function(a, b) { + return b[1] - a[1]; + }); + if (sortedTeas.length > 0) { - document.getElementById('favorite-tea')?.textContent = this.escapeHtml(sortedTeas[0][0]); - document.getElementById('favorite-count')?.textContent = sortedTeas[0][1]; + if (document.getElementById('favorite-tea')) document.getElementById('favorite-tea').textContent = this.escapeHtml(sortedTeas[0][0]); + if (document.getElementById('favorite-count')) document.getElementById('favorite-count').textContent = sortedTeas[0][1]; } else { - document.getElementById('favorite-tea')?.textContent = '-'; - document.getElementById('favorite-count')?.textContent = '0'; + if (document.getElementById('favorite-tea')) document.getElementById('favorite-tea').textContent = '-'; + if (document.getElementById('favorite-count')) document.getElementById('favorite-count').textContent = '0'; } }, // Charts rendern - renderCharts() { + renderCharts: function() { if (!this.state.currentUser) { - // Charts leeren this.clearCharts(); return; } @@ -661,46 +671,37 @@ const App = { this.renderYearChart(); }, - // Charts leeren - clearCharts() { - ['todayChart', 'monthChart', 'yearChart'].forEach(chartId => { - const ctx = document.getElementById(chartId); - if (ctx && this[chartId]) { - this[chartId].destroy(); - this[chartId] = null; - } - }); - }, - // Heute-Chart - renderTodayChart() { - const today = new Date().toISOString().split('T')[0]; - const todayConsumption = this.state.consumption.filter(c => c.date === today); + renderTodayChart: function() { + var today = new Date().toISOString().split('T')[0]; + var todayConsumption = this.state.consumption.filter(function(c) { + return c.date === today; + }); // Nach Teesorte gruppieren - const teaCounts = {}; - const teaColors = {}; + var teaCounts = {}; + var teaColors = {}; - todayConsumption.forEach(c => { - const teaName = c.teaName || 'Unbekannt'; + todayConsumption.forEach(function(c) { + var teaName = c.teaName || 'Unknown'; teaCounts[teaName] = (teaCounts[teaName] || 0) + c.amount; teaColors[teaName] = c.teaColor || '#CCCCCC'; }); - const teaNames = Object.keys(teaCounts); - const counts = Object.values(teaCounts); - const colors = teaNames.map(name => teaColors[name]); + var teaNames = Object.keys(teaCounts); + var counts = Object.values(teaCounts); + var colors = teaNames.map(function(name) { + return teaColors[name]; + }); - const ctx = document.getElementById('todayChart')?.getContext('2d'); - + var ctx = document.getElementById('todayChart'); if (!ctx) return; - // Zerstören Sie das vorherige Chart, falls vorhanden if (this.todayChart) { this.todayChart.destroy(); } - this.todayChart = new Chart(ctx, { + this.todayChart = new Chart(ctx.getContext('2d'), { type: 'doughnut', data: { labels: teaNames, @@ -729,36 +730,39 @@ const App = { }, // Monat-Chart - renderMonthChart() { - const currentMonth = new Date().getMonth(); - const currentYear = new Date().getFullYear(); + renderMonthChart: function() { + var currentMonth = new Date().getMonth(); + var currentYear = new Date().getFullYear(); - const monthConsumption = this.state.consumption.filter(c => { - const entryDate = new Date(c.date); + var monthConsumption = this.state.consumption.filter(function(c) { + var entryDate = new Date(c.date); return entryDate.getMonth() === currentMonth && entryDate.getFullYear() === currentYear; }); // Nach Tag gruppieren - const dayCounts = {}; - monthConsumption.forEach(c => { - const day = new Date(c.date).getDate(); + var dayCounts = {}; + monthConsumption.forEach(function(c) { + var day = new Date(c.date).getDate(); dayCounts[day] = (dayCounts[day] || 0) + c.amount; }); // Alle Tage des Monats - const daysInMonth = new Date(currentYear, currentMonth + 1, 0).getDate(); - const labels = Array.from({ length: daysInMonth }, (_, i) => (i + 1).toString()); - const data = labels.map(day => dayCounts[parseInt(day)] || 0); + var daysInMonth = new Date(currentYear, currentMonth + 1, 0).getDate(); + var labels = []; + var data = []; + for (var day = 1; day <= daysInMonth; day++) { + labels.push(day.toString()); + data.push(dayCounts[day] || 0); + } - const ctx = document.getElementById('monthChart')?.getContext('2d'); - + var ctx = document.getElementById('monthChart'); if (!ctx) return; if (this.monthChart) { this.monthChart.destroy(); } - this.monthChart = new Chart(ctx, { + this.monthChart = new Chart(ctx.getContext('2d'), { type: 'bar', data: { labels: labels, @@ -791,37 +795,38 @@ const App = { }, // Jahr-Chart - renderYearChart() { - const currentYear = new Date().getFullYear(); + renderYearChart: function() { + var currentYear = new Date().getFullYear(); - const yearConsumption = this.state.consumption.filter(c => { - const entryDate = new Date(c.date); + var yearConsumption = this.state.consumption.filter(function(c) { + var entryDate = new Date(c.date); return entryDate.getFullYear() === currentYear; }); // Nach Teesorte gruppieren - const teaCounts = {}; - const teaColors = {}; + var teaCounts = {}; + var teaColors = {}; - yearConsumption.forEach(c => { - const teaName = c.teaName || 'Unbekannt'; + yearConsumption.forEach(function(c) { + var teaName = c.teaName || 'Unknown'; teaCounts[teaName] = (teaCounts[teaName] || 0) + c.amount; teaColors[teaName] = c.teaColor || '#CCCCCC'; }); - const teaNames = Object.keys(teaCounts); - const counts = Object.values(teaCounts); - const colors = teaNames.map(name => teaColors[name]); + var teaNames = Object.keys(teaCounts); + var counts = Object.values(teaCounts); + var colors = teaNames.map(function(name) { + return teaColors[name]; + }); - const ctx = document.getElementById('yearChart')?.getContext('2d'); - + var ctx = document.getElementById('yearChart'); if (!ctx) return; if (this.yearChart) { this.yearChart.destroy(); } - this.yearChart = new Chart(ctx, { + this.yearChart = new Chart(ctx.getContext('2d'), { type: 'bar', data: { labels: teaNames, @@ -853,9 +858,19 @@ const App = { }); }, + // Charts leeren + clearCharts: function() { + ['todayChart', 'monthChart', 'yearChart'].forEach(function(chartId) { + if (this[chartId]) { + this[chartId].destroy(); + this[chartId] = null; + } + }.bind(this)); + }, + // Brühzeit-Label - getBrewTimeLabel(brewTime) { - const labels = { + getBrewTimeLabel: function(brewTime) { + var labels = { 'kurz': 'Kurz', 'normal': 'Normal', 'lang': 'Lang' @@ -864,37 +879,88 @@ const App = { }, // HTML escape - escapeHtml(text) { + escapeHtml: function(text) { if (!text) return ''; - const div = document.createElement('div'); + var div = document.createElement('div'); div.textContent = text; return div.innerHTML; }, + // Auth-Bereich verstecken + hideAuthSection: function() { + var authSection = document.getElementById('auth-section'); + if (authSection) { + authSection.classList.add('hidden'); + } + }, + + // Auth-Bereich anzeigen + showAuthSection: function() { + var authSection = document.getElementById('auth-section'); + if (authSection) { + authSection.classList.remove('hidden'); + } + }, + + // Angemeldete UI anzeigen + showSignedInUI: function() { + var signInBtn = document.getElementById('googleSignIn'); + var signOutBtn = document.getElementById('signOut'); + var authStatus = document.getElementById('auth-status'); + + if (signInBtn) signInBtn.classList.add('hidden'); + if (signOutBtn) signOutBtn.classList.remove('hidden'); + if (authStatus) { + authStatus.innerHTML = 'Angemeldet als: ' + (this.state.currentUser.displayName || this.state.currentUser.email) + ''; + } + }, + + // Abgemeldete UI anzeigen + showSignedOutUI: function() { + var signInBtn = document.getElementById('googleSignIn'); + var signOutBtn = document.getElementById('signOut'); + var authStatus = document.getElementById('auth-status'); + + if (signInBtn) signInBtn.classList.remove('hidden'); + if (signOutBtn) signOutBtn.classList.add('hidden'); + if (authStatus) { + authStatus.innerHTML = 'Nicht angemeldet'; + } + }, + + // Alle Render-Funktionen + renderAll: function() { + this.renderTeaTypes(); + this.renderTrackForm(); + this.renderHistory(); + this.renderStats(); + this.renderCharts(); + }, + // Toast-Nachricht anzeigen - showToast(message, type = 'success') { + showToast: function(message, type) { // Entferne bestehende Toasts - const existingToast = document.querySelector('.toast'); + var existingToast = document.querySelector('.toast'); if (existingToast) { existingToast.remove(); } - const toast = document.createElement('div'); - toast.className = `toast ${type}`; + var toast = document.createElement('div'); + toast.className = 'toast ' + (type || 'success'); toast.textContent = message; document.body.appendChild(toast); - setTimeout(() => { + setTimeout(function() { toast.style.animation = 'slideIn 0.3s ease-out reverse'; - setTimeout(() => { + setTimeout(function() { toast.remove(); }, 300); }, 3000); }, // Datum aktualisieren (Fußzeile) - updateDate() { - const yearSpan = document.getElementById('current-year'); + updateDate: function() { + var yearSpan = document.getElementById('current-year'); if (yearSpan) { yearSpan.textContent = new Date().getFullYear(); } @@ -902,6 +968,6 @@ const App = { }; // Initialisierung beim Laden der Seite -document.addEventListener('DOMContentLoaded', () => { +document.addEventListener('DOMContentLoaded', function() { App.init(); });