From 1f04f70eda318c95c18c15762a604a25e45361e2 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Mon, 29 Jun 2015 10:37:55 +0200 Subject: [PATCH] refactor(Router): idiomatic TS --- modules/angular2/src/router/location.ts | 3 +- .../angular2/src/router/path_recognizer.ts | 16 ++++------- .../angular2/src/router/route_recognizer.ts | 13 +++------ modules/angular2/src/router/route_registry.ts | 4 +-- modules/angular2/src/router/router.ts | 28 ++++++++----------- modules/angular2/src/router/router_link.ts | 8 ++---- modules/angular2/src/router/router_outlet.ts | 10 ++----- 7 files changed, 29 insertions(+), 53 deletions(-) diff --git a/modules/angular2/src/router/location.ts b/modules/angular2/src/router/location.ts index a058355866..663881ab7a 100644 --- a/modules/angular2/src/router/location.ts +++ b/modules/angular2/src/router/location.ts @@ -17,12 +17,11 @@ export const appBaseHrefToken: OpaqueToken = CONST_EXPR(new OpaqueToken('locatio */ @Injectable() export class Location { - private _subject: EventEmitter; + private _subject: EventEmitter = new EventEmitter(); private _baseHref: string; constructor(public _platformStrategy: LocationStrategy, @Optional() @Inject(appBaseHrefToken) href?: string) { - this._subject = new EventEmitter(); this._baseHref = stripTrailingSlash( stripIndexHtml(isPresent(href) ? href : this._platformStrategy.getBaseHref())); this._platformStrategy.onPopState((_) => this._onPopState(_)); diff --git a/modules/angular2/src/router/path_recognizer.ts b/modules/angular2/src/router/path_recognizer.ts index 54d441ae58..c8a20ec132 100644 --- a/modules/angular2/src/router/path_recognizer.ts +++ b/modules/angular2/src/router/path_recognizer.ts @@ -33,11 +33,10 @@ export class ContinuationSegment extends Segment { class StaticSegment extends Segment { regex: string; - name: string; + name: string = ''; constructor(public string: string) { super(); - this.name = ''; this.regex = escapeRegex(string); } @@ -46,8 +45,9 @@ class StaticSegment extends Segment { @IMPLEMENTS(Segment) class DynamicSegment { - regex: string; - constructor(public name: string) { this.regex = "([^/]+)"; } + regex: string = "([^/]+)"; + + constructor(public name: string) {} generate(params: StringMap): string { if (!StringMapWrapper.contains(params, this.name)) { @@ -60,8 +60,8 @@ class DynamicSegment { class StarSegment { - regex: string; - constructor(public name: string) { this.regex = "(.+)"; } + regex: string = "(.+)"; + constructor(public name: string) {} generate(params: StringMap): string { return normalizeBlank(StringMapWrapper.get(params, this.name)); @@ -134,10 +134,6 @@ export class PathRecognizer { terminal: boolean = true; 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 specificity = parsed['specificity']; var segments = parsed['segments']; diff --git a/modules/angular2/src/router/route_recognizer.ts b/modules/angular2/src/router/route_recognizer.ts index 7a16f4362e..79499b13b0 100644 --- a/modules/angular2/src/router/route_recognizer.ts +++ b/modules/angular2/src/router/route_recognizer.ts @@ -22,15 +22,9 @@ import {PathRecognizer, ContinuationSegment} from './path_recognizer'; * components. */ export class RouteRecognizer { - names: Map; - redirects: Map; - matchers: Map; - - constructor() { - this.names = new Map(); - this.matchers = new Map(); - this.redirects = new Map(); - } + names: Map = new Map(); + redirects: Map = new Map(); + matchers: Map = new Map(); addRedirect(path: string, target: string): void { if (path == '/') { @@ -113,6 +107,7 @@ export class RouteMatch { params: StringMap; matchedUrl: string; unmatchedUrl: string; + constructor({specificity, handler, params, matchedUrl, unmatchedUrl}: { specificity?: number, handler?: StringMap, diff --git a/modules/angular2/src/router/route_registry.ts b/modules/angular2/src/router/route_registry.ts index ae54d77d4a..2205815f56 100644 --- a/modules/angular2/src/router/route_registry.ts +++ b/modules/angular2/src/router/route_registry.ts @@ -29,9 +29,7 @@ import {Injectable} from 'angular2/di'; */ @Injectable() export class RouteRegistry { - _rules: Map; - - constructor() { this._rules = new Map(); } + _rules: Map = new Map(); /** * Given a component and a configuration object, add the route to this registry diff --git a/modules/angular2/src/router/router.ts b/modules/angular2/src/router/router.ts index 9cf204ac42..830d1e5c7f 100644 --- a/modules/angular2/src/router/router.ts +++ b/modules/angular2/src/router/router.ts @@ -1,6 +1,6 @@ import {Promise, PromiseWrapper, EventEmitter, ObservableWrapper} from 'angular2/src/facade/async'; 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 {Pipeline} from './pipeline'; @@ -28,25 +28,19 @@ import {Location} from './location'; * @exportedAs angular2/router */ export class Router { - navigating: boolean; + navigating: boolean = false; lastNavigationAttempt: string; - previousUrl: string; + previousUrl: string = null; + + private _currentInstruction: Instruction = null; + private _currentNavigation: Promise = PromiseWrapper.resolve(true); + private _outlet: RouterOutlet = null; + private _subject: EventEmitter = new EventEmitter(); - private _currentInstruction: Instruction; - private _currentNavigation: Promise; - private _outlet: RouterOutlet; - private _subject: EventEmitter; // todo(jeffbcross): rename _registry to registry 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, - public hostComponent: any) { - this.navigating = false; - this.previousUrl = null; - this._outlet = null; - this._subject = new EventEmitter(); - this._currentInstruction = null; - this._currentNavigation = PromiseWrapper.resolve(true); - } + public hostComponent: any) {} /** @@ -88,8 +82,8 @@ export class Router { * ]); * ``` */ - config(config: any): Promise { - if (config instanceof List) { + config(config: StringMap| List>): Promise { + if (isArray(config)) { (>config) .forEach((configObject) => { this._registry.config(this.hostComponent, configObject); }); } else { diff --git a/modules/angular2/src/router/router_link.ts b/modules/angular2/src/router/router_link.ts index 7ddea98ee1..edd00d0b89 100644 --- a/modules/angular2/src/router/router_link.ts +++ b/modules/angular2/src/router/router_link.ts @@ -38,7 +38,7 @@ import {Renderer} from 'angular2/src/render/api'; }) export class RouterLink { private _route: string; - private _params: StringMap; + private _params: StringMap = StringMapWrapper.create(); // the url displayed on the anchor element. _visibleHref: string; @@ -46,15 +46,13 @@ export class RouterLink { _navigationHref: string; constructor(private _elementRef: ElementRef, private _router: Router, private _location: Location, - private _renderer: Renderer) { - this._params = StringMapWrapper.create(); - } + private _renderer: Renderer) {} set route(changes: string) { this._route = changes; } set params(changes: StringMap) { this._params = changes; } - onClick() { + onClick(): boolean { this._router.navigate(this._navigationHref); return false; } diff --git a/modules/angular2/src/router/router_outlet.ts b/modules/angular2/src/router/router_outlet.ts index cf9c5b0d76..21323e95ea 100644 --- a/modules/angular2/src/router/router_outlet.ts +++ b/modules/angular2/src/router/router_outlet.ts @@ -22,10 +22,10 @@ import {Instruction, RouteParams} from './instruction' selector: 'router-outlet' }) export class RouterOutlet { - private _childRouter: routerMod.Router; - private _componentRef: ComponentRef; + private _childRouter: routerMod.Router = null; + private _componentRef: ComponentRef = null; private _elementRef: ElementRef; - private _currentInstruction: Instruction; + private _currentInstruction: Instruction = null; private _injector: Injector; constructor(elementRef: ElementRef, private _loader: DynamicComponentLoader, @@ -38,10 +38,6 @@ export class RouterOutlet { this._injector = _injector.getAppInjector(); this._elementRef = elementRef; - - this._childRouter = null; - this._componentRef = null; - this._currentInstruction = null; this._parentRouter.registerOutlet(this); }