Skip to content

Commit 1b1af3b

Browse files
refactor(url): Move coreservices.location to router.urlService
1 parent 02d77e4 commit 1b1af3b

File tree

3 files changed

+24
-35
lines changed

3 files changed

+24
-35
lines changed

src/router.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ export class UIRouter {
4242

4343
urlMatcherFactory: UrlMatcherFactory = new UrlMatcherFactory();
4444

45-
urlRouterProvider: UrlRouterProvider = new UrlRouterProvider(this.urlMatcherFactory, this.globals.params);
45+
urlRouterProvider: UrlRouterProvider = new UrlRouterProvider(this);
4646

47-
urlRouter: UrlRouter = new UrlRouter(this.urlRouterProvider);
47+
urlRouter: UrlRouter = new UrlRouter(this);
4848

4949
stateRegistry: StateRegistry = new StateRegistry(this.urlMatcherFactory, this.urlRouterProvider);
5050

src/url/urlRouter.ts

+21-32
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
* @coreapi
33
* @module url
44
*/ /** for typedoc */
5-
import {extend, bindFunctions, IInjectable, removeFrom} from "../common/common";
6-
import {isFunction, isString, isDefined, isArray} from "../common/predicates";
7-
import {UrlMatcher} from "./urlMatcher";
8-
import {services, $InjectorLike, LocationServices} from "../common/coreservices";
9-
import {UrlMatcherFactory} from "./urlMatcherFactory";
10-
import {StateParams} from "../params/stateParams";
11-
import {RawParams} from "../params/interface";
5+
import { extend, bindFunctions, IInjectable, removeFrom } from "../common/common";
6+
import { isFunction, isString, isDefined, isArray } from "../common/predicates";
7+
import { UrlMatcher } from "./urlMatcher";
8+
import { services, $InjectorLike, LocationServices } from "../common/coreservices";
9+
import { RawParams } from "../params/interface";
1210
import { Disposable } from "../interface";
11+
import { UIRouter } from "../router";
1312

1413
/** @hidden Returns a string that is a prefix of all strings matching the RegExp */
1514
function regExpPrefix(re: RegExp) {
@@ -32,8 +31,7 @@ function handleIfMatch($injector: $InjectorLike, $stateParams: RawParams, handle
3231
}
3332

3433
/** @hidden */
35-
function appendBasePath(url: string, isHtml5: boolean, absolute: boolean): string {
36-
let baseHref = services.locationConfig.baseHref();
34+
function appendBasePath(url: string, isHtml5: boolean, absolute: boolean, baseHref: string): string {
3735
if (baseHref === '/') return url;
3836
if (isHtml5) return baseHref.slice(0, -1) + url;
3937
if (absolute) return baseHref.slice(1) + url;
@@ -53,15 +51,7 @@ export class UrlRouterProvider implements Disposable {
5351
/** @hidden */
5452
interceptDeferred = false;
5553

56-
/** @hidden */
57-
private $urlMatcherFactory: UrlMatcherFactory;
58-
/** @hidden */
59-
private $stateParams: StateParams;
60-
61-
constructor($urlMatcherFactory: UrlMatcherFactory, $stateParams: StateParams) {
62-
this.$urlMatcherFactory = $urlMatcherFactory;
63-
this.$stateParams = $stateParams;
64-
}
54+
constructor(public router: UIRouter) { }
6555

6656
/** @internalapi */
6757
dispose() {
@@ -188,7 +178,8 @@ export class UrlRouterProvider implements Disposable {
188178
* Note: the handler may also invoke arbitrary code, such as `$state.go()`
189179
*/
190180
when(what: (RegExp|UrlMatcher|string), handler: string|IInjectable, ruleCallback = function(rule) {}) {
191-
let {$urlMatcherFactory, $stateParams} = this;
181+
let $urlMatcherFactory = this.router.urlMatcherFactory;
182+
let $stateParams = this.router.globals.params;
192183
let redirect, handlerIsString = isString(handler);
193184

194185
// @todo Queue this
@@ -282,13 +273,9 @@ export class UrlRouter implements Disposable {
282273
private location: string;
283274
/** @hidden */
284275
private listener: Function;
285-
/** @hidden */
286-
private urlRouterProvider: UrlRouterProvider;
287-
288276

289277
/** @hidden */
290-
constructor(urlRouterProvider: UrlRouterProvider) {
291-
this.urlRouterProvider = urlRouterProvider;
278+
constructor(public router: UIRouter) {
292279
bindFunctions(UrlRouter.prototype, this, this);
293280
}
294281

@@ -323,9 +310,11 @@ export class UrlRouter implements Disposable {
323310
*/
324311
sync(evt?) {
325312
if (evt && evt.defaultPrevented) return;
326-
let $loc = services.location;
327-
let rules = this.urlRouterProvider.rules;
328-
let otherwiseFn = this.urlRouterProvider.otherwiseFn;
313+
314+
let router = this.router;
315+
let $loc = router.urlService;
316+
let rules = router.urlRouterProvider.rules;
317+
let otherwiseFn = router.urlRouterProvider.otherwiseFn;
329318

330319
function check(rule: Function) {
331320
let handled = rule(services.$injector, $loc);
@@ -359,7 +348,7 @@ export class UrlRouter implements Disposable {
359348
* Internal API.
360349
*/
361350
update(read?: boolean) {
362-
let $loc = services.location;
351+
let $loc = this.router.urlService;
363352
if (read) {
364353
this.location = $loc.path();
365354
return;
@@ -380,7 +369,7 @@ export class UrlRouter implements Disposable {
380369
*/
381370
push(urlMatcher: UrlMatcher, params: RawParams, options: { replace?: (string|boolean) }) {
382371
let replace = options && !!options.replace;
383-
services.location.setUrl(urlMatcher.format(params || {}), replace);
372+
this.router.urlService.setUrl(urlMatcher.format(params || {}), replace);
384373
}
385374

386375
/**
@@ -408,13 +397,13 @@ export class UrlRouter implements Disposable {
408397
let url = urlMatcher.format(params);
409398
options = options || { absolute: false };
410399

411-
let cfg = services.locationConfig;
412-
let loc = services.location;
400+
let cfg = this.router.urlConfig;
401+
let loc = this.router.urlService;
413402
let isHtml5 = loc.html5Mode();
414403
if (!isHtml5 && url !== null) {
415404
url = "#" + loc.hashPrefix() + url;
416405
}
417-
url = appendBasePath(url, isHtml5, options.absolute);
406+
url = appendBasePath(url, isHtml5, options.absolute, cfg.baseHref());
418407

419408
if (!options.absolute || !url) {
420409
return url;

test/stateRegistrySpec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe("StateRegistry", () => {
7272
$state.transitionTo['calls'].reset();
7373
router.urlRouter.sync();
7474
expect($state.transitionTo['calls'].count()).toBe(0);
75-
expect(router.urlRouter['urlRouterProvider'].rules.length).toBe(0);
75+
expect(router.urlRouterProvider.rules.length).toBe(0);
7676
});
7777
});
7878

0 commit comments

Comments
 (0)