Skip to content

Instantly share code, notes, and snippets.

@libook
Last active August 29, 2015 14:14
Show Gist options
  • Save libook/0fb9e75ee3ea0c0a3cf1 to your computer and use it in GitHub Desktop.
Save libook/0fb9e75ee3ea0c0a3cf1 to your computer and use it in GitHub Desktop.
/**
* A helper module for AngularUI Router, which allows you to define your states as an object tree.
* @author Mark Lagendijk <[email protected]>
* @license MIT
*/
+function(){
angular.module('ui.router.stateHelper', [ 'ui.router' ])
.provider('stateHelper',['$stateProvider', function($stateProvider){
var self = this;
/**
* Recursively sets the states using $stateProvider.state.
* Child states are defined via a `children` property.
*
* 1. Recursively calls itself for all descendant states, by traversing the `children` properties.
* 2. Converts all the state names to dot notation, of the form `grandfather.father.state`.
* 3. Sets `parent` property of the descendant states.
*
* @param {Object} state - A regular ui.router state object.
* @param {Array} [state.children] - An optional array of child states.
* @param {Boolean} keepOriginalNames - An optional flag that prevents conversion of names to dot notation if true.
*/
this.state = function(state, keepOriginalNames){
if(!keepOriginalNames){
fixStateName(state);
}
$stateProvider.state(state);
if(state.children && state.children.length){
state.children.forEach(function(childState){
childState.parent = state;
self.state(childState, keepOriginalNames);
});
}
return self;
};
this.setNestedState = this.state;
self.$get = angular.noop;
/**
* Converts the name of a state to dot notation, of the form `grandfather.father.state`.
* @param state
*/
function fixStateName(state){
if(state.parent){
state.name = (angular.isObject(state.parent) ? state.parent.name : state.parent) + '.' + state.name;
}
}
}]);
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment