Skip to content

Commit ba07f39

Browse files
committed
refactor(router): convert to typescript
Fixes angular#2001
1 parent 4c8e11a commit ba07f39

31 files changed

Lines changed: 905 additions & 982 deletions
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export {RouteRegistry} from './src/router/route_registry';
1414
export {BrowserLocation} from './src/router/browser_location';
1515
export {Location} from './src/router/location';
1616
export {Pipeline} from './src/router/pipeline';
17-
export * from './src/router/route_config_annotation';
1817
export * from './src/router/route_config_decorator';
1918

2019
import {BrowserLocation} from './src/router/browser_location';
@@ -27,18 +26,16 @@ import {Location} from './src/router/location';
2726
import {appComponentTypeToken} from './src/core/application_tokens';
2827
import {bind} from './di';
2928
import {CONST_EXPR} from './src/facade/lang';
29+
import {List} from './src/facade/collection';
3030

31-
export const routerDirectives:List = CONST_EXPR([
32-
RouterOutlet,
33-
RouterLink
34-
]);
31+
export const routerDirectives: List<any> = CONST_EXPR([RouterOutlet, RouterLink]);
3532

36-
export var routerInjectables:List = [
33+
export var routerInjectables: List<any> = [
3734
RouteRegistry,
3835
Pipeline,
3936
BrowserLocation,
4037
Location,
41-
bind(Router).toFactory((registry, pipeline, location, appRoot) => {
42-
return new RootRouter(registry, pipeline, location, appRoot);
43-
}, [RouteRegistry, Pipeline, Location, appComponentTypeToken])
38+
bind(Router).toFactory((registry, pipeline, location, appRoot) =>
39+
{ return new RootRouter(registry, pipeline, location, appRoot);},
40+
[RouteRegistry, Pipeline, Location, appComponentTypeToken])
4441
];

modules/angular2/src/dom/dom_adapter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ export class DomAdapter {
110110
cssToRules(css: string): List<any> { throw _abstract(); }
111111
supportsDOMEvents(): boolean { throw _abstract(); }
112112
supportsNativeShadowDOM(): boolean { throw _abstract(); }
113-
getGlobalEventTarget(target: string) { throw _abstract(); }
114-
getHistory() { throw _abstract(); }
115-
getLocation() { throw _abstract(); }
116-
getBaseHref() { throw _abstract(); }
113+
getGlobalEventTarget(target: string): any { throw _abstract(); }
114+
getHistory(): any { throw _abstract(); }
115+
getLocation(): any { throw _abstract(); }
116+
getBaseHref(): string { throw _abstract(); }
117117
getUserAgent(): string { throw _abstract(); }
118118
setData(element, name: string, value: string) { throw _abstract(); }
119119
getData(element, name: string): string { throw _abstract(); }

modules/angular2/src/facade/browser.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ export 'dart:html'
1515
Node,
1616
MouseEvent,
1717
KeyboardEvent,
18-
Event;
18+
Event,
19+
EventTarget,
20+
History,
21+
Location,
22+
EventListener;
1923

2024
final _gc = context['gc'];
2125

modules/angular2/src/facade/browser.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ export var gc = window['gc'] ? () => window['gc']() : () => null;
1010
export const Event = Event;
1111
export const MouseEvent = MouseEvent;
1212
export const KeyboardEvent = KeyboardEvent;
13+
export const EventTarget = EventTarget;
14+
export const History = History;
15+
export const Location = Location;
16+
export const EventListener = EventListener;

modules/angular2/src/mock/location_mock.js renamed to modules/angular2/src/mock/location_mock.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import {Location} from 'angular2/src/router/location';
99
@proxy
1010
@IMPLEMENTS(Location)
1111
export class SpyLocation extends SpyObject {
12-
urlChanges:List<string>;
13-
_path:string;
14-
_subject:EventEmitter;
15-
_baseHref:string;
12+
urlChanges: List<string>;
13+
_path: string;
14+
_subject: EventEmitter;
15+
_baseHref: string;
1616

1717
constructor() {
1818
super();
@@ -22,29 +22,17 @@ export class SpyLocation extends SpyObject {
2222
this._baseHref = '';
2323
}
2424

25-
setInitialPath(url:string) {
26-
this._path = url;
27-
}
25+
setInitialPath(url: string) { this._path = url; }
2826

29-
setBaseHref(url:string) {
30-
this._baseHref = url;
31-
}
27+
setBaseHref(url: string) { this._baseHref = url; }
3228

33-
path():string {
34-
return this._path;
35-
}
29+
path(): string { return this._path; }
3630

37-
simulateUrlPop(pathname:string) {
38-
ObservableWrapper.callNext(this._subject, {
39-
'url': pathname
40-
});
41-
}
31+
simulateUrlPop(pathname: string) { ObservableWrapper.callNext(this._subject, {'url': pathname}); }
4232

43-
normalizeAbsolutely(url) {
44-
return this._baseHref + url;
45-
}
33+
normalizeAbsolutely(url) { return this._baseHref + url; }
4634

47-
go(url:string) {
35+
go(url: string) {
4836
url = this.normalizeAbsolutely(url);
4937
if (this._path == url) {
5038
return;
@@ -65,5 +53,5 @@ export class SpyLocation extends SpyObject {
6553
ObservableWrapper.subscribe(this._subject, onNext, onThrow, onReturn);
6654
}
6755

68-
noSuchMethod(m){return super.noSuchMethod(m);}
56+
noSuchMethod(m) { return super.noSuchMethod(m); }
6957
}

modules/angular2/src/router/browser_location.js

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {DOM} from 'angular2/src/dom/dom_adapter';
2+
import {Injectable} from 'angular2/di';
3+
import {EventListener, History, Location} from 'angular2/src/facade/browser';
4+
5+
@Injectable()
6+
export class BrowserLocation {
7+
private _location: Location;
8+
private _history: History;
9+
private _baseHref: string;
10+
11+
constructor() {
12+
this._location = DOM.getLocation();
13+
this._history = DOM.getHistory();
14+
this._baseHref = DOM.getBaseHref();
15+
}
16+
17+
onPopState(fn: EventListener): void {
18+
DOM.getGlobalEventTarget('window').addEventListener('popstate', fn, false);
19+
}
20+
21+
getBaseHref(): string { return this._baseHref; }
22+
23+
path(): string { return this._location.pathname; }
24+
25+
pushState(state: any, title: string, url: string) { this._history.pushState(state, title, url); }
26+
27+
forward(): void { this._history.forward(); }
28+
29+
back(): void { this._history.back(); }
30+
}

modules/angular2/src/router/instruction.js renamed to modules/angular2/src/router/instruction.ts

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
1-
import {Map, MapWrapper, StringMap, StringMapWrapper, List, ListWrapper} from 'angular2/src/facade/collection';
1+
import {
2+
Map,
3+
MapWrapper,
4+
StringMap,
5+
StringMapWrapper,
6+
List,
7+
ListWrapper
8+
} from 'angular2/src/facade/collection';
29
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
310
import {isPresent, normalizeBlank} from 'angular2/src/facade/lang';
411

512
export class RouteParams {
6-
params:StringMap<string, string>;
13+
constructor(public params: StringMap<string, string>) {}
714

8-
constructor(params:StringMap) {
9-
this.params = params;
10-
}
11-
12-
get(param:string): string {
13-
return normalizeBlank(StringMapWrapper.get(this.params, param));
14-
}
15+
get(param: string): string { return normalizeBlank(StringMapWrapper.get(this.params, param)); }
1516
}
1617

1718
/**
1819
* An `Instruction` represents the component hierarchy of the application based on a given route
1920
*/
2021
export class Instruction {
21-
component:any;
22-
_children:Map<string, Instruction>;
22+
component: any;
23+
private _children: StringMap<string, Instruction>;
2324

2425
// the part of the URL captured by this instruction
25-
capturedUrl:string;
26+
capturedUrl: string;
2627

2728
// the part of the URL captured by this instruction and all children
28-
accumulatedUrl:string;
29-
30-
params:StringMap<string, string>;
31-
reuse:boolean;
32-
specificity:number;
33-
34-
constructor({params, component, children, matchedUrl, parentSpecificity}:{params:StringMap, component:any, children:Map, matchedUrl:string, parentSpecificity:number} = {}) {
29+
accumulatedUrl: string;
30+
31+
params: StringMap<string, string>;
32+
reuse: boolean;
33+
specificity: number;
34+
35+
constructor({params, component, children, matchedUrl, parentSpecificity}: {
36+
params?: StringMap<string, any>,
37+
component?: any,
38+
children?: StringMap<string, Instruction>,
39+
matchedUrl?: string,
40+
parentSpecificity?: number
41+
} = {}) {
3542
this.reuse = false;
3643
this.capturedUrl = matchedUrl;
3744
this.accumulatedUrl = matchedUrl;
@@ -53,39 +60,38 @@ export class Instruction {
5360
this.params = params;
5461
}
5562

56-
hasChild(outletName:string):boolean {
63+
hasChild(outletName: string): boolean {
5764
return StringMapWrapper.contains(this._children, outletName);
5865
}
5966

6067
/**
6168
* Returns the child instruction with the given outlet name
6269
*/
63-
getChild(outletName:string):Instruction {
70+
getChild(outletName: string): Instruction {
6471
return StringMapWrapper.get(this._children, outletName);
6572
}
6673

6774
/**
6875
* (child:Instruction, outletName:string) => {}
6976
*/
70-
forEachChild(fn:Function): void {
71-
StringMapWrapper.forEach(this._children, fn);
72-
}
77+
forEachChild(fn: Function): void { StringMapWrapper.forEach(this._children, fn); }
7378

7479
/**
7580
* Does a synchronous, breadth-first traversal of the graph of instructions.
7681
* Takes a function with signature:
7782
* (child:Instruction, outletName:string) => {}
7883
*/
79-
traverseSync(fn:Function): void {
84+
traverseSync(fn: Function): void {
8085
this.forEachChild(fn);
8186
this.forEachChild((childInstruction, _) => childInstruction.traverseSync(fn));
8287
}
8388

8489

8590
/**
86-
* Takes a currently active instruction and sets a reuse flag on each of this instruction's children
91+
* Takes a currently active instruction and sets a reuse flag on each of this instruction's
92+
* children
8793
*/
88-
reuseComponentsFrom(oldInstruction:Instruction): void {
94+
reuseComponentsFrom(oldInstruction: Instruction): void {
8995
this.traverseSync((childInstruction, outletName) => {
9096
var oldInstructionChild = oldInstruction.getChild(outletName);
9197
if (shouldReuseComponent(childInstruction, oldInstructionChild)) {
@@ -95,16 +101,16 @@ export class Instruction {
95101
}
96102
}
97103

98-
function shouldReuseComponent(instr1:Instruction, instr2:Instruction): boolean {
104+
function shouldReuseComponent(instr1: Instruction, instr2: Instruction): boolean {
99105
return instr1.component == instr2.component &&
100-
StringMapWrapper.equals(instr1.params, instr2.params);
106+
StringMapWrapper.equals(instr1.params, instr2.params);
101107
}
102108

103-
function mapObjAsync(obj:StringMap, fn): Promise {
109+
function mapObjAsync(obj: StringMap<string, any>, fn): Promise<List<any>> {
104110
return PromiseWrapper.all(mapObj(obj, fn));
105111
}
106112

107-
function mapObj(obj:StringMap, fn: Function):List {
113+
function mapObj(obj: StringMap<any, any>, fn: Function): List<any> {
108114
var result = ListWrapper.create();
109115
StringMapWrapper.forEach(obj, (value, key) => ListWrapper.push(result, fn(value, key)));
110116
return result;
Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
import {BrowserLocation} from './browser_location';
22
import {StringWrapper} from 'angular2/src/facade/lang';
33
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
4+
import {Injectable} from 'angular2/di';
45

6+
@Injectable()
57
export class Location {
6-
_subject:EventEmitter;
7-
_browserLocation:BrowserLocation;
8-
_baseHref:string;
9-
constructor(browserLocation:BrowserLocation) {
8+
private _subject: EventEmitter;
9+
private _baseHref: string;
10+
11+
constructor(public _browserLocation: BrowserLocation) {
1012
this._subject = new EventEmitter();
11-
this._browserLocation = browserLocation;
1213
this._baseHref = stripIndexHtml(this._browserLocation.getBaseHref());
1314
this._browserLocation.onPopState((_) => this._onPopState(_));
1415
}
1516

16-
_onPopState(_): void {
17-
ObservableWrapper.callNext(this._subject, {
18-
'url': this.path()
19-
});
20-
}
17+
_onPopState(_): void { ObservableWrapper.callNext(this._subject, {'url': this.path()}); }
2118

22-
path(): string {
23-
return this.normalize(this._browserLocation.path());
24-
}
19+
path(): string { return this.normalize(this._browserLocation.path()); }
2520

26-
normalize(url: string): string {
27-
return this._stripBaseHref(stripIndexHtml(url));
28-
}
21+
normalize(url: string): string { return this._stripBaseHref(stripIndexHtml(url)); }
2922

3023
normalizeAbsolutely(url: string): string {
3124
if (url[0] != '/') {
@@ -48,18 +41,14 @@ export class Location {
4841
return url;
4942
}
5043

51-
go(url:string): void {
44+
go(url: string): void {
5245
var finalUrl = this.normalizeAbsolutely(url);
5346
this._browserLocation.pushState(null, '', finalUrl);
5447
}
5548

56-
forward(): void {
57-
this._browserLocation.forward();
58-
}
49+
forward(): void { this._browserLocation.forward(); }
5950

60-
back(): void {
61-
this._browserLocation.back();
62-
}
51+
back(): void { this._browserLocation.back(); }
6352

6453
subscribe(onNext, onThrow = null, onReturn = null): void {
6554
ObservableWrapper.subscribe(this._subject, onNext, onThrow, onReturn);

0 commit comments

Comments
 (0)