-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
207 lines (193 loc) · 6.86 KB
/
script.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
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
function setCookie(name, value, days) {
const date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
const expires = "expires=" + date.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";path=/";
}
function getCookie(name) {
const cookieName = name + "=";
const decodedCookie = decodeURIComponent(document.cookie);
const cookieArray = decodedCookie.split(";");
for (let i = 0; i < cookieArray.length; i++) {
let cookie = cookieArray[i];
while (cookie.charAt(0) == " ") {
cookie = cookie.substring(1);
}
if (cookie.indexOf(cookieName) == 0) {
return cookie.substring(cookieName.length, cookie.length);
}
}
return "";
}
function toggleTheme() {
const body = document.body;
const container = document.getElementById("timingsContainer");
const header = document.getElementById("header");
const themeIcon = document.getElementById("themeIcon");
const currentIcon = themeIcon.innerText;
body.classList.toggle("dark-mode-body");
container.classList.toggle("dark-mode-container");
header.classList.toggle("dark-mode-header");
document.documentElement.classList.toggle("sl-theme-dark");
if (currentIcon === "light_mode") {
themeIcon.innerText = "dark_mode";
setCookie("theme", "dark", 365);
} else {
themeIcon.innerText = "light_mode";
setCookie("theme", "light", 365);
}
}
function setThemeFromCookie() {
const theme = getCookie("theme");
if (theme === "dark") {
toggleTheme();
}
}
setThemeFromCookie();
document.addEventListener("DOMContentLoaded", function () {
const today = new Date();
const day = String(today.getDate()).padStart(2, "0");
const month = String(today.getMonth() + 1).padStart(2, "0"); // January is 0!
const year = today.getFullYear();
const currentDate = `${day}-${month}-${year}`;
const options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
};
const formattedDate = today.toLocaleDateString("id-ID", options);
document.getElementById("date").textContent = formattedDate;
const apiUrl =
"https://api.aladhan.com/v1/timings/${currentDate}?longitude=107.758736&latitude=-6.571589";
fetch(apiUrl)
.then((response) => response.json())
.then((data) => {
const hijriWeekday = data.data.date.hijri.weekday.en;
const hijriDay = data.data.date.hijri.day;
const hijriMonth = data.data.date.hijri.month.en;
const hijriYear = data.data.date.hijri.year;
document.getElementById("hijriDate").textContent =
hijriWeekday + ", " + hijriDay + " " + hijriMonth + " " + hijriYear;
const timings = data.data.timings;
delete timings.Sunset;
const timingsArray = Object.entries(timings);
const translatedNames = {
Lastthird: "Sepertiga Ketiga",
Imsak: "Imsak",
Fajr: "Subuh",
Sunrise: "Terbit (Dhuha)",
Dhuhr: "Dzuhur",
Asr: "Ashar",
Maghrib: "Maghrib",
Isha: "Isya",
Firstthird: "Sepertiga Pertama",
Midnight: "Sepertiga Kedua",
};
timingsArray.sort((a, b) => {
const timeA = getTimeValue(a);
const timeB = getTimeValue(b);
return timeA - timeB;
});
const timingsContainer = document.getElementById("timingsContainer");
timingsArray.forEach((timing) => {
const translatedName = translatedNames[timing[0]] || timing[0];
const card = createCard(translatedName, timing[1]);
timingsContainer.appendChild(card);
});
setInterval(function () {
countdownToNextPrayer(data.data.timings);
}, 500);
setInterval(function () {
updateProgressBar(data.data.timings);
}, 1000);
});
});
function createCard(title, time) {
const card = document.createElement("sl-card");
card.classList.add("card");
card.innerHTML = `
<h3>${title}</h3>
<p>${time}</p>
`;
return card;
}
function getTimeValue(timing) {
const timeName = timing[0];
if (timeName === "Midnight") return 24 * 60;
if (timeName === "Lastthird") return 23 * 60 + 60;
return convertToMinutes(timing[1]);
}
function convertToMinutes(timeString) {
const [hours, minutes] = timeString.split(":").map(Number);
return hours * 60 + minutes;
}
function updateClock() {
const now = new Date();
const hours = String(now.getHours()).padStart(2, "0");
const minutes = String(now.getMinutes()).padStart(2, "0");
const seconds = String(now.getSeconds()).padStart(2, "0");
document.getElementById("current-time").textContent =
`${hours}:${minutes}:${seconds}`;
}
const translatedNames = {
Lastthird: "Sepertiga Ketiga",
Imsak: "Imsak",
Fajr: "Subuh",
Sunrise: "Terbit (Dhuha)",
Dhuhr: "Dzuhur",
Asr: "Ashar",
Maghrib: "Maghrib",
Isha: "Isya",
Firstthird: "Sepertiga Pertama",
Midnight: "Sepertiga Kedua",
};
function countdownToNextPrayer(prayerTimings) {
const now = new Date();
const currentTimeInMinutes = now.getHours() * 60 + now.getMinutes();
let nextPrayerTime;
let nextPrayerName;
// Loop through prayer timings to find the next prayer
for (const [prayerName, prayerTime] of Object.entries(prayerTimings)) {
const prayerTimeInMinutes = convertToMinutes(prayerTime);
if (prayerTimeInMinutes > currentTimeInMinutes) {
nextPrayerTime = prayerTimeInMinutes;
nextPrayerName = prayerName;
break;
}
}
if (nextPrayerTime && nextPrayerName) {
const timeDifference = nextPrayerTime - currentTimeInMinutes;
const hoursRemaining = Math.floor(timeDifference / 60);
const minutesRemaining = timeDifference % 60;
const secondsRemaining = (60 - now.getSeconds()) % 60;
const translatedNextPrayerName =
translatedNames[nextPrayerName] || nextPrayerName;
const countdownText = `${hoursRemaining} jam ${minutesRemaining} menit ${secondsRemaining} detik menuju <b>${translatedNextPrayerName}</b>`;
document.getElementById("countdown").innerHTML = countdownText;
} else {
document.getElementById("countdown").textContent =
"Tidak ada sholat berikutnya hari ini.";
}
}
function updateProgressBar(prayerTimings) {
const now = new Date();
const currentTimeInMinutes = now.getHours() * 60 + now.getMinutes();
let nextPrayerTime;
// Loop through prayer timings to find the next prayer
for (const [, prayerTime] of Object.entries(prayerTimings)) {
const prayerTimeInMinutes = convertToMinutes(prayerTime);
if (prayerTimeInMinutes > currentTimeInMinutes) {
nextPrayerTime = prayerTimeInMinutes;
break;
}
}
if (nextPrayerTime) {
const timeDifference = nextPrayerTime - currentTimeInMinutes;
const progressBarValue = (timeDifference / 60) * 100; // Convert to percentage
document.getElementById("progressBar").value = 100 - progressBarValue;
} else {
document.getElementById("progressBar").value = 100; // Sholat berikutnya belum tersedia, jadi progress bar diatur ke 100%
}
}
setInterval(updateClock, 500);