yjernite HF Staff commited on
Commit
e506558
·
verified ·
1 Parent(s): c914b33

Delete js/router.js

Browse files
Files changed (1) hide show
  1. js/router.js +0 -292
js/router.js DELETED
@@ -1,292 +0,0 @@
1
- // router.js - Simple SPA router
2
- import { renderHomePage, getHomePageSidebar } from './pages/HomePage.js';
3
- import { renderAreaPage, getAreaPageSidebar } from './pages/AreaPage.js';
4
- import { renderResourcesPage, getResourcesPageSidebar } from './pages/ResourcesPage.js';
5
-
6
- class Router {
7
- constructor() {
8
- this.routes = {
9
- '/': 'home',
10
- '/home': 'home',
11
- '/efficiency': 'efficiency',
12
- '/personal': 'personal',
13
- '/rights': 'rights',
14
- '/ecosystems': 'ecosystems',
15
- '/about': 'resources'
16
- };
17
-
18
- this.currentPage = null;
19
- this.init();
20
- }
21
-
22
- init() {
23
- // Handle initial page load
24
- this.loadPage(window.location.pathname);
25
-
26
- // Handle browser back/forward
27
- window.addEventListener('popstate', (e) => {
28
- const path = window.location.pathname;
29
- const hash = window.location.hash;
30
- const fullUrl = path + hash;
31
-
32
- // Use navigateToUrl to ensure proper scroll behavior
33
- this.navigateToUrl(fullUrl);
34
- });
35
-
36
- // Handle hash changes for navigation
37
- window.addEventListener('hashchange', (e) => {
38
- const hash = window.location.hash.substring(1);
39
- if (hash) {
40
- // Scroll to the section after a short delay to ensure content is loaded
41
- setTimeout(() => this.scrollToSection(hash), 100);
42
- }
43
- });
44
-
45
- // Handle initial hash if present
46
- const initialHash = window.location.hash.substring(1);
47
- if (initialHash) {
48
- setTimeout(() => this.scrollToSection(initialHash), 200);
49
- }
50
-
51
- // Initialize unified navigation handling
52
- this.initializeNavigation();
53
- }
54
-
55
- async loadPage(path) {
56
- const route = this.routes[path] || 'home';
57
-
58
- if (this.currentPage === route) {
59
- // Same page, no need to reload
60
- return;
61
- }
62
-
63
- this.currentPage = route;
64
-
65
- try {
66
- switch (route) {
67
- case 'home':
68
- await this.loadHomePage();
69
- break;
70
- case 'efficiency':
71
- case 'personal':
72
- case 'rights':
73
- case 'ecosystems':
74
- await this.loadAreaPage(route);
75
- break;
76
- case 'resources':
77
- await this.loadResourcesPage();
78
- break;
79
- default:
80
- await this.loadHomePage();
81
- }
82
-
83
- } catch (error) {
84
- console.error('Error loading page:', error);
85
- // Fallback to home page
86
- await this.loadHomePage();
87
- }
88
-
89
- return Promise.resolve();
90
- }
91
-
92
- async loadHomePage() {
93
- const mainContent = document.getElementById('main-content');
94
- const leftSidebar = document.getElementById('left-sidebar');
95
-
96
- if (!mainContent) {
97
- return;
98
- }
99
-
100
- try {
101
- // Get page content and sidebar
102
- const homePage = renderHomePage();
103
-
104
- // Update content
105
- const contentContainer = mainContent.querySelector('.max-w-4xl') || mainContent;
106
- contentContainer.innerHTML = homePage.content;
107
-
108
- // Update sidebar if it exists
109
- if (leftSidebar) {
110
- leftSidebar.innerHTML = getHomePageSidebar();
111
- }
112
-
113
- // Initialize page
114
- if (homePage.init) {
115
- homePage.init();
116
- }
117
-
118
- // Update navigation state
119
- this.updateNavigation('home');
120
- } catch (error) {
121
- // Fallback to error content
122
- const contentContainer = mainContent.querySelector('.max-w-4xl') || mainContent;
123
- contentContainer.innerHTML = `
124
- <div class="bg-white rounded-lg shadow-sm p-8">
125
- <h1 class="text-3xl font-bold text-red-600 mb-6">Error Loading Page</h1>
126
- <p class="text-gray-700">Sorry, there was an error loading the home page.</p>
127
- </div>
128
- `;
129
- }
130
- }
131
-
132
- async loadAreaPage(area) {
133
- const mainContent = document.getElementById('main-content');
134
- const leftSidebar = document.getElementById('left-sidebar');
135
-
136
- if (!mainContent) {
137
- return;
138
- }
139
-
140
- try {
141
- // Get area page content and sidebar
142
- const areaPage = renderAreaPage(area);
143
-
144
- // Update content
145
- const contentContainer = mainContent.querySelector('.max-w-4xl') || mainContent;
146
- contentContainer.innerHTML = areaPage.content;
147
-
148
- // Update sidebar
149
- if (leftSidebar) {
150
- leftSidebar.innerHTML = getAreaPageSidebar(area);
151
- }
152
-
153
- // Initialize page
154
- if (areaPage.init) {
155
- areaPage.init();
156
- }
157
- } catch (error) {
158
- console.error(`Error loading ${area} page:`, error);
159
-
160
- // Fallback content
161
- const contentContainer = mainContent.querySelector('.max-w-4xl') || mainContent;
162
- contentContainer.innerHTML = `
163
- <div class="bg-white rounded-lg shadow-sm p-8">
164
- <h1 class="text-3xl font-bold text-red-600 mb-6">Error Loading Page</h1>
165
- <p class="text-gray-700">Sorry, there was an error loading the ${area} page.</p>
166
- </div>
167
- `;
168
- }
169
-
170
- this.updateNavigation(area);
171
- }
172
-
173
- async loadResourcesPage() {
174
- const mainContent = document.getElementById('main-content');
175
- const leftSidebar = document.getElementById('left-sidebar');
176
-
177
- if (!mainContent) return;
178
-
179
- try {
180
- // Get resources page content and sidebar
181
- const resourcesPage = renderResourcesPage();
182
-
183
- // Update content
184
- const contentContainer = mainContent.querySelector('.max-w-4xl') || mainContent;
185
- contentContainer.innerHTML = resourcesPage.content;
186
-
187
- // Update sidebar
188
- if (leftSidebar) {
189
- leftSidebar.innerHTML = getResourcesPageSidebar();
190
- }
191
-
192
- // Initialize page
193
- if (resourcesPage.init) {
194
- resourcesPage.init();
195
- }
196
- } catch (error) {
197
- // Fallback content
198
- const contentContainer = mainContent.querySelector('.max-w-4xl') || mainContent;
199
- contentContainer.innerHTML = `
200
- <div class="bg-white rounded-lg shadow-sm p-8">
201
- <h1 class="text-3xl font-bold text-red-600 mb-6">Error Loading Page</h1>
202
- <p class="text-gray-700">Sorry, there was an error loading the resources page.</p>
203
- </div>
204
- `;
205
- }
206
-
207
- this.updateNavigation('resources');
208
- }
209
-
210
- updateNavigation(currentPage) {
211
- // Update header navigation active states
212
- const navLinks = document.querySelectorAll('header nav a');
213
- navLinks.forEach(link => {
214
- link.classList.remove('text-blue-600', 'bg-blue-50');
215
- link.classList.add('text-gray-700');
216
-
217
- const href = link.getAttribute('href');
218
- if ((currentPage === 'home' && (href === '/' || href === '/home')) ||
219
- (currentPage === 'resources' && href === '/about') ||
220
- (currentPage !== 'home' && currentPage !== 'resources' && href === `/${currentPage}`)) {
221
- link.classList.remove('text-gray-700');
222
- link.classList.add('text-blue-600', 'bg-blue-50');
223
- }
224
- });
225
- }
226
-
227
-
228
- initializeNavigation() {
229
- // Use event delegation to handle all navigation links
230
- document.addEventListener('click', (e) => {
231
- const link = e.target.closest('a');
232
- if (!link) return;
233
-
234
- const href = link.getAttribute('href');
235
- if (!href) return;
236
-
237
- // Handle different types of links
238
- if (href.startsWith('/') && !href.startsWith('/js/') && !href.startsWith('/css/') && !href.startsWith('/images/')) {
239
- // Check if this is a known SPA route
240
- const path = href.split('#')[0];
241
- if (this.routes[path]) {
242
- e.preventDefault();
243
- this.navigateToUrl(href);
244
- }
245
- } else if (href.startsWith('#')) {
246
- // Same-page hash navigation
247
- e.preventDefault();
248
- this.scrollToSection(href.substring(1));
249
- }
250
- // External links are handled normally by browser
251
- });
252
- }
253
-
254
- navigateToUrl(fullUrl) {
255
- // Parse URL into path and hash
256
- const [path, hash] = fullUrl.split('#');
257
-
258
- // Update browser URL
259
- window.history.pushState({}, '', fullUrl);
260
-
261
- // Load page and then handle hash
262
- this.loadPage(path).then(() => {
263
- if (hash) {
264
- setTimeout(() => this.scrollToSection(hash), 100);
265
- } else {
266
- // No hash - scroll to top of page with a delay to ensure content is rendered
267
- setTimeout(() => {
268
- window.scrollTo({ top: 0, behavior: 'smooth' });
269
- }, 100);
270
- }
271
- });
272
- }
273
-
274
- scrollToSection(sectionId) {
275
- const element = document.getElementById(sectionId);
276
- if (element) {
277
- // Scroll to element with offset to show title
278
- const elementRect = element.getBoundingClientRect();
279
- const absoluteElementTop = elementRect.top + window.pageYOffset;
280
- const offset = 120; // Account for fixed header + some padding
281
-
282
- window.scrollTo({
283
- top: absoluteElementTop - offset,
284
- behavior: 'smooth'
285
- });
286
- }
287
- }
288
-
289
- }
290
-
291
- // Export router instance
292
- export const router = new Router();