chore: router move-only

This commit is contained in:
Miško Hevery
2016-05-02 17:11:21 +00:00
committed by Igor Minar
parent 072446aed3
commit d930ad1816
116 changed files with 1345 additions and 1374 deletions

View File

@ -0,0 +1,83 @@
import {Directive} from '@angular/core';
import {Location} from '@angular/common';
import {isString} from '../../src/facade/lang';
import {Router} from '../router';
import {Instruction} from '../instruction';
/**
* The RouterLink directive lets you link to specific parts of your app.
*
* Consider the following route configuration:
* ```
* @RouteConfig([
* { path: '/user', component: UserCmp, as: 'User' }
* ]);
* class MyComp {}
* ```
*
* When linking to this `User` route, you can write:
*
* ```
* <a [routerLink]="['./User']">link to user component</a>
* ```
*
* RouterLink expects the value to be an array of route names, followed by the params
* for that level of routing. For instance `['/Team', {teamId: 1}, 'User', {userId: 2}]`
* means that we want to generate a link for the `Team` route with params `{teamId: 1}`,
* and with a child route `User` with params `{userId: 2}`.
*
* The first route name should be prepended with `/`, `./`, or `../`.
* If the route begins with `/`, the router will look up the route from the root of the app.
* If the route begins with `./`, the router will instead look in the current component's
* children for the route. And if the route begins with `../`, the router will look at the
* current component's parent.
*/
@Directive({
selector: '[routerLink]',
inputs: ['routeParams: routerLink', 'target: target'],
host: {
'(click)': 'onClick()',
'[attr.href]': 'visibleHref',
'[class.router-link-active]': 'isRouteActive'
}
})
export class RouterLink {
private _routeParams: any[];
// the url displayed on the anchor element.
visibleHref: string;
target: string;
// the instruction passed to the router to navigate
private _navigationInstruction: Instruction;
constructor(private _router: Router, private _location: Location) {
// we need to update the link whenever a route changes to account for aux routes
this._router.subscribe((_) => this._updateLink());
}
// because auxiliary links take existing primary and auxiliary routes into account,
// we need to update the link whenever params or other routes change.
private _updateLink(): void {
this._navigationInstruction = this._router.generate(this._routeParams);
var navigationHref = this._navigationInstruction.toLinkUrl();
this.visibleHref = this._location.prepareExternalUrl(navigationHref);
}
get isRouteActive(): boolean { return this._router.isRouteActive(this._navigationInstruction); }
set routeParams(changes: any[]) {
this._routeParams = changes;
this._updateLink();
}
onClick(): boolean {
// If no target, or if target is _self, prevent default browser behavior
if (!isString(this.target) || this.target == '_self') {
this._router.navigateByInstruction(this._navigationInstruction);
return false;
}
return true;
}
}

View File

@ -0,0 +1,176 @@
import {PromiseWrapper, EventEmitter} from '../../src/facade/async';
import {StringMapWrapper} from '../../src/facade/collection';
import {isBlank, isPresent} from '../../src/facade/lang';
import {
Directive,
Attribute,
DynamicComponentLoader,
ComponentRef,
ViewContainerRef,
provide,
ReflectiveInjector,
OnDestroy,
Output
} from '@angular/core';
import * as routerMod from '../router';
import {ComponentInstruction, RouteParams, RouteData} from '../instruction';
import * as hookMod from '../lifecycle/lifecycle_annotations';
import {hasLifecycleHook} from '../lifecycle/route_lifecycle_reflector';
import {OnActivate, CanReuse, OnReuse, OnDeactivate, CanDeactivate} from '../interfaces';
let _resolveToTrue = PromiseWrapper.resolve(true);
/**
* A router outlet is a placeholder that Angular dynamically fills based on the application's route.
*
* ## Use
*
* ```
* <router-outlet></router-outlet>
* ```
*/
@Directive({selector: 'router-outlet'})
export class RouterOutlet implements OnDestroy {
name: string = null;
private _componentRef: Promise<ComponentRef<any>> = null;
private _currentInstruction: ComponentInstruction = null;
@Output('activate') public activateEvents = new EventEmitter<any>();
constructor(private _viewContainerRef: ViewContainerRef, private _loader: DynamicComponentLoader,
private _parentRouter: routerMod.Router, @Attribute('name') nameAttr: string) {
if (isPresent(nameAttr)) {
this.name = nameAttr;
this._parentRouter.registerAuxOutlet(this);
} else {
this._parentRouter.registerPrimaryOutlet(this);
}
}
/**
* Called by the Router to instantiate a new component during the commit phase of a navigation.
* This method in turn is responsible for calling the `routerOnActivate` hook of its child.
*/
activate(nextInstruction: ComponentInstruction): Promise<any> {
var previousInstruction = this._currentInstruction;
this._currentInstruction = nextInstruction;
var componentType = nextInstruction.componentType;
var childRouter = this._parentRouter.childRouter(componentType);
var providers = ReflectiveInjector.resolve([
provide(RouteData, {useValue: nextInstruction.routeData}),
provide(RouteParams, {useValue: new RouteParams(nextInstruction.params)}),
provide(routerMod.Router, {useValue: childRouter})
]);
this._componentRef =
this._loader.loadNextToLocation(componentType, this._viewContainerRef, providers);
return this._componentRef.then((componentRef) => {
this.activateEvents.emit(componentRef.instance);
if (hasLifecycleHook(hookMod.routerOnActivate, componentType)) {
return this._componentRef.then(
(ref: ComponentRef<any>) =>
(<OnActivate>ref.instance).routerOnActivate(nextInstruction, previousInstruction));
} else {
return componentRef;
}
});
}
/**
* Called by the {@link Router} during the commit phase of a navigation when an outlet
* reuses a component between different routes.
* This method in turn is responsible for calling the `routerOnReuse` hook of its child.
*/
reuse(nextInstruction: ComponentInstruction): Promise<any> {
var previousInstruction = this._currentInstruction;
this._currentInstruction = nextInstruction;
// it's possible the component is removed before it can be reactivated (if nested withing
// another dynamically loaded component, for instance). In that case, we simply activate
// a new one.
if (isBlank(this._componentRef)) {
return this.activate(nextInstruction);
} else {
return PromiseWrapper.resolve(
hasLifecycleHook(hookMod.routerOnReuse, this._currentInstruction.componentType) ?
this._componentRef.then(
(ref: ComponentRef<any>) =>
(<OnReuse>ref.instance).routerOnReuse(nextInstruction, previousInstruction)) :
true);
}
}
/**
* Called by the {@link Router} when an outlet disposes of a component's contents.
* This method in turn is responsible for calling the `routerOnDeactivate` hook of its child.
*/
deactivate(nextInstruction: ComponentInstruction): Promise<any> {
var next = _resolveToTrue;
if (isPresent(this._componentRef) && isPresent(this._currentInstruction) &&
hasLifecycleHook(hookMod.routerOnDeactivate, this._currentInstruction.componentType)) {
next = this._componentRef.then(
(ref: ComponentRef<any>) =>
(<OnDeactivate>ref.instance)
.routerOnDeactivate(nextInstruction, this._currentInstruction));
}
return next.then((_) => {
if (isPresent(this._componentRef)) {
var onDispose = this._componentRef.then((ref: ComponentRef<any>) => ref.destroy());
this._componentRef = null;
return onDispose;
}
});
}
/**
* Called by the {@link Router} during recognition phase of a navigation.
*
* If this resolves to `false`, the given navigation is cancelled.
*
* This method delegates to the child component's `routerCanDeactivate` hook if it exists,
* and otherwise resolves to true.
*/
routerCanDeactivate(nextInstruction: ComponentInstruction): Promise<boolean> {
if (isBlank(this._currentInstruction)) {
return _resolveToTrue;
}
if (hasLifecycleHook(hookMod.routerCanDeactivate, this._currentInstruction.componentType)) {
return this._componentRef.then(
(ref: ComponentRef<any>) =>
(<CanDeactivate>ref.instance)
.routerCanDeactivate(nextInstruction, this._currentInstruction));
} else {
return _resolveToTrue;
}
}
/**
* Called by the {@link Router} during recognition phase of a navigation.
*
* If the new child component has a different Type than the existing child component,
* this will resolve to `false`. You can't reuse an old component when the new component
* is of a different Type.
*
* Otherwise, this method delegates to the child component's `routerCanReuse` hook if it exists,
* or resolves to true if the hook is not present.
*/
routerCanReuse(nextInstruction: ComponentInstruction): Promise<boolean> {
var result;
if (isBlank(this._currentInstruction) ||
this._currentInstruction.componentType != nextInstruction.componentType) {
result = false;
} else if (hasLifecycleHook(hookMod.routerCanReuse, this._currentInstruction.componentType)) {
result = this._componentRef.then(
(ref: ComponentRef<any>) =>
(<CanReuse>ref.instance).routerCanReuse(nextInstruction, this._currentInstruction));
} else {
result = nextInstruction == this._currentInstruction ||
(isPresent(nextInstruction.params) && isPresent(this._currentInstruction.params) &&
StringMapWrapper.equals(nextInstruction.params, this._currentInstruction.params));
}
return <Promise<boolean>>PromiseWrapper.resolve(result);
}
ngOnDestroy(): void { this._parentRouter.unregisterPrimaryOutlet(this); }
}

View File

@ -0,0 +1 @@
../../facade/src

View File

