CSNET commited on
Commit
7ee89bc
·
verified ·
1 Parent(s): f93e15c

ทีนี้ช่วยทำให้เมนูแต่ละ link ใช้งานได้ทั้งหมด

Browse files
Files changed (1) hide show
  1. components/navbar.js +65 -34
components/navbar.js CHANGED
@@ -91,58 +91,67 @@ class MainNavbar extends HTMLElement {
91
  </style>
92
 
93
  <nav class="navbar">
94
- <a href="#today" class="nav-item active" data-page="today">
95
- <div class="active-indicator"></div>
96
- <i class="nav-icon" data-feather="home"></i>
97
- <span class="nav-label">วันนี้</span>
98
- </a>
99
- <a href="#sleep" class="nav-item" data-page="sleep">
100
- <div class="active-indicator"></div>
101
- <i class="nav-icon" data-feather="moon"></i>
102
- <span class="nav-label">การนอน</span>
103
- </a>
104
- <a href="#activity" class="nav-item" data-page="activity">
105
- <div class="active-indicator"></div>
106
- <i class="nav-icon" data-feather="zap"></i>
107
- <span class="nav-label">ออกกำลังกาย</span>
108
- </a>
109
- <a href="#nutrition" class="nav-item" data-page="nutrition">
110
- <div class="active-indicator"></div>
111
- <i class="nav-icon" data-feather="coffee"></i>
112
- <span class="nav-label">โภชนาการ</span>
113
- </a>
114
- <a href="#profile" class="nav-item" data-page="profile">
115
- <div class="active-indicator"></div>
116
- <i class="nav-icon" data-feather="user"></i>
117
- <span class="nav-label">โปรไฟล์</span>
118
- </a>
119
- </nav>
120
  `;
121
  }
122
-
123
  setupEventListeners() {
124
  const navItems = this.shadowRoot.querySelectorAll('.nav-item');
125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  navItems.forEach(item => {
127
  item.addEventListener('click', (e) => {
128
- e.preventDefault();
129
  const page = item.dataset.page;
130
- this.setActivePage(page);
131
 
132
- // Dispatch custom event
133
  this.dispatchEvent(new CustomEvent('page-change', {
134
  detail: { page },
135
  bubbles: true,
136
  composed: true
137
  }));
138
 
139
- // In real app, would navigate to actual page
140
  console.log(`เปลี่ยนหน้าไปที่: ${page}`);
141
  });
142
  });
143
  }
144
-
145
- setActivePage(page) {
146
  const navItems = this.shadowRoot.querySelectorAll('.nav-item');
147
  navItems.forEach(item => {
148
  if (item.dataset.page === page) {
@@ -153,11 +162,33 @@ class MainNavbar extends HTMLElement {
153
  });
154
  this.currentPage = page;
155
  }
156
-
157
  // Public method to set page from outside
158
  navigate(page) {
159
  this.setActivePage(page);
160
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  }
162
 
163
  customElements.define('main-navbar', MainNavbar);
 
91
  </style>
92
 
93
  <nav class="navbar">
94
+ <a href="index.html" class="nav-item active" data-page="today">
95
+ <div class="active-indicator"></div>
96
+ <i class="nav-icon" data-feather="home"></i>
97
+ <span class="nav-label">วันนี้</span>
98
+ </a>
99
+ <a href="sleep.html" class="nav-item" data-page="sleep">
100
+ <div class="active-indicator"></div>
101
+ <i class="nav-icon" data-feather="moon"></i>
102
+ <span class="nav-label">การนอน</span>
103
+ </a>
104
+ <a href="activity.html" class="nav-item" data-page="activity">
105
+ <div class="active-indicator"></div>
106
+ <i class="nav-icon" data-feather="zap"></i>
107
+ <span class="nav-label">ออกกำลังกาย</span>
108
+ </a>
109
+ <a href="nutrition.html" class="nav-item" data-page="nutrition">
110
+ <div class="active-indicator"></div>
111
+ <i class="nav-icon" data-feather="coffee"></i>
112
+ <span class="nav-label">โภชนาการ</span>
113
+ </a>
114
+ <a href="profile.html" class="nav-item" data-page="profile">
115
+ <div class="active-indicator"></div>
116
+ <i class="nav-icon" data-feather="user"></i>
117
+ <span class="nav-label">โปรไฟล์</span>
118
+ </a>
119
+ </nav>
120
  `;
121
  }
 
122
  setupEventListeners() {
123
  const navItems = this.shadowRoot.querySelectorAll('.nav-item');
124
 
125
+ // Set active page based on current URL
126
+ const currentPath = window.location.pathname;
127
+ const pageMap = {
128
+ '/index.html': 'today',
129
+ '/sleep.html': 'sleep',
130
+ '/activity.html': 'activity',
131
+ '/nutrition.html': 'nutrition',
132
+ '/profile.html': 'profile'
133
+ };
134
+
135
+ const currentPage = pageMap[currentPath] || 'today';
136
+ this.setActivePage(currentPage);
137
+
138
  navItems.forEach(item => {
139
  item.addEventListener('click', (e) => {
 
140
  const page = item.dataset.page;
 
141
 
142
+ // Dispatch custom event for other components
143
  this.dispatchEvent(new CustomEvent('page-change', {
144
  detail: { page },
145
  bubbles: true,
146
  composed: true
147
  }));
148
 
149
+ // Log navigation (in production, this would be analytics)
150
  console.log(`เปลี่ยนหน้าไปที่: ${page}`);
151
  });
152
  });
153
  }
154
+ setActivePage(page) {
 
155
  const navItems = this.shadowRoot.querySelectorAll('.nav-item');
156
  navItems.forEach(item => {
157
  if (item.dataset.page === page) {
 
162
  });
163
  this.currentPage = page;
164
  }
 
165
  // Public method to set page from outside
166
  navigate(page) {
167
  this.setActivePage(page);
168
  }
169
+
170
+ // Auto-detect current page on first load
171
+ detectCurrentPage() {
172
+ const currentPath = window.location.pathname;
173
+ const pageMap = {
174
+ '/index.html': 'today',
175
+ '/sleep.html': 'sleep',
176
+ '/activity.html': 'activity',
177
+ '/nutrition.html': 'nutrition',
178
+ '/profile.html': 'profile'
179
+ };
180
+
181
+ const currentPage = pageMap[currentPath] || 'today';
182
+ this.setActivePage(currentPage);
183
+ }
184
+
185
+ connectedCallback() {
186
+ this.attachShadow({ mode: 'open' });
187
+ this.currentPage = 'today';
188
+ this.render();
189
+ this.setupEventListeners();
190
+ this.detectCurrentPage(); // Auto-detect current page
191
+ }
192
  }
193
 
194
  customElements.define('main-navbar', MainNavbar);