Skip to content

Commit 315452b

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

7 files changed

+26
-22
lines changed

src/url/urlRouter.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,10 @@ export class UrlRouterProvider implements Disposable {
178178
* Note: the handler may also invoke arbitrary code, such as `$state.go()`
179179
*/
180180
when(what: (RegExp|UrlMatcher|string), handler: string|IInjectable, ruleCallback = function(rule) {}) {
181-
let $urlMatcherFactory = this.router.urlMatcherFactory;
182-
let $stateParams = this.router.globals.params;
181+
let router = this.router;
182+
let $urlMatcherFactory = router.urlMatcherFactory;
183+
let $stateParams = router.globals.params;
184+
let $loc = router.urlService;
183185
let redirect, handlerIsString = isString(handler);
184186

185187
// @todo Queue this
@@ -188,7 +190,6 @@ export class UrlRouterProvider implements Disposable {
188190
if (!handlerIsString && !isFunction(handler) && !isArray(handler))
189191
throw new Error("invalid 'handler' in when()");
190192

191-
let $loc = services.location;
192193
let strategies = {
193194
matcher: function (_what, _handler) {
194195
if (handlerIsString) {
@@ -341,7 +342,7 @@ export class UrlRouter implements Disposable {
341342
* This causes [[UrlRouter]] to start listening for changes to the URL, if it wasn't already listening.
342343
*/
343344
listen(): Function {
344-
return this.listener = this.listener || services.location.onChange(evt => this.sync(evt));
345+
return this.listener = this.listener || this.router.urlService.onChange(evt => this.sync(evt));
345346
}
346347

347348
/**

src/vanilla/pushStateLocation.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @internalapi
33
* @module vanilla
44
*/ /** */
5-
import { services, isDefined } from "../common/index";
5+
import { isDefined } from "../common/index";
66
import { LocationServices } from "../common/coreservices";
77
import { splitQuery, trimHashVal, getParams, locationPluginFactory } from "./utils";
88
import { LocationPlugin } from "./interface";
@@ -22,7 +22,7 @@ export class PushStateLocationService implements LocationServices, Disposable {
2222
private _location: Location;
2323
private _history: History;
2424

25-
constructor() {
25+
constructor(public router: UIRouter) {
2626
this._location = location;
2727
this._history = history;
2828
};
@@ -32,21 +32,22 @@ export class PushStateLocationService implements LocationServices, Disposable {
3232
}
3333

3434
path() {
35-
let base = services.locationConfig.baseHref();
35+
let base = this.router.urlConfig.baseHref();
3636
let path = this._location.pathname;
3737
let idx = path.indexOf(base);
3838
if (idx !== 0) throw new Error(`current url: ${path} does not start with <base> tag ${base}`);
3939
return path.substr(base.length);
4040
}
4141

4242
search() {
43-
return getParams(splitQuery(this._location.search)[1]);
43+
return getParams(splitQuery(this._location.search)[1]);
4444
}
4545

4646
setUrl(url: string, replace: boolean = false) {
4747
if (isDefined(url)) {
48-
if (replace) this._history.replaceState(null, null, services.locationConfig.baseHref() + url);
49-
else this._history.pushState(null, null, services.locationConfig.baseHref() + url);
48+
let fullUrl = this.router.urlConfig.baseHref() + url;
49+
if (replace) this._history.replaceState(null, null, fullUrl);
50+
else this._history.pushState(null, null, fullUrl);
5051
}
5152
}
5253

test/lazyLoadSpec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ describe('future state', function () {
288288
});
289289

290290
it('triggered by a URL sync should re-parse the URL to activate the lazy loaded state', (done) => {
291-
services.location.setUrl('/a/def');
291+
router.urlService.setUrl('/a/def');
292292
$urlRouter.sync();
293293
$transitions.onSuccess({}, () => {
294294
expect($state.current.name).toBe('A');
@@ -334,7 +334,7 @@ describe('future state', function () {
334334
});
335335

336336
it('should re-parse the URL to activate the final state', (done) => {
337-
services.location.setUrl('/a/def/b');
337+
router.urlService.setUrl('/a/def/b');
338338
$urlRouter.sync();
339339
$transitions.onSuccess({}, () => {
340340
expect($state.current.name).toBe('A.B');
@@ -452,7 +452,7 @@ describe('future state', function () {
452452
});
453453

454454
it('should load and activate a nested future state by url sync', (done) => {
455-
services.location.setUrl('/a/aid/b/bid');
455+
router.urlService.setUrl('/a/aid/b/bid');
456456
$urlRouter.sync();
457457
$transitions.onSuccess({}, (trans) => {
458458
expect($state.current.name).toBe('A.B');

test/stateRegistrySpec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe("StateRegistry", () => {
6464
registry.register(state);
6565

6666
spyOn($state, "transitionTo");
67-
services.location.setUrl("/foo");
67+
router.urlService.setUrl("/foo");
6868
router.urlRouter.sync();
6969
expect($state.transitionTo['calls'].mostRecent().args[0]).toBe(state.$$state());
7070

test/stateServiceSpec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ describe('stateService', function () {
3232
router = new UIRouter();
3333
router.plugin(TestingPlugin);
3434

35-
$loc = services.location;
36-
$state = router.stateService;
37-
$registry = router.stateRegistry;
35+
$loc = router.urlService;
36+
$state = router.stateService;
37+
$registry = router.stateRegistry;
3838
$transitions = router.transitionService;
3939
});
4040

test/vanilla.browserHistorySpec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import * as vanilla from "../src/vanilla"
66
describe('browserHistory implementation', () => {
77

88
let router: UIRouter;
9+
let locationProvider;
910
let makeMatcher;
10-
let locationProvider = services.location;
1111

1212
// Replace the `history` reference because PhantomJS does not support spying on it.
1313
function mockHistoryObject() {
@@ -26,6 +26,7 @@ describe('browserHistory implementation', () => {
2626
router.plugin(vanilla.servicesPlugin);
2727
router.plugin(vanilla.pushStateLocationPlugin);
2828
router.stateRegistry.stateQueue.autoFlush(router.stateService);
29+
locationProvider = router.urlService;
2930
makeMatcher = (url, config?) => {
3031
return new UrlMatcher(url, router.urlMatcherFactory.paramTypes, config)
3132
};
@@ -38,7 +39,7 @@ describe('browserHistory implementation', () => {
3839

3940
it('uses history.pushState when setting a url', () => {
4041
let service = mockHistoryObject();
41-
expect(services.location.html5Mode()).toBe(true);
42+
expect(router.urlService.html5Mode()).toBe(true);
4243
let stub = spyOn(service._history, 'pushState');
4344
router.urlRouter.push(makeMatcher('/hello/:name'), { name: 'world' }, {});
4445
expect(stub.calls.first().args[2]).toBe('/hello/world');
@@ -52,7 +53,7 @@ describe('browserHistory implementation', () => {
5253
});
5354

5455
it('returns the correct url query', () => {
55-
expect(services.location.html5Mode()).toBe(true);
56+
expect(router.urlService.html5Mode()).toBe(true);
5657
return router.stateService.go('path', {urlParam: 'bar'}).then(() => {
5758
expect(window.location.toString().includes('/path/bar')).toBe(true);
5859
expect(window.location.toString().includes('/#/path/bar')).toBe(false);

test/vanilla.hashHistorySpec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ describe('hashHistory implementation', () => {
55

66
let router;
77
let $state;
8+
let locationProvider;
89
let makeMatcher;
9-
let locationProvider = services.location;
1010

1111
beforeEach(() => {
1212
router = new UIRouter();
1313
router.plugin(vanilla.servicesPlugin);
1414
router.plugin(vanilla.hashLocationPlugin);
1515
$state = router.stateService;
1616
router.stateRegistry.stateQueue.autoFlush($state);
17+
locationProvider = router.urlService;
1718

1819
makeMatcher = (url, config?) => {
1920
return new UrlMatcher(url, router.urlMatcherFactory.paramTypes, config)
@@ -26,7 +27,7 @@ describe('hashHistory implementation', () => {
2627
});
2728

2829
it('reports html5Mode to be false', () => {
29-
expect(services.location.html5Mode()).toBe(false);
30+
expect(router.urlService.html5Mode()).toBe(false);
3031
});
3132

3233
it('returns the correct url query', (done) => {

0 commit comments

Comments
 (0)