Skip to content
This repository was archived by the owner on May 26, 2023. It is now read-only.

Commit d7aa6f0

Browse files
refactor(url): use urlService (not urlRouter) and switch private variable names
1 parent 11982b1 commit d7aa6f0

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

src/urlRouterProvider.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/** @module url */ /** */
22
import {
33
UIRouter,
4-
UrlRouter,
54
LocationServices,
65
$InjectorLike,
76
BaseUrlRule,
87
UrlRuleHandlerFn,
98
UrlMatcher,
109
IInjectable,
10+
UrlRouter,
1111
} from '@uirouter/core';
1212
import { services, isString, isFunction, isArray, identity } from '@uirouter/core';
1313

@@ -30,25 +30,19 @@ export interface RawNg1RuleFunction {
3030
* @deprecated
3131
*/
3232
export class UrlRouterProvider {
33-
/** @hidden */ _router: UIRouter;
34-
/** @hidden */ _urlRouter: UrlRouter;
35-
3633
static injectableHandler(router: UIRouter, handler): UrlRuleHandlerFn {
3734
return match => services.$injector.invoke(handler, null, { $match: match, $stateParams: router.globals.params });
3835
}
3936

4037
/** @hidden */
41-
constructor(router: UIRouter) {
42-
this._router = router;
43-
this._urlRouter = router.urlRouter;
44-
}
38+
constructor(/** @hidden */ private router: UIRouter) {}
4539

4640
/** @hidden */
47-
$get() {
48-
const urlRouter = this._urlRouter;
49-
urlRouter.update(true);
50-
if (!urlRouter.interceptDeferred) urlRouter.listen();
51-
return urlRouter;
41+
$get(): UrlRouter {
42+
const urlService = this.router.urlService;
43+
this.router.urlRouter.update(true);
44+
if (!urlService.interceptDeferred) urlService.listen();
45+
return this.router.urlRouter;
5246
}
5347

5448
/**
@@ -85,10 +79,10 @@ export class UrlRouterProvider {
8579
rule(ruleFn: RawNg1RuleFunction): UrlRouterProvider {
8680
if (!isFunction(ruleFn)) throw new Error("'rule' must be a function");
8781

88-
const match = () => ruleFn(services.$injector, this._router.locationService);
82+
const match = () => ruleFn(services.$injector, this.router.locationService);
8983

9084
const rule = new BaseUrlRule(match, identity);
91-
this._urlRouter.rule(rule);
85+
this.router.urlService.rules.rule(rule);
9286
return this;
9387
}
9488

@@ -119,12 +113,11 @@ export class UrlRouterProvider {
119113
* @return {object} `$urlRouterProvider` - `$urlRouterProvider` instance
120114
*/
121115
otherwise(rule: string | RawNg1RuleFunction): UrlRouterProvider {
122-
const urlRouter = this._urlRouter;
123-
116+
const urlRules = this.router.urlService.rules;
124117
if (isString(rule)) {
125-
urlRouter.otherwise(rule);
118+
urlRules.otherwise(rule);
126119
} else if (isFunction(rule)) {
127-
urlRouter.otherwise(() => rule(services.$injector, this._router.locationService));
120+
urlRules.otherwise(() => rule(services.$injector, this.router.locationService));
128121
} else {
129122
throw new Error("'rule' must be a string or function");
130123
}
@@ -172,10 +165,10 @@ export class UrlRouterProvider {
172165
*/
173166
when(what: RegExp | UrlMatcher | string, handler: string | IInjectable) {
174167
if (isArray(handler) || isFunction(handler)) {
175-
handler = UrlRouterProvider.injectableHandler(this._router, handler);
168+
handler = UrlRouterProvider.injectableHandler(this.router, handler);
176169
}
177170

178-
this._urlRouter.when(what, handler as any);
171+
this.router.urlService.rules.when(what, handler as any);
179172
return this;
180173
}
181174

@@ -210,6 +203,6 @@ export class UrlRouterProvider {
210203
* Passing no parameter is equivalent to `true`.
211204
*/
212205
deferIntercept(defer?: boolean) {
213-
this._urlRouter.deferIntercept(defer);
206+
this.router.urlService.deferIntercept(defer);
214207
}
215208
}

test/urlRouterSpec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,23 +294,23 @@ describe('UrlRouter', function() {
294294
});
295295

296296
describe('URL generation', function() {
297-
it('should return null when UrlMatcher rejects parameters', inject(function($urlRouter) {
297+
it('should return null when UrlMatcher rejects parameters', inject(function($urlRouter: UrlRouter) {
298298
$umf.type('custom', <any>{ is: val => val === 1138 });
299299
const matcher = $umf.compile('/foo/{param:custom}');
300300

301301
expect($urlRouter.href(matcher, { param: 1138 })).toBe('#/foo/1138');
302302
expect($urlRouter.href(matcher, { param: 5 })).toBeNull();
303303
}));
304304

305-
it('should handle the new html5Mode object config from Angular 1.3', inject(function($urlRouter) {
305+
it('should handle the new html5Mode object config from Angular 1.3', inject(function($urlRouter: UrlRouter) {
306306
$lp.html5Mode({
307307
enabled: false,
308308
});
309309

310310
expect($urlRouter.href($umf.compile('/hello'))).toBe('#/hello');
311311
}));
312312

313-
it('should return URLs with #fragments', inject(function($urlRouter) {
313+
it('should return URLs with #fragments', inject(function($urlRouter: UrlRouter) {
314314
// html5mode disabled
315315
$lp.html5Mode(false);
316316
expect(html5Compat($lp.html5Mode())).toBe(false);
@@ -325,7 +325,7 @@ describe('UrlRouter', function() {
325325
}));
326326

327327
it('should return URLs with #fragments when html5Mode is true & browser does not support ___pushState', inject(function(
328-
$urlRouter
328+
$urlRouter: UrlRouter
329329
) {
330330
$lp.html5Mode(true);
331331
$s['history'] = false;

0 commit comments

Comments
 (0)