forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathroute.js
281 lines (256 loc) · 9.53 KB
/
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
/**
* @workInProgress
* @ngdoc service
* @name angular.service.$route
* @requires $location
*
* @property {Object} current Reference to the current route definition.
* @property {Array.<Object>} routes Array of all configured routes.
*
* @description
* Watches `$location.hash` and tries to map the hash to an existing route
* definition. It is used for deep-linking URLs to controllers and views (HTML partials).
*
* The `$route` service is typically used in conjunction with {@link angular.widget.ng:view ng:view}
* widget.
*
* @example
This example shows how changing the URL hash causes the <tt>$route</tt>
to match a route against the URL, and the <tt>[[ng:include]]</tt> pulls in the partial.
Try changing the URL in the input box to see changes.
<doc:example>
<doc:source>
<script>
angular.service('myApp', function($route) {
$route.when('/Book/:bookId', {template:'rsrc/book.html', controller:BookCntl});
$route.when('/Book/:bookId/ch/:chapterId', {template:'rsrc/chapter.html', controller:ChapterCntl});
$route.onChange(function() {
$route.current.scope.params = $route.current.params;
});
}, {$inject: ['$route']});
function BookCntl() {
this.name = "BookCntl";
}
function ChapterCntl() {
this.name = "ChapterCntl";
}
</script>
Chose:
<a href="#/Book/Moby">Moby</a> |
<a href="#/Book/Moby/ch/1">Moby: Ch1</a> |
<a href="#/Book/Gatsby">Gatsby</a> |
<a href="#/Book/Gatsby/ch/4?key=value">Gatsby: Ch4</a><br/>
<input type="text" name="$location.hashPath" size="80" />
<pre>$location={{$location}}</pre>
<pre>$route.current.template={{$route.current.template}}</pre>
<pre>$route.current.params={{$route.current.params}}</pre>
<pre>$route.current.scope.name={{$route.current.scope.name}}</pre>
<hr/>
<ng:include src="$route.current.template" scope="$route.current.scope"/>
</doc:source>
<doc:scenario>
</doc:scenario>
</doc:example>
*/
angularServiceInject('$route', function(location, $updateView) {
var routes = {},
onChange = [],
matcher = switchRouteMatcher,
parentScope = this,
dirty = 0,
locationWatchProp = 'hash',
$route = {
routes: routes,
/**
* @workInProgress
* @ngdoc method
* @name angular.service.$route#onChange
* @methodOf angular.service.$route
*
* @param {function()} fn Function that will be called when `$route.current` changes.
* @returns {function()} The registered function.
*
* @description
* Register a handler function that will be called when route changes
*/
onChange: function(fn) {
onChange.push(fn);
return fn;
},
/**
* @workInProgress
* @ngdoc method
* @name angular.service.$route#parent
* @methodOf angular.service.$route
*
* @param {Scope} [scope=rootScope] Scope to be used as parent for newly created
* `$route.current.scope` scopes.
*
* @description
* Sets a scope to be used as the parent scope for scopes created on route change. If not
* set, defaults to the root scope.
*/
parent: function(scope) {
if (scope) parentScope = scope;
},
/**
* @workInProgress
* @ngdoc method
* @name angular.service.$route#when
* @methodOf angular.service.$route
*
* @param {string} path Route path (matched against `$location.hash`)
* @param {Object} params Mapping information to be assigned to `$route.current` on route
* match.
*
* Object properties:
*
* - `controller` – `{function()=}` – Controller fn that should be associated with newly
* created scope.
* - `template` – `{string=}` – path to an html template that should be used by
* {@link angular.widget.ng:view ng:view} or
* {@link angular.widget.ng:include ng:include} widgets.
* - `redirectTo` – {(string|function())=} – value to update
* {@link angular.service.$location $location} hash with and trigger route redirection.
*
* If `redirectTo` is a function, it will be called with the following parameters:
*
* - `{Object.<string>}` - route parameters extracted from the current
* `$location.hashPath` by applying the current route template.
* - `{string}` - current `$location.hash`
* - `{string}` - current `$location.hashPath`
* - `{string}` - current `$location.hashSearch`
*
* The custom `redirectTo` function is expected to return a string which will be used
* to update `$location.hash`.
*
* @returns {Object} route object
*
* @description
* Adds a new route definition to the `$route` service.
*/
when:function (path, params) {
if (isUndefined(path)) return routes; //TODO(im): remove - not needed!
var route = routes[path];
if (!route) route = routes[path] = {};
if (params) extend(route, params);
dirty++;
return route;
},
/**
* @workInProgress
* @ngdoc method
* @name angular.service.$route#otherwise
* @methodOf angular.service.$route
*
* @description
* Sets route definition that will be used on route change when no other route definition
* is matched.
*
* @param {Object} params Mapping information to be assigned to `$route.current`.
*/
otherwise: function(params) {
$route.when(null, params);
},
/**
* @workInProgress
* @ngdoc method
* @name angular.service.$route#reload
* @methodOf angular.service.$route
*
* @description
* Causes `$route` service to reload (and recreate the `$route.current` scope) upon the next
* eval even if {@link angular.service.$location $location} hasn't changed.
*/
reload: function() {
dirty++;
},
/**
* @workInProgress
* @ngdoc method
* @name angular.service.$route#watchHashPathOnly
* @methodOf angular.service.$route
*
* @description
* Causes `$route` service to be reload only if `$location.hashPath` changes. Other properties of
* {@link angular.service.$location $location service} won't trigger reloads.
*/
watchHashPathOnly: function(hashPathOnly) {
locationWatchProp = hashPathOnly ? 'hashPath' : 'hash';
}
};
function switchRouteMatcher(on, when, dstName) {
var regex = '^' + when.replace(/[\.\\\(\)\^\$]/g, "\$1") + '$',
params = [],
dst = {};
forEach(when.split(/\W/), function(param){
if (param) {
var paramRegExp = new RegExp(":" + param + "([\\W])");
if (regex.match(paramRegExp)) {
regex = regex.replace(paramRegExp, "([^\/]*)$1");
params.push(param);
}
}
});
var match = on.match(new RegExp(regex));
if (match) {
forEach(params, function(name, index){
dst[name] = match[index + 1];
});
if (dstName) this.$set(dstName, dst);
}
return match ? dst : null;
}
function updateRoute(){
var childScope, routeParams, pathParams, segmentMatch, key, redir;
$route.current = null;
forEach(routes, function(rParams, rPath) {
if (!pathParams) {
if (pathParams = matcher(location.hashPath, rPath)) {
routeParams = rParams;
}
}
});
// "otherwise" fallback
routeParams = routeParams || routes[null];
if(routeParams) {
if (routeParams.redirectTo) {
if (isString(routeParams.redirectTo)) {
// interpolate the redirectTo string
redir = {hashPath: '',
hashSearch: extend({}, location.hashSearch, pathParams)};
forEach(routeParams.redirectTo.split(':'), function(segment, i) {
if (i==0) {
redir.hashPath += segment;
} else {
segmentMatch = segment.match(/(\w+)(.*)/);
key = segmentMatch[1];
redir.hashPath += pathParams[key] || location.hashSearch[key];
redir.hashPath += segmentMatch[2] || '';
delete redir.hashSearch[key];
}
});
} else {
// call custom redirectTo function
redir = {hash: routeParams.redirectTo(pathParams, location.hash, location.hashPath,
location.hashSearch)};
}
location.update(redir);
$updateView(); //TODO this is to work around the $location<=>$browser issues
return;
}
childScope = createScope(parentScope);
$route.current = extend({}, routeParams, {
scope: childScope,
params: extend({}, location.hashSearch, pathParams)
});
}
//fire onChange callbacks
forEach(onChange, parentScope.$tryEval);
if (childScope) {
childScope.$become($route.current.controller);
}
}
this.$watch(function(){return dirty + location[locationWatchProp];}, updateRoute);
return $route;
}, ['$location', '$updateView']);