Adhaan/adhan-webapp/static/countdown.js
2025-05-27 11:50:28 +02:00

106 lines
3.3 KiB
JavaScript

function updateCurrentTime() {
const now = new Date();
const timeStr = now.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
document.getElementById("current-time").textContent = timeStr;
// Update datum informatie elke minuut
updateDateInfo();
}
function updateDateInfo() {
fetch('/api/date-info')
.then(response => response.json())
.then(data => {
const gregorianElement = document.querySelector('.gregorian-datum');
const hijriArabicElement = document.querySelector('.hijri-datum-arabic');
const hijriDutchElement = document.querySelector('.hijri-datum-dutch');
if (gregorianElement) gregorianElement.textContent = data.gregorian_full;
if (hijriArabicElement) hijriArabicElement.textContent = data.hijri_arabic;
if (hijriDutchElement) hijriDutchElement.textContent = data.hijri_dutch;
})
.catch(error => {
console.log('Fout bij bijwerken datum:', error);
});
}
// Gebedstijden ophalen uit een globale variabele (wordt in de template gezet)
let prayerTimes = window.prayerTimes || [];
let prayerNames = window.prayerNames || [];
let currentPrayerIndex = 0;
function startCountdowns(times, names) {
prayerTimes = times;
prayerNames = names;
currentPrayerIndex = getNextPrayerIndex();
startCountdown(prayerTimes[currentPrayerIndex], prayerNames[currentPrayerIndex]);
}
function getNextPrayerIndex() {
const now = new Date();
for (let i = 0; i < prayerTimes.length; i++) {
const [h, m] = prayerTimes[i].split(":");
const t = new Date();
t.setHours(parseInt(h));
t.setMinutes(parseInt(m));
t.setSeconds(0);
if (t > now) return i;
}
return 0; // fallback: eerste gebed
}
function startCountdown(targetTimeStr, prayerName) {
const parts = targetTimeStr.split(":");
let target = new Date();
target.setHours(parseInt(parts[0]));
target.setMinutes(parseInt(parts[1]));
target.setSeconds(0);
document.querySelector('.huidig-gebed .naam').textContent = prayerName;
function update() {
const now = new Date();
let diff = Math.floor((target - now) / 1000);
if (diff <= 0) {
// Speel adhaan af via browser
const audio = document.getElementById('adhanAudio');
if (audio) audio.play();
// Ga naar volgende gebed
currentPrayerIndex = (currentPrayerIndex + 1) % prayerTimes.length;
const nextTime = prayerTimes[currentPrayerIndex];
const nextName = prayerNames[currentPrayerIndex];
startCountdown(nextTime, nextName);
return;
}
const h = String(Math.floor(diff / 3600)).padStart(2, '0');
const m = String(Math.floor((diff % 3600) / 60)).padStart(2, '0');
const s = String(diff % 60).padStart(2, '0');
document.getElementById("countdown").textContent = `${h}:${m}:${s}`;
}
update();
window._countdownInterval && clearInterval(window._countdownInterval);
window._countdownInterval = setInterval(update, 1000);
}
function setupThemeToggle() {
const toggleBtn = document.getElementById("themeToggle");
const html = document.documentElement;
function applyTheme(theme) {
html.className = theme;
localStorage.setItem('theme', theme);
}
const saved = localStorage.getItem('theme') || 'light';
applyTheme(saved);
toggleBtn.addEventListener("click", () => {
const current = html.className === 'light' ? 'dark' : 'light';
applyTheme(current);
});
}