-
Notifications
You must be signed in to change notification settings - Fork 1
/
rooter.js
345 lines (246 loc) · 6.48 KB
/
rooter.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
(function(lib, global) {
if(typeof module === "object" && typeof module.exports === "object") {
module.exports = lib;
} else if(typeof define === "function" && define.amd) {
return define([], lib);
} else {
global.rooter = lib;
}
})(function(id) {
/*******************
* Helper functions
********************/
function noop() {}
function isString(arg) {
return typeof arg === "string";
}
function isNumber(arg) {
return typeof arg === "number";
}
function isFunction(arg) {
return typeof arg === "function";
}
function isArray(arg) {
return Object.prototype.toString.call(arg) === "[object Array]";
}
function typeError(expected, actual) {
throw new TypeError("Expected " + expected + ", got " + actual);
}
function isRegexPath(path) {
return /\/:[^\/]+/g.test(path);
}
function decodeParam(param) {
return decodeURIComponent(param);
}
function extractParams(route) {
var matched = window.location.pathname.match(route.path);
var params = {};
if(!matched) return params;
for(var i = 1; i < matched.length; i++) {
params[route.params[i - 1].slice(2)] = decodeParam(matched[i]);
}
return params;
}
function prefixPath(path, prefix) {
if(prefix) {
if(prefix.indexOf("/") === -1) {
path = "/" + prefix + (path === "/" ? "" : path);
} else {
path = prefix + (path === "/" ? "" : path);
}
}
return path;
}
/********************
* Object properties
*********************/
var routes = [];
var prefix = null;
var activePath = window.location.pathname;
var $root = document.getElementById(id) || null;
var transitionConfig = {
time: 400,
effect: "ease-in",
applyAfter: 200
};
var notFoundHandler = noop;
/*******************
* Private methods
********************/
function show() {
$root.style.opacity = 1;
$root.style.transition = (
"opacity " +
transitionConfig.time + "ms " +
transitionConfig.effect
);
}
function hide() {
$root.style.opacity = 0;
}
function getTransitionConfig() {
return transitionConfig;
}
function setTransitionConfig(opts) {
transitionConfig = opts;
}
function transitionView(route) {
hide();
setTimeout(function() {
route.handler(route.params ? extractParams(route) : null);
show();
}, transitionConfig.applyAfter);
}
function changeView(route) {
// handle not found page
if(isFunction(route)) {
route();
return;
}
if($root) {
transitionView(route);
} else {
route.handler(route.params ? extractParams(route) : null);
}
}
function addRegexRoute(path, cb, mw) {
var placeholder = /\/:[^\/]+/g;
var params = path.match(placeholder);
path = new RegExp("^" + path.replace(placeholder, '/([^/]+)') + "$");
routes.push({
path: path,
handler: cb,
isRegex: true,
params: params,
middleware: mw || null
});
}
function addRouteMiddleware(path, mw, cb) {
if(!isString(path)) typeError("string", typeof path);
if(!isArray(mw)) typeError("array", typeof mw);
if(!isFunction(cb)) typeError("function", typeof cb);
if(isRegexPath(path)) {
addRegexRoute(path, cb, mw);
} else {
routes.push({ path: path, middleware: mw, handler: cb });
}
}
function addRoute(path, handler) {
if(!isString(path)) typeError("string", typeof path);
if(!isFunction(handler)) typeError("function", typeof handler);
if(isRegexPath(path)) {
addRegexRoute(path, handler);
} else {
routes.push({ path: path, handler: handler });
}
}
function returnMatch(path) {
return routes.find(function(route) {
return (route.isRegex && path.match(route.path)) ||
route.path === path ? route : false;
});
}
function executeMiddleware(arr) {
arr.forEach(function(mw) {
if(isFunction(mw)) mw();
});
}
function findRoute(path) {
var match = returnMatch(path);
if(match) {
if(match.middleware) executeMiddleware(match.middleware);
changeView(match);
} else {
changeView(notFoundHandler);
}
}
function checkForRouteChange(path) {
if(activePath !== path) {
activePath = path;
findRoute(path);
}
}
function setupClickListener() {
document.addEventListener("click", function(e) {
var el = e.target;
if(el.nodeName.toLowerCase() === "a") {
if(el.href.indexOf(window.location.host) !== -1) {
e.preventDefault();
history.pushState(el.pathname, null, el.pathname);
checkForRouteChange(el.pathname);
}
}
}, false);
}
function setupPopstateListener() {
window.addEventListener("popstate", function(e) {
findRoute(e.state);
});
}
/*****************
* Public methods
******************/
function when(path, middleware, handler) {
// construct path with prefix if is set by namespace method
path = prefixPath(path, prefix);
if(arguments.length >= 3) {
addRouteMiddleware(path, middleware, handler);
} else {
handler = middleware;
addRoute(path, handler);
}
return this;
}
function getRoutes() {
return routes;
}
function namespace(name) {
prefix = name;
return this;
}
function start() {
setupClickListener();
setupPopstateListener();
findRoute(window.location.pathname);
}
function goTo(path) {
history.pushState(path, null, path);
findRoute(path);
}
function redirect(path) {
this.goTo(path);
}
function notFound(handler) {
notFoundHandler = handler;
return this;
}
function remove(path) {
routes = routes.filter(function(route) {
return (route.isRegex && path.match(route.path)) ||
(route.path === path) ? false : true;
});
}
function flush() {
routes = [];
activePath = window.location.pathname;
prefix = null;
transitionTime = 400;
notFoundHandler = noop;
}
/********************
* Export Public API
*********************/
return {
when: when,
notFound: notFound,
start: start,
namespace: namespace,
getRoutes: getRoutes,
getTransitionConfig: getTransitionConfig,
configureTransition: setTransitionConfig,
goTo: goTo,
redirect: redirect,
remove: remove,
flush: flush
}
}, this);