Skip to content

Commit eae113a

Browse files
committed
1.3.0
1 parent 8d1ac0d commit eae113a

File tree

5 files changed

+166
-26
lines changed

5 files changed

+166
-26
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# 1.3.0 (2014-05-15)
2+
3+
## Features
4+
5+
- New function `$routeSegment.getSegmentUrl(segmentName, routeParams)` which can return URL for the given segment ([2b255](https://github.com/artch/angular-route-segment/commit/2b255db63b7273be9f0c75b19c464620835db9b9)).
6+
- Some handy filters like `routeSegmentUrl`,`routeSegmentEqualsTo`, etc ([2b255](https://github.com/artch/angular-route-segment/commit/2b255db63b7273be9f0c75b19c464620835db9b9)).
7+
- New segment config option `default:true` which can be set if this child segment should be loaded by default when no child segment is specified in the route ([2eee0](https://github.com/artch/angular-route-segment/commit/2eee0a1dc7d6a6ff031d8451c06d4da5ae7e50fc)).
8+
- `template` and `templateUrl` can be set as injectable functions ([8d1ac](https://github.com/artch/angular-route-segment/commit/8d1ac0d1ea1aee9243f90e32e4e4387a717049ac)).
9+
10+
See updated [docs](https://github.com/artch/angular-route-segment/blob/master/README.md) and [demo example](http://angular-route-segment.com/src/example/) for usage.
11+
12+
## Bug fixes
13+
14+
- Fixed a bug when reloading a segment does not recover after the resolving error has gone ([ed11d](https://github.com/artch/angular-route-segment/commit/ed11d58e495ea7c611a59373fd6b909de1be33e3)).
15+
16+
17+
# 1.2.4 (2014-05-08)
18+
19+
- Fixed a bug with null exception on `contains` function ([1b014](https://github.com/artch/angular-route-segment/commit/1b014d3b5ea7740815c7e0b98467bdff556e6a5b) thanks to [paivaric](https://github.com/paivaric)).
20+
21+
# 1.2.3 (2014-04-07)
22+
23+
- Fixed a bug with double updates of view segments ([eb0d8](https://github.com/artch/angular-route-segment/commit/eb0d8a0c4aa01c2d8ab600aacef69e4a5479afd6)).
24+
- `options.autoLoadTemplates` is true by default ([afab3](https://github.com/artch/angular-route-segment/commit/afab3ae7b827b7141ebcf0b8130311dc5aac0d7d)).

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-route-segment",
3-
"version": "1.2.4",
3+
"version": "1.3.0",
44
"main": "build/angular-route-segment.js",
55
"ignore": [
66
"**/.*",

build/angular-route-segment.js

Lines changed: 138 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* angular-route-segment 1.2.3
2+
* angular-route-segment 1.3.0
33
* https://angular-route-segment.com
44
* @author Artem Chivchalov
55
* @license MIT License http://opensource.org/licenses/MIT
@@ -8,7 +8,8 @@
88

99
(function(angular) {
1010

11-
angular.module( 'route-segment', [] ).provider( '$routeSegment',
11+
var mod = angular.module( 'route-segment', [] );
12+
mod.provider( '$routeSegment',
1213
['$routeProvider', function($routeProvider) {
1314

1415
var $routeSegmentProvider = this;
@@ -32,7 +33,8 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
3233
};
3334

3435
var segments = this.segments = {},
35-
rootPointer = pointer(segments, null);
36+
rootPointer = pointer(segments, null),
37+
segmentRoutes = {};
3638

3739
function camelCase(name) {
3840
return name.replace(/([\:\-\_]+(.))/g, function(_, separator, letter, offset) {
@@ -127,6 +129,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
127129
*/
128130
$routeSegmentProvider.when = function(route, name) {
129131
$routeProvider.when(route, {segment: name});
132+
segmentRoutes[name] = route;
130133
return this;
131134
};
132135

@@ -186,6 +189,31 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
186189
if(this.chain[i] && this.chain[i].name == val)
187190
return true;
188191
return false;
192+
},
193+
194+
/**
195+
* A method for reverse routing which can return the route URL for the specified segment name
196+
* @param {string} segmentName The name of a segment as defined in `when()`
197+
* @param {Object} routeParams Route params hash to be put into route URL template
198+
*/
199+
getSegmentUrl: function(segmentName, routeParams) {
200+
var url, i, m;
201+
if(!segmentRoutes[segmentName])
202+
throw new Error('Can not get URL for segment with name `'+segmentName+'`');
203+
204+
routeParams = angular.extend({}, $routeParams, routeParams || {});
205+
206+
url = segmentRoutes[segmentName];
207+
for(i in routeParams) {
208+
var regexp = new RegExp('\:'+i+'[\*\?]?','g');
209+
url = url.replace(regexp, routeParams[i]);
210+
}
211+
url = url.replace(/\/\:.*?\?/g, '');
212+
213+
if(m = url.match(/\/\:([^\/]*)/))
214+
throw new Error('Route param `'+m[1]+'` is not specified for route `'+segmentRoutes[segmentName]+'`');
215+
216+
return url;
189217
}
190218
};
191219

@@ -199,7 +227,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
199227

200228
var segmentName = route.segment;
201229
var segmentNameChain = segmentName.split(".");
202-
var updates = [];
230+
var updates = [], lastUpdateIndex = -1;
203231

204232
for(var i=0; i < segmentNameChain.length; i++) {
205233

@@ -211,8 +239,10 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
211239
updates.length == 0 && !isDependenciesChanged(newSegment))
212240
// if we went back to the same state as we were before resolving new segment
213241
resolvingSemaphoreChain[i] = newSegment.name;
214-
else
242+
else {
215243
updates.push({index: i, newSegment: newSegment});
244+
lastUpdateIndex = i;
245+
}
216246
}
217247
}
218248

@@ -239,31 +269,54 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
239269
updateSegment(j, null);
240270
}
241271
}
242-
243-
244272
}
245273
})
246274
})(i);
247275
}
248-
249276
}
250277

251-
252-
253278
curSegmentPromise.then(function() {
254279

255280
// Removing redundant segment in case if new segment chain is shorter than old one
256281
if($routeSegment.chain.length > segmentNameChain.length) {
257282
var oldLength = $routeSegment.chain.length;
258283
var shortenBy = $routeSegment.chain.length - segmentNameChain.length;
259284
$routeSegment.chain.splice(-shortenBy, shortenBy);
260-
for(var i=segmentNameChain.length; i < oldLength; i++)
285+
for(var i=segmentNameChain.length; i < oldLength; i++) {
261286
updateSegment(i, null);
287+
lastUpdateIndex = $routeSegment.chain.length-1;
288+
}
289+
}
290+
}).then(function() {
291+
292+
var defaultChildUpdatePromise = $q.when();
293+
294+
if(lastUpdateIndex == $routeSegment.chain.length-1) {
295+
296+
var curSegment = getSegmentInChain(lastUpdateIndex, $routeSegment.name.split("."));
297+
298+
while(curSegment) {
299+
var children = curSegment.children, index = lastUpdateIndex+1;
300+
curSegment = null;
301+
for (var i in children) {
302+
(function(i, children, index) {
303+
if (children[i].params.default) {
304+
defaultChildUpdatePromise = defaultChildUpdatePromise.then(function () {
305+
return updateSegment(index, {name: i, params: children[i].params})
306+
.then(function (result) {
307+
if (result.success) broadcast(result.success);
308+
});
309+
});
310+
curSegment = children[i];
311+
lastUpdateIndex = index;
312+
}
313+
})(i, children, index);
314+
}
315+
}
262316
}
263-
})
264-
265-
266317

318+
return defaultChildUpdatePromise;
319+
});
267320
}
268321
});
269322

@@ -312,15 +365,25 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
312365
locals[key] = angular.isString(value) ? $injector.get(value) : $injector.invoke(value);
313366
});
314367

315-
if(params.template)
368+
if(params.template) {
369+
316370
locals.$template = params.template;
371+
if(angular.isFunction(locals.$template))
372+
locals.$template = $injector.invoke(locals.$template);
373+
}
317374

318-
if(options.autoLoadTemplates && params.templateUrl)
319-
locals.$template =
320-
$http.get(params.templateUrl, {cache: $templateCache})
321-
.then(function(response) {
375+
if(options.autoLoadTemplates && params.templateUrl) {
376+
377+
locals.$template = params.templateUrl;
378+
if(angular.isFunction(locals.$template))
379+
locals.$template = $injector.invoke(locals.$template);
380+
381+
locals.$template =
382+
$http.get(locals.$template, {cache: $templateCache})
383+
.then(function (response) {
322384
return response.data;
323385
});
386+
}
324387

325388
return $q.all(locals).then(
326389

@@ -334,7 +397,8 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
334397
params: params,
335398
locals: resolvedLocals,
336399
reload: function() {
337-
updateSegment(index, this).then(function(result) {
400+
var originalSegment = getSegmentInChain(index, $routeSegment.name.split("."));
401+
updateSegment(index, originalSegment).then(function(result) {
338402
if(result.success != undefined)
339403
broadcast(index);
340404
})
@@ -417,13 +481,65 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
417481

418482
return {
419483
name: nextName,
420-
params: curSegment.params
484+
params: curSegment.params,
485+
children: curSegment.children
421486
};
422487
}
423488

424489
return $routeSegment;
425490
}];
426-
}])
491+
}]);
492+
493+
/**
494+
* Usage:
495+
* <a ng-href="{{ 'index.list' | routeSegmentUrl }}">
496+
* <a ng-href="{{ 'index.list.itemInfo' | routeSegmentUrl: {id: 123} }}">
497+
*/
498+
mod.filter('routeSegmentUrl', ['$routeSegment', function($routeSegment) {
499+
return function(segmentName, params) {
500+
return $routeSegment.getSegmentUrl(segmentName, params);
501+
}
502+
}]);
503+
504+
/**
505+
* Usage:
506+
* <li ng-class="{active: ('index.list' | routeSegmentEqualsTo)}">
507+
*/
508+
mod.filter('routeSegmentEqualsTo', ['$routeSegment', function($routeSegment) {
509+
return function(value) {
510+
return $routeSegment.name == value;
511+
}
512+
}]);
513+
514+
/**
515+
* Usage:
516+
* <li ng-class="{active: ('section1' | routeSegmentStartsWith)}">
517+
*/
518+
mod.filter('routeSegmentStartsWith', ['$routeSegment', function($routeSegment) {
519+
return function(value) {
520+
return $routeSegment.startsWith(value);
521+
}
522+
}]);
523+
524+
/**
525+
* Usage:
526+
* <li ng-class="{active: ('itemInfo' | routeSegmentContains)}">
527+
*/
528+
mod.filter('routeSegmentContains', ['$routeSegment', function($routeSegment) {
529+
return function(value) {
530+
return $routeSegment.contains(value);
531+
}
532+
}]);
533+
534+
/**
535+
* Usage:
536+
* <li ng-class="{active: ('index.list.itemInfo' | routeSegmentEqualsTo) && ('id' | routeSegmentParam) == 123}">
537+
*/
538+
mod.filter('routeSegmentParam', ['$routeSegment', function($routeSegment) {
539+
return function(value) {
540+
return $routeSegment.$routeParams[value];
541+
}
542+
}]);
427543

428544

429545
})(angular);;'use strict';

0 commit comments

Comments
 (0)