-
Notifications
You must be signed in to change notification settings - Fork 0
/
slammy-1.0.js
188 lines (152 loc) · 5.44 KB
/
slammy-1.0.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
/**
* Slammy 1.0
*
* An extremely lightweight and easy hashbang
* route handler in standard JavaScript - completly
* without dependencies.
*
* @author Björn Wikström <bjorn@welcom.se>
* @license LGPL v3 <http://www.gnu.org/licenses/lgpl.html>
* @version 1.0
* @copyright Welcom Web i Göteborg AB 2013
*/
;(function (window, document, undef) {
/**
* If Slammy is already loaded, don't load it again
*/
if (typeof window.Slammy !== typeof undef) {
return;
}
var Slammy = function () {
/**
* Private variables
*/
var _routes = [],
_lastHash = false,
_noSuchRoute = function () {};
/**
* Match a route specified via `addRoute()` or `addRoutes()`
* to the current hashbang. If no such route was defined,
* call `noSuchRoute()`.
*
* @param route {String} The current hashbang route
* @returns {Void}
*/
var _findRouteAndRun = function (route) {
var found = false;
for (var i = 0; i < _routes.length; i++) {
if (_routes[i].route === route) {
found = true;
_routes[i].callback.call(window.Slammy, {});
break;
} else {
var variables = _routes[i].route.substr(1).match(/\:([a-z0-9]+)/ig),
clean = _routes[i].route.replace(/\/\:([a-z0-9]+)/ig, '');
if (route.indexOf(clean) === 0) {
var params = route.substr(route.indexOf(clean) + clean.length + 1).split('/'),
out = {};
for (var j = 0; j < variables.length; j++) {
if (params[j]) {
out[variables[j].substr(1)] = params[j];
}
}
found = true;
_routes[i].callback.call(window.Slammy, out);
break;
}
}
}
if (!found) {
window.Slammy.noSuchRoute();
}
};
/**
* A lightweight function to check the current hashbang,
* see if changed from last check.
*
* @returns {Void}
*/
var _checkHash = function () {
var hash = window.location.hash.substr(1);
if (hash !== _lastHash) {
_findRouteAndRun(hash);
}
_lastHash = hash;
};
/**
* Set up a time interval to check the
* hashbang with `_checkHash()`.
*/
setInterval(function () {
_checkHash();
}, 50);
return {
/**
* Adds a route to be handled by Slammy,
* and a callback function to be called
* when the route is visited.
*
* @param name {String} A hashbang route
* @param callback {Function} Callback for the route
* @returns {Object} The Slammy object
*/
addRoute: function (name, callback) {
_routes.push({
'route': name,
'callback': callback
});
return this;
},
/**
* Add a bunch of routes in an array
*
* @param routes {Array} A list of routes
* @returns {Object} The Slammy object
*/
addRoutes: function (routes) {
if (typeof routes !== 'object') {
return false;
}
for (var p in routes) {
if (routes.hasOwnProperty(p) && typeof routes[p] === 'function') {
this.addRoute(p, routes[p]);
}
}
return this;
},
/**
* Add a callback function for when a hashbang
* route wasn't found.
*
* @param fn {Function} The not-found-callback
* @returns {Object} The Slammy object
*/
noSuchRoute: function (fn) {
if (typeof fn === 'function') {
_noSuchRoute = fn;
} else {
_noSuchRoute.call(this, window.location.hash);
}
return this;
},
/**
* Run a route
*
* @param route {String} A specified route
* @returns {Void}
*/
run: function (route) {
var hash = _lastHash = window.location.hash.substr(1);
if (hash && hash !== '') {
_findRouteAndRun(hash);
} else {
window.location.hash = '#' + route;
}
}
};
}();
/**
* Add Slammy to the global namespace
*/
window.Slammy = Slammy;
})(window, document);