Final-Rasd / script.js
AhmedMostafaAttia's picture
Create script.js
4c2054c verified
raw
history blame
5.57 kB
// Rasd - script.js
let currentLanguage = localStorage.getItem('rasdLanguage') || 'ar';
document.addEventListener('DOMContentLoaded', () => {
initializeLanguage();
setupEventListeners();
if (window.location.pathname.includes('dashboard.html')) {
initializeDashboard();
} else if (window.location.pathname.includes('certification.html')) {
initializeCertificationPage();
} else {
initializeHomePage();
}
});
function setupEventListeners() {
document.querySelectorAll('#lang-toggle-btn').forEach(btn => {
btn.addEventListener('click', toggleLanguage);
});
}
function initializeLanguage() {
const htmlEl = document.getElementById('html');
if (currentLanguage === 'ar') {
htmlEl.lang = 'ar';
htmlEl.dir = 'rtl';
htmlEl.classList.add('rtl');
htmlEl.classList.remove('ltr');
} else {
htmlEl.lang = 'en';
htmlEl.dir = 'ltr';
htmlEl.classList.add('ltr');
htmlEl.classList.remove('rtl');
}
updateLanguageUI();
updateTextContent();
}
function toggleLanguage() {
currentLanguage = currentLanguage === 'en' ? 'ar' : 'en';
localStorage.setItem('rasdLanguage', currentLanguage);
initializeLanguage();
}
function updateLanguageUI() {
document.querySelectorAll('#lang-toggle-btn').forEach(btn => {
btn.textContent = currentLanguage === 'en' ? 'ع' : 'En';
});
}
function updateTextContent() {
if (!translations) return;
document.querySelectorAll('[data-en]').forEach(el => {
const key = currentLanguage === 'en' ? el.dataset.en : el.dataset.ar;
if (key) el.textContent = key;
});
}
// Home Page Logic
function initializeHomePage() {
document.querySelectorAll('.counter').forEach(counter => {
const target = +counter.dataset.target;
counter.textContent = '0';
const updateCount = () => {
const current = +counter.textContent;
const increment = target / 200;
if (current < target) {
counter.textContent = `${Math.ceil(current + increment)}`;
setTimeout(updateCount, 10);
} else {
counter.textContent = target.toLocaleString();
}
};
updateCount();
});
if (document.getElementById('footer-year')) {
document.getElementById('footer-year').textContent = new Date().getFullYear();
}
}
// Dashboard Logic
function initializeDashboard() {
const sidebarLinks = document.querySelectorAll('.sidebar-link');
sidebarLinks.forEach(link => {
link.addEventListener('click', e => {
e.preventDefault();
sidebarLinks.forEach(l => l.classList.remove('active'));
link.classList.add('active');
const dashboardId = link.dataset.dashboard;
document.querySelectorAll('.dashboard-panel').forEach(p => p.classList.add('hidden'));
document.getElementById(`dashboard-${dashboardId}`).classList.remove('hidden');
const titleEl = document.getElementById('dashboard-title');
titleEl.dataset.en = link.querySelector('[data-en]').dataset.en;
titleEl.dataset.ar = link.querySelector('[data-ar]').dataset.ar;
updateTextContent(); // Re-run to update the title
});
});
// Initial content load
const initialDashboard = document.querySelector('.sidebar-link.active');
const titleEl = document.getElementById('dashboard-title');
titleEl.dataset.en = initialDashboard.querySelector('[data-en]').dataset.en;
titleEl.dataset.ar = initialDashboard.querySelector('[data-ar]').dataset.ar;
updateTextContent();
}
// Certification Page Logic
function initializeCertificationPage() {
const verifyBtn = document.getElementById('verify-btn');
const resultDiv = document.getElementById('verify-result');
const inputField = document.getElementById('verify-input');
verifyBtn.addEventListener('click', () => {
resultDiv.classList.remove('hidden');
resultDiv.innerHTML = `<i class="fas fa-spinner fa-spin text-2xl text-spa-green"></i>`;
setTimeout(() => {
if (inputField.value.trim() !== "") {
resultDiv.innerHTML = `
<div class="p-4 bg-green-100 border-l-4 border-green-500 text-green-700">
<div class="flex items-center">
<i class="fas fa-check-circle text-2xl mr-4"></i>
<div>
<p class="font-bold">${currentLanguage === 'ar' ? 'أصلي وموثق' : 'Authentic & Verified'}</p>
<p class="text-sm">${currentLanguage === 'ar' ? 'تم التحقق من هذه الصورة وهي تابعة لوكالة الأنباء السعودية.' : 'This image has been verified as authentic SPA property.'}</p>
</div>
</div>
</div>`;
} else {
resultDiv.innerHTML = `
<div class="p-4 bg-red-100 border-l-4 border-red-500 text-red-700">
<p>${currentLanguage === 'ar' ? 'الرجاء إدخال معرف صالح.' : 'Please enter a valid ID.'}</p>
</div>`;
}
}, 1500);
});
if (document.getElementById('footer-year-cert')) {
document.getElementById('footer-year-cert').textContent = new Date().getFullYear();
}
}