Skip to content

Commit c3857b5

Browse files
refactor(ParamTypes): Remove global registry instance variable
refactor(ng1.init): Set services.$q/$injector in ui.router.init ng1 module
1 parent 55f3d3d commit c3857b5

File tree

8 files changed

+155
-147
lines changed

8 files changed

+155
-147
lines changed

src/ng1/services.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,6 @@ export function annotateController(controllerExpression: (IInjectable|string)):
157157
}
158158
}
159159

160-
runBlock.$inject = ['$injector', '$q'];
161-
function runBlock($injector: IInjectorService, $q: IQService) {
162-
services.$injector = $injector;
163-
services.$q = $q;
164-
}
165-
166-
app.run(runBlock);
167-
168160
let router: UIRouter = null;
169161

170162
$uiRouter.$inject = ['$locationProvider'];
@@ -222,6 +214,15 @@ function $uiRouter($locationProvider: ILocationProvider) {
222214

223215
// The 'ui.router' ng1 module depends on 'ui.router.init' module.
224216
angular.module('ui.router.init', []).provider("$uiRouter", <any> $uiRouter);
217+
218+
runBlock.$inject = ['$injector', '$q'];
219+
function runBlock($injector: IInjectorService, $q: IQService) {
220+
services.$injector = $injector;
221+
services.$q = $q;
222+
}
223+
224+
angular.module('ui.router.init').run(runBlock);
225+
225226
// This effectively calls $get() to init when we enter runtime
226227
angular.module('ui.router.init').run(['$uiRouter', function($uiRouter: UIRouter) { }]);
227228

src/params/param.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {RawParams, ParamDeclaration} from "../params/interface";
66
import {services} from "../common/coreservices";
77
import {matcherConfig} from "../url/urlMatcherConfig";
88
import {ParamType} from "./type";
9-
import {paramTypes} from "./paramTypes";
9+
import {ParamTypes} from "./paramTypes";
1010

1111
let hasOwn = Object.prototype.hasOwnProperty;
1212
let isShorthand = (cfg: ParamDeclaration) =>
@@ -24,7 +24,7 @@ function unwrapShorthand(cfg: ParamDeclaration): ParamDeclaration {
2424
});
2525
}
2626

