|
1 | 1 | function Target(path, matcher, delegate) { |
2 | | - this.path = path; |
3 | | - this.matcher = matcher; |
4 | | - this.delegate = delegate; |
| 2 | + this.path = path |
| 3 | + this.matcher = matcher |
| 4 | + this.delegate = delegate |
5 | 5 | } |
6 | 6 |
|
7 | 7 | Target.prototype = { |
8 | 8 | to: function(target, callback) { |
9 | | - var delegate = this.delegate; |
| 9 | + var delegate = this.delegate |
10 | 10 |
|
11 | 11 | if (delegate && delegate.willAddRoute) { |
12 | | - target = delegate.willAddRoute(this.matcher.target, target); |
| 12 | + target = delegate.willAddRoute(this.matcher.target, target) |
13 | 13 | } |
14 | 14 |
|
15 | | - this.matcher.add(this.path, target); |
| 15 | + this.matcher.add(this.path, target) |
16 | 16 |
|
17 | 17 | if (callback) { |
18 | | - if (callback.length === 0) { throw new Error("You must have an argument in the function passed to `to`"); } |
19 | | - this.matcher.addChild(this.path, target, callback, this.delegate); |
| 18 | + if (callback.length === 0) { throw new Error("You must have an argument in the function passed to `to`") } |
| 19 | + this.matcher.addChild(this.path, target, callback, this.delegate) |
20 | 20 | } |
21 | | - return this; |
| 21 | + return this |
22 | 22 | } |
23 | | -}; |
| 23 | +} |
24 | 24 |
|
25 | 25 | function Matcher(target) { |
26 | | - this.routes = {}; |
27 | | - this.children = {}; |
28 | | - this.target = target; |
| 26 | + this.routes = {} |
| 27 | + this.children = {} |
| 28 | + this.target = target |
29 | 29 | } |
30 | 30 |
|
31 | 31 | Matcher.prototype = { |
32 | 32 | add: function(path, handler) { |
33 | | - this.routes[path] = handler; |
| 33 | + this.routes[path] = handler |
34 | 34 | }, |
35 | 35 |
|
36 | 36 | addChild: function(path, target, callback, delegate) { |
37 | | - var matcher = new Matcher(target); |
38 | | - this.children[path] = matcher; |
| 37 | + var matcher = new Matcher(target) |
| 38 | + this.children[path] = matcher |
39 | 39 |
|
40 | | - var match = generateMatch(path, matcher, delegate); |
| 40 | + var match = generateMatch(path, matcher, delegate) |
41 | 41 |
|
42 | 42 | if (delegate && delegate.contextEntered) { |
43 | | - delegate.contextEntered(target, match); |
| 43 | + delegate.contextEntered(target, match) |
44 | 44 | } |
45 | 45 |
|
46 | | - callback(match); |
| 46 | + callback(match) |
47 | 47 | } |
48 | | -}; |
| 48 | +} |
49 | 49 |
|
50 | 50 | function generateMatch(startingPath, matcher, delegate) { |
51 | 51 | return function(path, nestedCallback) { |
52 | | - var fullPath = startingPath + path; |
| 52 | + var fullPath = startingPath + path |
53 | 53 |
|
54 | 54 | if (nestedCallback) { |
55 | | - nestedCallback(generateMatch(fullPath, matcher, delegate)); |
| 55 | + nestedCallback(generateMatch(fullPath, matcher, delegate)) |
56 | 56 | } else { |
57 | | - return new Target(startingPath + path, matcher, delegate); |
| 57 | + return new Target(startingPath + path, matcher, delegate) |
58 | 58 | } |
59 | | - }; |
| 59 | + } |
60 | 60 | } |
61 | 61 |
|
62 | 62 | function addRoute(routeArray, path, handler) { |
63 | | - var len = 0; |
| 63 | + var len = 0 |
64 | 64 | for (var i=0, l=routeArray.length; i<l; i++) { |
65 | | - len += routeArray[i].path.length; |
| 65 | + len += routeArray[i].path.length |
66 | 66 | } |
67 | 67 |
|
68 | | - path = path.substr(len); |
69 | | - var route = { path: path, handler: handler }; |
70 | | - routeArray.push(route); |
| 68 | + path = path.substr(len) |
| 69 | + var route = { path: path, handler: handler } |
| 70 | + routeArray.push(route) |
71 | 71 | } |
72 | 72 |
|
73 | 73 | function eachRoute(baseRoute, matcher, callback, binding) { |
74 | | - var routes = matcher.routes; |
| 74 | + var routes = matcher.routes |
75 | 75 |
|
76 | 76 | for (var path in routes) { |
77 | 77 | if (routes.hasOwnProperty(path)) { |
78 | | - var routeArray = baseRoute.slice(); |
79 | | - addRoute(routeArray, path, routes[path]); |
| 78 | + var routeArray = baseRoute.slice() |
| 79 | + addRoute(routeArray, path, routes[path]) |
80 | 80 |
|
81 | 81 | if (matcher.children[path]) { |
82 | | - eachRoute(routeArray, matcher.children[path], callback, binding); |
| 82 | + eachRoute(routeArray, matcher.children[path], callback, binding) |
83 | 83 | } else { |
84 | | - callback.call(binding, routeArray); |
| 84 | + callback.call(binding, routeArray) |
85 | 85 | } |
86 | 86 | } |
87 | 87 | } |
88 | 88 | } |
89 | 89 |
|
90 | 90 | export default function(callback, addRouteCallback) { |
91 | | - var matcher = new Matcher(); |
| 91 | + var matcher = new Matcher() |
92 | 92 |
|
93 | | - callback(generateMatch("", matcher, this.delegate)); |
| 93 | + callback(generateMatch("", matcher, this.delegate)) |
94 | 94 |
|
95 | 95 | eachRoute([], matcher, function(route) { |
96 | | - if (addRouteCallback) { addRouteCallback(this, route); } |
97 | | - else { this.add(route); } |
98 | | - }, this); |
| 96 | + if (addRouteCallback) { addRouteCallback(this, route) } |
| 97 | + else { this.add(route) } |
| 98 | + }, this) |
99 | 99 | } |
0 commit comments