-
Notifications
You must be signed in to change notification settings - Fork 3
/
jquery.route.js
298 lines (232 loc) · 8.37 KB
/
jquery.route.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
/*!
* jQuery.hashrrouteoute
*
* Plugin for doing hash & url routing. Now with middleware!
*
*
* @version 2.1
* @author Mikko Tikkanen <[email protected]>
*/
/* jshint browser: true, devel: true */
/* global jQuery */
;(function(window, document, $, undefined) {
'use strict';
var methods = {}, // Methods namespace
O = { // Options
baseUrl: '',
verbose: false // Display logs
},
middleware = [], // Middleware stack
routeEls = [], // Route elements
$document = $(document); // Document element
/* Route
* ------------------------------------------------------------------------------------------ */
methods.route = function(route, callback) {
if(route === null || route === undefined) { return; }
// Set options. Also, callback needs to be set.
if(typeof route === 'object') { O = $.extend({}, O, route); return; }
if(!callback) { return; }
// Switch wildcards to wilcard params
route = route.replace(/(\*)/g, ':*');
// Add baseurl to the route
route = O.baseUrl + route;
// Create route object
route = {
route: route, // Original route
test: route, // Test regexp
isHash: (/^#/.test(route)), // Hash or url route
paramsNameList: [], // Param name list
callback: callback // Route callback function
};
// Remove the hash from regexp
route.test = route.test.replace('#', '');
// Collect param names & convert test to more regexp like structure
if(route.route.indexOf(':') !== -1) {
var i = 1;
route.test = route.test.replace(/:([^\/]+)/g, function(match, submatch) {
var pattern = '([^\/]+)'; // Default param pattern
// Wildcards, change pattern to accept anything
if(submatch === '*') {
pattern = '?(.*)';
submatch = '$'+i;
}
// If it's optional, change pattern to match "zero or more ("+" to "*")
if(/\?$/.test(submatch)) { pattern = pattern.replace('+', '*'); }
// Add parameter to namelist
submatch = submatch.replace(/^([^a-zA-Z0-9$]*)|([^a-zA-Z0-9$]*)$/g, ''); // TODO: Better way to do this?
route.paramsNameList.push(submatch);
i++;
return pattern;
});
}
// Set test regexp (strip start/end slashes from the original match)
route.test = new RegExp('^\/?'+route.test.replace(/^[\/]*|[\/]*$/g, '')+'\/?$', 'i');
// Add route to target element (default to document)
var el = this || $document;
var arr = el.data('routes.route') || [];
arr.push(route);
el.data('routes.route', arr);
// Store route element (if not already in the array)
var found = false;
$.each(routeEls, function(i, elem) {
if(el[0] === elem[0]) { found = true; return false; }
});
if(!found) { routeEls.push(el); }
};
/* Add middleware
* ------------------------------------------------------------------------------------------ */
methods.middleware = function(fnc) {
middleware.push(fnc);
_log('Middleware added. '+fnc.toString().substring(0, 100).replace(/[\W]*$/g, '')+'...');
};
/* Trigger route
* ------------------------------------------------------------------------------------------ */
methods.trigger = function(data) {
var e = e || jQuery.Event('popstate'); // from history API
e.url = location.pathname;
if(data) { e.data = data; }
e.params = {};
_runRoutes.call(this, e);
};
/* Add 404 route
* ------------------------------------------------------------------------------------------ */
methods[404] = function(fnc) {
methods.route.call(this, '(.*)', fnc);
};
/* Set options
* ------------------------------------------------------------------------------------------ */
methods.set = methods.options = function(key, value) {
if(typeof key === 'object') {
$.extend(O, key);
} else {
O[key] = value;
}
O.baseUrl += (!/\/$/.test(O.baseUrl) ? '/' : ''); // Make sure baseUrl ends with slash
};
/* Plugin base logic
* ========================================================================================== */
$.route = $.fn.route = function(method) {
if(methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else {
return methods.route.apply(this, arguments);
}
};
/* Hashchange, setup event and run middleware which in turn runs routes
* ------------------------------------------------------------------------------------------ */
function _hashchange(e) {
console.log('!! hashchange', e);
e = e || jQuery.Event('hashchange');
e.hash = location.hash.substring(1);
e.params = {};
_runMiddleware(e);
}
$(window).on('hashchange.route', _hashchange);
/* Urlchange, setup event and run middleware which in turn runs routes
* ------------------------------------------------------------------------------------------ */
function _urlchange(e) {
console.log('!! popstate!', e);
e = e || jQuery.Event('popstate'); // from history API
e.url = location.pathname;
e.params = {};
_runMiddleware(e);
}
$(window).on('popstate.route', _urlchange);
/* Popstate, setup event and run middleware which in turn runs routes
* ------------------------------------------------------------------------------------------ */
function _popstate(e) {
e = e || jQuery.Event('popstate'); // from history API
e.url = location.pathname;
e.params = {};
_runMiddleware(e);
}
$(window).on('popstate.route', _urlchange);
/* Run middleware
* ------------------------------------------------------------------------------------------ */
function _runMiddleware(e) {
/* jshint validthis: true */
var self = this;
// If no middleware is defined, run routes
if(!middleware.length) {
_runRoutes(e);
return;
}
// Create next function for advancing in middleware stack & call it to start the run
_log('Running middleware stack...');
var i = -1;
var next = function() {
i++;
if(i >= middleware.length) {
_runRoutes.call(self, e);
return;
}
middleware[i].next = next;
middleware[i].call(middleware[i], e);
};
next();
}
/* Run routes
* ------------------------------------------------------------------------------------------ */
function _runRoutes(e) {
/* jshint validthis: true */
var subject = (e.type === 'hashchange' ? e.hash : window.location.pathname); // Figure out the subject to run the routes to
// If "this" is specified, run routes to that element. Otherwise, run everything
var els = routeEls;
if(this && this instanceof jQuery) { els = [this]; }
// Loop through route elements
var route;
$.each(els, function(i, el) {
var arr = el.data('routes.route') || [];
// Go through routes
$.each(arr, function(i, obj) {
route = obj;
if(!route.test.test(subject)) { return; } // Evaluate the route
_log('Found route.', route.route);
// It's a hit! Process the route
var params = {};
if(route.paramsNameList.length) {
var matches = subject.match(route.test);
matches.shift(); // Remove the full match from the beginning
$.each(matches, function(i, match) {
//if(/\//.test(match)) { match = match.split('/'); }
if(/\$/.test(route.paramsNameList[i])) { match = match.split('/'); }
//console.log(route.paramsNameList[i]);
e.params[route.paramsNameList[i]] = match;
});
}
// Set variable according if elements current route changed
e.routeChanged = el.data('route.current') !== route.route;
el.data('route.current', route.route);
// Fire callback and exit route looping
route.callback.call(el, e);
return false;
});
});
// If there's active no route, create one with test regexp for current hash (for activating the nav links)
if(!route) { _log('Route not found.'); }
route = route || { test: new RegExp('^\/?'+window.location.hash.substring(1).replace(/^[\/]*|[\/]*$/g, '')+'\/?$', "i") };
// Activate nav links
_log('Activating nav links.');
$('nav, .nav').find('a[href^=#]').each(function(i, el) {
el = $(el);
el.removeClass('active');
var href = el.attr('href').substring(1);
if(route.test.test(href)) {
el.addClass('active');
}
});
}
/* Log helper
* ------------------------------------------------------------------------------------------ */
function _log() {
if(!O.verbose) { return; }
var args = [].slice.call(arguments, 0);
args[0] = 'jQuery.route - '+args[0];
console.log.apply(window.console, args);
}
// When everything's ready, fire hashchange
$(document).ready(function() {
_hashchange();
_urlchange();
});
})(window, document, jQuery);