@ -0,0 +1,316 @@
import {StringMapWrapper} from '../src/facade/collection';
import {isPresent, isBlank, normalizeBlank} from '../src/facade/lang';
import {PromiseWrapper} from '../src/facade/async';
/**
* `RouteParams` is an immutable map of parameters for the given route
* based on the url matcher and optional parameters for that route.
*
* You can inject `RouteParams` into the constructor of a component to use it.
*
* ### Example
*
* ```
* import {Component} from '@angular/core';
* import {bootstrap} from '@angular/platform-browser/browser';
* import {Router, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig, RouteParams} from
* 'angular2/router';
*
* @Component({directives: [ROUTER_DIRECTIVES]})
* @RouteConfig([
* {path: '/user/:id', component: UserCmp, name: 'UserCmp'},
* ])
* class AppCmp {}
*
* @Component({ template: 'user: {{id}}' })
* class UserCmp {
* id: string;
* constructor(params: RouteParams) {
* this.id = params.get('id');
* }
* }
*
* bootstrap(AppCmp, ROUTER_PROVIDERS);
* ```
*/
export class RouteParams {
constructor(public params: {[key: string]: string}) {}
get(param: string): string { return normalizeBlank(StringMapWrapper.get(this.params, param)); }
}
/**
* `RouteData` is an immutable map of additional data you can configure in your {@link Route}.
*
* You can inject `RouteData` into the constructor of a component to use it.
*
* ### Example
*
* ```
* import {Component} from '@angular/core';
* import {bootstrap} from '@angular/platform-browser/browser';
* import {Router, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig, RouteData} from
* 'angular2/router';
*
* @Component({directives: [ROUTER_DIRECTIVES]})
* @RouteConfig([
* {path: '/user/:id', component: UserCmp, name: 'UserCmp', data: {isAdmin: true}},
* ])
* class AppCmp {}
*
* @Component({
* ...,
* template: 'user: {{isAdmin}}'
* })
* class UserCmp {
* string: isAdmin;
* constructor(data: RouteData) {
* this.isAdmin = data.get('isAdmin');
* }
* }
*
* bootstrap(AppCmp, ROUTER_PROVIDERS);
* ```
*/
export class RouteData {
constructor(public data: {[key: string]: any} = /*@ts2dart_const*/ {}) {}
get(key: string): any { return normalizeBlank(StringMapWrapper.get(this.data, key)); }
}
export var BLANK_ROUTE_DATA = new RouteData();
/**
* `Instruction` is a tree of {@link ComponentInstruction}s with all the information needed
* to transition each component in the app to a given route, including all auxiliary routes.
*
* `Instruction`s can be created using {@link Router#generate}, and can be used to
* perform route changes with {@link Router#navigateByInstruction}.
*
* ### Example
*
* ```
* import {Component} from '@angular/core';
* import {bootstrap} from '@angular/platform-browser/browser';
* import {Router, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig} from '@angular/router';
*
* @Component({directives: [ROUTER_DIRECTIVES]})
* @RouteConfig([
* {...},
* ])
* class AppCmp {
* constructor(router: Router) {
* var instruction = router.generate(['/MyRoute']);
* router.navigateByInstruction(instruction);
* }
* }
*
* bootstrap(AppCmp, ROUTER_PROVIDERS);
* ```
*/
export abstract class Instruction {
constructor(public component: ComponentInstruction, public child: Instruction,
public auxInstruction: {[key: string]: Instruction}) {}
get urlPath(): string { return isPresent(this.component) ? this.component.urlPath : ''; }
get urlParams(): string[] { return isPresent(this.component) ? this.component.urlParams : []; }
get specificity(): string {
var total = '';
if (isPresent(this.component)) {
total += this.component.specificity;
}
if (isPresent(this.child)) {
total += this.child.specificity;
}
return total;
}
abstract resolveComponent(): Promise<ComponentInstruction>;
/**
* converts the instruction into a URL string
*/
toRootUrl(): string { return this.toUrlPath() + this.toUrlQuery(); }
/** @internal */
_toNonRootUrl(): string {
return this._stringifyPathMatrixAuxPrefixed() +
(isPresent(this.child) ? this.child._toNonRootUrl() : '');
}
toUrlQuery(): string { return this.urlParams.length > 0 ? ('?' + this.urlParams.join('&')) : ''; }
/**
* Returns a new instruction that shares the state of the existing instruction, but with
* the given child {@link Instruction} replacing the existing child.
*/
replaceChild(child: Instruction): Instruction {
return new ResolvedInstruction(this.component, child, this.auxInstruction);
}
/**
* If the final URL for the instruction is ``
*/
toUrlPath(): string {
return this.urlPath + this._stringifyAux() +
(isPresent(this.child) ? this.child._toNonRootUrl() : '');
}
// default instructions override these
toLinkUrl(): string {
return this.urlPath + this._stringifyAux() +
(isPresent(this.child) ? this.child._toLinkUrl() : '') + this.toUrlQuery();
}
// this is the non-root version (called recursively)
/** @internal */
_toLinkUrl(): string {
return this._stringifyPathMatrixAuxPrefixed() +
(isPresent(this.child) ? this.child._toLinkUrl() : '');
}
/** @internal */
_stringifyPathMatrixAuxPrefixed(): string {
var primary = this._stringifyPathMatrixAux();
if (primary.length > 0) {
primary = '/' + primary;
}
return primary;
}
/** @internal */
_stringifyMatrixParams(): string {
return this.urlParams.length > 0 ? (';' + this.urlParams.join(';')) : '';
}
/** @internal */
_stringifyPathMatrixAux(): string {
if (isBlank(this.component)) {
return '';
}
return this.urlPath + this._stringifyMatrixParams() + this._stringifyAux();
}
/** @internal */
_stringifyAux(): string {
var routes = [];
StringMapWrapper.forEach(this.auxInstruction, (auxInstruction: Instruction, _: string) => {
routes.push(auxInstruction._stringifyPathMatrixAux());
});
if (routes.length > 0) {
return '(' + routes.join('//') + ')';
}
return '';
}
}
/**
* a resolved instruction has an outlet instruction for itself, but maybe not for...
*/
export class ResolvedInstruction extends Instruction {
constructor(component: ComponentInstruction, child: Instruction,
auxInstruction: {[key: string]: Instruction}) {
super(component, child, auxInstruction);
}
resolveComponent(): Promise<ComponentInstruction> {
return PromiseWrapper.resolve(this.component);
}
}
/**
* Represents a resolved default route
*/
export class DefaultInstruction extends ResolvedInstruction {
constructor(component: ComponentInstruction, child: DefaultInstruction) {
super(component, child, {});
}
toLinkUrl(): string { return ''; }
/** @internal */
_toLinkUrl(): string { return ''; }
}
/**
* Represents a component that may need to do some redirection or lazy loading at a later time.
*/
export class UnresolvedInstruction extends Instruction {
constructor(private _resolver: () => Promise<Instruction>, private _urlPath: string = '',
private _urlParams: string[] = /*@ts2dart_const*/[]) {
super(null, null, {});
}
get urlPath(): string {
if (isPresent(this.component)) {
return this.component.urlPath;
}
if (isPresent(this._urlPath)) {
return this._urlPath;
}
return '';
}
get urlParams(): string[] {
if (isPresent(this.component)) {
return this.component.urlParams;
}
if (isPresent(this._urlParams)) {
return this._urlParams;
}
return [];
}
resolveComponent(): Promise<ComponentInstruction> {
if (isPresent(this.component)) {
return PromiseWrapper.resolve(this.component);
}
return this._resolver().then((instruction: Instruction) => {
this.child = isPresent(instruction) ? instruction.child : null;
return this.component = isPresent(instruction) ? instruction.component : null;
});
}
}
export class RedirectInstruction extends ResolvedInstruction {
constructor(component: ComponentInstruction, child: Instruction,
auxInstruction: {[key: string]: Instruction}, private _specificity: string) {
super(component, child, auxInstruction);
}
get specificity(): string { return this._specificity; }
}
/**
* A `ComponentInstruction` represents the route state for a single component.
*
* `ComponentInstructions` is a public API. Instances of `ComponentInstruction` are passed
* to route lifecycle hooks, like {@link CanActivate}.
*
* `ComponentInstruction`s are [hash consed](https://en.wikipedia.org/wiki/Hash_consing). You should
* never construct one yourself with "new." Instead, rely on router's internal recognizer to
* construct `ComponentInstruction`s.
*
* You should not modify this object. It should be treated as immutable.
*/
export class ComponentInstruction {
reuse: boolean = false;
public routeData: RouteData;
/**
* @internal
*/
constructor(public urlPath: string, public urlParams: string[], data: RouteData,
public componentType, public terminal: boolean, public specificity: string,
public params: {[key: string]: string} = null, public routeName: string) {
this.routeData = isPresent(data) ? data : BLANK_ROUTE_DATA;
}
}

View File

@ -0,0 +1,124 @@
import {ComponentInstruction} from './instruction';
import {global} from '../src/facade/lang';
// This is here only so that after TS transpilation the file is not empty.
// TODO(rado): find a better way to fix this, or remove if likely culprit
// https://github.com/systemjs/systemjs/issues/487 gets closed.
var __ignore_me = global;
var __make_dart_analyzer_happy: Promise<any> = null;
/**
* Defines route lifecycle method `routerOnActivate`, which is called by the router at the end of a
* successful route navigation.
*
* For a single component's navigation, only one of either {@link OnActivate} or {@link OnReuse}
* will be called depending on the result of {@link CanReuse}.
*
* The `routerOnActivate` hook is called with two {@link ComponentInstruction}s as parameters, the
* first
* representing the current route being navigated to, and the second parameter representing the
* previous route or `null`.
*
* If `routerOnActivate` returns a promise, the route change will wait until the promise settles to
* instantiate and activate child components.
*
* ### Example
* {@example router/ts/on_activate/on_activate_example.ts region='routerOnActivate'}
*/
export interface OnActivate {
routerOnActivate(nextInstruction: ComponentInstruction,
prevInstruction: ComponentInstruction): any |
Promise<any>;
}
/**
* Defines route lifecycle method `routerOnReuse`, which is called by the router at the end of a
* successful route navigation when {@link CanReuse} is implemented and returns or resolves to true.
*
* For a single component's navigation, only one of either {@link OnActivate} or {@link OnReuse}
* will be called, depending on the result of {@link CanReuse}.
*
* The `routerOnReuse` hook is called with two {@link ComponentInstruction}s as parameters, the
* first
* representing the current route being navigated to, and the second parameter representing the
* previous route or `null`.
*
* ### Example
* {@example router/ts/reuse/reuse_example.ts region='reuseCmp'}
*/
export interface OnReuse {
routerOnReuse(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction): any |
Promise<any>;
}
/**
* Defines route lifecycle method `routerOnDeactivate`, which is called by the router before
* destroying
* a component as part of a route change.
*
* The `routerOnDeactivate` hook is called with two {@link ComponentInstruction}s as parameters, the
* first
* representing the current route being navigated to, and the second parameter representing the
* previous route.
*
* If `routerOnDeactivate` returns a promise, the route change will wait until the promise settles.
*
* ### Example
* {@example router/ts/on_deactivate/on_deactivate_example.ts region='routerOnDeactivate'}
*/
export interface OnDeactivate {
routerOnDeactivate(nextInstruction: ComponentInstruction,
prevInstruction: ComponentInstruction): any |
Promise<any>;
}
/**
* Defines route lifecycle method `routerCanReuse`, which is called by the router to determine
* whether a
* component should be reused across routes, or whether to destroy and instantiate a new component.
*
* The `routerCanReuse` hook is called with two {@link ComponentInstruction}s as parameters, the
* first
* representing the current route being navigated to, and the second parameter representing the
* previous route.
*
* If `routerCanReuse` returns or resolves to `true`, the component instance will be reused and the
* {@link OnDeactivate} hook will be run. If `routerCanReuse` returns or resolves to `false`, a new
* component will be instantiated, and the existing component will be deactivated and removed as
* part of the navigation.
*
* If `routerCanReuse` throws or rejects, the navigation will be cancelled.
*
* ### Example
* {@example router/ts/reuse/reuse_example.ts region='reuseCmp'}
*/
export interface CanReuse {
routerCanReuse(nextInstruction: ComponentInstruction,
prevInstruction: ComponentInstruction): boolean |
Promise<boolean>;
}
/**
* Defines route lifecycle method `routerCanDeactivate`, which is called by the router to determine
* if a component can be removed as part of a navigation.
*
* The `routerCanDeactivate` hook is called with two {@link ComponentInstruction}s as parameters,
* the
* first representing the current route being navigated to, and the second parameter
* representing the previous route.
*
* If `routerCanDeactivate` returns or resolves to `false`, the navigation is cancelled. If it
* returns or
* resolves to `true`, then the navigation continues, and the component will be deactivated
* (the {@link OnDeactivate} hook will be run) and removed.
*
* If `routerCanDeactivate` throws or rejects, the navigation is also cancelled.
*
* ### Example
* {@example router/ts/can_deactivate/can_deactivate_example.ts region='routerCanDeactivate'}
*/
export interface CanDeactivate {
routerCanDeactivate(nextInstruction: ComponentInstruction,
prevInstruction: ComponentInstruction): boolean |
Promise<boolean>;
}

View File

@ -0,0 +1,8 @@
/**
* This indirection is needed for TS compilation path.
* See comment in lifecycle_annotations.ts.
*/
library angular2.router.lifecycle_annotations;
export "./lifecycle_annotations_impl.dart";

View File

@ -0,0 +1,46 @@
/**
* This indirection is needed to free up Component, etc symbols in the public API
* to be used by the decorator versions of these annotations.
*/
import {makeDecorator} from '../../core_private';
import {CanActivate as CanActivateAnnotation} from './lifecycle_annotations_impl';
import {ComponentInstruction} from '../instruction';
export {
routerCanReuse,
routerCanDeactivate,
routerOnActivate,
routerOnReuse,
routerOnDeactivate
} from './lifecycle_annotations_impl';
/**
* Defines route lifecycle hook `CanActivate`, which is called by the router to determine
* if a component can be instantiated as part of a navigation.
*
* <aside class="is-right">
* Note that unlike other lifecycle hooks, this one uses an annotation rather than an interface.
* This is because the `CanActivate` function is called before the component is instantiated.
* </aside>
*
* The `CanActivate` hook is called with two {@link ComponentInstruction}s as parameters, the first
* representing the current route being navigated to, and the second parameter representing the
* previous route or `null`.
*
* ```typescript
* @CanActivate((next, prev) => boolean | Promise<boolean>)
* ```
*
* If `CanActivate` returns or resolves to `false`, the navigation is cancelled.
* If `CanActivate` throws or rejects, the navigation is also cancelled.
* If `CanActivate` returns or resolves to `true`, navigation continues, the component is
* instantiated, and the {@link OnActivate} hook of that component is called if implemented.
*
* ### Example
*
* {@example router/ts/can_activate/can_activate_example.ts region='canActivate' }
*/
export var CanActivate: (hook: (next: ComponentInstruction, prev: ComponentInstruction) =>
Promise<boolean>| boolean) => ClassDecorator =
makeDecorator(CanActivateAnnotation);

View File

@ -0,0 +1,21 @@
/* @ts2dart_const */
export class RouteLifecycleHook {
constructor(public name: string) {}
}
/* @ts2dart_const */
export class CanActivate {
constructor(public fn: Function) {}
}
export const routerCanReuse: RouteLifecycleHook =
/*@ts2dart_const*/ new RouteLifecycleHook("routerCanReuse");
export const routerCanDeactivate: RouteLifecycleHook =
/*@ts2dart_const*/ new RouteLifecycleHook("routerCanDeactivate");
export const routerOnActivate: RouteLifecycleHook =
/*@ts2dart_const*/ new RouteLifecycleHook("routerOnActivate");
export const routerOnReuse: RouteLifecycleHook =
/*@ts2dart_const*/ new RouteLifecycleHook("routerOnReuse");
export const routerOnDeactivate: RouteLifecycleHook =
/*@ts2dart_const*/ new RouteLifecycleHook("routerOnDeactivate");

View File

@ -0,0 +1,38 @@
library angular.router.route_lifecycle_reflector;
import 'package:angular2/src/router/lifecycle/lifecycle_annotations_impl.dart';
import 'package:angular2/src/router/interfaces.dart';
import 'package:angular2/src/core/reflection/reflection.dart';
bool hasLifecycleHook(RouteLifecycleHook e, type) {
if (type is! Type) return false;
final List interfaces = reflector.interfaces(type);
var interface;
if (e == routerOnActivate) {
interface = OnActivate;
} else if (e == routerOnDeactivate) {
interface = OnDeactivate;
} else if (e == routerOnReuse) {
interface = OnReuse;
} else if (e == routerCanDeactivate) {
interface = CanDeactivate;
} else if (e == routerCanReuse) {
interface = CanReuse;
}
return interfaces.contains(interface);
}
Function getCanActivateHook(type) {
final List annotations = reflector.annotations(type);
for (var annotation in annotations) {
if (annotation is CanActivate) {
return annotation.fn;
}
}
return null;
}

View File

