Skip to content

Commit 2239ea7

Browse files
refactor(params):Move stateParams to its own file
1 parent c319e28 commit 2239ea7

File tree

4 files changed

+147
-151
lines changed

4 files changed

+147
-151
lines changed

src/params/module.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
/**!*/
99
export * from "./param";
1010
export * from "./paramTypes";
11+
export * from "./stateParams";
1112
export * from "./type";

src/params/stateParams.ts

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import {IServiceProviderFactory} from "angular";
2+
import {forEach, ancestors, extend, copy} from "../common/common";
3+
4+
export function StateParams() { }
5+
6+
$StateParamsProvider.$inject = [];
7+
function $StateParamsProvider() {
8+
9+
function stateParamsFactory() {
10+
let observers = {}, current = {};
11+
12+
function unhook(key, func) {
13+
return function() {
14+
forEach(key.split(" "), function(k) {
15+
observers[k].splice(observers[k].indexOf(func), 1);
16+
});
17+
};
18+
}
19+
20+
function observeChange(key, val?: any) {
21+
if (!observers[key] || !observers[key].length) return;
22+
23+
forEach(observers[key], function(func) {
24+
func(val);
25+
});
26+
}
27+
28+
29+
StateParams.prototype.$digest = function() {
30+
function updateValue(val, key) {
31+
if (val === current[key] || !this.hasOwnProperty(key)) return;
32+
current[key] = val;
33+
observeChange(key, val);
34+
}
35+
forEach(this, updateValue, this);
36+
};
37+
38+
/**
39+
* Merges a set of parameters with all parameters inherited between the common parents of the
40+
* current state and a given destination state.
41+
*
42+
* @param {Object} newParams The set of parameters which will be composited with inherited params.
43+
* @param {Object} $current Internal definition of object representing the current state.
44+
* @param {Object} $to Internal definition of object representing state to transition to.
45+
*/
46+
StateParams.prototype.$inherit = function(newParams, $current, $to) {
47+
let parents = ancestors($current, $to), parentParams, inherited = {}, inheritList = [];
48+
49+
for (let i in parents) {
50+
if (!parents[i].params) continue;
51+
parentParams = Object.keys(parents[i].params);
52+
if (!parentParams.length) continue;
53+
54+
for (let j in parentParams) {
55+
if (inheritList.indexOf(parentParams[j]) >= 0) continue;
56+
inheritList.push(parentParams[j]);
57+
inherited[parentParams[j]] = this[parentParams[j]];
58+
}
59+
}
60+
return extend({}, inherited, newParams);
61+
};
62+
63+
StateParams.prototype.$set = function(params, url) {
64+
let hasChanged = false, abort = false;
65+
66+
if (url) {
67+
forEach(params, function(val, key) {
68+
if ((url.parameter(key) || {}).dynamic !== true) abort = true;
69+
});
70+
}
71+
if (abort) return false;
72+
73+
function updateValue(val, key) {
74+
if (val !== this[key]) {
75+
this[key] = val;
76+
observeChange(key);
77+
hasChanged = true;
78+
}
79+
}
80+
forEach(params, updateValue, this);
81+
82+
this.$sync();
83+
return hasChanged;
84+
};
85+
86+
StateParams.prototype.$sync = function() {
87+
copy(this, current);
88+
return this;
89+
};
90+
91+
StateParams.prototype.$off = function() {
92+
observers = {};
93+
return this;
94+
};
95+
96+
StateParams.prototype.$raw = function() {
97+
let raw = {};
98+
for (let key in this) {
99+
if (!StateParams.prototype.hasOwnProperty(key))
100+
raw[key] = this[key];
101+
}
102+
return raw;
103+
};
104+
105+
StateParams.prototype.$localize = function(state, params) {
106+
let localized = new StateParams();
107+
params = params || this;
108+
109+
forEach(state.params, function(val, key) {
110+
localized[key] = params[key];
111+
});
112+
return localized;
113+
};
114+
115+
StateParams.prototype.$observe = function(key, func) {
116+
forEach(key.split(" "), function(k) {
117+
(observers[k] || (observers[k] = [])).push(func);
118+
});
119+
return unhook(key, func);
120+
};
121+
122+
return new StateParams();
123+
}
124+
125+
let global = stateParamsFactory();
126+
127+
this.$get = $get;
128+
$get.$inject = ['$rootScope'];
129+
function $get( $rootScope) {
130+
131+
$rootScope.$watch(function() {
132+
global.$digest();
133+
});
134+
135+
return global;
136+
}
137+
}
138+
139+
angular.module('ui.router.state')
140+
.provider('$stateParams', <IServiceProviderFactory> $StateParamsProvider);

src/state/state.ts

+5-150
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,18 @@ import {
66
import {Queue} from "../common/queue";
77
import {IServiceProviderFactory, IPromise} from "angular";
88

9-
import { StateService, StateDeclaration, StateOrName, HrefOptions, ViewDeclaration } from "./interface";
10-
import {Glob} from "./glob";
11-
import {StateQueueManager} from "./stateQueueManager";
12-
import {StateBuilder} from "./stateBuilder";
13-
import {StateMatcher} from "./stateMatcher";
14-
import {TargetState} from "./targetState";
9+
import {StateService, StateDeclaration, StateOrName, HrefOptions, ViewDeclaration } from "./interface";
10+
import {Glob, StateQueueManager, StateBuilder, StateMatcher, TargetState} from "./module";
1511

1612
import {ITransitionService, TransitionOptions, TreeChanges} from "../transition/interface";
17-
import {Transition} from "../transition/transition";
18-
import {RejectFactory} from "../transition/rejectFactory";
19-
import {defaultTransOpts} from "../transition/transitionService";
13+
import {Transition, RejectFactory, defaultTransOpts} from "../transition/module";
2014

21-
import {Node} from "../path/node";
22-
import {PathFactory} from "../path/pathFactory";
15+
import {PathFactory, Node} from "../path/module";
2316

2417
import {RawParams, ParamsOrArray} from "../params/interface";
2518
import {TransitionManager} from "./hooks/transitionManager";
2619

27-
import {paramTypes} from "../params/paramTypes";
28-
import {Param} from "../params/param";
29-
import {Type} from "../params/type";
20+
import {paramTypes, Param, Type, StateParams} from "../params/module";
3021

3122
import {UrlMatcher} from "../url/urlMatcher";
3223
import {ViewConfig} from "../view/view";
@@ -913,142 +904,6 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactoryProvider) {
913904
}
914905
}
915906

