Skip to content

Commit cab1d0e

Browse files
committed
feat(router): allow configuring app base href via token
1 parent 0c282e8 commit cab1d0e

File tree

5 files changed

+134
-65
lines changed

5 files changed

+134
-65
lines changed

modules/angular2/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export {RouterLink} from './src/router/router_link';
1212
export {RouteParams} from './src/router/instruction';
1313
export {RouteRegistry} from './src/router/route_registry';
1414
export {BrowserLocation} from './src/router/browser_location';
15-
export {Location} from './src/router/location';
15+
export {Location, appBaseHrefToken} from './src/router/location';
1616
export {Pipeline} from './src/router/pipeline';
1717
export * from './src/router/route_config_decorator';
1818

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {proxy, SpyObject} from 'angular2/test_lib';
2+
import {IMPLEMENTS, BaseException} from 'angular2/src/facade/lang';
3+
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
4+
import {List, ListWrapper} from 'angular2/src/facade/collection';
5+
import {BrowserLocation} from 'angular2/src/router/browser_location';
6+
7+
@proxy
8+
@IMPLEMENTS(BrowserLocation)
9+
export class DummyBrowserLocation extends SpyObject {
10+
internalBaseHref: string = '/';
11+
internalPath: string = '/';
12+
internalTitle: string = '';
13+
urlChanges: List<string> = ListWrapper.create();
14+
_subject: EventEmitter = new EventEmitter();
15+
constructor() { super(); }
16+
17+
simulatePopState(url): void {
18+
this.internalPath = url;
19+
ObservableWrapper.callNext(this._subject, null);
20+
}
21+
22+
path(): string { return this.internalPath; }
23+
24+
simulateUrlPop(pathname: string): void {
25+
ObservableWrapper.callNext(this._subject, {'url': pathname});
26+
}
27+
28+
pushState(ctx: any, title: string, url: string): void {
29+
this.internalTitle = title;
30+
this.internalPath = url;
31+
ListWrapper.push(this.urlChanges, url);
32+
}
33+
34+
forward(): void { throw new BaseException('Not implemented yet!'); }
35+
36+
back(): void { throw new BaseException('Not implemented yet!'); }
37+
38+
onPopState(fn): void { ObservableWrapper.subscribe(this._subject, fn); }
39+
40+
getBaseHref(): string { return this.internalBaseHref; }
41+
42+
noSuchMethod(m) { return super.noSuchMethod(m); }
43+
}

modules/angular2/src/router/location.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import {BrowserLocation} from './browser_location';
2-
import {StringWrapper} from 'angular2/src/facade/lang';
2+
import {StringWrapper, isPresent, CONST_EXPR} from 'angular2/src/facade/lang';
33
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
4-
import {Injectable} from 'angular2/di';
4+
import {OpaqueToken, Injectable, Optional, Inject} from 'angular2/di';
5+
6+
export const appBaseHrefToken: OpaqueToken = CONST_EXPR(new OpaqueToken('locationHrefToken'));
57