@ -0,0 +1,20 @@
import {Type} from '@angular/core';
import {RouteLifecycleHook, CanActivate} from './lifecycle_annotations_impl';
import {reflector} from '@angular/core';
export function hasLifecycleHook(e: RouteLifecycleHook, type): boolean {
if (!(type instanceof Type)) return false;
return e.name in(<any>type).prototype;
}
export function getCanActivateHook(type): Function {
var annotations = reflector.annotations(type);
for (let i = 0; i < annotations.length; i += 1) {
let annotation = annotations[i];
if (annotation instanceof CanActivate) {
return annotation.fn;
}
}
return null;
}

View File

@ -0,0 +1,4 @@
{
"name": "@angular/router",
"version": "0.2.0"
}

View File

@ -0,0 +1,3 @@
library angular2.router.route_config_decorator;
export './route_config_impl.dart';

View File

@ -0,0 +1,13 @@
import {RouteConfig as RouteConfigAnnotation, RouteDefinition} from './route_config_impl';
import {makeDecorator} from '../../core_private';
export {Route, Redirect, AuxRoute, AsyncRoute, RouteDefinition} from './route_config_impl';
// Copied from RouteConfig in route_config_impl.
/**
* The `RouteConfig` decorator defines routes for a given component.
*
* It takes an array of {@link RouteDefinition}s.
*/
export var RouteConfig: (configs: RouteDefinition[]) => ClassDecorator =
makeDecorator(RouteConfigAnnotation);

View File

@ -0,0 +1,193 @@
import {Type} from '../../src/facade/lang';
import {RouteDefinition} from '../route_definition';
import {RegexSerializer} from '../rules/route_paths/regex_route_path';
export {RouteDefinition} from '../route_definition';
var __make_dart_analyzer_happy: Promise<any> = null;
/**
* The `RouteConfig` decorator defines routes for a given component.
*
* It takes an array of {@link RouteDefinition}s.
* @ts2dart_const
*/
export class RouteConfig {
constructor(public configs: RouteDefinition[]) {}
}
/* @ts2dart_const */
export abstract class AbstractRoute implements RouteDefinition {
name: string;
useAsDefault: boolean;
path: string;
regex: string;
serializer: RegexSerializer;
data: {[key: string]: any};
constructor({name, useAsDefault, path, regex, serializer, data}: RouteDefinition) {
this.name = name;
this.useAsDefault = useAsDefault;
this.path = path;
this.regex = regex;
this.serializer = serializer;
this.data = data;
}
}
/**
* `Route` is a type of {@link RouteDefinition} used to route a path to a component.
*
* It has the following properties:
* - `path` is a string that uses the route matcher DSL.
* - `component` a component type.
* - `name` is an optional `CamelCase` string representing the name of the route.
* - `data` is an optional property of any type representing arbitrary route metadata for the given
* route. It is injectable via {@link RouteData}.
* - `useAsDefault` is a boolean value. If `true`, the child route will be navigated to if no child
* route is specified during the navigation.
*
* ### Example
* ```
* import {RouteConfig, Route} from '@angular/router';
*
* @RouteConfig([
* new Route({path: '/home', component: HomeCmp, name: 'HomeCmp' })
* ])
* class MyApp {}
* ```
* @ts2dart_const
*/
export class Route extends AbstractRoute {
component: any;
aux: string = null;
constructor({name, useAsDefault, path, regex, serializer, data, component}: RouteDefinition) {
super({
name: name,
useAsDefault: useAsDefault,
path: path,
regex: regex,
serializer: serializer,
data: data
});
this.component = component;
}
}
/**
* `AuxRoute` is a type of {@link RouteDefinition} used to define an auxiliary route.
*
* It takes an object with the following properties:
* - `path` is a string that uses the route matcher DSL.
* - `component` a component type.
* - `name` is an optional `CamelCase` string representing the name of the route.
* - `data` is an optional property of any type representing arbitrary route metadata for the given
* route. It is injectable via {@link RouteData}.
*
* ### Example
* ```
* import {RouteConfig, AuxRoute} from '@angular/router';
*
* @RouteConfig([
* new AuxRoute({path: '/home', component: HomeCmp})
* ])
* class MyApp {}
* ```
* @ts2dart_const
*/
export class AuxRoute extends AbstractRoute {
component: any;
constructor({name, useAsDefault, path, regex, serializer, data, component}: RouteDefinition) {
super({
name: name,
useAsDefault: useAsDefault,
path: path,
regex: regex,
serializer: serializer,
data: data
});
this.component = component;
}
}
/**
* `AsyncRoute` is a type of {@link RouteDefinition} used to route a path to an asynchronously
* loaded component.
*
* It has the following properties:
* - `path` is a string that uses the route matcher DSL.
* - `loader` is a function that returns a promise that resolves to a component.
* - `name` is an optional `CamelCase` string representing the name of the route.
* - `data` is an optional property of any type representing arbitrary route metadata for the given
* route. It is injectable via {@link RouteData}.
* - `useAsDefault` is a boolean value. If `true`, the child route will be navigated to if no child
* route is specified during the navigation.
*
* ### Example
* ```
* import {RouteConfig, AsyncRoute} from '@angular/router';
*
* @RouteConfig([
* new AsyncRoute({path: '/home', loader: () => Promise.resolve(MyLoadedCmp), name:
* 'MyLoadedCmp'})
* ])
* class MyApp {}
* ```
* @ts2dart_const
*/
export class AsyncRoute extends AbstractRoute {
loader: () => Promise<Type>;
aux: string = null;
constructor({name, useAsDefault, path, regex, serializer, data, loader}: RouteDefinition) {
super({
name: name,
useAsDefault: useAsDefault,
path: path,
regex: regex,
serializer: serializer,
data: data
});
this.loader = loader;
}
}
/**
* `Redirect` is a type of {@link RouteDefinition} used to route a path to a canonical route.
*
* It has the following properties:
* - `path` is a string that uses the route matcher DSL.
* - `redirectTo` is an array representing the link DSL.
*
* Note that redirects **do not** affect how links are generated. For that, see the `useAsDefault`
* option.
*
* ### Example
* ```
* import {RouteConfig, Route, Redirect} from '@angular/router';
*
* @RouteConfig([
* new Redirect({path: '/', redirectTo: ['/Home'] }),
* new Route({path: '/home', component: HomeCmp, name: 'Home'})
* ])
* class MyApp {}
* ```
* @ts2dart_const
*/
export class Redirect extends AbstractRoute {
redirectTo: any[];
constructor({name, useAsDefault, path, regex, serializer, data, redirectTo}: RouteDefinition) {
super({
name: name,
useAsDefault: useAsDefault,
path: path,
regex: regex,
serializer: serializer,
data: data
});
this.redirectTo = redirectTo;
}
}

View File

@ -0,0 +1,30 @@
library angular2.src.router.route_config_normalizer;
import "route_config_decorator.dart";
import "../route_definition.dart";
import "../route_registry.dart";
import "package:angular2/src/facade/lang.dart";
import "package:angular2/src/facade/exceptions.dart" show BaseException;
RouteDefinition normalizeRouteConfig(RouteDefinition config, RouteRegistry registry) {
if (config is AsyncRoute) {
configRegistryAndReturnType(componentType) {
registry.configFromComponent(componentType);
return componentType;
}
loader() {
return config.loader().then(configRegistryAndReturnType);
}
return new AsyncRoute(path: config.path, loader: loader, name: config.name, data: config.data, useAsDefault: config.useAsDefault);
}
return config;
}
void assertComponentExists(Type component, String path) {
if (component == null) {
throw new BaseException(
'Component for route "${path}" is not defined, or is not a class.');
}
}

View File

@ -0,0 +1,109 @@
import {AsyncRoute, AuxRoute, Route, Redirect, RouteDefinition} from './route_config_decorator';
import {ComponentDefinition} from '../route_definition';
import {isType, Type} from '../../src/facade/lang';
import {BaseException} from '../../src/facade/exceptions';
import {RouteRegistry} from '../route_registry';
/**
* Given a JS Object that represents a route config, returns a corresponding Route, AsyncRoute,
* AuxRoute or Redirect object.
*
* Also wraps an AsyncRoute's loader function to add the loaded component's route config to the
* `RouteRegistry`.
*/
export function normalizeRouteConfig(config: RouteDefinition,
registry: RouteRegistry): RouteDefinition {
if (config instanceof AsyncRoute) {
var wrappedLoader = wrapLoaderToReconfigureRegistry(config.loader, registry);
return new AsyncRoute({
path: config.path,
loader: wrappedLoader,
name: config.name,
data: config.data,
useAsDefault: config.useAsDefault
});
}
if (config instanceof Route || config instanceof Redirect || config instanceof AuxRoute) {
return <RouteDefinition>config;
}
if ((+!!config.component) + (+!!config.redirectTo) + (+!!config.loader) != 1) {
throw new BaseException(
`Route config should contain exactly one "component", "loader", or "redirectTo" property.`);
}
if (config.as && config.name) {
throw new BaseException(`Route config should contain exactly one "as" or "name" property.`);
}
if (config.as) {
config.name = config.as;
}
if (config.loader) {
var wrappedLoader = wrapLoaderToReconfigureRegistry(config.loader, registry);
return new AsyncRoute({
path: config.path,
loader: wrappedLoader,
name: config.name,
data: config.data,
useAsDefault: config.useAsDefault
});
}
if (config.aux) {
return new AuxRoute({path: config.aux, component:<Type>config.component, name: config.name});
}
if (config.component) {
if (typeof config.component == 'object') {
let componentDefinitionObject = <ComponentDefinition>config.component;
if (componentDefinitionObject.type == 'constructor') {
return new Route({
path: config.path,
component:<Type>componentDefinitionObject.constructor,
name: config.name,
data: config.data,
useAsDefault: config.useAsDefault
});
} else if (componentDefinitionObject.type == 'loader') {
return new AsyncRoute({
path: config.path,
loader: componentDefinitionObject.loader,
name: config.name,
data: config.data,
useAsDefault: config.useAsDefault
});
} else {
throw new BaseException(
`Invalid component type "${componentDefinitionObject.type}". Valid types are "constructor" and "loader".`);
}
}
return new Route(<{
path: string;
component: Type;
name?: string;
data?: {[key: string]: any};
useAsDefault?: boolean;
}>config);
}
if (config.redirectTo) {
return new Redirect({path: config.path, redirectTo: config.redirectTo});
}
return config;
}
function wrapLoaderToReconfigureRegistry(loader: Function, registry: RouteRegistry): () =>
Promise<Type> {
return () => {
return loader().then((componentType) => {
registry.configFromComponent(componentType);
return componentType;
});
};
}
export function assertComponentExists(component: Type, path: string): void {
if (!isType(component)) {
throw new BaseException(`Component for route "${path}" is not defined, or is not a class.`);
}
}

View File

@ -0,0 +1,10 @@
library angular2.src.router.route_definition;
abstract class RouteDefinition {
final String path;
final String name;
final bool useAsDefault;
final String regex;
final Function serializer;
const RouteDefinition({this.path, this.name, this.useAsDefault : false, this.regex, this.serializer});
}

View File

@ -0,0 +1,39 @@
import {Type} from '../src/facade/lang';
import {RegexSerializer} from './rules/route_paths/regex_route_path';
/**
* `RouteDefinition` defines a route within a {@link RouteConfig} decorator.
*
* Supported keys:
* - `path` or `aux` (requires exactly one of these)
* - `component`, `loader`, `redirectTo` (requires exactly one of these)
* - `name` or `as` (optional) (requires exactly one of these)
* - `data` (optional)
*
* See also {@link Route}, {@link AsyncRoute}, {@link AuxRoute}, and {@link Redirect}.
*/
export interface RouteDefinition {
path?: string;
aux?: string;
regex?: string;
serializer?: RegexSerializer;
component?: Type | ComponentDefinition;
loader?: () => Promise<Type>;
redirectTo?: any[];
as?: string;
name?: string;
data?: any;
useAsDefault?: boolean;
}
/**
* Represents either a component type (`type` is `component`) or a loader function
* (`type` is `loader`).
*
* See also {@link RouteDefinition}.
*/
export interface ComponentDefinition {
type: string;
loader?: () => Promise<Type>;
component?: Type;
}

View File