916-
export function StateParams() { }
917-
918-
$StateParamsProvider.$inject = [];
919-
function $StateParamsProvider() {
920-
921-
function stateParamsFactory() {
922-
let observers = {}, current = {};
923-
924-
function unhook(key, func) {
925-
return function() {
926-
forEach(key.split(" "), function(k) {
927-
observers[k].splice(observers[k].indexOf(func), 1);
928-
});
929-
};
930-
}
931-
932-
function observeChange(key, val?: any) {
933-
if (!observers[key] || !observers[key].length) return;
934-
935-
forEach(observers[key], function(func) {
936-
func(val);
937-
});
938-
}
939-
940-
941-
StateParams.prototype.$digest = function() {
942-
function updateValue(val, key) {
943-
if (val === current[key] || !this.hasOwnProperty(key)) return;
944-
current[key] = val;
945-
observeChange(key, val);
946-
}
947-
forEach(this, updateValue, this);
948-
};
949-
950-
/**
951-
* Merges a set of parameters with all parameters inherited between the common parents of the
952-
* current state and a given destination state.
953-
*
954-
* @param {Object} newParams The set of parameters which will be composited with inherited params.
955-
* @param {Object} $current Internal definition of object representing the current state.
956-
* @param {Object} $to Internal definition of object representing state to transition to.
957-
*/
958-
StateParams.prototype.$inherit = function(newParams, $current, $to) {
959-
let parents = ancestors($current, $to), parentParams, inherited = {}, inheritList = [];
960-
961-
for (let i in parents) {
962-
if (!parents[i].params) continue;
963-
parentParams = Object.keys(parents[i].params);
964-
if (!parentParams.length) continue;
965-
966-
for (let j in parentParams) {
967-
if (inheritList.indexOf(parentParams[j]) >= 0) continue;
968-
inheritList.push(parentParams[j]);
969-
inherited[parentParams[j]] = this[parentParams[j]];
970-
}
971-
}
972-
return extend({}, inherited, newParams);
973-
};
974-
975-
StateParams.prototype.$set = function(params, url) {
976-
let hasChanged = false, abort = false;
977-
978-
if (url) {
979-
forEach(params, function(val, key) {
980-
if ((url.parameter(key) || {}).dynamic !== true) abort = true;
981-
});
982-
}
983-
if (abort) return false;
984-
985-
function updateValue(val, key) {
986-
if (val !== this[key]) {
987-
this[key] = val;
988-
observeChange(key);
989-
hasChanged = true;
990-
}
991-
}
992-
forEach(params, updateValue, this);
993-
994-
this.$sync();
995-
return hasChanged;
996-
};
997-
998-
StateParams.prototype.$sync = function() {
999-
copy(this, current);
1000-
return this;
1001-
};
1002-
1003-
StateParams.prototype.$off = function() {
1004-
observers = {};
1005-
return this;
1006-
};
1007-
1008-
StateParams.prototype.$raw = function() {
1009-
let raw = {};
1010-
for (let key in this) {
1011-
if (!StateParams.prototype.hasOwnProperty(key))
1012-
raw[key] = this[key];
1013-
}
1014-
return raw;
1015-
};
1016-
1017-
StateParams.prototype.$localize = function(state, params) {
1018-
let localized = new StateParams();
1019-
params = params || this;
1020-
1021-
forEach(state.params, function(val, key) {
1022-
localized[key] = params[key];
1023-
});
1024-
return localized;
1025-
};
1026-
1027-
StateParams.prototype.$observe = function(key, func) {
1028-
forEach(key.split(" "), function(k) {
1029-
(observers[k] || (observers[k] = [])).push(func);
1030-
});
1031-
return unhook(key, func);
1032-
};
1033-
1034-
return new StateParams();
1035-
}
1036-
1037-
let global = stateParamsFactory();
1038-
1039-
this.$get = $get;
1040-
$get.$inject = ['$rootScope'];
1041-
function $get( $rootScope) {
1042-
1043-
$rootScope.$watch(function() {
1044-
global.$digest();
1045-
});
1046-
1047-
return global;
1048-
}
1049-
}
1050-
1051907
angular.module('ui.router.state')
1052-
.provider('$stateParams', <IServiceProviderFactory> $StateParamsProvider)
1053908
.provider('$state', <IServiceProviderFactory> $StateProvider)
1054909
.run(['$state', function($state) { /* This effectively calls $get() to init when we enter runtime */ }]);

src/state/stateEvents.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {IServiceProviderFactory} from "angular";
55
import {extend, applyPairs, isFunction} from "../common/common";
66

77
import {StateService, StateProvider} from "./interface";
8-
import {StateParams} from "./state";
8+
import {StateParams} from "../params/module";
99
import {TargetState} from "./targetState";
1010

1111
import {Transition} from "../transition/transition";

0 commit comments

Comments
 (0)