refactor(Router): idiomatic TS

This commit is contained in:
Victor Berchet 2015-06-29 10:37:55 +02:00
parent eea989bef8
commit 1f04f70eda
7 changed files with 29 additions and 53 deletions

View File

@ -17,12 +17,11 @@ export const appBaseHrefToken: OpaqueToken = CONST_EXPR(new OpaqueToken('locatio
*/ */
@Injectable() @Injectable()
export class Location { export class Location {
private _subject: EventEmitter; private _subject: EventEmitter = new EventEmitter();
private _baseHref: string; private _baseHref: string;
constructor(public _platformStrategy: LocationStrategy, constructor(public _platformStrategy: LocationStrategy,
@Optional() @Inject(appBaseHrefToken) href?: string) { @Optional() @Inject(appBaseHrefToken) href?: string) {
this._subject = new EventEmitter();
this._baseHref = stripTrailingSlash( this._baseHref = stripTrailingSlash(
stripIndexHtml(isPresent(href) ? href : this._platformStrategy.getBaseHref())); stripIndexHtml(isPresent(href) ? href : this._platformStrategy.getBaseHref()));
this._platformStrategy.onPopState((_) => this._onPopState(_)); this._platformStrategy.onPopState((_) => this._onPopState(_));

View File

@ -33,11 +33,10 @@ export class ContinuationSegment extends Segment {
class StaticSegment extends Segment { class StaticSegment extends Segment {
regex: string; regex: string;
name: string; name: string = '';
constructor(public string: string) { constructor(public string: string) {
super(); super();
this.name = '';
this.regex = escapeRegex(string); this.regex = escapeRegex(string);
} }
@ -46,8 +45,9 @@ class StaticSegment extends Segment {
@IMPLEMENTS(Segment) @IMPLEMENTS(Segment)
class DynamicSegment { class DynamicSegment {
regex: string; regex: string = "([^/]+)";
constructor(public name: string) { this.regex = "([^/]+)"; }
constructor(public name: string) {}
generate(params: StringMap<string, string>): string { generate(params: StringMap<string, string>): string {
if (!StringMapWrapper.contains(params, this.name)) { if (!StringMapWrapper.contains(params, this.name)) {
@ -60,8 +60,8 @@ class DynamicSegment {
class StarSegment { class StarSegment {
regex: string; regex: string = "(.+)";
constructor(public name: string) { this.regex = "(.+)"; } constructor(public name: string) {}
generate(params: StringMap<string, string>): string { generate(params: StringMap<string, string>): string {
return normalizeBlank(StringMapWrapper.get(params, this.name)); return normalizeBlank(StringMapWrapper.get(params, this.name));
@ -134,10 +134,6 @@ export class PathRecognizer {
terminal: boolean = true; terminal: boolean = true;
constructor(public path: string, public handler: any) { constructor(public path: string, public handler: any) {
this.segments = [];
// TODO: use destructuring assignment
// see https://github.com/angular/ts2dart/issues/158
var parsed = parsePathString(path); var parsed = parsePathString(path);
var specificity = parsed['specificity']; var specificity = parsed['specificity'];
var segments = parsed['segments']; var segments = parsed['segments'];

View File

@ -22,15 +22,9 @@ import {PathRecognizer, ContinuationSegment} from './path_recognizer';
* components. * components.
*/ */
export class RouteRecognizer { export class RouteRecognizer {
names: Map<string, PathRecognizer>; names: Map<string, PathRecognizer> = new Map();
redirects: Map<string, string>; redirects: Map<string, string> = new Map();
matchers: Map<RegExp, PathRecognizer>; matchers: Map<RegExp, PathRecognizer> = new Map();
constructor() {
this.names = new Map();
this.matchers = new Map();
this.redirects = new Map();
}
addRedirect(path: string, target: string): void { addRedirect(path: string, target: string): void {
if (path == '/') { if (path == '/') {
@ -113,6 +107,7 @@ export class RouteMatch {
params: StringMap<string, string>; params: StringMap<string, string>;
matchedUrl: string; matchedUrl: string;
unmatchedUrl: string; unmatchedUrl: string;
constructor({specificity, handler, params, matchedUrl, unmatchedUrl}: { constructor({specificity, handler, params, matchedUrl, unmatchedUrl}: {
specificity?: number, specificity?: number,
handler?: StringMap<string, any>, handler?: StringMap<string, any>,

View File

@ -29,9 +29,7 @@ import {Injectable} from 'angular2/di';
*/ */
@Injectable() @Injectable()
export class RouteRegistry { export class RouteRegistry {
_rules: Map<any, RouteRecognizer>; _rules: Map<any, RouteRecognizer> = new Map();
constructor() { this._rules = new Map(); }
/** /**
* Given a component and a configuration object, add the route to this registry * Given a component and a configuration object, add the route to this registry

View File

@ -1,6 +1,6 @@
import {Promise, PromiseWrapper, EventEmitter, ObservableWrapper} from 'angular2/src/facade/async'; import {Promise, PromiseWrapper, EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
import {Map, MapWrapper, List, ListWrapper} from 'angular2/src/facade/collection'; import {Map, MapWrapper, List, ListWrapper} from 'angular2/src/facade/collection';
import {isBlank, isPresent, Type} from 'angular2/src/facade/lang'; import {isBlank, isPresent, Type, isArray} from 'angular2/src/facade/lang';
import {RouteRegistry} from './route_registry'; import {RouteRegistry} from './route_registry';
import {Pipeline} from './pipeline'; import {Pipeline} from './pipeline';
@ -28,25 +28,19 @@ import {Location} from './location';
* @exportedAs angular2/router * @exportedAs angular2/router
*/ */
export class Router { export class Router {
navigating: boolean; navigating: boolean = false;
lastNavigationAttempt: string; lastNavigationAttempt: string;
previousUrl: string; previousUrl: string = null;
private _currentInstruction: Instruction = null;
private _currentNavigation: Promise<any> = PromiseWrapper.resolve(true);
private _outlet: RouterOutlet = null;
private _subject: EventEmitter = new EventEmitter();
private _currentInstruction: Instruction;
private _currentNavigation: Promise<any>;
private _outlet: RouterOutlet;
private _subject: EventEmitter;
// todo(jeffbcross): rename _registry to registry since it is accessed from subclasses // todo(jeffbcross): rename _registry to registry since it is accessed from subclasses
// todo(jeffbcross): rename _pipeline to pipeline since it is accessed from subclasses // todo(jeffbcross): rename _pipeline to pipeline since it is accessed from subclasses
constructor(public _registry: RouteRegistry, public _pipeline: Pipeline, public parent: Router, constructor(public _registry: RouteRegistry, public _pipeline: Pipeline, public parent: Router,
public hostComponent: any) { public hostComponent: any) {}
this.navigating = false;
this.previousUrl = null;
this._outlet = null;
this._subject = new EventEmitter();
this._currentInstruction = null;
this._currentNavigation = PromiseWrapper.resolve(true);
}
/** /**
@ -88,8 +82,8 @@ export class Router {
* ]); * ]);
* ``` * ```
*/ */
config(config: any): Promise<any> { config(config: StringMap<string, any>| List<StringMap<string, any>>): Promise<any> {
if (config instanceof List) { if (isArray(config)) {
(<List<any>>config) (<List<any>>config)
.forEach((configObject) => { this._registry.config(this.hostComponent, configObject); }); .forEach((configObject) => { this._registry.config(this.hostComponent, configObject); });
} else { } else {

View File

@ -38,7 +38,7 @@ import {Renderer} from 'angular2/src/render/api';
}) })
export class RouterLink { export class RouterLink {
private _route: string; private _route: string;
private _params: StringMap<string, string>; private _params: StringMap<string, string> = StringMapWrapper.create();
// the url displayed on the anchor element. // the url displayed on the anchor element.
_visibleHref: string; _visibleHref: string;
@ -46,15 +46,13 @@ export class RouterLink {
_navigationHref: string; _navigationHref: string;
constructor(private _elementRef: ElementRef, private _router: Router, private _location: Location, constructor(private _elementRef: ElementRef, private _router: Router, private _location: Location,
private _renderer: Renderer) { private _renderer: Renderer) {}
this._params = StringMapWrapper.create();
}
set route(changes: string) { this._route = changes; } set route(changes: string) { this._route = changes; }
set params(changes: StringMap<string, string>) { this._params = changes; } set params(changes: StringMap<string, string>) { this._params = changes; }
onClick() { onClick(): boolean {
this._router.navigate(this._navigationHref); this._router.navigate(this._navigationHref);
return false; return false;
} }

View File

@ -22,10 +22,10 @@ import {Instruction, RouteParams} from './instruction'
selector: 'router-outlet' selector: 'router-outlet'
}) })
export class RouterOutlet { export class RouterOutlet {
private _childRouter: routerMod.Router; private _childRouter: routerMod.Router = null;
private _componentRef: ComponentRef; private _componentRef: ComponentRef = null;
private _elementRef: ElementRef; private _elementRef: ElementRef;
private _currentInstruction: Instruction; private _currentInstruction: Instruction = null;
private _injector: Injector; private _injector: Injector;
constructor(elementRef: ElementRef, private _loader: DynamicComponentLoader, constructor(elementRef: ElementRef, private _loader: DynamicComponentLoader,
@ -38,10 +38,6 @@ export class RouterOutlet {
this._injector = _injector.getAppInjector(); this._injector = _injector.getAppInjector();
this._elementRef = elementRef; this._elementRef = elementRef;
this._childRouter = null;
this._componentRef = null;
this._currentInstruction = null;
this._parentRouter.registerOutlet(this); this._parentRouter.registerOutlet(this);
} }