forked from marklagendijk/ui-router.stateHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatehelper.js
48 lines (44 loc) · 1.81 KB
/
statehelper.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
/**
* A helper module for AngularUI Router, which allows you to define your states as an object tree.
* @author Mark Lagendijk <[email protected]>
* @license MIT
*/
angular.module('ui.router.stateHelper', ['ui.router'])
.provider('stateHelper', 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.setNestedState = 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.setNestedState(childState, keepOriginalNames);
});
}
};
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 = state.parent.name + '.' + state.name;
}
}
})
;