HerzaJ commited on
Commit
9d9ec01
·
verified ·
1 Parent(s): df5ac96

Update public/index.js

Browse files
Files changed (1) hide show
  1. public/index.js +59 -34
public/index.js CHANGED
@@ -228,41 +228,22 @@ if (navMenu) {
228
  }
229
 
230
  document.querySelectorAll('.nav-link').forEach(link => {
231
- link.addEventListener('click', (e) => {
232
- const href = link.getAttribute('href');
233
- if (href && !href.startsWith('#')) {
234
- closeMenu();
235
- } else if (href && href.startsWith('#')) {
236
- e.preventDefault();
237
- const target = document.querySelector(href);
238
- if (target) {
239
- closeMenu();
240
- setTimeout(() => {
241
- target.scrollIntoView({ behavior: 'smooth', block: 'start' });
242
- }, 400);
243
- }
244
- }
245
- });
246
  });
247
 
248
  document.querySelectorAll('a[href^="#"]').forEach(anchor => {
249
- anchor.addEventListener('click', function (e) {
250
- const href = this.getAttribute('href');
251
- if (!href || href === '#') return;
252
-
253
- e.preventDefault();
254
- const target = document.querySelector(href);
255
- if (target) {
256
- if (navMenu && navMenu.classList.contains('active')) {
257
- closeMenu();
258
- setTimeout(() => {
259
- target.scrollIntoView({ behavior: 'smooth', block: 'start' });
260
- }, 400);
261
- } else {
262
  target.scrollIntoView({ behavior: 'smooth', block: 'start' });
263
  }
264
- }
265
- });
266
  });
267
 
268
  window.addEventListener('load', () => {
@@ -294,13 +275,56 @@ function checkAuthStatus() {
294
  navMenuEl.innerHTML = `
295
  <a href="/dashboard" class="nav-link"><i class="fas fa-chart-line"></i> Dashboard</a>
296
  <a href="/profile" class="nav-link"><i class="fas fa-user"></i> Profile</a>
297
- <a href="#" onclick="logout()" class="nav-link"><i class="fas fa-sign-out-alt"></i> Logout</a>
298
  `;
299
 
300
- document.querySelectorAll('.nav-link').forEach(link => {
301
- link.addEventListener('click', closeMenu);
302
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303
  }
 
 
 
 
 
 
304
  }
305
  }
306
 
@@ -538,6 +562,7 @@ document.addEventListener('DOMContentLoaded', function() {
538
  preventHorizontalScroll();
539
  loadStats();
540
  checkAuthStatus();
 
541
  setInterval(loadStats, 30000);
542
 
543
  setTimeout(() => {
 
228
  }
229
 
230
  document.querySelectorAll('.nav-link').forEach(link => {
231
+ link.addEventListener('click', handleNavLinkClick);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  });
233
 
234
  document.querySelectorAll('a[href^="#"]').forEach(anchor => {
235
+ if (!anchor.classList.contains('nav-link')) {
236
+ anchor.addEventListener('click', function (e) {
237
+ const href = this.getAttribute('href');
238
+ if (!href || href === '#') return;
239
+
240
+ e.preventDefault();
241
+ const target = document.querySelector(href);
242
+ if (target) {
 
 
 
 
 
243
  target.scrollIntoView({ behavior: 'smooth', block: 'start' });
244
  }
245
+ });
246
+ }
247
  });
248
 
249
  window.addEventListener('load', () => {
 
275
  navMenuEl.innerHTML = `
276
  <a href="/dashboard" class="nav-link"><i class="fas fa-chart-line"></i> Dashboard</a>
277
  <a href="/profile" class="nav-link"><i class="fas fa-user"></i> Profile</a>
278
+ <a href="#" class="nav-link" id="logoutBtn"><i class="fas fa-sign-out-alt"></i> Logout</a>
279
  `;
280
 
281
+ attachNavLinkListeners();
282
+ }
283
+ } else {
284
+ attachNavLinkListeners();
285
+ }
286
+ }
287
+
288
+ function attachNavLinkListeners() {
289
+ document.querySelectorAll('.nav-link').forEach(link => {
290
+ link.removeEventListener('click', handleNavLinkClick);
291
+ link.addEventListener('click', handleNavLinkClick);
292
+ });
293
+
294
+ const logoutBtn = document.getElementById('logoutBtn');
295
+ if (logoutBtn) {
296
+ logoutBtn.addEventListener('click', (e) => {
297
+ e.preventDefault();
298
+ closeMenu();
299
+ setTimeout(() => {
300
+ logout();
301
+ }, 400);
302
+ });
303
+ }
304
+ }
305
+
306
+ function handleNavLinkClick(e) {
307
+ const href = this.getAttribute('href');
308
+
309
+ if (!href || href === '#' || this.id === 'logoutBtn') {
310
+ return;
311
+ }
312
+
313
+ if (href.startsWith('#')) {
314
+ e.preventDefault();
315
+ const target = document.querySelector(href);
316
+ if (target) {
317
+ closeMenu();
318
+ setTimeout(() => {
319
+ target.scrollIntoView({ behavior: 'smooth', block: 'start' });
320
+ }, 400);
321
  }
322
+ } else {
323
+ e.preventDefault();
324
+ closeMenu();
325
+ setTimeout(() => {
326
+ window.location.href = href;
327
+ }, 400);
328
  }
329
  }
330
 
 
562
  preventHorizontalScroll();
563
  loadStats();
564
  checkAuthStatus();
565
+ attachNavLinkListeners();
566
  setInterval(loadStats, 30000);
567
 
568
  setTimeout(() => {