Skip to content

Commit 1c21f9f

Browse files
committed
Added route option to when function
Renamed arguments of when function and added the route argument (Mapping information to be assigned to $route.current on route match.) of the angular-route when function.
1 parent beee185 commit 1c21f9f

File tree

1 file changed

+69
-64
lines changed

1 file changed

+69
-64
lines changed

src/route-segment.js

Lines changed: 69 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
var mod = angular.module( 'route-segment', [] );
66
mod.provider( '$routeSegment',
77
['$routeProvider', function($routeProvider) {
8-
8+
99
var $routeSegmentProvider = this;
10-
10+
1111
var options = $routeSegmentProvider.options = {
12-
12+
1313
/**
1414
* When true, it will resolve `templateUrl` automatically via $http service and put its
1515
* contents into `template`.
1616
* @type {boolean}
1717
*/
1818
autoLoadTemplates: true,
19-
19+
2020
/**
2121
* When true, all attempts to call `within` method on non-existing segments will throw an error (you would
2222
* usually want this behavior in production). When false, it will transparently create new empty segment
@@ -25,46 +25,46 @@ mod.provider( '$routeSegment',
2525
*/
2626
strictMode: false
2727
};
28-
28+
2929
var segments = this.segments = {},
3030
rootPointer = pointer(segments, null),
3131
segmentRoutes = {};
32-
32+
3333
function camelCase(name) {
3434
return name.replace(/([\:\-\_]+(.))/g, function(_, separator, letter, offset) {
3535
return offset ? letter.toUpperCase() : letter;
3636
});
3737
}
38-
38+
3939
function pointer(segment, parent) {
40-
40+
4141
if(!segment)
4242
throw new Error('Invalid pointer segment');
43-
43+
4444
var lastAddedName;
45-
45+
4646
return {
47-
47+
4848
/**
4949
* Adds new segment at current pointer level.
50-
*
50+
*
5151
* @param string} name Name of a segment.
5252
* @param {Object} params Segment's parameters hash. The following params are supported:
5353
* - `template` provides HTML for the given segment view;
5454
* - `templateUrl` is a template should be fetched from network via this URL;
5555
* - `controller` is attached to the given segment view when compiled and linked,
5656
* this can be any controller definition AngularJS supports;
57-
* - `dependencies` is an array of route param names which are forcing the view
57+
* - `dependencies` is an array of route param names which are forcing the view
5858
* to recreate when changed;
5959
* - `watcher` is a $watch-function for recreating the view when its returning value
6060
* is changed;
6161
* - `resolve` is a hash of functions or injectable names which should be resolved
6262
* prior to instantiating the template and the controller;
6363
* - `untilResolved` is the alternate set of params (e.g. `template` and `controller`)
64-
* which should be used before resolving is completed;
65-
* - `resolveFailed` is the alternate set of params which should be used
64+
* which should be used before resolving is completed;
65+
* - `resolveFailed` is the alternate set of params which should be used
6666
* if resolving failed;
67-
*
67+
*
6868
* @returns {Object} The same level pointer.
6969
*/
7070
segment: function(name, params) {
@@ -79,11 +79,11 @@ mod.provider( '$routeSegment',
7979
*
8080
* @param {string} childName An existing segment's name. If undefined, then the last added segment is selected.
8181
* @returns {Object} The pointer to the child segment.
82-
*/
83-
within: function(childName) {
82+
*/
83+
within: function(childName) {
8484
var child;
8585
childName = childName || lastAddedName;
86-
86+
8787
if(child = segment[camelCase(childName)]) {
8888
if(child.children == undefined)
8989
child.children = {};
@@ -93,19 +93,19 @@ mod.provider( '$routeSegment',
9393
throw new Error('Cannot get into unknown `'+childName+'` segment');
9494
else {
9595
child = segment[camelCase(childName)] = {params: {}, children: {}};
96-
}
96+
}
9797
}
9898
return pointer(child.children, this);
9999
},
100-
100+
101101
/**
102102
* Traverses up in the tree.
103103
* @returns {Object} The pointer which are parent to the current one;
104104
*/
105105
up: function() {
106106
return parent;
107107
},
108-
108+
109109
/**
110110
* Traverses to the root.
111111
* @returns The root pointer.
@@ -115,29 +115,34 @@ mod.provider( '$routeSegment',
115115
}
116116
}
117117
}
118-
118+
119119
/**
120120
* The shorthand for $routeProvider.when() method with specified route name.
121-
* @param {string} route Route URL, e.g. '/foo/bar'
121+
* @param {string} path Route URL, e.g. '/foo/bar'
122122
* @param {string} name Fully qualified route name, e.g. 'foo.bar'
123+
* @param {Object} route Mapping information to be assigned to $route.current on route match.
123124
*/
124-
$routeSegmentProvider.when = function(route, name) {
125-
$routeProvider.when(route, {segment: name});
126-
segmentRoutes[name] = route;
125+
$routeSegmentProvider.when = function(path, name, route) {
126+
if (route == undefined)
127+
route = {};
128+
route.segment = name;
129+
130+
$routeProvider.when(path, route);
131+
segmentRoutes[name] = path;
127132
return this;
128133
};
129-
134+
130135
// Extending the provider with the methods of rootPointer
131136
// to start configuration.
132137
angular.extend($routeSegmentProvider, rootPointer);
133-
134-
138+
139+
135140
// the service factory
136141
this.$get = ['$rootScope', '$q', '$http', '$templateCache', '$route', '$routeParams', '$injector',
137142
function($rootScope, $q, $http, $templateCache, $route, $routeParams, $injector) {
138-
139-
var $routeSegment = {
140-
143+
144+
var $routeSegment = {
145+
141146
/**
142147
* Fully qualified name of current active route
143148
* @type {string}
@@ -151,7 +156,7 @@ mod.provider( '$routeSegment',
151156
* @type {Object}
152157
*/
153158
$routeParams: angular.copy($routeParams),
154-
159+
155160
/**
156161
* Array of segments splitted by each level separately. Each item contains the following properties:
157162
* - `name` is the name of a segment;
@@ -162,7 +167,7 @@ mod.provider( '$routeSegment',
162167
* @type {Array.<Object>}
163168
*/
164169
chain: [],
165-
170+
166171
/**
167172
* Helper method for checking whether current route starts with the given string
168173
* @param {string} val
@@ -172,7 +177,7 @@ mod.provider( '$routeSegment',
172177
var regexp = new RegExp('^'+val);
173178
return regexp.test($routeSegment.name);
174179
},
175-
180+
176181
/**
177182
* Helper method for checking whether current route contains the given string
178183
* @param {string} val
@@ -209,22 +214,22 @@ mod.provider( '$routeSegment',
209214

210215
return url;
211216
}
212-
};
217+
};
213218

214219
var resolvingSemaphoreChain = {};
215-
220+
216221
// When a route changes, all interested parties should be notified about new segment chain
217222
$rootScope.$on('$routeChangeSuccess', function(event, args) {
218223

219-
var route = args.$route || args.$$route;
224+
var route = args.$route || args.$$route;
220225
if(route && route.segment) {
221226

222227
var segmentName = route.segment;
223228
var segmentNameChain = segmentName.split(".");
224229
var updates = [], lastUpdateIndex = -1;
225-
230+
226231
for(var i=0; i < segmentNameChain.length; i++) {
227-
232+
228233
var newSegment = getSegmentInChain( i, segmentNameChain );
229234

230235
if(resolvingSemaphoreChain[i] != newSegment.name || updates.length > 0 || isDependenciesChanged(newSegment)) {
@@ -237,7 +242,7 @@ mod.provider( '$routeSegment',
237242
updates.push({index: i, newSegment: newSegment});
238243
lastUpdateIndex = i;
239244
}
240-
}
245+
}
241246
}
242247

243248
var curSegmentPromise = $q.when();
@@ -305,7 +310,7 @@ mod.provider( '$routeSegment',
305310
lastUpdateIndex = index;
306311
}
307312
})(i, children, index);
308-
313+
309314

310315
}
311316
}
@@ -315,7 +320,7 @@ mod.provider( '$routeSegment',
315320
});
316321
}
317322
});
318-
323+
319324
function isDependenciesChanged(segment) {
320325

321326
var result = false;
@@ -352,22 +357,22 @@ mod.provider( '$routeSegment',
352357
else
353358
return resolve(index, segment.name, segment.params);
354359
}
355-
360+
356361
function resolve(index, name, params) {
357-
362+
358363
var locals = angular.extend({}, params.resolve);
359-
364+
360365
angular.forEach(locals, function(value, key) {
361366
locals[key] = angular.isString(value) ? $injector.get(value) : $injector.invoke(value);
362367
});
363-
368+
364369
if(params.template) {
365370

366371
locals.$template = params.template;
367372
if(angular.isFunction(locals.$template))
368373
locals.$template = $injector.invoke(locals.$template);
369374
}
370-
375+
371376
if(options.autoLoadTemplates && params.templateUrl) {
372377

373378
locals.$template = params.templateUrl;
@@ -382,7 +387,7 @@ mod.provider( '$routeSegment',
382387
}
383388

384389
return $q.all(locals).then(
385-
390+
386391
function(resolvedLocals) {
387392

388393
if(resolvingSemaphoreChain[index] != name)
@@ -427,9 +432,9 @@ mod.provider( '$routeSegment',
427432

428433
return {success: index};
429434
},
430-
435+
431436
function(error) {
432-
437+
433438
if(params.resolveFailed) {
434439
var newResolve = {error: function() { return $q.when(error); }};
435440
return resolve(index, name, angular.extend({resolve: newResolve}, params.resolveFailed));
@@ -454,34 +459,34 @@ mod.provider( '$routeSegment',
454459
index: index,
455460
segment: $routeSegment.chain[index] || null } );
456461
}
457-
462+
458463
function getSegmentInChain(segmentIdx, segmentNameChain) {
459-
460-
if(!segmentNameChain)
461-
return null;
462-
463-
if(segmentIdx >= segmentNameChain.length)
464-
return null;
465-
464+
465+
if(!segmentNameChain)
466+
return null;
467+
468+
if(segmentIdx >= segmentNameChain.length)
469+
return null;
470+
466471
var curSegment = segments, nextName;
467-
for(var i=0;i<=segmentIdx;i++) {
472+
for(var i=0;i<=segmentIdx;i++) {
468473

469474
nextName = segmentNameChain[i];
470475

471476
if(curSegment[ camelCase(nextName) ] != undefined)
472477
curSegment = curSegment[ camelCase(nextName) ];
473-
478+
474479
if(i < segmentIdx)
475480
curSegment = curSegment.children;
476481
}
477-
482+
478483
return {
479484
name: nextName,
480485
params: curSegment.params,
481486
children: curSegment.children
482487
};
483488
}
484-
489+
485490
return $routeSegment;
486491
}];
487492
}]);

0 commit comments

Comments
 (0)