Skip to content

Commit

Permalink
app software structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Martins committed Apr 17, 2024
1 parent e6b50cc commit 9b069f6
Showing 9 changed files with 62 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
Empty file.
Empty file added MoodRisers app/index.html
Empty file.
Empty file.
Empty file added MoodRisers app/js/index.js
Empty file.
55 changes: 55 additions & 0 deletions MoodRisers app/js/router.js
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 };
7 changes: 7 additions & 0 deletions MoodRisers app/js/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
home: {
path: "/",
controller: "homeController",
},

};
Empty file.
Empty file.

0 comments on commit 9b069f6

Please sign in to comment.