-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrouter.js
357 lines (317 loc) · 13.3 KB
/
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
// RequireJS Router - A scalable, lazy loading, AMD router.
//
// Version: 0.8.0
//
// The MIT License (MIT)
// Copyright (c) 2014 Erik Ringsmuth
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
define([], function() {
'use strict';
// Private closure variables
var cachedUrlPaths = {};
var eventHandlers = {
statechange: [],
routeload: []
};
// In some modern browsers a hashchange also fires a popstate. There isn't a check to see if the browser will fire
// one or both. We have to keep track of the previous state to prevent it from fireing a statechange twice.
var previousState = '';
var popstateHashchangeEventLisener = function popstateHashchangeEventLisener() {
if (previousState != window.location.href) {
previousState = window.location.href;
router.fire('statechange');
}
};
// router public interface
//
// There is only one instance of the router. Loading it in multiple modules will always load the same router.
var router = {
// router.init([options]) - initializes the router
init: function init(options) {
if (typeof(options) === 'undefined') options = {};
// Set up the window popstate and hashchange event listeners
if (window.addEventListener) {
window.addEventListener('popstate', popstateHashchangeEventLisener, false);
window.addEventListener('hashchange', popstateHashchangeEventLisener, false);
} else {
// IE 8 and lower
window.attachEvent('popstate', popstateHashchangeEventLisener); // In case pushState has been polyfilled
window.attachEvent('onhashchange', popstateHashchangeEventLisener);
}
// Call loadCurrentRoute on every statechange event
if (options.loadCurrentRouteOnStateChange !== false) {
router.on('statechange', function onstatechange() {
router.loadCurrentRoute();
});
}
// Fire the initial statechange event
if (options.fireInitialStateChange !== false) {
router.fire('statechange');
}
return router;
},
// router.routes - All registered routes
routes: {},
// router.activeRoute - A reference to the active route
activeRoute: {},
// router.registerRoutes(routes) - Register routes
//
// This will add the routes to the existing routes. Specifying a route with the same name as an existing route will
// overwrite the old route with the new one.
//
// Example
// router.registerRoutes({
// home: {path: '/', moduleId: 'home/homeView'},
// customer: {path: '/customer/:id', moduleId: 'customer/customerView'},
// notFound: {path: '*', moduleId: 'notFound/notFoundView'}
// })
registerRoutes: function registerRoutes(routes) {
for (var route in routes) {
if (routes.hasOwnProperty(route)) {
router.routes[route] = routes[route];
}
}
return router;
},
// router.on(eventName, eventHandler([arg1, [arg2]]) {}) - Register an event handler
//
// The two main events are 'statechange' and 'routeload'.
on: function on(eventName, eventHandler) {
if (typeof(eventHandlers[eventName]) === 'undefined') eventHandlers[eventName] = [];
eventHandlers[eventName].push(eventHandler);
return router;
},
// router.fire(eventName, [arg1, [arg2]]) - Fire an event
//
// This will call all eventName event handlers with the arguments passed in.
fire: function fire(eventName) {
if (eventHandlers[eventName]) {
var eventArguments = Array.prototype.slice.call(arguments, 1);
for (var i = 0; i < eventHandlers[eventName].length; i++) {
eventHandlers[eventName][i].apply(router, eventArguments);
}
}
return router;
},
// router.off(eventName, eventHandler) - Remove an event handler
//
// If you want remove an event handler you need to keep a reference to it so you can tell router.off() with the
// original event handler.
off: function off(eventName, eventHandler) {
if (eventHandlers[eventName]) {
var eventHandlerIndex = eventHandlers[eventName].indexOf(eventHandler);
if (eventHandlerIndex !== -1) {
eventHandlers[eventName].splice(eventHandlerIndex, 1);
}
}
return router;
},
// router.loadCurrentRoute() - Manually tell the router to load the module for the current route
loadCurrentRoute: function loadCurrentRoute() {
for (var i in router.routes) {
if (router.routes.hasOwnProperty(i)) {
var route = router.routes[i];
if (router.testRoute(route)) {
// This is the first route to match the current URL
// Replace router.activeRoute with this route
router.activeRoute.active = false;
route.active = true;
router.activeRoute = route;
// Load the route's module
require([route.moduleId], function(module) {
// Make sure this is still the active route from when loadCurrentRoute was called. The asynchronous nature
// of AMD loaders means we could have fireed multiple hashchanges or popstates before the AMD module finished
// loading. If we navigate to route /a then navigate to route /b but /b finishes loading before /a we don't
// want /a to be rendered since we're actually at route /b.
if (route.active) {
router.fire('routeload', module, router.routeArguments(route, window.location.href));
}
});
break;
}
}
}
return router;
},
// urlPath(url) - Parses the url to get the path
//
// This will return the hash path if it exists or return the real path if no hash path exists.
//
// Example URL = 'http://domain.com/other/path?queryParam3=false#/example/path?queryParam1=true&queryParam2=example%20string'
// path = '/example/path'
//
// Note: The URL must contain the protocol like 'http(s)://'
urlPath: function urlPath(url) {
// Check the cache to see if we've already parsed this URL
if (typeof(cachedUrlPaths[url]) !== 'undefined') {
return cachedUrlPaths[url];
}
// The relative URI is everything after the third slash including the third slash
// Example relativeUri = '/other/path?queryParam3=false#/example/path?queryParam1=true&queryParam2=example%20string'
var splitUrl = url.split('/');
var relativeUri = '/' + splitUrl.splice(3, splitUrl.length - 3).join('/');
// The path is everything in the relative URI up to the first ? or #
// Example path = '/other/path'
var path = relativeUri.split(/[\?#]/)[0];
// The hash is everything from the first # up to the the search starting with ? if it exists
// Example hash = '#/example/path'
var hashIndex = relativeUri.indexOf('#');
if (hashIndex !== -1) {
var hash = relativeUri.substring(hashIndex).split('?')[0];
if (hash.substring(0, 2) === '#/') {
// Hash path
path = hash.substring(1);
} else if (hash.substring(0, 3) === '#!/') {
// Hashbang path
path = hash.substring(2);
}
}
// Cache the path for this URL
cachedUrlPaths[url] = path;
return path;
},
// router.testRoute(route, [url]) - Test if the route matches the current URL
//
// This algorithm tries to fail or succeed as quickly as possible for the most common cases.
testRoute: function testRoute(route, url) {
// Example path = '/example/path'
// Example route: `exampleRoute: {path: '/example/*', moduleId: 'example/exampleView'}`
var path = router.urlPath(url || window.location.href);
// If the path is an exact match or '*' then the route is a match
if (route.path === path || route.path === '*') {
return true;
}
// Test if it's a regular expression
if (route.path instanceof RegExp) {
return route.path.test(path);
}
// Look for wildcards
if (route.path.indexOf('*') === -1 && route.path.indexOf(':') === -1) {
// No wildcards and we already made sure it wasn't an exact match so the test fails
return false;
}
// Example pathSegments = ['', example', 'path']
var pathSegments = path.split('/');
// Example routePathSegments = ['', 'example', '*']
var routePathSegments = route.path.split('/');
// There must be the same number of path segments or it isn't a match
if (pathSegments.length !== routePathSegments.length) {
return false;
}
// Check equality of each path segment
for (var i in routePathSegments) {
if (routePathSegments.hasOwnProperty(i)) {
// The path segments must be equal, be a wildcard segment '*', or be a path parameter like ':id'
var routeSegment = routePathSegments[i];
if (routeSegment !== pathSegments[i] && routeSegment !== '*' && routeSegment.charAt(0) !== ':') {
// The path segment wasn't the same string and it wasn't a wildcard or parameter
return false;
}
}
}
// Nothing failed. The route matches the URL.
return true;
},
// router.routeArguments([route, [url]]) - Gets the path variables and query parameter values from the URL
//
// Both parameters are optional.
routeArguments: function routeArguments(route, url) {
if (!route) route = router.activeRoute;
if (!url) url = window.location.href;
var args = {};
var path = router.urlPath(url);
// Example pathSegments = ['', example', 'path']
var pathSegments = path.split('/');
// Example routePathSegments = ['', 'example', '*']
var routePathSegments = [];
if (route && route.path && !(route.path instanceof RegExp)) {
routePathSegments = route.path.split('/');
}
// Get path variables
// URL '/customer/123'
// and route `{path: '/customer/:id'}`
// gets id = '123'
for (var segmentIndex in routePathSegments) {
if (routePathSegments.hasOwnProperty(segmentIndex)) {
var routeSegment = routePathSegments[segmentIndex];
if (routeSegment.charAt(0) === ':') {
args[routeSegment.substring(1)] = pathSegments[segmentIndex];
}
}
}
// Get the query parameter values
// The search is the query parameters including the leading '?'
var searchIndex = url.indexOf('?');
var search = '';
if (searchIndex !== -1) {
search = url.substring(searchIndex);
var hashIndex = search.indexOf('#');
if (hashIndex !== -1) {
search = search.substring(0, hashIndex);
}
}
// If it's a hash URL we need to get the search from the hash
var hashPathIndex = url.indexOf('#/');
var hashBangPathIndex = url.indexOf('#!/');
if (hashPathIndex !== -1 || hashBangPathIndex !== -1) {
var hash = '';
if (hashPathIndex !== -1) {
hash = url.substring(hashPathIndex);
} else {
hash = url.substring(hashBangPathIndex);
}
searchIndex = hash.indexOf('?');
if (searchIndex !== -1) {
search = hash.substring(searchIndex);
}
}
var queryParameters = search.substring(1).split('&');
// split() on an empty string has a strange behavior of returning [''] instead of []
if (queryParameters.length === 1 && queryParameters[0] === '') {
queryParameters = [];
}
for (var i in queryParameters) {
if (queryParameters.hasOwnProperty(i)) {
var queryParameter = queryParameters[i];
var queryParameterParts = queryParameter.split('=');
args[queryParameterParts[0]] = queryParameterParts.splice(1, queryParameterParts.length - 1).join('=');
}
}
// Parse the arguments into unescaped strings, numbers, or booleans
for (var arg in args) {
var value = args[arg];
if (value === 'true') {
args[arg] = true;
} else if (value === 'false') {
args[arg] = false;
} else if (!isNaN(value) && value !== '' && value.charAt(0) !== '0') {
// numeric
args[arg] = +value;
} else {
// string
args[arg] = decodeURIComponent(value);
}
}
return args;
}
};
// Return the router
return router;
});