-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.js
41 lines (37 loc) · 906 Bytes
/
route.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
// ROUTER (NEED TO REFACTOR AND COMBINE W/INDEX.JS FILE IN SRC)
window.onload = () => {
const activeRoutes = Array.from(document.querySelectorAll('[route]'));
function navigate(event) {
let route = event.target.attributes[0].value;
let routeInfo = myFirstRouter.routes.filter(function (routers) {
return routers.path === route;
})[0];
if (!routeInfo) {
window.history.pushState({}, '', 'error')
window.location.pathname = './src/pages/checkout/checkout.html'
}
};
activeRoutes.forEach(route => {
route.addEventListener('click', navigate, false);
});
};
const Router = function (name, routes) {
return {
name: name,
routes: routes
}
};
const myFirstRouter = new Router('myFirstRouter', [
{
path: '/',
name: 'Route'
},
{
path: '/checkout',
name: 'Checkout'
},
{
path: '/products',
name: 'Products'
}
]);