@ -0,0 +1,547 @@
import {ListWrapper, Map, StringMapWrapper} from '../src/facade/collection';
import {PromiseWrapper} from '../src/facade/async';
import {
isPresent,
isArray,
isBlank,
isType,
isString,
isStringMap,
Type,
StringWrapper,
Math,
getTypeNameForDebugging,
} from '../src/facade/lang';
import {BaseException} from '../src/facade/exceptions';
import {Injectable, Inject, OpaqueToken, reflector} from '@angular/core';
import {RouteConfig, Route, AuxRoute, RouteDefinition} from './route_config/route_config_impl';
import {PathMatch, RedirectMatch, RouteMatch} from './rules/rules';
import {RuleSet} from './rules/rule_set';
import {
Instruction,
ResolvedInstruction,
RedirectInstruction,
UnresolvedInstruction,
DefaultInstruction
} from './instruction';
import {normalizeRouteConfig, assertComponentExists} from './route_config/route_config_normalizer';
import {parser, Url, convertUrlParamsToArray} from './url_parser';
import {GeneratedUrl} from './rules/route_paths/route_path';
var _resolveToNull = PromiseWrapper.resolve<Instruction>(null);
// A LinkItemArray is an array, which describes a set of routes
// The items in the array are found in groups:
// - the first item is the name of the route
// - the next items are:
// - an object containing parameters
// - or an array describing an aux route
// export type LinkRouteItem = string | Object;
// export type LinkItem = LinkRouteItem | Array<LinkRouteItem>;
// export type LinkItemArray = Array<LinkItem>;
/**
* Token used to bind the component with the top-level {@link RouteConfig}s for the
* application.
*
* ### Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm))
*
* ```
* import {Component} from '@angular/core';
* import {
* ROUTER_DIRECTIVES,
* ROUTER_PROVIDERS,
* RouteConfig
* } from '@angular/router';
*
* @Component({directives: [ROUTER_DIRECTIVES]})
* @RouteConfig([
* {...},
* ])
* class AppCmp {
* // ...
* }
*
* bootstrap(AppCmp, [ROUTER_PROVIDERS]);
* ```
*/
export const ROUTER_PRIMARY_COMPONENT: OpaqueToken =
/*@ts2dart_const*/ new OpaqueToken('RouterPrimaryComponent');
/**
* The RouteRegistry holds route configurations for each component in an Angular app.
* It is responsible for creating Instructions from URLs, and generating URLs based on route and
* parameters.
*/
@Injectable()
export class RouteRegistry {
private _rules = new Map<any, RuleSet>();
constructor(@Inject(ROUTER_PRIMARY_COMPONENT) private _rootComponent: Type) {}
/**
* Given a component and a configuration object, add the route to this registry
*/
config(parentComponent: any, config: RouteDefinition): void {
config = normalizeRouteConfig(config, this);
// this is here because Dart type guard reasons
if (config instanceof Route) {
assertComponentExists(config.component, config.path);
} else if (config instanceof AuxRoute) {
assertComponentExists(config.component, config.path);
}
var rules = this._rules.get(parentComponent);
if (isBlank(rules)) {
rules = new RuleSet();
this._rules.set(parentComponent, rules);
}
var terminal = rules.config(config);
if (config instanceof Route) {
if (terminal) {
assertTerminalComponent(config.component, config.path);
} else {
this.configFromComponent(config.component);
}
}
}
/**
* Reads the annotations of a component and configures the registry based on them
*/
configFromComponent(component: any): void {
if (!isType(component)) {
return;
}
// Don't read the annotations from a type more than once
// this prevents an infinite loop if a component routes recursively.
if (this._rules.has(component)) {
return;
}
var annotations = reflector.annotations(component);
if (isPresent(annotations)) {
for (var i = 0; i < annotations.length; i++) {
var annotation = annotations[i];
if (annotation instanceof RouteConfig) {
let routeCfgs: RouteDefinition[] = annotation.configs;
routeCfgs.forEach(config => this.config(component, config));
}
}
}
}
/**
* Given a URL and a parent component, return the most specific instruction for navigating
* the application into the state specified by the url
*/
recognize(url: string, ancestorInstructions: Instruction[]): Promise<Instruction> {
var parsedUrl = parser.parse(url);
return this._recognize(parsedUrl, []);
}
/**
* Recognizes all parent-child routes, but creates unresolved auxiliary routes
*/
private _recognize(parsedUrl: Url, ancestorInstructions: Instruction[],
_aux = false): Promise<Instruction> {
var parentInstruction = ListWrapper.last(ancestorInstructions);
var parentComponent = isPresent(parentInstruction) ? parentInstruction.component.componentType :
this._rootComponent;
var rules = this._rules.get(parentComponent);
if (isBlank(rules)) {
return _resolveToNull;
}
// Matches some beginning part of the given URL
var possibleMatches: Promise<RouteMatch>[] =
_aux ? rules.recognizeAuxiliary(parsedUrl) : rules.recognize(parsedUrl);
var matchPromises: Promise<Instruction>[] = possibleMatches.map(
(candidate: Promise<RouteMatch>) => candidate.then((candidate: RouteMatch) => {
if (candidate instanceof PathMatch) {
var auxParentInstructions: Instruction[] =
ancestorInstructions.length > 0 ? [ListWrapper.last(ancestorInstructions)] : [];
var auxInstructions =
this._auxRoutesToUnresolved(candidate.remainingAux, auxParentInstructions);
var instruction = new ResolvedInstruction(candidate.instruction, null, auxInstructions);
if (isBlank(candidate.instruction) || candidate.instruction.terminal) {
return instruction;
}
var newAncestorInstructions: Instruction[] = ancestorInstructions.concat([instruction]);
return this._recognize(candidate.remaining, newAncestorInstructions)
.then((childInstruction) => {
if (isBlank(childInstruction)) {
return null;
}
// redirect instructions are already absolute
if (childInstruction instanceof RedirectInstruction) {
return childInstruction;
}
instruction.child = childInstruction;
return instruction;
});
}
if (candidate instanceof RedirectMatch) {
var instruction =
this.generate(candidate.redirectTo, ancestorInstructions.concat([null]));
return new RedirectInstruction(instruction.component, instruction.child,
instruction.auxInstruction, candidate.specificity);
}
}));
if ((isBlank(parsedUrl) || parsedUrl.path == '') && possibleMatches.length == 0) {
return PromiseWrapper.resolve(this.generateDefault(parentComponent));
}
return PromiseWrapper.all<Instruction>(matchPromises).then(mostSpecific);
}
private _auxRoutesToUnresolved(auxRoutes: Url[],
parentInstructions: Instruction[]): {[key: string]: Instruction} {
var unresolvedAuxInstructions: {[key: string]: Instruction} = {};
auxRoutes.forEach((auxUrl: Url) => {
unresolvedAuxInstructions[auxUrl.path] = new UnresolvedInstruction(
() => { return this._recognize(auxUrl, parentInstructions, true); });
});
return unresolvedAuxInstructions;
}
/**
* Given a normalized list with component names and params like: `['user', {id: 3 }]`
* generates a url with a leading slash relative to the provided `parentComponent`.
*
* If the optional param `_aux` is `true`, then we generate starting at an auxiliary
* route boundary.
*/
generate(linkParams: any[], ancestorInstructions: Instruction[], _aux = false): Instruction {
var params = splitAndFlattenLinkParams(linkParams);
var prevInstruction;
// The first segment should be either '.' (generate from parent) or '' (generate from root).
// When we normalize above, we strip all the slashes, './' becomes '.' and '/' becomes ''.
if (ListWrapper.first(params) == '') {
params.shift();
prevInstruction = ListWrapper.first(ancestorInstructions);
ancestorInstructions = [];
} else {
prevInstruction = ancestorInstructions.length > 0 ? ancestorInstructions.pop() : null;
if (ListWrapper.first(params) == '.') {
params.shift();
} else if (ListWrapper.first(params) == '..') {
while (ListWrapper.first(params) == '..') {
if (ancestorInstructions.length <= 0) {
throw new BaseException(
`Link "${ListWrapper.toJSON(linkParams)}" has too many "../" segments.`);
}
prevInstruction = ancestorInstructions.pop();
params = ListWrapper.slice(params, 1);
}
// we're on to implicit child/sibling route
} else {
// we must only peak at the link param, and not consume it
let routeName = ListWrapper.first(params);
let parentComponentType = this._rootComponent;
let grandparentComponentType = null;
if (ancestorInstructions.length > 1) {
let parentComponentInstruction = ancestorInstructions[ancestorInstructions.length - 1];
let grandComponentInstruction = ancestorInstructions[ancestorInstructions.length - 2];
parentComponentType = parentComponentInstruction.component.componentType;
grandparentComponentType = grandComponentInstruction.component.componentType;
} else if (ancestorInstructions.length == 1) {
parentComponentType = ancestorInstructions[0].component.componentType;
grandparentComponentType = this._rootComponent;
}
// For a link with no leading `./`, `/`, or `../`, we look for a sibling and child.
// If both exist, we throw. Otherwise, we prefer whichever exists.
var childRouteExists = this.hasRoute(routeName, parentComponentType);
var parentRouteExists = isPresent(grandparentComponentType) &&
this.hasRoute(routeName, grandparentComponentType);
if (parentRouteExists && childRouteExists) {
let msg =
`Link "${ListWrapper.toJSON(linkParams)}" is ambiguous, use "./" or "../" to disambiguate.`;
throw new BaseException(msg);
}
if (parentRouteExists) {
prevInstruction = ancestorInstructions.pop();
}
}
}
if (params[params.length - 1] == '') {
params.pop();
}
if (params.length > 0 && params[0] == '') {
params.shift();
}
if (params.length < 1) {
let msg = `Link "${ListWrapper.toJSON(linkParams)}" must include a route name.`;
throw new BaseException(msg);
}
var generatedInstruction =
this._generate(params, ancestorInstructions, prevInstruction, _aux, linkParams);
// we don't clone the first (root) element
for (var i = ancestorInstructions.length - 1; i >= 0; i--) {
let ancestorInstruction = ancestorInstructions[i];
if (isBlank(ancestorInstruction)) {
break;
}
generatedInstruction = ancestorInstruction.replaceChild(generatedInstruction);
}
return generatedInstruction;
}
/*
* Internal helper that does not make any assertions about the beginning of the link DSL.
* `ancestorInstructions` are parents that will be cloned.
* `prevInstruction` is the existing instruction that would be replaced, but which might have
* aux routes that need to be cloned.
*/
private _generate(linkParams: any[], ancestorInstructions: Instruction[],
prevInstruction: Instruction, _aux = false, _originalLink: any[]): Instruction {
let parentComponentType = this._rootComponent;
let componentInstruction = null;
let auxInstructions: {[key: string]: Instruction} = {};
let parentInstruction: Instruction = ListWrapper.last(ancestorInstructions);
if (isPresent(parentInstruction) && isPresent(parentInstruction.component)) {
parentComponentType = parentInstruction.component.componentType;
}
if (linkParams.length == 0) {
let defaultInstruction = this.generateDefault(parentComponentType);
if (isBlank(defaultInstruction)) {
throw new BaseException(
`Link "${ListWrapper.toJSON(_originalLink)}" does not resolve to a terminal instruction.`);
}
return defaultInstruction;
}
// for non-aux routes, we want to reuse the predecessor's existing primary and aux routes
// and only override routes for which the given link DSL provides
if (isPresent(prevInstruction) && !_aux) {
auxInstructions = StringMapWrapper.merge(prevInstruction.auxInstruction, auxInstructions);
componentInstruction = prevInstruction.component;
}
var rules = this._rules.get(parentComponentType);
if (isBlank(rules)) {
throw new BaseException(
`Component "${getTypeNameForDebugging(parentComponentType)}" has no route config.`);
}
let linkParamIndex = 0;
let routeParams: {[key: string]: any} = {};
// first, recognize the primary route if one is provided
if (linkParamIndex < linkParams.length && isString(linkParams[linkParamIndex])) {
let routeName = linkParams[linkParamIndex];
if (routeName == '' || routeName == '.' || routeName == '..') {
throw new BaseException(`"${routeName}/" is only allowed at the beginning of a link DSL.`);
}
linkParamIndex += 1;
if (linkParamIndex < linkParams.length) {
let linkParam = linkParams[linkParamIndex];
if (isStringMap(linkParam) && !isArray(linkParam)) {
routeParams = linkParam;
linkParamIndex += 1;
}
}
var routeRecognizer = (_aux ? rules.auxRulesByName : rules.rulesByName).get(routeName);
if (isBlank(routeRecognizer)) {
throw new BaseException(
`Component "${getTypeNameForDebugging(parentComponentType)}" has no route named "${routeName}".`);
}
// Create an "unresolved instruction" for async routes
// we'll figure out the rest of the route when we resolve the instruction and
// perform a navigation
if (isBlank(routeRecognizer.handler.componentType)) {
var generatedUrl: GeneratedUrl = routeRecognizer.generateComponentPathValues(routeParams);
return new UnresolvedInstruction(() => {
return routeRecognizer.handler.resolveComponentType().then((_) => {
return this._generate(linkParams, ancestorInstructions, prevInstruction, _aux,
_originalLink);
});
}, generatedUrl.urlPath, convertUrlParamsToArray(generatedUrl.urlParams));
}
componentInstruction = _aux ? rules.generateAuxiliary(routeName, routeParams) :
rules.generate(routeName, routeParams);
}
// Next, recognize auxiliary instructions.
// If we have an ancestor instruction, we preserve whatever aux routes are active from it.
while (linkParamIndex < linkParams.length && isArray(linkParams[linkParamIndex])) {
let auxParentInstruction: Instruction[] = [parentInstruction];
let auxInstruction = this._generate(linkParams[linkParamIndex], auxParentInstruction, null,
true, _originalLink);
// TODO: this will not work for aux routes with parameters or multiple segments
auxInstructions[auxInstruction.component.urlPath] = auxInstruction;
linkParamIndex += 1;
}
var instruction = new ResolvedInstruction(componentInstruction, null, auxInstructions);
// If the component is sync, we can generate resolved child route instructions
// If not, we'll resolve the instructions at navigation time
if (isPresent(componentInstruction) && isPresent(componentInstruction.componentType)) {
let childInstruction: Instruction = null;
if (componentInstruction.terminal) {
if (linkParamIndex >= linkParams.length) {
// TODO: throw that there are extra link params beyond the terminal component
}
} else {
let childAncestorComponents: Instruction[] = ancestorInstructions.concat([instruction]);
let remainingLinkParams = linkParams.slice(linkParamIndex);
childInstruction = this._generate(remainingLinkParams, childAncestorComponents, null, false,
_originalLink);
}
instruction.child = childInstruction;
}
return instruction;
}
public hasRoute(name: string, parentComponent: any): boolean {
var rules = this._rules.get(parentComponent);
if (isBlank(rules)) {
return false;
}
return rules.hasRoute(name);
}
public generateDefault(componentCursor: Type): Instruction {
if (isBlank(componentCursor)) {
return null;
}
var rules = this._rules.get(componentCursor);
if (isBlank(rules) || isBlank(rules.defaultRule)) {
return null;
}
var defaultChild = null;
if (isPresent(rules.defaultRule.handler.componentType)) {
var componentInstruction = rules.defaultRule.generate({});
if (!rules.defaultRule.terminal) {
defaultChild = this.generateDefault(rules.defaultRule.handler.componentType);
}
return new DefaultInstruction(componentInstruction, defaultChild);
}
return new UnresolvedInstruction(() => {
return rules.defaultRule.handler.resolveComponentType().then(
(_) => this.generateDefault(componentCursor));
});
}
}
/*
* Given: ['/a/b', {c: 2}]
* Returns: ['', 'a', 'b', {c: 2}]
*/
function splitAndFlattenLinkParams(linkParams: any[]): any[] {
var accumulation = [];
linkParams.forEach(function(item: any) {
if (isString(item)) {
var strItem: string = <string>item;
accumulation = accumulation.concat(strItem.split('/'));
} else {
accumulation.push(item);
}
});
return accumulation;
}
/*
* Given a list of instructions, returns the most specific instruction
*/
function mostSpecific(instructions: Instruction[]): Instruction {
instructions = instructions.filter((instruction) => isPresent(instruction));
if (instructions.length == 0) {
return null;
}
if (instructions.length == 1) {
return instructions[0];
}
var first = instructions[0];
var rest = instructions.slice(1);
return rest.reduce((instruction: Instruction, contender: Instruction) => {
if (compareSpecificityStrings(contender.specificity, instruction.specificity) == -1) {
return contender;
}
return instruction;
}, first);
}
/*
* Expects strings to be in the form of "[0-2]+"
* Returns -1 if string A should be sorted above string B, 1 if it should be sorted after,
* or 0 if they are the same.
*/
function compareSpecificityStrings(a: string, b: string): number {
var l = Math.min(a.length, b.length);
for (var i = 0; i < l; i += 1) {
var ai = StringWrapper.charCodeAt(a, i);
var bi = StringWrapper.charCodeAt(b, i);
var difference = bi - ai;
if (difference != 0) {
return difference;
}
}
return a.length - b.length;
}
function assertTerminalComponent(component, path) {
if (!isType(component)) {
return;
}
var annotations = reflector.annotations(component);
if (isPresent(annotations)) {
for (var i = 0; i < annotations.length; i++) {
var annotation = annotations[i];
if (annotation instanceof RouteConfig) {
throw new BaseException(
`Child routes are not allowed for "${path}". Use "..." on the parent's route path.`);
}
}
}
}

