-
Notifications
You must be signed in to change notification settings - Fork 7
/
router.js
48 lines (41 loc) · 1.15 KB
/
router.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
// template
const homeTemplate = require("./pages/home.hbs");
const aboutTemplate = require("./pages/about.hbs");
const projectTemplate = require("./pages/project.hbs");
const editTemplate = require("./pages/edit.hbs");
const loginTemplate = require("./pages/login.hbs");
const joinTemplate = require("./pages/join.hbs");
const Home = homeTemplate();
const About = aboutTemplate();
const Project = projectTemplate();
const Edit = editTemplate();
const Login = loginTemplate();
const Join = joinTemplate();
const routes = {
"/": Home,
"/home": Home,
"/about": About,
"/project": Project,
"/edit": Edit,
"/login": Login,
"/join": Join
};
// entry point
const initialRoutes = (element) => {
renderHTML(element, routes["/"]);
window.onpopstate = () =>
renderHTML(element, routes[window.location.pathname]);
};
// set browser history
const historyRouterPush = (pathName, element) => {
window.history.pushState({}, pathName, window.location.origin + pathName);
renderHTML(element, routes[pathName]);
};
// render
const renderHTML = (element, route) => {
element.innerHTML = route;
};
module.exports = {
initialRoutes,
historyRouterPush,
};