-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathstatehelper.js
86 lines (75 loc) · 3.24 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* 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', ['$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.
* @deprecated {Boolean} keepOriginalNames - An optional flag that prevents conversion
* of names to dot notation if true. (use options.keepOriginalNames instead)
* @param {Object} [options] - An optional options object.
* @param {Boolean} [options.keepOriginalNames=false] An optional flag that
* prevents conversion of names to dot notation if true.
* @param {Boolean} [options.siblingTraversal=false] An optional flag that
* adds `nextSibling` and `previousSibling` properties when enabled
*/
this.state = function(state){
var args = Array.prototype.slice.apply(arguments);
var options = {
keepOriginalNames: false,
siblingTraversal: false
};
if (typeof args[1] === 'boolean') {
options.keepOriginalNames = args[1];
}
else if (typeof args[1] === 'object') {
angular.extend(options, args[1]);
}
if (!options.keepOriginalNames) {
fixStateName(state);
}
$stateProvider.state(state);
if(state.children && state.children.length){
state.children.forEach(function(childState){
childState.parent = state;
self.state(childState, options);
});
if (options.siblingTraversal) {
addSiblings(state);
}
}
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;
}
}
function addSiblings(state) {
state.children.forEach(function (childState, idx, array) {
if (array[idx + 1]) {
childState.nextSibling = array[idx + 1].name;
}
if (array[idx - 1]) {
childState.previousSibling = array[idx - 1].name;
}
});
}
}]);