View File

@ -0,0 +1,578 @@
import {PromiseWrapper, EventEmitter, ObservableWrapper} from '../src/facade/async';
import {Map, StringMapWrapper} from '../src/facade/collection';
import {isBlank, isPresent, Type} from '../src/facade/lang';
import {BaseException} from '../src/facade/exceptions';
import {Location} from '@angular/common';
import {RouteRegistry, ROUTER_PRIMARY_COMPONENT} from './route_registry';
import {ComponentInstruction, Instruction} from './instruction';
import {RouterOutlet} from './directives/router_outlet';
import {getCanActivateHook} from './lifecycle/route_lifecycle_reflector';
import {RouteDefinition} from './route_config/route_config_impl';
import {Injectable, Inject} from '@angular/core';
let _resolveToTrue = PromiseWrapper.resolve(true);
let _resolveToFalse = PromiseWrapper.resolve(false);
/**
* The `Router` is responsible for mapping URLs to components.
*
* You can see the state of the router by inspecting the read-only field `router.navigating`.
* This may be useful for showing a spinner, for instance.
*
* ## Concepts
*
* Routers and component instances have a 1:1 correspondence.
*
* The router holds reference to a number of {@link RouterOutlet}.
* An outlet is a placeholder that the router dynamically fills in depending on the current URL.
*
* When the router navigates from a URL, it must first recognize it and serialize it into an
* `Instruction`.
* The router uses the `RouteRegistry` to get an `Instruction`.
*/
@Injectable()
export class Router {
navigating: boolean = false;
lastNavigationAttempt: string;
/**
* The current `Instruction` for the router
*/
public currentInstruction: Instruction = null;
private _currentNavigation: Promise<any> = _resolveToTrue;
private _outlet: RouterOutlet = null;
private _auxRouters = new Map<string, Router>();
private _childRouter: Router;
private _subject: EventEmitter<any> = new EventEmitter();
constructor(public registry: RouteRegistry, public parent: Router, public hostComponent: any,
public root?: Router) {}
/**
* Constructs a child router. You probably don't need to use this unless you're writing a reusable
* component.
*/
childRouter(hostComponent: any): Router {
return this._childRouter = new ChildRouter(this, hostComponent);
}
/**
* Constructs a child router. You probably don't need to use this unless you're writing a reusable
* component.
*/
auxRouter(hostComponent: any): Router { return new ChildRouter(this, hostComponent); }
/**
* Register an outlet to be notified of primary route changes.
*
* You probably don't need to use this unless you're writing a reusable component.
*/
registerPrimaryOutlet(outlet: RouterOutlet): Promise<any> {
if (isPresent(outlet.name)) {
throw new BaseException(`registerPrimaryOutlet expects to be called with an unnamed outlet.`);
}
if (isPresent(this._outlet)) {
throw new BaseException(`Primary outlet is already registered.`);
}
this._outlet = outlet;
if (isPresent(this.currentInstruction)) {
return this.commit(this.currentInstruction, false);
}
return _resolveToTrue;
}
/**
* Unregister an outlet (because it was destroyed, etc).
*
* You probably don't need to use this unless you're writing a custom outlet implementation.
*/
unregisterPrimaryOutlet(outlet: RouterOutlet): void {
if (isPresent(outlet.name)) {
throw new BaseException(`registerPrimaryOutlet expects to be called with an unnamed outlet.`);
}
this._outlet = null;
}
/**
* Register an outlet to notified of auxiliary route changes.
*
* You probably don't need to use this unless you're writing a reusable component.
*/
registerAuxOutlet(outlet: RouterOutlet): Promise<any> {
var outletName = outlet.name;
if (isBlank(outletName)) {
throw new BaseException(`registerAuxOutlet expects to be called with an outlet with a name.`);
}
var router = this.auxRouter(this.hostComponent);
this._auxRouters.set(outletName, router);
router._outlet = outlet;
var auxInstruction;
if (isPresent(this.currentInstruction) &&
isPresent(auxInstruction = this.currentInstruction.auxInstruction[outletName])) {
return router.commit(auxInstruction);
}
return _resolveToTrue;
}
/**
* Given an instruction, returns `true` if the instruction is currently active,
* otherwise `false`.
*/
isRouteActive(instruction: Instruction): boolean {
var router: Router = this;
if (isBlank(this.currentInstruction)) {
return false;
}
// `instruction` corresponds to the root router
while (isPresent(router.parent) && isPresent(instruction.child)) {
router = router.parent;
instruction = instruction.child;
}
if (isBlank(instruction.component) || isBlank(this.currentInstruction.component) ||
this.currentInstruction.component.routeName != instruction.component.routeName) {
return false;
}
let paramEquals = true;
if (isPresent(this.currentInstruction.component.params)) {
StringMapWrapper.forEach(instruction.component.params, (value, key) => {
if (this.currentInstruction.component.params[key] !== value) {
paramEquals = false;
}
});
}
return paramEquals;
}
/**
* Dynamically update the routing configuration and trigger a navigation.
*
* ### Usage
*
* ```
* router.config([
* { 'path': '/', 'component': IndexComp },
* { 'path': '/user/:id', 'component': UserComp },
* ]);
* ```
*/
config(definitions: RouteDefinition[]): Promise<any> {
definitions.forEach(
(routeDefinition) => { this.registry.config(this.hostComponent, routeDefinition); });
return this.renavigate();
}
/**
* Navigate based on the provided Route Link DSL. It's preferred to navigate with this method
* over `navigateByUrl`.
*
* ### Usage
*
* This method takes an array representing the Route Link DSL:
* ```
* ['./MyCmp', {param: 3}]
* ```
* See the {@link RouterLink} directive for more.
*/
navigate(linkParams: any[]): Promise<any> {
var instruction = this.generate(linkParams);
return this.navigateByInstruction(instruction, false);
}
/**
* Navigate to a URL. Returns a promise that resolves when navigation is complete.
* It's preferred to navigate with `navigate` instead of this method, since URLs are more brittle.
*
* If the given URL begins with a `/`, router will navigate absolutely.
* If the given URL does not begin with `/`, the router will navigate relative to this component.
*/
navigateByUrl(url: string, _skipLocationChange: boolean = false): Promise<any> {
return this._currentNavigation = this._currentNavigation.then((_) => {
this.lastNavigationAttempt = url;
this._startNavigating();
return this._afterPromiseFinishNavigating(this.recognize(url).then((instruction) => {
if (isBlank(instruction)) {
return false;
}
return this._navigate(instruction, _skipLocationChange);
}));
});
}
/**
* Navigate via the provided instruction. Returns a promise that resolves when navigation is
* complete.
*/
navigateByInstruction(instruction: Instruction,
_skipLocationChange: boolean = false): Promise<any> {
if (isBlank(instruction)) {
return _resolveToFalse;
}
return this._currentNavigation = this._currentNavigation.then((_) => {
this._startNavigating();
return this._afterPromiseFinishNavigating(this._navigate(instruction, _skipLocationChange));
});
}
/** @internal */
_settleInstruction(instruction: Instruction): Promise<any> {
return instruction.resolveComponent().then((_) => {
var unsettledInstructions: Array<Promise<any>> = [];
if (isPresent(instruction.component)) {
instruction.component.reuse = false;
}
if (isPresent(instruction.child)) {
unsettledInstructions.push(this._settleInstruction(instruction.child));
}
StringMapWrapper.forEach(instruction.auxInstruction, (instruction: Instruction, _) => {
unsettledInstructions.push(this._settleInstruction(instruction));
});
return PromiseWrapper.all(unsettledInstructions);
});
}
/** @internal */
_navigate(instruction: Instruction, _skipLocationChange: boolean): Promise<any> {
return this._settleInstruction(instruction)
.then((_) => this._routerCanReuse(instruction))
.then((_) => this._canActivate(instruction))
.then((result: boolean) => {
if (!result) {
return false;
}
return this._routerCanDeactivate(instruction)
.then((result: boolean) => {
if (result) {
return this.commit(instruction, _skipLocationChange)
.then((_) => {
this._emitNavigationFinish(instruction.toRootUrl());
return true;
});
}
});
});
}
private _emitNavigationFinish(url): void { ObservableWrapper.callEmit(this._subject, url); }
/** @internal */
_emitNavigationFail(url): void { ObservableWrapper.callError(this._subject, url); }
private _afterPromiseFinishNavigating(promise: Promise<any>): Promise<any> {
return PromiseWrapper.catchError(promise.then((_) => this._finishNavigating()), (err) => {
this._finishNavigating();
throw err;
});
}
/*
* Recursively set reuse flags
*/
/** @internal */
_routerCanReuse(instruction: Instruction): Promise<any> {
if (isBlank(this._outlet)) {
return _resolveToFalse;
}
if (isBlank(instruction.component)) {
return _resolveToTrue;
}
return this._outlet.routerCanReuse(instruction.component)
.then((result) => {
instruction.component.reuse = result;
if (result && isPresent(this._childRouter) && isPresent(instruction.child)) {
return this._childRouter._routerCanReuse(instruction.child);
}
});
}
private _canActivate(nextInstruction: Instruction): Promise<boolean> {
return canActivateOne(nextInstruction, this.currentInstruction);
}
private _routerCanDeactivate(instruction: Instruction): Promise<boolean> {
if (isBlank(this._outlet)) {
return _resolveToTrue;
}
var next: Promise<boolean>;
var childInstruction: Instruction = null;
var reuse: boolean = false;
var componentInstruction: ComponentInstruction = null;
if (isPresent(instruction)) {
childInstruction = instruction.child;
componentInstruction = instruction.component;
reuse = isBlank(instruction.component) || instruction.component.reuse;
}
if (reuse) {
next = _resolveToTrue;
} else {
next = this._outlet.routerCanDeactivate(componentInstruction);
}
// TODO: aux route lifecycle hooks
return next.then<boolean>((result): boolean | Promise<boolean> => {
if (result == false) {
return false;
}
if (isPresent(this._childRouter)) {
// TODO: ideally, this closure would map to async-await in Dart.
// For now, casting to any to suppress an error.
return <any>this._childRouter._routerCanDeactivate(childInstruction);
}
return true;
});
}
/**
* Updates this router and all descendant routers according to the given instruction
*/
commit(instruction: Instruction, _skipLocationChange: boolean = false): Promise<any> {
this.currentInstruction = instruction;
var next: Promise<any> = _resolveToTrue;
if (isPresent(this._outlet) && isPresent(instruction.component)) {
var componentInstruction = instruction.component;
if (componentInstruction.reuse) {
next = this._outlet.reuse(componentInstruction);
} else {
next =
this.deactivate(instruction).then((_) => this._outlet.activate(componentInstruction));
}
if (isPresent(instruction.child)) {
next = next.then((_) => {
if (isPresent(this._childRouter)) {
return this._childRouter.commit(instruction.child);
}
});
}
}
var promises: Promise<any>[] = [];
this._auxRouters.forEach((router, name) => {
if (isPresent(instruction.auxInstruction[name])) {
promises.push(router.commit(instruction.auxInstruction[name]));
}
});
return next.then((_) => PromiseWrapper.all(promises));
}
/** @internal */
_startNavigating(): void { this.navigating = true; }
/** @internal */
_finishNavigating(): void { this.navigating = false; }
/**
* Subscribe to URL updates from the router
*/
subscribe(onNext: (value: any) => void, onError?: (value: any) => void): Object {
return ObservableWrapper.subscribe(this._subject, onNext, onError);
}
/**
* Removes the contents of this router's outlet and all descendant outlets
*/
deactivate(instruction: Instruction): Promise<any> {
var childInstruction: Instruction = null;
var componentInstruction: ComponentInstruction = null;
if (isPresent(instruction)) {
childInstruction = instruction.child;
componentInstruction = instruction.component;
}
var next: Promise<any> = _resolveToTrue;
if (isPresent(this._childRouter)) {
next = this._childRouter.deactivate(childInstruction);
}
if (isPresent(this._outlet)) {
next = next.then((_) => this._outlet.deactivate(componentInstruction));
}
// TODO: handle aux routes
return next;
}
/**
* Given a URL, returns an instruction representing the component graph
*/
recognize(url: string): Promise<Instruction> {
var ancestorComponents = this._getAncestorInstructions();
return this.registry.recognize(url, ancestorComponents);
}
private _getAncestorInstructions(): Instruction[] {
var ancestorInstructions: Instruction[] = [this.currentInstruction];
var ancestorRouter: Router = this;
while (isPresent(ancestorRouter = ancestorRouter.parent)) {
ancestorInstructions.unshift(ancestorRouter.currentInstruction);
}
return ancestorInstructions;
}
/**
* Navigates to either the last URL successfully navigated to, or the last URL requested if the
* router has yet to successfully navigate.
*/
renavigate(): Promise<any> {
if (isBlank(this.lastNavigationAttempt)) {
return this._currentNavigation;
}
return this.navigateByUrl(this.lastNavigationAttempt);
}
/**
* Generate an `Instruction` based on the provided Route Link DSL.
*/
generate(linkParams: any[]): Instruction {
var ancestorInstructions = this._getAncestorInstructions();
return this.registry.generate(linkParams, ancestorInstructions);
}
}
@Injectable()
export class RootRouter extends Router {
/** @internal */
_location: Location;
/** @internal */
_locationSub: Object;
constructor(registry: RouteRegistry, location: Location,
@Inject(ROUTER_PRIMARY_COMPONENT) primaryComponent: Type) {
super(registry, null, primaryComponent);
this.root = this;
this._location = location;
this._locationSub = this._location.subscribe((change) => {
// we call recognize ourselves
this.recognize(change['url'])
.then((instruction) => {
if (isPresent(instruction)) {
this.navigateByInstruction(instruction, isPresent(change['pop']))
.then((_) => {
// this is a popstate event; no need to change the URL
if (isPresent(change['pop']) && change['type'] != 'hashchange') {
return;
}
var emitPath = instruction.toUrlPath();
var emitQuery = instruction.toUrlQuery();
if (emitPath.length > 0 && emitPath[0] != '/') {
emitPath = '/' + emitPath;
}
// We've opted to use pushstate and popState APIs regardless of whether you
// an app uses HashLocationStrategy or PathLocationStrategy.
// However, apps that are migrating might have hash links that operate outside
// angular to which routing must respond.
// Therefore we know that all hashchange events occur outside Angular.
// To support these cases where we respond to hashchanges and redirect as a
// result, we need to replace the top item on the stack.
if (change['type'] == 'hashchange') {
if (instruction.toRootUrl() != this._location.path()) {
this._location.replaceState(emitPath, emitQuery);
}
} else {
this._location.go(emitPath, emitQuery);
}
});
} else {
this._emitNavigationFail(change['url']);
}
});
});
this.registry.configFromComponent(primaryComponent);
this.navigateByUrl(location.path());
}
commit(instruction: Instruction, _skipLocationChange: boolean = false): Promise<any> {
var emitPath = instruction.toUrlPath();
var emitQuery = instruction.toUrlQuery();
if (emitPath.length > 0 && emitPath[0] != '/') {
emitPath = '/' + emitPath;
}
var promise = super.commit(instruction);
if (!_skipLocationChange) {
promise = promise.then((_) => { this._location.go(emitPath, emitQuery); });
}
return promise;
}
dispose(): void {
if (isPresent(this._locationSub)) {
ObservableWrapper.dispose(this._locationSub);
this._locationSub = null;
}
}
}
class ChildRouter extends Router {
constructor(parent: Router, hostComponent) {
super(parent.registry, parent, hostComponent, parent.root);
this.parent = parent;
}
navigateByUrl(url: string, _skipLocationChange: boolean = false): Promise<any> {
// Delegate navigation to the root router
return this.parent.navigateByUrl(url, _skipLocationChange);
}
navigateByInstruction(instruction: Instruction,
_skipLocationChange: boolean = false): Promise<any> {
// Delegate navigation to the root router
return this.parent.navigateByInstruction(instruction, _skipLocationChange);
}
}
function canActivateOne(nextInstruction: Instruction,
prevInstruction: Instruction): Promise<boolean> {
var next = _resolveToTrue;
if (isBlank(nextInstruction.component)) {
return next;
}
if (isPresent(nextInstruction.child)) {
next = canActivateOne(nextInstruction.child,
isPresent(prevInstruction) ? prevInstruction.child : null);
}
return next.then<boolean>((result: boolean): boolean => {
if (result == false) {
return false;
}
if (nextInstruction.component.reuse) {
return true;
}
var hook = getCanActivateHook(nextInstruction.component.componentType);
if (isPresent(hook)) {
return hook(nextInstruction.component,
isPresent(prevInstruction) ? prevInstruction.component : null);
}
return true;
});
}

