forked from camme/jquery-router-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.router.js
371 lines (300 loc) · 10.7 KB
/
jquery.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
plugin name: router
jquery plugin to handle routes with both hash and push state
why? why another routing plugin? because i couldnt find one that handles both hash and pushstate
created by 24hr // camilo.tapia
author twitter: camilo.tapia
Copyright 2011 camilo tapia // 24hr (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
(function($){
var hasPushState = (history && history.pushState);
var hasHashState = !hasPushState && ("onhashchange" in window) && false;
var router = {};
var routeList = [];
var eventAdded = false;
var currentUsedUrl = location.href; //used for ie to hold the current url
var firstRoute = true;
var errorCallback = function () {};
// hold the latest route that was activated
router.currentId = "";
router.currentParameters = {};
// Create a default error handler
router.errorCallback = errorCallback;
router.capabilities = {
hash: hasHashState,
pushState: hasPushState,
timer: !hasHashState && !hasPushState
};
// reset all routes
router.reset = function()
{
var router = {};
var routeList = [];
router.currentId = "";
router.currentParameters = {};
}
router.add = function(route, id, callback)
{
// if we only get a route and a callback, we switch the arguments
if (typeof id == "function")
{
callback = id;
delete id;
}
var isRegExp = typeof route == "object";
if (!isRegExp)
{
// remove the last slash to unifiy all routes
if (route.lastIndexOf("/") == route.length - 1)
{
route = route.substring(0, route.length - 1);
}
// if the routes where created with an absolute url ,we have to remove the absolut part anyway, since we cant change that much
route = route.replace(location.protocol + "//", "").replace(location.hostname, "");
}
var routeItem = {
route: route,
callback: callback,
type: isRegExp ? "regexp" : "string",
id: id
}
routeList.push(routeItem);
// we add the event listener after the first route is added so that we dont need to listen to events in vain
if (!eventAdded)
{
bindStateEvents();
}
};
router.addErrorHandler = function (callback)
{
this.errorCallback = callback;
};
function bindStateEvents()
{
eventAdded = true;
// default value telling router that we havent replaced the url from a hash. yet.
router.fromHash = false;
if (hasPushState)
{
// if we get a request with a qualified hash (ie it begins with #!)
if (location.hash.indexOf("#!/") === 0)
{
// replace the state
var url = location.pathname + location.hash.replace(/^#!\//gi, "");
history.replaceState({}, "", url);
// this flag tells router that the url was converted from hash to popstate
router.fromHash = true;
}
$(window).bind("popstate", handleRoutes);
}
else if (hasHashState)
{
$(window).bind("hashchange.router", handleRoutes);
}
else
{
// if no events are available we use a timer to check periodically for changes in the url
setInterval(
function()
{
if (location.href != currentUsedUrl)
{
handleRoutes();
currentUsedUrl = location.href;
}
}, 500
);
}
}
bindStateEvents();
router.go = function(url, title)
{
if (hasPushState)
{
history.pushState({}, title, url);
checkRoutes();
}
else
{
// remove part of url that we dont use
url = url.replace(location.protocol + "//", "").replace(location.hostname, "");
var hash = url.replace(location.pathname, "");
if (hash.indexOf("!") < 0)
{
hash = "!/" + hash;
}
location.hash = hash;
}
};
// do a check without affecting the history
router.check = router.redo = function()
{
checkRoutes(true);
};
// parse and wash the url to process
function parseUrl(url)
{
var currentUrl = url ? url : location.pathname;
currentUrl = decodeURI(currentUrl);
// if no pushstate is availabe we have to use the hash
if (!hasPushState)
{
if (location.hash.indexOf("#!/") === 0)
{
currentUrl += location.hash.substring(3);
}
else
{
return '';
}
}
// and if the last character is a slash, we just remove it
currentUrl = currentUrl.replace(/\/$/, "");
return currentUrl;
}
// get the current parameters for either a specified url or the current one if parameters is ommited
router.parameters = function(url)
{
// parse the url so that we handle a unified url
var currentUrl = parseUrl(url);
// get the list of actions for the current url
var list = getParameters(currentUrl);
// if the list is empty, return an empty object
if (list.length == 0)
{
router.currentParameters = {};
}
// if we got results, return the first one. at least for now
else
{
router.currentParameters = list[0].data;
}
return router.currentParameters;
}
function getParameters(url)
{
var dataList = [];
// console.log("ROUTES:");
for(var i = 0, ii = routeList.length; i < ii; i++)
{
var route = routeList[i];
// check for mathing reg exp
if (route.type == "regexp")
{
var result = url.match(route.route);
if (result)
{
var data = {};
data.matches = result;
dataList.push(
{
route: route,
data: data
}
);
// saves the current route id
router.currentId = route.id;
// break after first hit
break;
}
}
// check for mathing string routes
else
{
var currentUrlParts = url.split("/");
var routeParts = route.route.split("/");
//console.log("matchCounter ", matchCounter, url, route.route)
// first check so that they have the same amount of elements at least
if (routeParts.length == currentUrlParts.length)
{
var data = {};
var matched = true;
var matchCounter = 0;
for(var j = 0, jj = routeParts.length; j < jj; j++)
{
var isParam = routeParts[j].indexOf(":") === 0;
if (isParam)
{
data[routeParts[j].substring(1)] = decodeURI(currentUrlParts[j]);
matchCounter++;
}
else
{
if (routeParts[j] == currentUrlParts[j])
{
matchCounter++;
}
}
}
// break after first hit
if (routeParts.length == matchCounter)
{
dataList.push(
{
route: route,
data: data
}
);
// saved the current route id
router.currentId = route.id;
router.currentParameters = data;
break;
}
}
}
}
return dataList;
}
function checkRoutes()
{
var currentUrl = parseUrl(location.pathname);
// check if something is catched
var actionList = getParameters(currentUrl);
// If no routes have been matched
if (actionList.length == 0) {
// Invoke error handler
return router.errorCallback(currentUrl);
}
// ietrate trough result (but it will only kick in one)
for(var i = 0, ii = actionList.length; i < ii; i++)
{
actionList[i].route.callback(actionList[i].data);
}
}
function handleRoutes(e)
{
if (e != null && e.originalEvent && e.originalEvent.state !== undefined)
{
checkRoutes();
}
else if (hasHashState)
{
checkRoutes();
}
else if (!hasHashState && !hasPushState)
{
checkRoutes();
}
}
if (!$.router)
{
$.router = router;
}
else
{
if (window.console && window.console.warn)
{
console.warn("jQuery.status already defined. Something is using the same name.");
}
}
})( jQuery );