-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
149 lines (113 loc) · 3.95 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* Simple router in vanilla JavaScript. Works only with basic routes without parameters
* It uses routes of hash type
*/
class Router {
/**
* @param {String} el - The DOM element using the querySelector form
* @param {Function} rootRouteCallback - A callback function for the root route
* @param {Function} emptyRouteCallback - (Optional) A callback function for the undefined routes
*/
constructor( el, rootRouteCallback, emptyRouteCallback ) {
// Initializing the routes array
this.routes = [];
// Checking that the el element has been set
if( !!el !== false ) {
this.el = el;
} else {
throw new Error("Router needs a DOM entry point.");
}
// Checking that the el element has been set
if( !!rootRouteCallback !== false ) {
this.rootRouteCallback = rootRouteCallback;
} else {
throw new Error("Router needs a callback function for the root route.");
}
// Defining a default behaviour for an empty route, and changing it in case of getting it as parameter
this.emptyRouteCallback = emptyRouteCallback !== undefined ? emptyRouteCallback : () => {
return "The current route doesn't exist";
};
// Starting the on has change event handler
window.onhashchange = this.hashChanged.bind(this);
}
/**
* Reads the URL when the class is loaded and renders the HTML
*/
start() {
const htmlOutput = this.getHTML( document.baseURI );
this.render( htmlOutput );
}
/**
* Tries to find a URL, and returns its HTML or the not found HTML
* @param {String} url - The location pathname
*
* @return {String} - The HTML
*/
getHTML( url ) {
let htmlOutput = null;
const baseURL = window.location.protocol + '//' + window.location.hostname + (location.port ? ':'+location.port: '') + '/';
if( url === baseURL ) {
htmlOutput = this.rootRouteCallback();
} else if( this.hashURL( url ) !== null
&& this.routes[ this.hashURL( url ) ] !== undefined ) {
htmlOutput = this.routes[this.hashURL( url )]();
} else {
htmlOutput = this.emptyRouteCallback();
}
return htmlOutput;
}
/**
* It adds a new valid route
* @param {String} route - The route to be created, prefix must be /
* @param {Class} callback to be fired when the route is reached
*/
addRoute( route, callback ) {
if( !!route !== false
&& typeof( callback ) === 'function' ) {
this.routes[route] = callback;
} else {
throw new Error("The route can't be empty and the callback must be a function");
}
}
/**
* Gets an array of routes
*
* @return {Array}
*/
get getRoutes() {
return this.routes;
}
/**
* Action to be trigered everytime that the hash changes in the URL.
* It gets the old and new URL and runs the specified method.
* @param {Object} context
*/
hashChanged( context ) {
const newURL = context.newURL;
this.render( this.getHTML( newURL ) );
}
/**
* Prints the resulting HTML of the callback function into the selected DOM element
* @param {String} htmlOutput
*/
render( htmlOutput ) {
document.querySelector(this.el).innerHTML = "";
if( !!htmlOutput !== false ) {
document.querySelector(this.el).innerHTML = htmlOutput;
}
}
/**
* Receives an URL and returns the hash
* @param {string} url
*
* @return {string} The current route
*/
hashURL( url ) {
const hashStart = url.indexOf( '#/' ) + 1;
if( hashStart !== 0 ) {
return url.substr( hashStart, url.length - hashStart );
}
return null;
}
}
export default Router;