Repository navigation
Expand file tree
/
Copy pathsalah.js
More file actions
542 lines (459 loc) · 25.5 KB
/
Copy pathsalah.js
File metadata and controls
542 lines (459 loc) · 25.5 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
document.addEventListener('DOMContentLoaded', function() {
const hamburger = document.getElementById('hamburger');
const navLinks = document.getElementById('navLinks');
const scrollTopBtn = document.getElementById('scrollTop');
const notification = document.getElementById('notification');
const errorMessage = document.getElementById('errorMessage');
const retryErrorButton = document.getElementById('retryErrorButton');
const prayerTimesSection = document.getElementById('prayer-times-section');
const sunriseTime = document.getElementById('sunriseTime');
const fajrTime = document.getElementById('fajrTime');
const dhuhrTime = document.getElementById('dhuhrTime');
const asrTime = document.getElementById('asrTime');
const maghribTime = document.getElementById('maghribTime');
const ishaTime = document.getElementById('ishaTime');
const nextPrayerInfo = document.getElementById('nextPrayerInfo');
const remainingTime = document.getElementById('remainingTime');
const compassStatus = document.getElementById('compassStatus');
const qiblaDegree = document.getElementById('qiblaDegree');
const compassAccuracy = document.getElementById('compassAccuracy');
const startCompass = document.getElementById('startCompass');
const stopCompass = document.getElementById('stopCompass');
const compassArrow = document.querySelector('.compass-arrow');
const compassMarkers = document.getElementById('compassMarkers');
const prayerAlert = document.getElementById('prayerAlert');
const alertPrayerName = document.getElementById('alertPrayerName');
const athanAudio = document.getElementById('athanAudio');
const stopAthan = document.getElementById('stopAthan');
const closeAlert = document.getElementById('closeAlert');
const athanButtons = document.querySelectorAll('.athan-btn');
let prayerTimesData = {};
let nextPrayerTimer = null;
let compassWatchId = null;
let userLocation = {};
let activeAlarms = {
fajr: false,
dhuhr: false,
asr: false,
maghrib: false,
isha: false
};
// التحقق مما إذا كان الجهاز محمولاً
function isMobileDevice() {
return (typeof window.orientation !== "undefined") || (navigator.userAgent.indexOf('IEMobile') !== -1);
};
initPage();
function initPage() {
hamburger.addEventListener('click', toggleMenu);
scrollTopBtn.addEventListener('click', scrollToTop);
window.addEventListener('scroll', toggleScrollTopButton);
// إخفاء أزرار البوصلة إذا كان الجهاز ليس محمولاً
if (!isMobileDevice()) {
startCompass.style.display = 'none';
stopCompass.style.display = 'none';
compassStatus.textContent = "هذه الميزة متاحة فقط على الأجهزة المحمولة";
} else {
startCompass.addEventListener('click', startCompassFunction);
stopCompass.addEventListener('click', stopCompassFunction);
}
stopAthan.addEventListener('click', stopAthanFunction);
closeAlert.addEventListener('click', closeAlertFunction);
retryErrorButton.addEventListener('click', fetchPrayerTimes);
athanButtons.forEach(btn => {
btn.addEventListener('click', function() {
const prayer = this.getAttribute('data-prayer');
toggleAthanAlarm(prayer);
});
});
createCompassMarkers();
fetchPrayerTimes();
loadAlarms();
// تحديث السنة الحالية
document.getElementById('current-year').textContent = new Date().getFullYear();
}
function createCompassMarkers() {
// إضافة علامات الاتجاهات على البوصلة
for (let i = 0; i < 360; i += 15) {
const marker = document.createElement('div');
marker.className = 'compass-marker';
if (i % 90 === 0) {
marker.className += ' main';
const directionText = document.createElement('div');
directionText.style.position = 'absolute';
directionText.style.left = '50%';
directionText.style.transform = 'translateX(-50%)';
directionText.style.top = '15px';
directionText.style.fontSize = '12px';
directionText.style.fontWeight = 'bold';
if (i === 0) directionText.textContent = 'N';
else if (i === 90) directionText.textContent = 'E';
else if (i === 180) directionText.textContent = 'S';
else if (i === 270) directionText.textContent = 'W';
marker.appendChild(directionText);
}
marker.style.transform = `rotate(${i}deg) translateY(-95%)`;
compassMarkers.appendChild(marker);
}
}
function toggleMenu() {
navLinks.classList.toggle('active');
const isExpanded = navLinks.classList.contains('active');
hamburger.setAttribute('aria-expanded', isExpanded);
}
function scrollToTop() {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
}
function toggleScrollTopButton() {
if (window.pageYOffset > 300) {
scrollTopBtn.classList.add('active');
} else {
scrollTopBtn.classList.remove('active');
}
}
function showNotification(message, type) {
notification.textContent = message;
notification.className = 'notification ' + type;
setTimeout(() => {
notification.classList.remove('success', 'error');
setTimeout(() => {
notification.className = 'notification';
}, 500);
}, 3000);
}
function showError() {
errorMessage.classList.remove('hidden');
prayerTimesSection.classList.add('hidden');
}
function hideError() {
errorMessage.classList.add('hidden');
prayerTimesSection.classList.remove('hidden');
}
function fetchPrayerTimes() {
const loadingText = "جاري التحميل...";
sunriseTime.textContent = loadingText;
fajrTime.textContent = loadingText;
dhuhrTime.textContent = loadingText;
asrTime.textContent = loadingText;
maghribTime.textContent = loadingText;
ishaTime.textContent = loadingText;
nextPrayerInfo.textContent = "جاري تحديد موعد الصلاة القادمة...";
remainingTime.textContent = "--:--:--";
hideError();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
position => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
userLocation = { latitude, longitude };
fetchPrayerTimesData(latitude, longitude);
},
error => {
console.error('Error getting location:', error);
fetchLocationByIP();
},
{
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 60000
}
);
} else {
fetchLocationByIP();
}
}
function fetchLocationByIP() {
fetch('https://ipapi.co/json/')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
if (data && data.latitude && data.longitude) {
userLocation = { latitude: data.latitude, longitude: data.longitude };
fetchPrayerTimesData(data.latitude, data.longitude, data.city, data.country_name);
} else {
throw new Error('Invalid location data');
}
})
.catch(error => {
console.error('Error fetching location by IP:', error);
showError();
showNotification("تعذر تحديد موقعك. يرجى التحقق من اتصال الإنترنت", "error");
});
}
function fetchPrayerTimesData(latitude, longitude, city = '', country = '') {
// إضافة التاريخ الحالي لضمان الحصول على أوقات اليوم الصحيحة
const today = new Date();
const dateString = `${today.getDate()}-${today.getMonth() + 1}-${today.getFullYear()}`;
const method = 4;
const apiUrl = `https://api.aladhan.com/v1/timings/${dateString}?latitude=${latitude}&longitude=${longitude}&method=${method}`;
fetch(apiUrl)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log("API Response:", data);
if (data && data.data && data.data.timings) {
const timings = data.data.timings;
prayerTimesData = {
timings: timings,
date: data.data.date,
meta: data.data.meta
};
displayPrayerTimes(timings);
// حساب الصلاة التالية فوراً
calculateNextPrayer(timings);
// بدء العد التنازلي كل ثانية
if (nextPrayerTimer) clearInterval(nextPrayerTimer);
nextPrayerTimer = setInterval(() => calculateNextPrayer(timings), 1000);
checkAlarms();
// إظهار رسالة نجاح
showNotification("تم تحميل مواقيت الصلاة بنجاح", "success");
} else {
throw new Error('Prayer times data not available');
}
})
.catch(error => {
console.error('Error fetching prayer times:', error);
showError();
showNotification("تعذر جلب مواقيت الصلاة. يرجى التحقق من اتصال الإنترنت", "error");
});
}
function displayPrayerTimes(timings) {
sunriseTime.textContent = convertTo12HourFormat(timings.Sunrise);
fajrTime.textContent = convertTo12HourFormat(timings.Fajr);
dhuhrTime.textContent = convertTo12HourFormat(timings.Dhuhr);
asrTime.textContent = convertTo12HourFormat(timings.Asr);
maghribTime.textContent = convertTo12HourFormat(timings.Maghrib);
ishaTime.textContent = convertTo12HourFormat(timings.Isha);
}
function convertTo12HourFormat(time) {
const [hours, minutes] = time.split(':');
let period = 'ص';
let adjustedHours = parseInt(hours);
if (adjustedHours >= 12) {
period = 'م';
if (adjustedHours > 12) {
adjustedHours -= 12;
}
}
if (adjustedHours === 0) {
adjustedHours = 12;
}
return `${adjustedHours}:${minutes} ${period}`;
}
// دالة محسنة لحساب الوقت المتبقي للصلاة التالية مع حساب الثواني
function calculateNextPrayer(timings) {
const now = new Date();
const currentTimeInSeconds = now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds();
// تحويل أوقات الصلاة إلى ثوانٍ من بداية اليوم
const prayerTimesInSeconds = {
Fajr: convertTimeToSeconds(timings.Fajr),
Dhuhr: convertTimeToSeconds(timings.Dhuhr),
Asr: convertTimeToSeconds(timings.Asr),
Maghrib: convertTimeToSeconds(timings.Maghrib),
Isha: convertTimeToSeconds(timings.Isha)
};
let nextPrayer = null;
let nextPrayerName = '';
let timeRemainingInSeconds = 0;
// البحث عن الصلاة التالية
const prayers = ['Fajr', 'Dhuhr', 'Asr', 'Maghrib', 'Isha'];
for (const prayer of prayers) {
if (prayerTimesInSeconds[prayer] > currentTimeInSeconds) {
nextPrayer = prayer;
nextPrayerName = getPrayerArabicName(prayer);
timeRemainingInSeconds = prayerTimesInSeconds[prayer] - currentTimeInSeconds;
break;
}
}
// إذا لم نجد صلاة تالية (أي أننا بعد العشاء)، نعتبر الفجر هو الصلاة التالية (في اليوم التالي)
if (!nextPrayer) {
nextPrayer = 'Fajr';
nextPrayerName = getPrayerArabicName('Fajr');
// الوقت المتبقي = (24 ساعة - الوقت الحالي) + وقت الفجر
timeRemainingInSeconds = (24 * 3600 - currentTimeInSeconds) + prayerTimesInSeconds.Fajr;
}
// تحويل الثواني إلى ساعات ودقائق وثواني
const hours = Math.floor(timeRemainingInSeconds / 3600);
const minutes = Math.floor((timeRemainingInSeconds % 3600) / 60);
const seconds = timeRemainingInSeconds % 60;
// تحديث واجهة المستخدم
nextPrayerInfo.innerHTML = `الوقت المتبقي لصلاة <span style="color: var(--gold); font-weight: bold;">${nextPrayerName}</span>:`;
remainingTime.textContent = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
// التحقق إذا حان وقت صلاة حالية
checkCurrentPrayer(prayerTimesInSeconds, currentTimeInSeconds);
}
// دالة لتحويل الوقت من صيغة "HH:MM" إلى ثوانٍ
function convertTimeToSeconds(timeStr) {
const [hours, minutes] = timeStr.split(':').map(Number);
return (hours * 60 + minutes) * 60;
}
// دالة محسنة للتحقق من الصلاة الحالية باستخدام الثواني
function checkCurrentPrayer(prayerTimesInSeconds, currentTimeInSeconds) {
const prayerThreshold = 120; // 120 ثانية (دقيقتين)
const prayers = ['Fajr', 'Dhuhr', 'Asr', 'Maghrib', 'Isha'];
for (const prayer of prayers) {
// التحقق إذا كنا ضمن نطاق 2 دقيقة من وقت الصلاة
if (Math.abs(currentTimeInSeconds - prayerTimesInSeconds[prayer]) <= prayerThreshold) {
if (activeAlarms[prayer.toLowerCase()]) {
showPrayerAlert(prayer);
break;
}
}
}
}
function getPrayerArabicName(prayer) {
const prayerNames = {
'Fajr': 'الفجر',
'Dhuhr': 'الظهر',
'Asr': 'العصر',
'Maghrib': 'المغرب',
'Isha': 'العشاء',
'Sunrise': 'الشروق'
};
return prayerNames[prayer] || prayer;
}
function startCompassFunction() {
if (!isMobileDevice()) {
showNotification("هذه الميزة متاحة فقط على الأجهزة المحمولة", "error");
return;
}
if (!userLocation.latitude || !userLocation.longitude) {
showNotification("يتم استخدام موقع مكة المكرمة للبوصلة", "error");
userLocation = { latitude: 21.3891, longitude: 39.8579 };
}
if (window.DeviceOrientationEvent) {
if (typeof DeviceOrientationEvent.requestPermission === 'function') {
DeviceOrientationEvent.requestPermission()
.then(permissionState => {
if (permissionState === 'granted') {
initCompass();
} else {
showNotification("تم رفض صلاحيات البوصلة", "error");
}
})
.catch(console.error);
} else {
initCompass();
}
} else {
compassStatus.textContent = "المتصفح أو الجهاز لا يدعم البوصلة";
showNotification("المتصفح أو الجهاز لا يدعم البوصلة", "error");
}
}
function initCompass() {
compassStatus.textContent = "جاري تحديد اتجاه القبلة...";
startCompass.disabled = true;
stopCompass.disabled = false;
const qiblaDirection = Qibla(userLocation.latitude, userLocation.longitude);
qiblaDegree.textContent = `الزاوية: ${Math.round(qiblaDirection)}°`;
window.addEventListener('deviceorientation', handleCompass);
}
function handleCompass(event) {
if (event.alpha !== null) {
const alpha = event.alpha;
const compassHeading = 360 - alpha;
const qiblaDirection = Qibla(userLocation.latitude, userLocation.longitude);
const angle = (compassHeading - qiblaDirection + 360) % 360;
compassArrow.style.transform = `translateX(-50%) rotate(${angle}deg)`;
compassStatus.textContent = "البوصلة نشطة - وجه الجهاز نحو القبلة";
// عرض دقة البيانات إذا كانت متاحة
if (event.webkitCompassAccuracy !== undefined) {
const accuracy = event.webkitCompassAccuracy;
compassAccuracy.textContent = `الدقة: ${accuracy}°`;
}
}
}
function stopCompassFunction() {
window.removeEventListener('deviceorientation', handleCompass);
compassStatus.textContent = "البوصلة متوقفة";
compassArrow.style.transform = "translateX(-50%) rotate(0deg)";
startCompass.disabled = false;
stopCompass.disabled = true;
compassAccuracy.textContent = "الدقة: --";
}
function toggleAthanAlarm(prayer) {
activeAlarms[prayer] = !activeAlarms[prayer];
updateAthanButton(prayer);
saveAlarms();
const prayerName = getPrayerArabicName(prayer.charAt(0).toUpperCase() + prayer.slice(1));
const message = activeAlarms[prayer] ?
`تم تفعيل التنبيه لصلاة ${prayerName}` :
`تم إلغاء التنبيه لصلاة ${prayerName}`;
showNotification(message, "success");
}
function updateAthanButton(prayer) {
const button = document.querySelector(`.athan-btn[data-prayer="${prayer}"]`);
if (button) {
if (activeAlarms[prayer]) {
button.innerHTML = '<i class="fas fa-bell-slash"></i> إلغاء التنبيه';
button.style.backgroundColor = "#f44336";
} else {
button.innerHTML = '<i class="fas fa-bell"></i> تنبيه';
button.style.backgroundColor = "";
}
}
}
function showPrayerAlert(prayer) {
const prayerName = getPrayerArabicName(prayer);
alertPrayerName.textContent = `حان وقت صلاة ${prayerName}`;
athanAudio.currentTime = 0;
athanAudio.play().catch(e => console.error('Error playing athan:', e));
prayerAlert.style.display = 'flex';
setTimeout(() => {
if (!prayerAlert.style.display || prayerAlert.style.display !== 'none') {
stopAthanFunction();
}
}, 5 * 60 * 1000);
}
function stopAthanFunction() {
athanAudio.pause();
athanAudio.currentTime = 0;
prayerAlert.style.display = 'none';
}
function closeAlertFunction() {
stopAthanFunction();
}
function checkAlarms() {
if (!prayerTimesData.timings) return;
const now = new Date();
const currentTimeInSeconds = now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds();
const prayerTimesInSeconds = {
fajr: convertTimeToSeconds(prayerTimesData.timings.Fajr),
dhuhr: convertTimeToSeconds(prayerTimesData.timings.Dhuhr),
asr: convertTimeToSeconds(prayerTimesData.timings.Asr),
maghrib: convertTimeToSeconds(prayerTimesData.timings.Maghrib),
isha: convertTimeToSeconds(prayerTimesData.timings.Isha)
};
const prayerThreshold = 120; // 120 ثانية (دقيقتين)
for (const [prayer, time] of Object.entries(prayerTimesInSeconds)) {
if (Math.abs(currentTimeInSeconds - time) <= prayerThreshold && activeAlarms[prayer]) {
showPrayerAlert(prayer);
break;
}
}
}
function saveAlarms() {
localStorage.setItem('prayerAlarms', JSON.stringify(activeAlarms));
}
function loadAlarms() {
const savedAlarms = localStorage.getItem('prayerAlarms');
if (savedAlarms) {
activeAlarms = JSON.parse(savedAlarms);
for (const prayer in activeAlarms) {
if (activeAlarms[prayer]) {
updateAthanButton(prayer);
}
}
}
}
});