27-
function getType(cfg: ParamDeclaration, urlType: ParamType, location: DefType, id: string) {
27+
function getType(cfg: ParamDeclaration, urlType: ParamType, location: DefType, id: string, paramTypes: ParamTypes) {
2828
if (cfg.type && urlType && urlType.name !== 'string') throw new Error(`Param '${id}' has two type configurations.`);
2929
if (cfg.type && urlType && urlType.name === 'string' && paramTypes.type(cfg.type as string)) return paramTypes.type(cfg.type as string);
3030
if (urlType) return urlType;
@@ -66,9 +66,9 @@ export class Param {
6666
dynamic: boolean;
6767
config: any;
6868

69-
constructor(id: string, type: ParamType, config: ParamDeclaration, location: DefType) {
69+
constructor(id: string, type: ParamType, config: ParamDeclaration, location: DefType, paramTypes: ParamTypes) {
7070
config = unwrapShorthand(config);
71-
type = getType(config, type, location, id);
71+
type = getType(config, type, location, id, paramTypes);
7272
let arrayMode = getArrayMode();
7373
type = arrayMode ? type.$asArray(arrayMode, location === DefType.SEARCH) : type;
7474
let isOptional = config.value !== undefined;
@@ -137,18 +137,18 @@ export class Param {
137137
}
138138

139139
/** Creates a new [[Param]] from a CONFIG block */
140-
static fromConfig(id: string, type: ParamType, config: any): Param {
141-
return new Param(id, type, config, DefType.CONFIG);
140+
static fromConfig(id: string, type: ParamType, config: any, paramTypes: ParamTypes): Param {
141+
return new Param(id, type, config, DefType.CONFIG, paramTypes);
142142
}
143143

144144
/** Creates a new [[Param]] from a url PATH */
145-
static fromPath(id: string, type: ParamType, config: any): Param {
146-
return new Param(id, type, config, DefType.PATH);
145+
static fromPath(id: string, type: ParamType, config: any, paramTypes: ParamTypes): Param {
146+
return new Param(id, type, config, DefType.PATH, paramTypes);
147147
}
148148

149149
/** Creates a new [[Param]] from a url SEARCH */
150-
static fromSearch(id: string, type: ParamType, config: any): Param {
151-
return new Param(id, type, config, DefType.SEARCH);
150+
static fromSearch(id: string, type: ParamType, config: any, paramTypes: ParamTypes): Param {
151+
return new Param(id, type, config, DefType.SEARCH, paramTypes);
152152
}
153153

154154
static values(params: Param[], values: RawParams = {}): RawParams {

src/params/paramTypes.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,3 @@ export class ParamTypes {
107107
}
108108
}
109109
}
110-
111-
export let paramTypes = new ParamTypes();

src/state/stateBuilder.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {UrlMatcher} from "../url/urlMatcher";
1313
import {Resolvable} from "../resolve/resolvable";
1414
import {services} from "../common/coreservices";
1515
import {ResolvePolicy} from "../resolve/interface";
16+
import {ParamTypes} from "../params/paramTypes";
1617

1718
const parseUrl = (url: string): any => {
1819
if (!isString(url)) return false;
@@ -71,12 +72,13 @@ function navigableBuilder(state: State) {
7172
return !isRoot(state) && state.url ? state : (state.parent ? state.parent.navigable : null);
7273
};
7374

75+
const getParamsBuilder = (paramTypes: ParamTypes) =>
7476
function paramsBuilder(state: State): { [key: string]: Param } {
75-
const makeConfigParam = (config: any, id: string) => Param.fromConfig(id, null, config);
77+
const makeConfigParam = (config: any, id: string) => Param.fromConfig(id, null, config, paramTypes);
7678
let urlParams: Param[] = (state.url && state.url.parameters({inherit: false})) || [];
7779
let nonUrlParams: Param[] = values(mapObj(omit(state.params || {}, urlParams.map(prop('id'))), makeConfigParam));
7880
return urlParams.concat(nonUrlParams).map(p => [p.id, p]).reduce(applyPairs, {});
79-
}
81+
};
8082

8183
function pathBuilder(state: State) {
8284
return state.parent ? state.parent.path.concat(state) : /*root*/ [state];
@@ -217,7 +219,7 @@ export class StateBuilder {
217219
url: [ getUrlBuilder($urlMatcherFactoryProvider, root) ],
218220
// Keep track of the closest ancestor state that has a URL (i.e. is navigable)
219221
navigable: [ getNavigableBuilder(isRoot) ],
220-
params: [ paramsBuilder ],
222+
params: [ getParamsBuilder($urlMatcherFactoryProvider.paramTypes) ],
221223
// Each framework-specific ui-router implementation should define its own `views` builder
222224
// e.g., src/ng1/statebuilders/views.ts
223225
views: [],

src/url/urlMatcher.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import {prop, propEq } from "../common/hof";
77
import {isArray, isString} from "../common/predicates";
88
import {Param} from "../params/param";
9-
import {paramTypes} from "../params/paramTypes";
9+
import {ParamTypes} from "../params/paramTypes";
1010
import {isDefined} from "../common/predicates";
1111
import {DefType} from "../params/param";
1212
import {unnestR} from "../common/common";
@@ -112,11 +112,12 @@ export class UrlMatcher {
112112

113113
/**
114114
* @param pattern The pattern to compile into a matcher.
115+
* @param paramTypes The [[ParamTypes]] registry
115116
* @param config A configuration object
116117
* - `caseInsensitive` - `true` if URL matching should be case insensitive, otherwise `false`, the default value (for backward compatibility) is `false`.
117118
* - `strict` - `false` if matching against a URL with a trailing slash should be treated as equivalent to a URL without a trailing slash, the default value is `true`.
118119
*/
119-
constructor(pattern: string, public config?: any) {
120+
constructor(pattern: string, paramTypes: ParamTypes, public config?: any) {
120121
this.pattern = pattern;
121122
this.config = defaults(this.config, {
122123
params: {},
@@ -171,7 +172,7 @@ export class UrlMatcher {
171172
if (p.segment.indexOf('?') >= 0) break; // we're into the search part
172173

173174
checkParamErrors(p.id);
174-
this._params.push(Param.fromPath(p.id, p.type, this.config.paramMap(p.cfg, false)));
175+
this._params.push(Param.fromPath(p.id, p.type, this.config.paramMap(p.cfg, false), paramTypes));
175176
this._segments.push(p.segment);
176177
patterns.push([p.segment, tail(this._params)]);
177178
last = placeholder.lastIndex;
@@ -191,7 +192,7 @@ export class UrlMatcher {
191192
while ((m = searchPlaceholder.exec(search))) {
192193
p = matchDetails(m, true);
193194
checkParamErrors(p.id);
194-
this._params.push(Param.fromSearch(p.id, p.type, this.config.paramMap(p.cfg, true)));
195+
this._params.push(Param.fromSearch(p.id, p.type, this.config.paramMap(p.cfg, true), paramTypes));
195196
last = placeholder.lastIndex;
196197
// check if ?&
197198
}

src/url/urlMatcherFactory.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import {isObject, isDefined, isFunction} from "../common/predicates";
55
import {UrlMatcher} from "./urlMatcher";
66
import {matcherConfig} from "./urlMatcherConfig";
77
import {Param} from "../params/param";
8-
import {paramTypes} from "../params/paramTypes";
9-
import {ParamType} from "../params/type";
8+
import {ParamTypes} from "../params/paramTypes";
109
import {ParamTypeDefinition} from "../params/interface";
1110

1211
/** @hidden */
@@ -24,6 +23,8 @@ function getDefaultConfig() {
2423
* `$urlMatcherFactor` or ng1 providers as `$urlMatcherFactoryProvider`.
2524
*/
2625
export class UrlMatcherFactory {
26+
paramTypes = new ParamTypes();
27+
2728
constructor() {
2829
extend(this, { UrlMatcher, Param });
2930
}
@@ -71,7 +72,7 @@ export class UrlMatcherFactory {
7172
* @returns The UrlMatcher.
7273
*/
7374
compile(pattern: string, config?: { [key: string]: any }) {
74-
return new UrlMatcher(pattern, extend(getDefaultConfig(), config));
75+
return new UrlMatcher(pattern, this.paramTypes, extend(getDefaultConfig(), config));
7576
}
7677

7778
/**
@@ -112,14 +113,14 @@ export class UrlMatcherFactory {
112113
* See [[ParamTypeDefinition]] for examples
113114
*/
114115
type(name: string, definition?: ParamTypeDefinition, definitionFn?: () => ParamTypeDefinition) {
115-
let type = paramTypes.type(name, definition, definitionFn);
116+
let type = this.paramTypes.type(name, definition, definitionFn);
116117
return !isDefined(definition) ? type : this;
117118
};
118119

119120
/** @hidden */
120121
$get() {
121-
paramTypes.enqueue = false;
122-
paramTypes._flushTypeQueue();
122+
this.paramTypes.enqueue = false;
123+
this.paramTypes._flushTypeQueue();
123124
return this;
124125
};
125126
}

0 commit comments

Comments
 (0)