View File

@ -0,0 +1,41 @@
import {ROUTER_PROVIDERS_COMMON} from './router_providers_common';
import {Provider} from '@angular/core';
import {BrowserPlatformLocation} from '@angular/platform-browser';
import {PlatformLocation} from '@angular/common';
/**
* A list of {@link Provider}s. To use the router, you must add this to your application.
*
* ### Example ([live demo](http://plnkr.co/edit/iRUP8B5OUbxCWQ3AcIDm))
*
* ```
* import {Component} from '@angular/core';
* import {
* ROUTER_DIRECTIVES,
* ROUTER_PROVIDERS,
* RouteConfig
* } from '@angular/router';
*
* @Component({directives: [ROUTER_DIRECTIVES]})
* @RouteConfig([
* {...},
* ])
* class AppCmp {
* // ...
* }
*
* bootstrap(AppCmp, [ROUTER_PROVIDERS]);
* ```
*/
export const ROUTER_PROVIDERS: any[] = /*@ts2dart_const*/[
ROUTER_PROVIDERS_COMMON,
/*@ts2dart_const*/ (
/* @ts2dart_Provider */ {provide: PlatformLocation, useClass: BrowserPlatformLocation}),
];
/**
* Use {@link ROUTER_PROVIDERS} instead.
*
* @deprecated
*/
export const ROUTER_BINDINGS = /*@ts2dart_const*/ ROUTER_PROVIDERS;

View File

@ -0,0 +1,39 @@
import {ApplicationRef, Provider} from '@angular/core';
import {LocationStrategy, PathLocationStrategy, Location} from '@angular/common';
import {Router, RootRouter} from './router';
import {RouteRegistry, ROUTER_PRIMARY_COMPONENT} from './route_registry';
import {Type} from '../src/facade/lang';
import {BaseException} from '../src/facade/exceptions';
/**
* The Platform agnostic ROUTER PROVIDERS
*/
export const ROUTER_PROVIDERS_COMMON: any[] = /*@ts2dart_const*/[
RouteRegistry,
/* @ts2dart_Provider */ {provide: LocationStrategy, useClass: PathLocationStrategy},
Location,
{
provide: Router,
useFactory: routerFactory,
deps: [RouteRegistry, Location, ROUTER_PRIMARY_COMPONENT, ApplicationRef]
},
{
provide: ROUTER_PRIMARY_COMPONENT,
useFactory: routerPrimaryComponentFactory,
deps: /*@ts2dart_const*/ ([ApplicationRef])
}
];
function routerFactory(registry: RouteRegistry, location: Location, primaryComponent: Type,
appRef: ApplicationRef): RootRouter {
var rootRouter = new RootRouter(registry, location, primaryComponent);
appRef.registerDisposeListener(() => rootRouter.dispose());
return rootRouter;
}
function routerPrimaryComponentFactory(app: ApplicationRef): Type {
if (app.componentTypes.length == 0) {
throw new BaseException("Bootstrap at least one component before injecting Router.");
}
return app.componentTypes[0];
}

View File

@ -0,0 +1,26 @@
import {isPresent, Type} from '../../../src/facade/lang';
import {RouteHandler} from './route_handler';
import {RouteData, BLANK_ROUTE_DATA} from '../../instruction';
export class AsyncRouteHandler implements RouteHandler {
/** @internal */
_resolvedComponent: Promise<Type> = null;
componentType: Type;
public data: RouteData;
constructor(private _loader: () => Promise<Type>, data: {[key: string]: any} = null) {
this.data = isPresent(data) ? new RouteData(data) : BLANK_ROUTE_DATA;
}
resolveComponentType(): Promise<Type> {
if (isPresent(this._resolvedComponent)) {
return this._resolvedComponent;
}
return this._resolvedComponent = this._loader().then((componentType) => {
this.componentType = componentType;
return componentType;
});
}
}

View File

@ -0,0 +1,8 @@
import {Type} from '../../../src/facade/lang';
import {RouteData} from '../../instruction';
export interface RouteHandler {
componentType: Type;
resolveComponentType(): Promise<any>;
data: RouteData;
}

View File

@ -0,0 +1,19 @@
import {PromiseWrapper} from '../../../src/facade/async';
import {isPresent, Type} from '../../../src/facade/lang';
import {RouteHandler} from './route_handler';
import {RouteData, BLANK_ROUTE_DATA} from '../../instruction';
export class SyncRouteHandler implements RouteHandler {
public data: RouteData;
/** @internal */
_resolvedComponent: Promise<any> = null;
constructor(public componentType: Type, data?: {[key: string]: any}) {
this._resolvedComponent = PromiseWrapper.resolve(componentType);
this.data = isPresent(data) ? new RouteData(data) : BLANK_ROUTE_DATA;
}
resolveComponentType(): Promise<any> { return this._resolvedComponent; }
}

View File

