Skip to content

Commit

Permalink
1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
artch committed May 14, 2014
1 parent 8d1ac0d commit eae113a
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 26 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 1.3.0 (2014-05-15)

## Features

- New function `$routeSegment.getSegmentUrl(segmentName, routeParams)` which can return URL for the given segment ([2b255](https://github.com/artch/angular-route-segment/commit/2b255db63b7273be9f0c75b19c464620835db9b9)).
- Some handy filters like `routeSegmentUrl`,`routeSegmentEqualsTo`, etc ([2b255](https://github.com/artch/angular-route-segment/commit/2b255db63b7273be9f0c75b19c464620835db9b9)).
- 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)).
- `template` and `templateUrl` can be set as injectable functions ([8d1ac](https://github.com/artch/angular-route-segment/commit/8d1ac0d1ea1aee9243f90e32e4e4387a717049ac)).

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.

## Bug fixes

- 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)).


# 1.2.4 (2014-05-08)

- 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)).

# 1.2.3 (2014-04-07)

- Fixed a bug with double updates of view segments ([eb0d8](https://github.com/artch/angular-route-segment/commit/eb0d8a0c4aa01c2d8ab600aacef69e4a5479afd6)).
- `options.autoLoadTemplates` is true by default ([afab3](https://github.com/artch/angular-route-segment/commit/afab3ae7b827b7141ebcf0b8130311dc5aac0d7d)).
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-route-segment",
"version": "1.2.4",
"version": "1.3.0",
"main": "build/angular-route-segment.js",
"ignore": [
"**/.*",
Expand Down
160 changes: 138 additions & 22 deletions build/angular-route-segment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* angular-route-segment 1.2.3
* angular-route-segment 1.3.0
* https://angular-route-segment.com
* @author Artem Chivchalov
* @license MIT License http://opensource.org/licenses/MIT
Expand All @@ -8,7 +8,8 @@

(function(angular) {

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

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

var segments = this.segments = {},
rootPointer = pointer(segments, null);
rootPointer = pointer(segments, null),
segmentRoutes = {};

function camelCase(name) {
return name.replace(/([\:\-\_]+(.))/g, function(_, separator, letter, offset) {
Expand Down Expand Up @@ -127,6 +129,7 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
*/
$routeSegmentProvider.when = function(route, name) {
$routeProvider.when(route, {segment: name});
segmentRoutes[name] = route;
return this;
};

Expand Down Expand Up @@ -186,6 +189,31 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
if(this.chain[i] && this.chain[i].name == val)
return true;
return false;
},

/**
* A method for reverse routing which can return the route URL for the specified segment name
* @param {string} segmentName The name of a segment as defined in `when()`
* @param {Object} routeParams Route params hash to be put into route URL template
*/
getSegmentUrl: function(segmentName, routeParams) {
var url, i, m;
if(!segmentRoutes[segmentName])
throw new Error('Can not get URL for segment with name `'+segmentName+'`');

routeParams = angular.extend({}, $routeParams, routeParams || {});

url = segmentRoutes[segmentName];
for(i in routeParams) {
var regexp = new RegExp('\:'+i+'[\*\?]?','g');
url = url.replace(regexp, routeParams[i]);
}
url = url.replace(/\/\:.*?\?/g, '');

if(m = url.match(/\/\:([^\/]*)/))
throw new Error('Route param `'+m[1]+'` is not specified for route `'+segmentRoutes[segmentName]+'`');

return url;
}
};

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

var segmentName = route.segment;
var segmentNameChain = segmentName.split(".");
var updates = [];
var updates = [], lastUpdateIndex = -1;

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

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

Expand All @@ -239,31 +269,54 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
updateSegment(j, null);
}
}


}
})
})(i);
}

}



