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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class HeaderComponent {
|
| 2 |
+
static render() {
|
| 3 |
+
const header = document.getElementById('mainHeader');
|
| 4 |
+
if (!header) return;
|
| 5 |
+
|
| 6 |
+
header.innerHTML = `
|
| 7 |
+
<div class="header-content">
|
| 8 |
+
<div class="header-brand">
|
| 9 |
+
<h1>🎓 RewardApp</h1>
|
| 10 |
+
<span class="header-subtitle">پلتفرم آموزش توسعه وب</span>
|
| 11 |
+
</div>
|
| 12 |
+
|
| 13 |
+
<div class="header-stats" id="headerStats">
|
| 14 |
+
<!-- توسط JavaScript پر میشود -->
|
| 15 |
+
</div>
|
| 16 |
+
|
| 17 |
+
<div class="header-actions">
|
| 18 |
+
<button class="header-btn" onclick="HeaderComponent.showUserMenu()">
|
| 19 |
+
👤 حساب کاربری
|
| 20 |
+
</button>
|
| 21 |
+
<button class="header-btn" onclick="HeaderComponent.toggleTheme()">
|
| 22 |
+
🌙 تغییر تم
|
| 23 |
+
</button>
|
| 24 |
+
</div>
|
| 25 |
+
</div>
|
| 26 |
+
|
| 27 |
+
<div class="user-menu" id="userMenu" style="display: none;">
|
| 28 |
+
<div class="menu-content">
|
| 29 |
+
<div class="menu-header">
|
| 30 |
+
<div class="user-avatar">U</div>
|
| 31 |
+
<div class="user-details">
|
| 32 |
+
<span class="user-name" id="menuUserName">کاربر مهمان</span>
|
| 33 |
+
<span class="user-role" id="menuUserRole">ورود نکرده</span>
|
| 34 |
+
</div>
|
| 35 |
+
</div>
|
| 36 |
+
<div class="menu-items">
|
| 37 |
+
<a href="login.html" class="menu-item">🔐 ورود به سیستم</a>
|
| 38 |
+
<a href="register.html" class="menu-item">📝 ثبت نام</a>
|
| 39 |
+
<hr class="menu-divider">
|
| 40 |
+
<button class="menu-item" onclick="HeaderComponent.resetProgress()">
|
| 41 |
+
🔄 بازنشانی پیشرفت
|
| 42 |
+
</button>
|
| 43 |
+
<button class="menu-item" onclick="HeaderComponent.exportData()">
|
| 44 |
+
💾 ذخیره اطلاعات
|
| 45 |
+
</button>
|
| 46 |
+
<hr class="menu-divider">
|
| 47 |
+
<button class="menu-item" onclick="HeaderComponent.showAbout()">
|
| 48 |
+
ℹ️ درباره برنامه
|
| 49 |
+
</button>
|
| 50 |
+
</div>
|
| 51 |
+
</div>
|
| 52 |
+
</div>
|
| 53 |
+
`;
|
| 54 |
+
|
| 55 |
+
this.updateStats();
|
| 56 |
+
this.setupEventListeners();
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
static updateStats() {
|
| 60 |
+
const headerStats = document.getElementById('headerStats');
|
| 61 |
+
if (!headerStats) return;
|
| 62 |
+
|
| 63 |
+
const user = authManager.getCurrentUser();
|
| 64 |
+
|
| 65 |
+
if (user) {
|
| 66 |
+
const stats = learningLogic.getUserStats();
|
| 67 |
+
headerStats.innerHTML = `
|
| 68 |
+
<div class="stat-item">
|
| 69 |
+
<span class="stat-label">سطح</span>
|
| 70 |
+
<span class="stat-value">${stats.level}</span>
|
| 71 |
+
</div>
|
| 72 |
+
<div class="stat-item">
|
| 73 |
+
<span class="stat-label">امتیاز</span>
|
| 74 |
+
<span class="stat-value">${Utils.formatNumber(stats.totalScore)}</span>
|
| 75 |
+
</div>
|
| 76 |
+
<div class="stat-item">
|
| 77 |
+
<span class="stat-label">استریک</span>
|
| 78 |
+
<span class="stat-value">${stats.streak}</span>
|
| 79 |
+
</div>
|
| 80 |
+
`;
|
| 81 |
+
} else {
|
| 82 |
+
headerStats.innerHTML = `
|
| 83 |
+
<div class="stat-item">
|
| 84 |
+
<span class="stat-label">وضعیت</span>
|
| 85 |
+
<span class="stat-value">ورود نکرده</span>
|
| 86 |
+
</div>
|
| 87 |
+
`;
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
static setupEventListeners() {
|
| 92 |
+
// بستن منو با کلیک خارج از آن
|
| 93 |
+
document.addEventListener('click', (e) => {
|
| 94 |
+
const userMenu = document.getElementById('userMenu');
|
| 95 |
+
const headerBtn = e.target.closest('.header-btn');
|
| 96 |
+
|
| 97 |
+
if (userMenu && !userMenu.contains(e.target) && !headerBtn) {
|
| 98 |
+
userMenu.style.display = 'none';
|
| 99 |
+
}
|
| 100 |
+
});
|
| 101 |
+
|
| 102 |
+
// بهروزرسانی آمار هنگام تغییر وضعیت
|
| 103 |
+
document.addEventListener('authStateChanged', () => {
|
| 104 |
+
this.updateStats();
|
| 105 |
+
this.updateUserMenu();
|
| 106 |
+
});
|
| 107 |
+
|
| 108 |
+
document.addEventListener('progressUpdated', () => {
|
| 109 |
+
this.updateStats();
|
| 110 |
+
});
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
static showUserMenu() {
|
| 114 |
+
const userMenu = document.getElementById('userMenu');
|
| 115 |
+
if (!userMenu) return;
|
| 116 |
+
|
| 117 |
+
this.updateUserMenu();
|
| 118 |
+
userMenu.style.display = userMenu.style.display === 'none' ? 'block' : 'none';
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
static updateUserMenu() {
|
| 122 |
+
const menuUserName = document.getElementById('menuUserName');
|
| 123 |
+
const menuUserRole = document.getElementById('menuUserRole');
|
| 124 |
+
|
| 125 |
+
if (!menuUserName || !menuUserRole) return;
|
| 126 |
+
|
| 127 |
+
const user = authManager.getCurrentUser();
|
| 128 |
+
|
| 129 |
+
if (user) {
|
| 130 |
+
menuUserName.textContent = user.profile.fullName || user.username;
|
| 131 |
+
menuUserRole.textContent = this.getRoleText(user.role);
|
| 132 |
+
} else {
|
| 133 |
+
menuUserName.textContent = 'کاربر مهمان';
|
| 134 |
+
menuUserRole.textContent = 'ورود نکرده';
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
static getRoleText(role) {
|
| 139 |
+
const roles = {
|
| 140 |
+
'student': 'دانشجو',
|
| 141 |
+
'instructor': 'مربی',
|
| 142 |
+
'admin': 'مدیر'
|
| 143 |
+
};
|
| 144 |
+
return roles[role] || role;
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
static toggleTheme() {
|
| 148 |
+
const currentTheme = localStorage.getItem('theme') || 'light';
|
| 149 |
+
const newTheme = currentTheme === 'light' ? 'dark' : 'light';
|
| 150 |
+
|
| 151 |
+
localStorage.setItem('theme', newTheme);
|
| 152 |
+
document.body.setAttribute('data-theme', newTheme);
|
| 153 |
+
|
| 154 |
+
Utils.showNotification(
|
| 155 |
+
`تم ${newTheme === 'dark' ? 'تاریک' : 'روشن'} فعال شد`,
|
| 156 |
+
'success'
|
| 157 |
+
);
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
static resetProgress() {
|
| 161 |
+
if (confirm('آیا از بازنشانی تمام پیشرفت خود مطمئن هستید؟ این عمل غیرقابل برگشت است.')) {
|
| 162 |
+
learningLogic.resetProgress();
|
| 163 |
+
Utils.showNotification('پیشرفت با موفقیت بازنشانی شد', 'success');
|
| 164 |
+
document.dispatchEvent(new CustomEvent('progressUpdated'));
|
| 165 |
+
this.showUserMenu(); // بستن منو
|
| 166 |
+
}
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
static exportData() {
|
| 170 |
+
const userProgress = learningLogic.userProgress;
|
| 171 |
+
const dataStr = JSON.stringify(userProgress, null, 2);
|
| 172 |
+
const dataBlob = new Blob([dataStr], { type: 'application/json' });
|
| 173 |
+
|
| 174 |
+
const url = URL.createObjectURL(dataBlob);
|
| 175 |
+
const link = document.createElement('a');
|
| 176 |
+
link.href = url;
|
| 177 |
+
link.download = `rewardapp-backup-${Utils.getCurrentDate()}.json`;
|
| 178 |
+
document.body.appendChild(link);
|
| 179 |
+
link.click();
|
| 180 |
+
document.body.removeChild(link);
|
| 181 |
+
URL.revokeObjectURL(url);
|
| 182 |
+
|
| 183 |
+
Utils.showNotification('اطلاعات با موفقیت ذخیره شد', 'success');
|
| 184 |
+
this.showUserMenu(); // بستن منو
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
static showAbout() {
|
| 188 |
+
const aboutHTML = `
|
| 189 |
+
<div class="about-modal">
|
| 190 |
+
<h3>🎓 درباره RewardApp</h3>
|
| 191 |
+
<div class="about-content">
|
| 192 |
+
<p><strong>نسخه:</strong> ۱.۰.۰</p>
|
| 193 |
+
<p><strong>توضیحات:</strong> پلتفرم آموزشی تعاملی برای یادگیری توسعه وب</p>
|
| 194 |
+
<p><strong>امکانات:</strong></p>
|
| 195 |
+
<ul>
|
| 196 |
+
<li>📖 درسهای تعاملی</li>
|
| 197 |
+
<li>❓ آزمونهای کوتاه</li>
|
| 198 |
+
<li>✏️ تمرینهای عملی</li>
|
| 199 |
+
<li>🏆 سیستم پاداش و سطحبندی</li>
|
| 200 |
+
<li>📊 ردیابی پیشرفت</li>
|
| 201 |
+
</ul>
|
| 202 |
+
<hr>
|
| 203 |
+
<p>ساخته شده با ❤️ برای جامعه توسعهدهندگان وب</p>
|
| 204 |
+
</div>
|
| 205 |
+
<button class="btn btn-primary" onclick="ModalComponent.closeCurrentModal()">
|
| 206 |
+
بستن
|
| 207 |
+
</button>
|
| 208 |
+
</div>
|
| 209 |
+
`;
|
| 210 |
+
|
| 211 |
+
ModalComponent.showCustomModal('about-modal', aboutHTML);
|
| 212 |
+
this.showUserMenu(); // بستن منو
|
| 213 |
+
}
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
// اعمال تم ذخیره شده
|
| 217 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 218 |
+
const savedTheme = localStorage.getItem('theme');
|
| 219 |
+
if (savedTheme) {
|
| 220 |
+
document.body.setAttribute('data-theme', savedTheme);
|
| 221 |
+
}
|
| 222 |
+
});
|