@ -0,0 +1,309 @@
import {RegExpWrapper, StringWrapper, isPresent, isBlank} from '../../../src/facade/lang';
import {BaseException} from '../../../src/facade/exceptions';
import {StringMapWrapper} from '../../../src/facade/collection';
import {TouchMap, normalizeString} from '../../utils';
import {Url, RootUrl, convertUrlParamsToArray} from '../../url_parser';
import {RoutePath, GeneratedUrl, MatchedUrl} from './route_path';
/**
* `ParamRoutePath`s are made up of `PathSegment`s, each of which can
* match a segment of a URL. Different kind of `PathSegment`s match
* URL segments in different ways...
*/
interface PathSegment {
name: string;
generate(params: TouchMap): string;
match(path: string): boolean;
specificity: string;
hash: string;
}
/**
* Identified by a `...` URL segment. This indicates that the
* Route will continue to be matched by child `Router`s.
*/
class ContinuationPathSegment implements PathSegment {
name: string = '';
specificity = '';
hash = '...';
generate(params: TouchMap): string { return ''; }
match(path: string): boolean { return true; }
}
/**
* Identified by a string not starting with a `:` or `*`.
* Only matches the URL segments that equal the segment path
*/
class StaticPathSegment implements PathSegment {
name: string = '';
specificity = '2';
hash: string;
constructor(public path: string) { this.hash = path; }
match(path: string): boolean { return path == this.path; }
generate(params: TouchMap): string { return this.path; }
}
/**
* Identified by a string starting with `:`. Indicates a segment
* that can contain a value that will be extracted and provided to
* a matching `Instruction`.
*/
class DynamicPathSegment implements PathSegment {
static paramMatcher = /^:([^\/]+)$/g;
specificity = '1';
hash = ':';
constructor(public name: string) {}
match(path: string): boolean { return path.length > 0; }
generate(params: TouchMap): string {
if (!StringMapWrapper.contains(params.map, this.name)) {
throw new BaseException(
`Route generator for '${this.name}' was not included in parameters passed.`);
}
return encodeDynamicSegment(normalizeString(params.get(this.name)));
}
}
/**
* Identified by a string starting with `*` Indicates that all the following
* segments match this route and that the value of these segments should
* be provided to a matching `Instruction`.
*/
class StarPathSegment implements PathSegment {
static wildcardMatcher = /^\*([^\/]+)$/g;
specificity = '0';
hash = '*';
constructor(public name: string) {}
match(path: string): boolean { return true; }
generate(params: TouchMap): string { return normalizeString(params.get(this.name)); }
}
/**
* Parses a URL string using a given matcher DSL, and generates URLs from param maps
*/
export class ParamRoutePath implements RoutePath {
specificity: string;
terminal: boolean = true;
hash: string;
private _segments: PathSegment[];
/**
* Takes a string representing the matcher DSL
*/
constructor(public routePath: string) {
this._assertValidPath(routePath);
this._parsePathString(routePath);
this.specificity = this._calculateSpecificity();
this.hash = this._calculateHash();
var lastSegment = this._segments[this._segments.length - 1];
this.terminal = !(lastSegment instanceof ContinuationPathSegment);
}
matchUrl(url: Url): MatchedUrl {
var nextUrlSegment = url;
var currentUrlSegment: Url;
var positionalParams = {};
var captured: string[] = [];
for (var i = 0; i < this._segments.length; i += 1) {
var pathSegment = this._segments[i];
currentUrlSegment = nextUrlSegment;
if (pathSegment instanceof ContinuationPathSegment) {
break;
}
if (isPresent(currentUrlSegment)) {
// the star segment consumes all of the remaining URL, including matrix params
if (pathSegment instanceof StarPathSegment) {
positionalParams[pathSegment.name] = currentUrlSegment.toString();
captured.push(currentUrlSegment.toString());
nextUrlSegment = null;
break;
}
captured.push(currentUrlSegment.path);
if (pathSegment instanceof DynamicPathSegment) {
positionalParams[pathSegment.name] = decodeDynamicSegment(currentUrlSegment.path);
} else if (!pathSegment.match(currentUrlSegment.path)) {
return null;
}
nextUrlSegment = currentUrlSegment.child;
} else if (!pathSegment.match('')) {
return null;
}
}
if (this.terminal && isPresent(nextUrlSegment)) {
return null;
}
var urlPath = captured.join('/');
var auxiliary = [];
var urlParams = [];
var allParams = positionalParams;
if (isPresent(currentUrlSegment)) {
// If this is the root component, read query params. Otherwise, read matrix params.
var paramsSegment = url instanceof RootUrl ? url : currentUrlSegment;
if (isPresent(paramsSegment.params)) {
allParams = StringMapWrapper.merge(paramsSegment.params, positionalParams);
urlParams = convertUrlParamsToArray(paramsSegment.params);
} else {
allParams = positionalParams;
}
auxiliary = currentUrlSegment.auxiliary;
}
return new MatchedUrl(urlPath, urlParams, allParams, auxiliary, nextUrlSegment);
}
generateUrl(params: {[key: string]: any}): GeneratedUrl {
var paramTokens = new TouchMap(params);
var path = [];
for (var i = 0; i < this._segments.length; i++) {
let segment = this._segments[i];
if (!(segment instanceof ContinuationPathSegment)) {
path.push(segment.generate(paramTokens));
}
}
var urlPath = path.join('/');
var nonPositionalParams = paramTokens.getUnused();
var urlParams = nonPositionalParams;
return new GeneratedUrl(urlPath, urlParams);
}
toString(): string { return this.routePath; }
private _parsePathString(routePath: string) {
// normalize route as not starting with a "/". Recognition will
// also normalize.
if (routePath.startsWith("/")) {
routePath = routePath.substring(1);
}
var segmentStrings = routePath.split('/');
this._segments = [];
var limit = segmentStrings.length - 1;
for (var i = 0; i <= limit; i++) {
var segment = segmentStrings[i], match;
if (isPresent(match = RegExpWrapper.firstMatch(DynamicPathSegment.paramMatcher, segment))) {
this._segments.push(new DynamicPathSegment(match[1]));
} else if (isPresent(
match = RegExpWrapper.firstMatch(StarPathSegment.wildcardMatcher, segment))) {
this._segments.push(new StarPathSegment(match[1]));
} else if (segment == '...') {
if (i < limit) {
throw new BaseException(
`Unexpected "..." before the end of the path for "${routePath}".`);
}
this._segments.push(new ContinuationPathSegment());
} else {
this._segments.push(new StaticPathSegment(segment));
}
}
}
private _calculateSpecificity(): string {
// The "specificity" of a path is used to determine which route is used when multiple routes
// match
// a URL. Static segments (like "/foo") are the most specific, followed by dynamic segments
// (like
// "/:id"). Star segments add no specificity. Segments at the start of the path are more
// specific
// than proceeding ones.
//
// The code below uses place values to combine the different types of segments into a single
// string that we can sort later. Each static segment is marked as a specificity of "2," each
// dynamic segment is worth "1" specificity, and stars are worth "0" specificity.
var i, length = this._segments.length, specificity;
if (length == 0) {
// a single slash (or "empty segment" is as specific as a static segment
specificity += '2';
} else {
specificity = '';
for (i = 0; i < length; i++) {
specificity += this._segments[i].specificity;
}
}
return specificity;
}
private _calculateHash(): string {
// this function is used to determine whether a route config path like `/foo/:id` collides with
// `/foo/:name`
var i, length = this._segments.length;
var hashParts = [];
for (i = 0; i < length; i++) {
hashParts.push(this._segments[i].hash);
}
return hashParts.join('/');
}
private _assertValidPath(path: string) {
if (StringWrapper.contains(path, '#')) {
throw new BaseException(
`Path "${path}" should not include "#". Use "HashLocationStrategy" instead.`);
}
var illegalCharacter = RegExpWrapper.firstMatch(ParamRoutePath.RESERVED_CHARS, path);
if (isPresent(illegalCharacter)) {
throw new BaseException(
`Path "${path}" contains "${illegalCharacter[0]}" which is not allowed in a route config.`);
}
}
static RESERVED_CHARS = RegExpWrapper.create('//|\\(|\\)|;|\\?|=');
}
let REGEXP_PERCENT = /%/g;
let REGEXP_SLASH = /\//g;
let REGEXP_OPEN_PARENT = /\(/g;
let REGEXP_CLOSE_PARENT = /\)/g;
let REGEXP_SEMICOLON = /;/g;
function encodeDynamicSegment(value: string): string {
if (isBlank(value)) {
return null;
}
value = StringWrapper.replaceAll(value, REGEXP_PERCENT, '%25');
value = StringWrapper.replaceAll(value, REGEXP_SLASH, '%2F');
value = StringWrapper.replaceAll(value, REGEXP_OPEN_PARENT, '%28');
value = StringWrapper.replaceAll(value, REGEXP_CLOSE_PARENT, '%29');
value = StringWrapper.replaceAll(value, REGEXP_SEMICOLON, '%3B');
return value;
}
let REGEXP_ENC_SEMICOLON = /%3B/ig;
let REGEXP_ENC_CLOSE_PARENT = /%29/ig;
let REGEXP_ENC_OPEN_PARENT = /%28/ig;
let REGEXP_ENC_SLASH = /%2F/ig;
let REGEXP_ENC_PERCENT = /%25/ig;
function decodeDynamicSegment(value: string): string {
if (isBlank(value)) {
return null;
}
value = StringWrapper.replaceAll(value, REGEXP_ENC_SEMICOLON, ';');
value = StringWrapper.replaceAll(value, REGEXP_ENC_CLOSE_PARENT, ')');
value = StringWrapper.replaceAll(value, REGEXP_ENC_OPEN_PARENT, '(');
value = StringWrapper.replaceAll(value, REGEXP_ENC_SLASH, '/');
value = StringWrapper.replaceAll(value, REGEXP_ENC_PERCENT, '%');
return value;
}

View File

@ -0,0 +1,40 @@
import {RegExpWrapper, RegExpMatcherWrapper, isBlank} from '../../../src/facade/lang';
import {Url} from '../../url_parser';
import {RoutePath, GeneratedUrl, MatchedUrl} from './route_path';
export interface RegexSerializer { (params: {[key: string]: any}): GeneratedUrl; }
export class RegexRoutePath implements RoutePath {
public hash: string;
public terminal: boolean = true;
public specificity: string = '2';
private _regex: RegExp;
constructor(private _reString: string, private _serializer: RegexSerializer) {
this.hash = this._reString;
this._regex = RegExpWrapper.create(this._reString);
}
matchUrl(url: Url): MatchedUrl {
var urlPath = url.toString();
var params: {[key: string]: string} = {};
var matcher = RegExpWrapper.matcher(this._regex, urlPath);
var match = RegExpMatcherWrapper.next(matcher);
if (isBlank(match)) {
return null;
}
for (let i = 0; i < match.length; i += 1) {
params[i.toString()] = match[i];
}
return new MatchedUrl(urlPath, [], params, [], null);
}
generateUrl(params: {[key: string]: any}): GeneratedUrl { return this._serializer(params); }
toString(): string { return this._reString; }
}

View File

@ -0,0 +1,20 @@
import {Url} from '../../url_parser';
export class MatchedUrl {
constructor(public urlPath: string, public urlParams: string[],
public allParams: {[key: string]: any}, public auxiliary: Url[], public rest: Url) {}
}
export class GeneratedUrl {
constructor(public urlPath: string, public urlParams: {[key: string]: any}) {}
}
export interface RoutePath {
specificity: string;
terminal: boolean;
hash: string;
matchUrl(url: Url): MatchedUrl;
generateUrl(params: {[key: string]: any}): GeneratedUrl;
toString(): string;
}

View File

@ -0,0 +1,187 @@
import {isBlank, isPresent, isFunction} from '../../src/facade/lang';
import {BaseException} from '../../src/facade/exceptions';
import {Map} from '../../src/facade/collection';
import {PromiseWrapper} from '../../src/facade/async';
import {AbstractRule, RouteRule, RedirectRule, RouteMatch, PathMatch} from './rules';
import {
Route,
AsyncRoute,
AuxRoute,
Redirect,
RouteDefinition
} from '../route_config/route_config_impl';
import {AsyncRouteHandler} from './route_handlers/async_route_handler';
import {SyncRouteHandler} from './route_handlers/sync_route_handler';
import {RoutePath} from './route_paths/route_path';
import {ParamRoutePath} from './route_paths/param_route_path';
import {RegexRoutePath} from './route_paths/regex_route_path';
import {Url} from '../url_parser';
import {ComponentInstruction} from '../instruction';
/**
* A `RuleSet` is responsible for recognizing routes for a particular component.
* It is consumed by `RouteRegistry`, which knows how to recognize an entire hierarchy of
* components.
*/
export class RuleSet {
rulesByName = new Map<string, RouteRule>();
// map from name to rule
auxRulesByName = new Map<string, RouteRule>();
// map from starting path to rule
auxRulesByPath = new Map<string, RouteRule>();
// TODO: optimize this into a trie
rules: AbstractRule[] = [];
// the rule to use automatically when recognizing or generating from this rule set
defaultRule: RouteRule = null;
/**
* Configure additional rules in this rule set from a route definition
* @returns {boolean} true if the config is terminal
*/
config(config: RouteDefinition): boolean {
let handler;
if (isPresent(config.name) && config.name[0].toUpperCase() != config.name[0]) {
let suggestedName = config.name[0].toUpperCase() + config.name.substring(1);
throw new BaseException(
`Route "${config.path}" with name "${config.name}" does not begin with an uppercase letter. Route names should be CamelCase like "${suggestedName}".`);
}
if (config instanceof AuxRoute) {
handler = new SyncRouteHandler(config.component, config.data);
let routePath = this._getRoutePath(config);
let auxRule = new RouteRule(routePath, handler, config.name);
this.auxRulesByPath.set(routePath.toString(), auxRule);
if (isPresent(config.name)) {
this.auxRulesByName.set(config.name, auxRule);
}
return auxRule.terminal;
}
let useAsDefault = false;
if (config instanceof Redirect) {
let routePath = this._getRoutePath(config);
let redirector = new RedirectRule(routePath, config.redirectTo);
this._assertNoHashCollision(redirector.hash, config.path);
this.rules.push(redirector);
return true;
}
if (config instanceof Route) {
handler = new SyncRouteHandler(config.component, config.data);
useAsDefault = isPresent(config.useAsDefault) && config.useAsDefault;
} else if (config instanceof AsyncRoute) {
handler = new AsyncRouteHandler(config.loader, config.data);
useAsDefault = isPresent(config.useAsDefault) && config.useAsDefault;
}
let routePath = this._getRoutePath(config);
let newRule = new RouteRule(routePath, handler, config.name);
this._assertNoHashCollision(newRule.hash, config.path);
if (useAsDefault) {
if (isPresent(this.defaultRule)) {
throw new BaseException(`Only one route can be default`);
}
this.defaultRule = newRule;
}
this.rules.push(newRule);
if (isPresent(config.name)) {
this.rulesByName.set(config.name, newRule);
}
return newRule.terminal;
}
/**
* Given a URL, returns a list of `RouteMatch`es, which are partial recognitions for some route.
*/
recognize(urlParse: Url): Promise<RouteMatch>[] {
var solutions = [];
this.rules.forEach((routeRecognizer: AbstractRule) => {
var pathMatch = routeRecognizer.recognize(urlParse);
if (isPresent(pathMatch)) {
solutions.push(pathMatch);
}
});
// handle cases where we are routing just to an aux route
if (solutions.length == 0 && isPresent(urlParse) && urlParse.auxiliary.length > 0) {
return [PromiseWrapper.resolve(new PathMatch(null, null, urlParse.auxiliary))];
}
return solutions;
}
recognizeAuxiliary(urlParse: Url): Promise<RouteMatch>[] {
var routeRecognizer: RouteRule = this.auxRulesByPath.get(urlParse.path);
if (isPresent(routeRecognizer)) {
return [routeRecognizer.recognize(urlParse)];
}
return [PromiseWrapper.resolve(null)];
}
hasRoute(name: string): boolean { return this.rulesByName.has(name); }
componentLoaded(name: string): boolean {
return this.hasRoute(name) && isPresent(this.rulesByName.get(name).handler.componentType);
}
loadComponent(name: string): Promise<any> {
return this.rulesByName.get(name).handler.resolveComponentType();
}
generate(name: string, params: any): ComponentInstruction {
var rule: RouteRule = this.rulesByName.get(name);
if (isBlank(rule)) {
return null;
}
return rule.generate(params);
}
generateAuxiliary(name: string, params: any): ComponentInstruction {
var rule: RouteRule = this.auxRulesByName.get(name);
if (isBlank(rule)) {
return null;
}
return rule.generate(params);
}
private _assertNoHashCollision(hash: string, path) {
this.rules.forEach((rule) => {
if (hash == rule.hash) {
throw new BaseException(
`Configuration '${path}' conflicts with existing route '${rule.path}'`);
}
});
}
private _getRoutePath(config: RouteDefinition): RoutePath {
if (isPresent(config.regex)) {
if (isFunction(config.serializer)) {
return new RegexRoutePath(config.regex, config.serializer);
} else {
throw new BaseException(
`Route provides a regex property, '${config.regex}', but no serializer property`);
}
}
if (isPresent(config.path)) {
// Auxiliary routes do not have a slash at the start
let path = (config instanceof AuxRoute && config.path.startsWith('/')) ?
config.path.substring(1) :
config.path;
return new ParamRoutePath(path);
}
throw new BaseException('Route must provide either a path or regex property');
}
}

