-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.route32.js
154 lines (145 loc) · 6 KB
/
jquery.route32.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
/**
* Route32 --## Simple Anchor Location Router ##--
* executes callback on location hash change that matches declared routes.
* Intenteded to be use as a piece on JavaScript MVC Apps
*
* @author Rolando Garro <[email protected]>
* @requires jQuery
*/
//BEGIN..
//Requires jQuery
if(typeof jQuery != "undefined"){
/* begin Route32 */
function Route32(options){
//Settings
var settings = $.extend({
//true executes routes from location hash changes, false from clicks
'automatic':true,
//selector from which we listen clicks when automatic false allow re-execution of same route
'selector':'.nav',
//manual drive is click based so needs an intentional delay to proper update latest hash
'manualShiftChangeTime':100
},options);
//array of hashes containing hashsregexpstr,callbackfunc pairs
var routes = [];
var activeHash = '';
var lastHash = '';
//has variables
var hasVariables = false;
//methods
var methods = {
//initial method
'init':function(){
},
//verifies is string is a valid location hash
'isValidHash':function(hashStr){
return true;
},
'isValidCallbackfunc':function(callbackfunc){
if(typeof callbackfunc == "function"){
return true;
}else{
return false;
}
},
'getHashValue':function(evt){
//return window.location.hash;
return "#" + evt.newURL.split("#")[1]
},
'activeHashFromLocation':function(){
return "#" + window.location.hash.split("#")[1];
},
'hasVariables':function(){
if(window.location.hash.split("#").length >2){
hasVariables = true;
return true;
}else{
hasVariables = false;
return false
}
},
'getVariables':function(){
var varstr = '';
varstr = window.location.hash.split("#")[2];
var retObj = new Object();
if(/[?]([\w#!:.?+=&%@!\-\/])/.test(varstr)){
var tmpstr = varstr.slice(1);
var p = tmpstr.split("&");
for(var i=0;i<p.length;i++){
var h = p[i].split("=");
var cmdstr = "retObj."+h[0]+" = '"+h[1]+"';";
eval(cmdstr);
}
return retObj;
}else{
alert("Correct variable format is #?name=value sepaired by &.");
}
},
'executeCurrent':function(){
$.each(routes,function(index,value){
if(value.hash == activeHash){
if(methods.hasVariables()){
vars = methods.getVariables();
value.callback(vars);
}else{
value.callback();
}
}
});
},
'updateHashExecute':function(){
window.onhashchange = function(evt){
activeHash = methods.getHashValue(evt);
methods.executeCurrent();
};
},
'manualDrive':function(){
$(settings.selector).live('click',function(){
setTimeout(function(){
activeHash = methods.activeHashFromLocation();
methods.executeCurrent();
},settings.manualShiftChangeTime);
});
}
};
//variables can be passed after a second # singn on the hash as a query string
this.getVariables = function(){
}
//adds routes
this.add = function(hashRegexpStr,callbackfunc){
if(methods.isValidHash(hashRegexpStr) && methods.isValidCallbackfunc(callbackfunc)){
routes.push({hash:hashRegexpStr,callback:callbackfunc});
}else{
alert('route should be a valid hash string, callback function pair.');
}
};
//starts driving
this.drive = function(){
if(routes.length > 0){
if(settings.automatic){
//start listening location changes
methods.updateHashExecute();
}else{
//listen selector click
methods.manualDrive();
}
activeHash = methods.activeHashFromLocation();
if(activeHash.length > 1){//initial verification
methods.executeCurrent();
}
}else{
alert('use add method to add routes');
}
};
//executes actual route arbitrarily
this.again = function(){
methods.executeCurrent();
};
methods.init();
return this;
};
/* end Route32 */
}else{
alert("jQuery is required to ride Route32.");
}
//END..