RewardApp / trash /js /app.js
Mhdeusi's picture
Rename js/app.js to trash/js/app.js
6f9e264 verified
class LearningPlatform {
constructor() {
this.currentDay = 1;
this.maxDays = 10;
this.isInitialized = false;
}
async initialize() {
try {
// مقداردهی اولیه سیستم احراز هویت
AuthMiddleware.initializeApp();
// بارگذاری کامپوننت‌ها
this.loadComponents();
// بارگذاری محتوا
await this.loadContent();
// راه‌اندازی رابط کاربری
this.setupEventListeners();
this.updateUI();
this.isInitialized = true;
console.log('Platform initialized successfully');
} catch (error) {
console.error('Failed to initialize platform:', error);
this.showError('خطا در بارگذاری برنامه. لطفاً صفحه را رفرش کنید.');
}
}
loadComponents() {
// بارگذاری هدر و فوتر
if (typeof HeaderComponent !== 'undefined') {
HeaderComponent.render();
}
if (typeof NavigationComponent !== 'undefined') {
NavigationComponent.render();
}
}
async loadContent() {
// بارگذاری همزمان محتوا
await Promise.all([
this.loadLesson(),
this.loadQuiz(),
this.loadExercise()
]);
}
async loadLesson() {
try {
const response = await fetch(`data/lessons/day${this.currentDay}.json`);
if (!response.ok) throw new Error('Lesson not found');
const lessonData = await response.json();
if (typeof LessonUI !== 'undefined') {
LessonUI.render(lessonData);
}
} catch (error) {
console.error('Error loading lesson:', error);
document.getElementById('lessonContent').innerHTML =
'<div class="error">خطا در بارگذاری محتوا</div>';
}
}
async loadQuiz() {
try {
const response = await fetch(`data/quizzes/day${this.currentDay}.json`);
if (!response.ok) throw new Error('Quiz not found');
const quizData = await response.json();
if (typeof QuizUI !== 'undefined') {
QuizUI.render(quizData);
}
} catch (error) {
console.error('Error loading quiz:', error);
document.getElementById('quizForm').innerHTML =
'<div class="error">خطا در بارگذاری آزمون</div>';
}
}
async loadExercise() {
try {
const response = await fetch(`data/exercises/day${this.currentDay}.json`);
if (!response.ok) throw new Error('Exercise not found');
const exerciseData = await response.json();
if (typeof ExerciseUI !== 'undefined') {
ExerciseUI.render(exerciseData);
}
} catch (error) {
console.error('Error loading exercise:', error);
document.getElementById('exerciseContent').innerHTML =
'<div class="error">خطا در بارگذاری تمرین</div>';
}
}
setupEventListeners() {
// مدیریت تغییر روز
document.addEventListener('dayChanged', (event) => {
this.currentDay = event.detail.day;
this.loadContent();
this.updateUI();
});
// مدیریت وضعیت احراز هویت
document.addEventListener('authStateChanged', () => {
this.updateUI();
});
// جلوگیری از ارسال فرم‌ها
document.addEventListener('submit', (e) => {
e.preventDefault();
});
}
updateUI() {
this.updateAuthDisplay();
this.updateProgressDisplay();
this.updateNavigation();
}
updateAuthDisplay() {
const authSection = document.getElementById('authSection');
const user = authManager.getCurrentUser();
if (authSection) {
if (user) {
authSection.innerHTML = `
<div class="user-welcome">
<div class="user-avatar">
${user.profile.fullName ? user.profile.fullName.charAt(0) : user.username.charAt(0)}
</div>
<div class="user-info">
<span class="welcome-text">خوش آمدید, ${user.profile.fullName || user.username}!</span>
<span class="user-role">${this.getRoleText(user.role)}</span>
</div>
<button onclick="app.logout()" class="btn btn-outline">خروج</button>
</div>
`;
} else {
authSection.innerHTML = `
<div class="auth-actions">
<p>برای ذخیره پیشرفت و شرکت در آزمون‌ها وارد شوید</p>
<div class="auth-buttons">
<button onclick="app.showLogin()" class="btn btn-primary">ورود</button>
<button onclick="app.showRegister()" class="btn btn-secondary">ثبت نام</button>
</div>
</div>
`;
}
}
}
updateProgressDisplay() {
const user = authManager.getCurrentUser();
const progressContent = document.getElementById('progressContent');
if (progressContent && user) {
const progress = user.progress || {};
progressContent.innerHTML = `
<div class="progress-stats">
<div class="stat-card">
<div class="stat-value">${progress.totalScore || 0}</div>
<div class="stat-label">امتیاز کل</div>
</div>
<div class="stat-card">
<div class="stat-value">${progress.completedLessons?.length || 0}</div>
<div class="stat-label">درس‌های تکمیل شده</div>
</div>
<div class="stat-card">
<div class="stat-value">${this.currentDay}</div>
<div class="stat-label">درس فعلی</div>
</div>
</div>
`;
}
}
updateNavigation() {
const progressIndicator = document.getElementById('progressIndicator');
if (progressIndicator) {
progressIndicator.innerHTML = `
<span>روز ${this.currentDay} از ${this.maxDays}</span>
<div class="navigation-buttons">
<button onclick="app.previousDay()" ${this.currentDay <= 1 ? 'disabled' : ''}
class="btn btn-sm btn-outline">قبلی</button>
<button onclick="app.nextDay()" ${this.currentDay >= this.maxDays ? 'disabled' : ''}
class="btn btn-sm btn-primary">بعدی</button>
</div>
`;
}
}
getRoleText(role) {
const roles = {
'student': 'دانشجو',
'instructor': 'مربی',
'admin': 'مدیر'
};
return roles[role] || role;
}
// متدهای عمومی
showLogin() {
ModalComponent.showLogin();
}
showRegister() {
ModalComponent.showRegister();
}
logout() {
authManager.logout();
this.updateUI();
ModalComponent.showMessage('خروج', 'با موفقیت از سیستم خارج شدید.');
}
nextDay() {
if (this.currentDay < this.maxDays) {
this.currentDay++;
this.loadContent();
this.updateUI();
}
}
previousDay() {
if (this.currentDay > 1) {
this.currentDay--;
this.loadContent();
this.updateUI();
}
}
showError(message) {
ModalComponent.showError(message);
}
}
// ایجاد نمونه اصلی برنامه
const app = new LearningPlatform();
// راه‌اندازی برنامه هنگام بارگذاری صفحه
document.addEventListener('DOMContentLoaded', () => {
app.initialize();
});