View File

@ -0,0 +1,119 @@
import {isPresent, isBlank} from '../../src/facade/lang';
import {BaseException} from '../../src/facade/exceptions';
import {PromiseWrapper} from '../../src/facade/promise';
import {Map} from '../../src/facade/collection';
import {RouteHandler} from './route_handlers/route_handler';
import {Url, convertUrlParamsToArray} from '../url_parser';
import {ComponentInstruction} from '../instruction';
import {RoutePath, GeneratedUrl} from './route_paths/route_path';
// RouteMatch objects hold information about a match between a rule and a URL
export abstract class RouteMatch {}
export class PathMatch extends RouteMatch {
constructor(public instruction: ComponentInstruction, public remaining: Url,
public remainingAux: Url[]) {
super();
}
}
export class RedirectMatch extends RouteMatch {
constructor(public redirectTo: any[], public specificity) { super(); }
}
// Rules are responsible for recognizing URL segments and generating instructions
export interface AbstractRule {
hash: string;
path: string;
recognize(beginningSegment: Url): Promise<RouteMatch>;
generate(params: {[key: string]: any}): ComponentInstruction;
}
export class RedirectRule implements AbstractRule {
public hash: string;
constructor(private _pathRecognizer: RoutePath, public redirectTo: any[]) {
this.hash = this._pathRecognizer.hash;
}
get path() { return this._pathRecognizer.toString(); }
set path(val) { throw new BaseException('you cannot set the path of a RedirectRule directly'); }
/**
* Returns `null` or a `ParsedUrl` representing the new path to match
*/
recognize(beginningSegment: Url): Promise<RouteMatch> {
var match = null;
if (isPresent(this._pathRecognizer.matchUrl(beginningSegment))) {
match = new RedirectMatch(this.redirectTo, this._pathRecognizer.specificity);
}
return PromiseWrapper.resolve(match);
}
generate(params: {[key: string]: any}): ComponentInstruction {
throw new BaseException(`Tried to generate a redirect.`);
}
}
// represents something like '/foo/:bar'
export class RouteRule implements AbstractRule {
specificity: string;
terminal: boolean;
hash: string;
private _cache: Map<string, ComponentInstruction> = new Map<string, ComponentInstruction>();
// TODO: cache component instruction instances by params and by ParsedUrl instance
constructor(private _routePath: RoutePath, public handler: RouteHandler,
private _routeName: string) {
this.specificity = this._routePath.specificity;
this.hash = this._routePath.hash;
this.terminal = this._routePath.terminal;
}
get path() { return this._routePath.toString(); }
set path(val) { throw new BaseException('you cannot set the path of a RouteRule directly'); }
recognize(beginningSegment: Url): Promise<RouteMatch> {
var res = this._routePath.matchUrl(beginningSegment);
if (isBlank(res)) {
return null;
}
return this.handler.resolveComponentType().then((_) => {
var componentInstruction = this._getInstruction(res.urlPath, res.urlParams, res.allParams);
return new PathMatch(componentInstruction, res.rest, res.auxiliary);
});
}
generate(params: {[key: string]: any}): ComponentInstruction {
var generated = this._routePath.generateUrl(params);
var urlPath = generated.urlPath;
var urlParams = generated.urlParams;
return this._getInstruction(urlPath, convertUrlParamsToArray(urlParams), params);
}
generateComponentPathValues(params: {[key: string]: any}): GeneratedUrl {
return this._routePath.generateUrl(params);
}
private _getInstruction(urlPath: string, urlParams: string[],
params: {[key: string]: any}): ComponentInstruction {
if (isBlank(this.handler.componentType)) {
throw new BaseException(`Tried to get instruction before the type was loaded.`);
}
var hashKey = urlPath + '?' + urlParams.join('&');
if (this._cache.has(hashKey)) {
return this._cache.get(hashKey);
}
var instruction =
new ComponentInstruction(urlPath, urlParams, this.handler.data, this.handler.componentType,
this.terminal, this.specificity, params, this._routeName);
this._cache.set(hashKey, instruction);
return instruction;
}
}

View File

@ -0,0 +1,242 @@
import {StringMapWrapper} from '../src/facade/collection';
import {isPresent, isBlank, RegExpWrapper} from '../src/facade/lang';
import {BaseException} from '../src/facade/exceptions';
export function convertUrlParamsToArray(urlParams: {[key: string]: any}): string[] {
var paramsArray = [];
if (isBlank(urlParams)) {
return [];
}
StringMapWrapper.forEach(
urlParams, (value, key) => { paramsArray.push((value === true) ? key : key + '=' + value); });
return paramsArray;
}
// Convert an object of url parameters into a string that can be used in an URL
export function serializeParams(urlParams: {[key: string]: any}, joiner = '&'): string {
return convertUrlParamsToArray(urlParams).join(joiner);
}
/**
* This class represents a parsed URL
*/
export class Url {
constructor(public path: string, public child: Url = null,
public auxiliary: Url[] = /*@ts2dart_const*/[],
public params: {[key: string]: any} = /*@ts2dart_const*/ {}) {}
toString(): string {
return this.path + this._matrixParamsToString() + this._auxToString() + this._childString();
}
segmentToString(): string { return this.path + this._matrixParamsToString(); }
/** @internal */
_auxToString(): string {
return this.auxiliary.length > 0 ?
('(' + this.auxiliary.map(sibling => sibling.toString()).join('//') + ')') :
'';
}
private _matrixParamsToString(): string {
var paramString = serializeParams(this.params, ';');
if (paramString.length > 0) {
return ';' + paramString;
}
return '';
}
/** @internal */
_childString(): string { return isPresent(this.child) ? ('/' + this.child.toString()) : ''; }
}
export class RootUrl extends Url {
constructor(path: string, child: Url = null, auxiliary: Url[] = /*@ts2dart_const*/[],
params: {[key: string]: any} = null) {
super(path, child, auxiliary, params);
}
toString(): string {
return this.path + this._auxToString() + this._childString() + this._queryParamsToString();
}
segmentToString(): string { return this.path + this._queryParamsToString(); }
private _queryParamsToString(): string {
if (isBlank(this.params)) {
return '';
}
return '?' + serializeParams(this.params);
}
}
export function pathSegmentsToUrl(pathSegments: string[]): Url {
var url = new Url(pathSegments[pathSegments.length - 1]);
for (var i = pathSegments.length - 2; i >= 0; i -= 1) {
url = new Url(pathSegments[i], url);
}
return url;
}
var SEGMENT_RE = RegExpWrapper.create('^[^\\/\\(\\)\\?;=&#]+');
function matchUrlSegment(str: string): string {
var match = RegExpWrapper.firstMatch(SEGMENT_RE, str);
return isPresent(match) ? match[0] : '';
}
var QUERY_PARAM_VALUE_RE = RegExpWrapper.create('^[^\\(\\)\\?;&#]+');
function matchUrlQueryParamValue(str: string): string {
var match = RegExpWrapper.firstMatch(QUERY_PARAM_VALUE_RE, str);
return isPresent(match) ? match[0] : '';
}
export class UrlParser {
private _remaining: string;
peekStartsWith(str: string): boolean { return this._remaining.startsWith(str); }
capture(str: string): void {
if (!this._remaining.startsWith(str)) {
throw new BaseException(`Expected "${str}".`);
}
this._remaining = this._remaining.substring(str.length);
}
parse(url: string): Url {
this._remaining = url;
if (url == '' || url == '/') {
return new Url('');
}
return this.parseRoot();
}
// segment + (aux segments) + (query params)
parseRoot(): RootUrl {
if (this.peekStartsWith('/')) {
this.capture('/');
}
var path = matchUrlSegment(this._remaining);
this.capture(path);
var aux: Url[] = [];
if (this.peekStartsWith('(')) {
aux = this.parseAuxiliaryRoutes();
}
if (this.peekStartsWith(';')) {
// TODO: should these params just be dropped?
this.parseMatrixParams();
}
var child = null;
if (this.peekStartsWith('/') && !this.peekStartsWith('//')) {
this.capture('/');
child = this.parseSegment();
}
var queryParams: {[key: string]: any} = null;
if (this.peekStartsWith('?')) {
queryParams = this.parseQueryParams();
}
return new RootUrl(path, child, aux, queryParams);
}
// segment + (matrix params) + (aux segments)
parseSegment(): Url {
if (this._remaining.length == 0) {
return null;
}
if (this.peekStartsWith('/')) {
this.capture('/');
}
var path = matchUrlSegment(this._remaining);
this.capture(path);
var matrixParams: {[key: string]: any} = null;
if (this.peekStartsWith(';')) {
matrixParams = this.parseMatrixParams();
}
var aux: Url[] = [];
if (this.peekStartsWith('(')) {
aux = this.parseAuxiliaryRoutes();
}
var child: Url = null;
if (this.peekStartsWith('/') && !this.peekStartsWith('//')) {
this.capture('/');
child = this.parseSegment();
}
return new Url(path, child, aux, matrixParams);
}
parseQueryParams(): {[key: string]: any} {
var params: {[key: string]: any} = {};
this.capture('?');
this.parseQueryParam(params);
while (this._remaining.length > 0 && this.peekStartsWith('&')) {
this.capture('&');
this.parseQueryParam(params);
}
return params;
}
parseMatrixParams(): {[key: string]: any} {
var params: {[key: string]: any} = {};
while (this._remaining.length > 0 && this.peekStartsWith(';')) {
this.capture(';');
this.parseParam(params);
}
return params;
}
parseParam(params: {[key: string]: any}): void {
var key = matchUrlSegment(this._remaining);
if (isBlank(key)) {
return;
}
this.capture(key);
var value: any = true;
if (this.peekStartsWith('=')) {
this.capture('=');
var valueMatch = matchUrlSegment(this._remaining);
if (isPresent(valueMatch)) {
value = valueMatch;
this.capture(value);
}
}
params[key] = value;
}
parseQueryParam(params: {[key: string]: any}): void {
var key = matchUrlSegment(this._remaining);
if (isBlank(key)) {
return;
}
this.capture(key);
var value: any = true;
if (this.peekStartsWith('=')) {
this.capture('=');
var valueMatch = matchUrlQueryParamValue(this._remaining);
if (isPresent(valueMatch)) {
value = valueMatch;
this.capture(value);
}
}
params[key] = value;
}
parseAuxiliaryRoutes(): Url[] {
var routes: Url[] = [];
this.capture('(');
while (!this.peekStartsWith(')') && this._remaining.length > 0) {
routes.push(this.parseSegment());
if (this.peekStartsWith('//')) {
this.capture('//');
}
}
this.capture(')');
return routes;
}
}
export var parser = new UrlParser();

View File

@ -0,0 +1,37 @@
import {isPresent, isBlank} from '../src/facade/lang';
import {StringMapWrapper} from '../src/facade/collection';
export class TouchMap {
map: {[key: string]: string} = {};
keys: {[key: string]: boolean} = {};
constructor(map: {[key: string]: any}) {
if (isPresent(map)) {
StringMapWrapper.forEach(map, (value, key) => {
this.map[key] = isPresent(value) ? value.toString() : null;
this.keys[key] = true;
});
}
}
get(key: string): string {
StringMapWrapper.delete(this.keys, key);
return this.map[key];
}
getUnused(): {[key: string]: any} {
var unused: {[key: string]: any} = {};
var keys = StringMapWrapper.keys(this.keys);
keys.forEach(key => unused[key] = StringMapWrapper.get(this.map, key));
return unused;
}
}
export function normalizeString(obj: any): string {
if (isBlank(obj)) {
return null;
} else {
return obj.toString();
}
}