forked from MarcoMartins89/MoodRiser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marco Martins
committed
Apr 17, 2024
1 parent
e6b50cc
commit 9b069f6
Showing
9 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import routes from '/js/routes.js' | ||
|
||
function setCurrentRoute({ path, controller }) { | ||
|
||
routes.currentPath.path = path; | ||
routes.currentPath.controller = controller; | ||
|
||
} | ||
|
||
async function launchController(controllerName, args) { | ||
|
||
const module = await import(`./controller/${controllerName}.js`); | ||
module.default.init(args); | ||
} | ||
|
||
function navigate(path) { | ||
|
||
if (path === routes.currentPath.path) { | ||
return; | ||
} | ||
|
||
const pathKey = path.split('/')[1]; | ||
|
||
console.log("this is path: " + path); | ||
console.log("this is path key: " + pathKey); | ||
|
||
const route = routes[pathKey] || routes.home; | ||
console.log('this is controller ' + route.controller); | ||
|
||
setCurrentRoute(route); | ||
launchController(route.controller, path.split('/').slice(2)); | ||
|
||
} | ||
|
||
|
||
function getPath(urlStr) { | ||
return new URL(urlStr).hash.slice(1); | ||
} | ||
|
||
function navigateOnHashChange() { | ||
addEventListener('hashchange', (e) => { | ||
const path = getPath(e.newURL); | ||
navigate(path); | ||
}) | ||
} | ||
|
||
function init() { | ||
|
||
window.location.hash = window.location.hash || routes.home.path; | ||
|
||
navigate(getPath(window.location.href)); | ||
navigateOnHashChange(); | ||
} | ||
|
||
export default { init }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default { | ||
home: { | ||
path: "/", | ||
controller: "homeController", | ||
}, | ||
|
||
}; |
Empty file.
Empty file.