curSegmentPromise.then(function() {

// Removing redundant segment in case if new segment chain is shorter than old one
if($routeSegment.chain.length > segmentNameChain.length) {
var oldLength = $routeSegment.chain.length;
var shortenBy = $routeSegment.chain.length - segmentNameChain.length;
$routeSegment.chain.splice(-shortenBy, shortenBy);
for(var i=segmentNameChain.length; i < oldLength; i++)
for(var i=segmentNameChain.length; i < oldLength; i++) {
updateSegment(i, null);
lastUpdateIndex = $routeSegment.chain.length-1;
}
}
}).then(function() {

var defaultChildUpdatePromise = $q.when();

if(lastUpdateIndex == $routeSegment.chain.length-1) {

var curSegment = getSegmentInChain(lastUpdateIndex, $routeSegment.name.split("."));

while(curSegment) {
var children = curSegment.children, index = lastUpdateIndex+1;
curSegment = null;
for (var i in children) {
(function(i, children, index) {
if (children[i].params.default) {
defaultChildUpdatePromise = defaultChildUpdatePromise.then(function () {
return updateSegment(index, {name: i, params: children[i].params})
.then(function (result) {
if (result.success) broadcast(result.success);
});
});
curSegment = children[i];
lastUpdateIndex = index;
}
})(i, children, index);
}
}
}
})



return defaultChildUpdatePromise;
});
}
});

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

if(params.template)
if(params.template) {

locals.$template = params.template;
if(angular.isFunction(locals.$template))
locals.$template = $injector.invoke(locals.$template);
}

if(options.autoLoadTemplates && params.templateUrl)
locals.$template =
$http.get(params.templateUrl, {cache: $templateCache})
.then(function(response) {
if(options.autoLoadTemplates && params.templateUrl) {

locals.$template = params.templateUrl;
if(angular.isFunction(locals.$template))
locals.$template = $injector.invoke(locals.$template);

locals.$template =
$http.get(locals.$template, {cache: $templateCache})
.then(function (response) {
return response.data;
});
}

return $q.all(locals).then(

Expand All @@ -334,7 +397,8 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',
params: params,
locals: resolvedLocals,
reload: function() {
updateSegment(index, this).then(function(result) {
var originalSegment = getSegmentInChain(index, $routeSegment.name.split("."));
updateSegment(index, originalSegment).then(function(result) {
if(result.success != undefined)
broadcast(index);
})
Expand Down Expand Up @@ -417,13 +481,65 @@ angular.module( 'route-segment', [] ).provider( '$routeSegment',

return {
name: nextName,
params: curSegment.params
params: curSegment.params,
children: curSegment.children
};
}

return $routeSegment;
}];
}])
}]);

/**
* Usage:
* <a ng-href="{{ 'index.list' | routeSegmentUrl }}">
* <a ng-href="{{ 'index.list.itemInfo' | routeSegmentUrl: {id: 123} }}">
*/
mod.filter('routeSegmentUrl', ['$routeSegment', function($routeSegment) {
return function(segmentName, params) {
return $routeSegment.getSegmentUrl(segmentName, params);
}
}]);

/**
* Usage:
* <li ng-class="{active: ('index.list' | routeSegmentEqualsTo)}">
*/
mod.filter('routeSegmentEqualsTo', ['$routeSegment', function($routeSegment) {
return function(value) {
return $routeSegment.name == value;
}
}]);

/**
* Usage:
* <li ng-class="{active: ('section1' | routeSegmentStartsWith)}">
*/
mod.filter('routeSegmentStartsWith', ['$routeSegment', function($routeSegment) {
return function(value) {
return $routeSegment.startsWith(value);
}
}]);

/**
* Usage:
* <li ng-class="{active: ('itemInfo' | routeSegmentContains)}">
*/
mod.filter('routeSegmentContains', ['$routeSegment', function($routeSegment) {
return function(value) {
return $routeSegment.contains(value);
}
}]);

/**
* Usage:
* <li ng-class="{active: ('index.list.itemInfo' | routeSegmentEqualsTo) && ('id' | routeSegmentParam) == 123}">
*/
mod.filter('routeSegmentParam', ['$routeSegment', function($routeSegment) {
return function(value) {
return $routeSegment.$routeParams[value];
}
}]);


})(angular);;'use strict';
Expand Down
Loading

0 comments on commit eae113a

Please sign in to comment.