68
@Injectable()
79
export class Location {
810
private _subject: EventEmitter;
911
private _baseHref: string;
1012

11-
constructor(public _browserLocation: BrowserLocation) {
13+
constructor(public _browserLocation: BrowserLocation,
14+
@Optional() @Inject(appBaseHrefToken) href?: string) {
1215
this._subject = new EventEmitter();
13-
this._baseHref = stripIndexHtml(this._browserLocation.getBaseHref());
16+
this._baseHref = stripIndexHtml(isPresent(href) ? href : this._browserLocation.getBaseHref());
1417
this._browserLocation.onPopState((_) => this._onPopState(_));
1518
}
1619

modules/angular2/test/router/location_spec.ts

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,50 @@ import {
1111
beforeEachBindings,
1212
SpyObject
1313
} from 'angular2/test_lib';
14-
import {IMPLEMENTS} from 'angular2/src/facade/lang';
15-
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
1614

15+
import {Injector, bind} from 'angular2/di';
16+
import {CONST_EXPR} from 'angular2/src/facade/lang';
17+
import {Location, appBaseHrefToken} from 'angular2/src/router/location';
1718
import {BrowserLocation} from 'angular2/src/router/browser_location';
18-
import {Location} from 'angular2/src/router/location';
19+
import {DummyBrowserLocation} from 'angular2/src/mock/browser_location_mock';
1920

2021
export function main() {
2122
describe('Location', () => {
2223

2324
var browserLocation, location;
2425

25-
beforeEach(() => {
26+
function makeLocation(baseHref: string = '/my/app', binding: any = CONST_EXPR([])): Location {
2627
browserLocation = new DummyBrowserLocation();
27-
browserLocation.spy('pushState');
28-
browserLocation.baseHref = '/my/app';
29-
location = new Location(browserLocation);
30-
});
28+
browserLocation.internalBaseHref = baseHref;
29+
let injector = Injector.resolveAndCreate(
30+
[Location, bind(BrowserLocation).toValue(browserLocation), binding]);
31+
return location = injector.get(Location);
32+
}
33+
34+
beforeEach(makeLocation);
3135

3236
it('should normalize relative urls on navigate', () => {
3337
location.go('user/btford');
34-
expect(browserLocation.spy('pushState'))
35-
.toHaveBeenCalledWith(null, '', '/my/app/user/btford');
38+
expect(browserLocation.path()).toEqual('/my/app/user/btford');
3639
});
3740

3841
it('should not prepend urls with starting slash when an empty URL is provided',
39-
() => { expect(location.normalizeAbsolutely('')).toEqual(browserLocation.baseHref); });
42+
() => { expect(location.normalizeAbsolutely('')).toEqual(browserLocation.getBaseHref()); });
4043

4144
it('should not prepend path with an extra slash when a baseHref has a trailing slash', () => {
42-
browserLocation = new DummyBrowserLocation();
43-
browserLocation.spy('pushState');
44-
browserLocation.baseHref = '/my/slashed/app/';
45-
location = new Location(browserLocation);
45+
let location = makeLocation('/my/slashed/app/');
4646
expect(location.normalizeAbsolutely('/page')).toEqual('/my/slashed/app/page');
4747
});
4848

4949
it('should not append urls with leading slash on navigate', () => {
5050
location.go('/my/app/user/btford');
51-
expect(browserLocation.spy('pushState'))
52-
.toHaveBeenCalledWith(null, '', '/my/app/user/btford');
51+
expect(browserLocation.path()).toEqual('/my/app/user/btford');
5352
});
5453

5554
it('should remove index.html from base href', () => {
56-
browserLocation.baseHref = '/my/app/index.html';
57-
location = new Location(browserLocation);
55+
let location = makeLocation('/my/app/index.html');
5856
location.go('user/btford');
59-
expect(browserLocation.spy('pushState'))
60-
.toHaveBeenCalledWith(null, '', '/my/app/user/btford');
57+
expect(browserLocation.path()).toEqual('/my/app/user/btford');
6158
});
6259

6360
it('should normalize urls on popstate', inject([AsyncTestCompleter], (async) => {
@@ -72,31 +69,11 @@ export function main() {
7269
browserLocation.internalPath = '/my/app/user/btford';
7370
expect(location.path()).toEqual('/user/btford');
7471
});
75-
});
76-
}
77-
78-
@proxy
79-
@IMPLEMENTS(BrowserLocation)
80-
class DummyBrowserLocation extends SpyObject {
81-
baseHref;
82-
internalPath;
83-
_subject: EventEmitter;
84-
constructor() {
85-
super();
86-
this.internalPath = '/';
87-
this._subject = new EventEmitter();
88-
}
89-
90-
simulatePopState(url) {
91-
this.internalPath = url;
92-
ObservableWrapper.callNext(this._subject, null);
93-
}
9472

95-
path() { return this.internalPath; }
96-
97-
onPopState(fn) { ObservableWrapper.subscribe(this._subject, fn); }
98-
99-
getBaseHref() { return this.baseHref; }
100-
101-
noSuchMethod(m) { return super.noSuchMethod(m); }
73+
it('should use optional base href param', () => {
74+
let location = makeLocation('/', bind(appBaseHrefToken).toValue('/my/custom/href'));
75+
location.go('user/btford');
76+
expect(browserLocation.path()).toEqual('/my/custom/href/user/btford');
77+
});
78+
});
10279
}

modules/angular2/test/router/router_integration_spec.ts

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ import {DOM} from 'angular2/src/dom/dom_adapter';
1717
import {bind} from 'angular2/di';
1818
import {DOCUMENT_TOKEN} from 'angular2/src/render/dom/dom_renderer';
1919
import {RouteConfig} from 'angular2/src/router/route_config_decorator';
20-
import {routerInjectables, Router} from 'angular2/router';
21-
import {RouterOutlet} from 'angular2/src/router/router_outlet';
22-
import {SpyLocation} from 'angular2/src/mock/location_mock';
23-
import {Location} from 'angular2/src/router/location';
2420
import {PromiseWrapper} from 'angular2/src/facade/async';
2521
import {BaseException} from 'angular2/src/facade/lang';
22+
import {routerInjectables, Router, appBaseHrefToken, routerDirectives} from 'angular2/router';
23+
import {BrowserLocation} from 'angular2/src/router/browser_location';
24+
import {DummyBrowserLocation} from 'angular2/src/mock/browser_location_mock';
2625

2726
export function main() {
2827
describe('router injectables', () => {
@@ -33,17 +32,23 @@ export function main() {
3332
DOM.appendChild(fakeDoc.body, el);
3433
testBindings = [
3534
routerInjectables,
36-
bind(Location).toClass(SpyLocation),
35+
bind(BrowserLocation)
36+
.toFactory(() => {
37+
var browserLocation = new DummyBrowserLocation();
38+
browserLocation.spy('pushState');
39+
return browserLocation;
40+
}),
3741
bind(DOCUMENT_TOKEN).toValue(fakeDoc)
3842
];
3943
});
4044

41-
it('should support bootstrap a simple app', inject([AsyncTestCompleter], (async) => {
45+
it('should bootstrap a simple app', inject([AsyncTestCompleter], (async) => {
4246
bootstrap(AppCmp, testBindings)
4347
.then((applicationRef) => {
4448
var router = applicationRef.hostComponent.router;
4549
router.subscribe((_) => {
4650
expect(el).toHaveText('outer { hello }');
51+
expect(applicationRef.hostComponent.location.path()).toEqual('/');
4752
async.done();
4853
});
4954
});
@@ -62,22 +67,64 @@ export function main() {
6267
});
6368
}));
6469

70+
it('should bootstrap an app with a hierarchy', inject([AsyncTestCompleter], (async) => {
71+
bootstrap(HierarchyAppCmp, testBindings)
72+
.then((applicationRef) => {
73+
var router = applicationRef.hostComponent.router;
74+
router.subscribe((_) => {
75+
expect(el).toHaveText('root { parent { hello } }');
76+
expect(applicationRef.hostComponent.location.path()).toEqual('/parent/child');
77+
async.done();
78+
});
79+
router.navigate('/parent/child');
80+
});
81+
}));
82+
83+
it('should bootstrap an app with a custom app base href',
84+
inject([AsyncTestCompleter], (async) => {
85+
bootstrap(HierarchyAppCmp, [testBindings, bind(appBaseHrefToken).toValue('/my/app')])
86+
.then((applicationRef) => {
87+
var router = applicationRef.hostComponent.router;
88+
router.subscribe((_) => {
89+
expect(el).toHaveText('root { parent { hello } }');
90+
expect(applicationRef.hostComponent.location.path())
91+
.toEqual('/my/app/parent/child');
92+
async.done();
93+
});
94+
router.navigate('/parent/child');
95+
});
96+
}));
97+
6598
// TODO: add a test in which the child component has bindings
6699
});
67100
}
68101

69102

70103
@Component({selector: 'hello-cmp'})
71-
@View({template: "hello"})
104+
@View({template: 'hello'})
72105
class HelloCmp {
73106
}
74107

75108
@Component({selector: 'app-cmp'})
76-
@View({template: "outer { <router-outlet></router-outlet> }", directives: [RouterOutlet]})
109+
@View({template: "outer { <router-outlet></router-outlet> }", directives: routerDirectives})
77110
@RouteConfig([{path: '/', component: HelloCmp}])
78111
class AppCmp {
79-
router: Router;
80-
constructor(router: Router) { this.router = router; }
112+
constructor(public router: Router, public location: BrowserLocation) {}
113+
}
114+
115+
116+
@Component({selector: 'parent-cmp'})
117+
@View({template: `parent { <router-outlet></router-outlet> }`, directives: routerDirectives})
118+
@RouteConfig([{path: '/child', component: HelloCmp}])
119+
class ParentCmp {
120+
}
121+
122+
123+
@Component({selector: 'app-cmp'})
124+
@View({template: `root { <router-outlet></router-outlet> }`, directives: routerDirectives})
125+
@RouteConfig([{path: '/parent', component: ParentCmp}])
126+
class HierarchyAppCmp {
127+
constructor(public router: Router, public location: BrowserLocation) {}
81128
}
82129

83130
@Component({selector: 'oops-cmp'})
@@ -87,9 +134,8 @@ class BrokenCmp {
87134
}
88135

89136
@Component({selector: 'app-cmp'})
90-
@View({template: "outer { <router-outlet></router-outlet> }", directives: [RouterOutlet]})
137+
@View({template: "outer { <router-outlet></router-outlet> }", directives: routerDirectives})
91138
@RouteConfig([{path: '/cause-error', component: BrokenCmp}])
92139
class BrokenAppCmp {
93-
router: Router;
94-
constructor(router: Router) { this.router = router; }
140+
constructor(public router: Router, public location: BrowserLocation) {}
95141
}

0 commit comments

Comments
 (0)