forked from albertosarullo/Router.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.js
136 lines (114 loc) · 4.41 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
/*
Router.js - by Alberto Sarullo
forked by Marco Colombo
forked by Daniele Messa
https://github.com/albertosarullo/Router.js
*/
/* global window, document */
/* exported Router */
;(function (window, document, undefined) {
'use strict';
window.Router = (function(){
var app,
interval,
callback,
oldHash = '/Router.js',
routes = [ {controller: 'home', route: ''} ];
app = {
init: function init(routesArray) {
routes = routesArray || [];
interval = setInterval(function checkAddressChanged() {
// console.log(location.hash, oldHash);
if (document.location.hash !== oldHash) {
onChangeLocation(document.location.hash);
oldHash = document.location.hash;
}
}, 250);
return app;
},
deinit: function deinit() {
clearInterval(interval);
return app;
},
onChange: function onChange(callbackFunction) {
callback = callbackFunction;
// onChangeLocation(location.hash, oldHash);
return app;
},
go: function go(controller, params) {
var i,
route,
paramsNumber,
findedParams,
paramsMatches,
param;
controller = (controller === "") ? "home" : controller;
for (i = 0; i < routes.length; i = i + 1) {
if (routes[i].controller === controller) {
route = routes[i].route;
paramsNumber = 0;
findedParams = 0;
paramsMatches = route.match(/:[^\s\/]+/ig);
if (paramsMatches) {
paramsNumber = paramsMatches.length;
}
for (param in params) {
if (params.hasOwnProperty(param)) {
if (route.indexOf(':' + param) !== -1) {
findedParams = findedParams + 1;
route = route.replace(":" + param, params[param]);
}
}
}
if (findedParams === paramsNumber) {
break;
} else {
route = undefined;
}
}
}
if ( route !== undefined ) {
setTimeout(function() {
document.location.hash = route;
}, 0);
}
}
};
function onChangeLocation(hash) {
var i,
route,
matcher,
matchResult,
routeEvent,
j,
names;
for (i = 0; i < routes.length; i = i + 1) {
route = routes[i].route;
matcher = new RegExp(route.replace(/:[^\s\/]+/ig, '([^\\s\\/]+)'));
if (matcher.test(hash)) {
matchResult = hash.match(matcher);
// console.log("Router::onChangeLocation(",hash,") ",matchResult);
//routeEvent = document.createEvent("Event");
//routeEvent.initEvent("RouteChanged", true, true);
routeEvent = {
params: {},
controller: routes[i].controller
};
if (matchResult) {
matchResult.shift();
names = route.match(/:[^\s\/]+/ig);
for (j = 0; j < matchResult.length; j = j + 1) {
routeEvent.params[names[j].substr(1)] = matchResult[j];
}
}
// document.dispatchEvent(routeEvent);
if (callback) {
callback(routeEvent.controller, routeEvent.params);
}
break;
}
}
}
return app;
})();
})(window, document);