-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.simpleRouter.js
60 lines (46 loc) · 1.71 KB
/
jquery.simpleRouter.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
/**
* jQuery Simple Router Plugin 1.0.1
* http://www.lfbittencourt.com/jquery-simple-router-plugin
*
* Copyright (c) 2011 Luís Fernando Bittencourt
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Take this one as a lightweight SugarSkull:
* https://github.com/hij1nx/SugarSkull
*/
(function($) {
// Default settings
var settings = {
checkInterval: 50,
isCaseInsensitive: false,
routes: null
};
var hash = null;
$.simpleRouter = function(options) {
if (options) {
$.extend(settings, options);
}
if (jQuery.isPlainObject(settings.routes)) {
window.setInterval(function() {
var currentHash = document.location.hash;
if (currentHash != hash) {
hash = currentHash;
for (var i in settings.routes) {
if (jQuery.isFunction(settings.routes[i])) {
var regExp = new RegExp(i, settings.isCaseInsensitive ? 'i' : '');
if (regExp.test(hash)) {
var matches = hash.match(regExp);
// Remove the entire input from the matches
matches.shift();
// Call the function passing matches as arguments
settings.routes[i].apply(null, matches);
break;
}
}
}
}
}, settings.checkInterval);
}
};
})(jQuery);