-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
130 lines (116 loc) · 3.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import Header from './components/Header.js';
import Footer from './components/Footer.js';
const contentContainer = document.getElementById('root');
const headerElement = document.getElementById('header');
const footerElement = document.getElementById('footer');
document.addEventListener('DOMContentLoaded', () => {
// Define routes
const routes = [
{ path: '/', component: 'HomePage', css: 'home' },
{ path: '/profile', component: 'ProfilePage', css: 'profile' },
{ path: '/predict', component: 'PredictPage', css: 'predict' },
{ path: '/myTeam', component: 'MyTeamPage', css: 'myTeam' },
{ path: '/settings', component: 'SettingsPage', css: 'settings' },
{
path: '/privacy',
component: 'PrivacyPage',
css: 'privacy',
},
{ path: '/signIn', component: 'SignInPage', css: 'signIn' },
{ path: '/signUp', component: 'SignUpPage', css: 'signUp' },
{
path: '/changePassword',
component: 'ChangePasswordPage',
css: 'changePassword',
},
{
path: '/usersProfile',
component: 'usersProfilePage',
css: 'usersProfile',
},
{ path: '/followers', component: 'FollowersPage', css: 'followers' },
{ path: '/followings', component: 'FollowingsPage', css: 'followings' },
];
// Initial load of the content based on the current URL path
navigateToPage(window.location.pathname);
// Handle navigation changes
document.addEventListener('click', (event) => {
if (
event.target.tagName === 'A' &&
event.target.getAttribute('href').startsWith('/')
) {
event.preventDefault();
const path = event.target.getAttribute('href');
navigateToPage(path);
history.pushState(null, null, path);
}
});
// Handle back and forward buttons
window.addEventListener('popstate', () => {
navigateToPage(window.location.pathname);
});
function navigateToPage(path) {
const route = findRoute(path);
loadCssFile(route.css);
// Import the module dynamically based on the route
const modulePath = `./components/${route.component}.js`;
importModule(modulePath);
}
function findRoute(path) {
const notFoundRoute = {
path: '/404',
component: 'NotFoundPage',
css: 'notFound',
};
const matchedRoute =
routes.find((route) => route.path === path) || notFoundRoute;
// Dynamically add cases based on routes array
if (routes.some((route) => route.path === path)) {
return matchedRoute;
} else {
return notFoundRoute;
}
}
function loadCssFile(cssFile) {
if (cssFile) {
const cssPath = `./assets/css/${cssFile}.css`;
const existingStyle = document.getElementById('dynamic-style');
if (existingStyle) {
existingStyle.parentNode.removeChild(existingStyle);
}
const style = document.createElement('link');
style.id = 'dynamic-style';
style.rel = 'stylesheet';
style.href = cssPath;
document.head.appendChild(style);
} else {
// Remove the CSS file if it exists
const existingStyle = document.getElementById('dynamic-style');
if (existingStyle) {
existingStyle.parentNode.removeChild(existingStyle);
}
}
}
async function importModule(modulePath) {
try {
headerElement.innerHTML = await Header.content();
footerElement.innerHTML = await Footer.content();
const module = await import(modulePath);
const { content } = module.default;
// Fetch data dynamically
const data = await content();
contentContainer.innerHTML = data;
} catch (error) {
console.error(`Error loading module at ${modulePath}:`, error);
// If module not found, load the 404 page
handleNotFound();
}
}
function handleNotFound() {
import('./components/NotFoundPage.js').then(
({ default: notFoundContent }) => {
contentContainer.innerHTML = notFoundContent.content();
}
);
}
});