From a67f2314f997a785d01163498b231435cb9b4680 Mon Sep 17 00:00:00 2001 From: Brian Ford Date: Mon, 15 Jun 2015 14:34:14 -0700 Subject: [PATCH] feat(router): add support for hash-based location Closes #2555 --- modules/angular2/router.ts | 1 + .../src/router/hash_location_strategy.ts | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 modules/angular2/src/router/hash_location_strategy.ts diff --git a/modules/angular2/router.ts b/modules/angular2/router.ts index 70a041e09b..fecc2a7f34 100644 --- a/modules/angular2/router.ts +++ b/modules/angular2/router.ts @@ -12,6 +12,7 @@ export {RouterLink} from './src/router/router_link'; export {RouteParams} from './src/router/instruction'; export {RouteRegistry} from './src/router/route_registry'; export {LocationStrategy} from './src/router/location_strategy'; +export {HashLocationStrategy} from './src/router/hash_location_strategy'; export {HTML5LocationStrategy} from './src/router/html5_location_strategy'; export {Location, appBaseHrefToken} from './src/router/location'; export {Pipeline} from './src/router/pipeline'; diff --git a/modules/angular2/src/router/hash_location_strategy.ts b/modules/angular2/src/router/hash_location_strategy.ts new file mode 100644 index 0000000000..9d3540448b --- /dev/null +++ b/modules/angular2/src/router/hash_location_strategy.ts @@ -0,0 +1,32 @@ +import {DOM} from 'angular2/src/dom/dom_adapter'; +import {Injectable} from 'angular2/di'; +import {LocationStrategy} from './location_strategy'; +import {EventListener, History, Location} from 'angular2/src/facade/browser'; + +@Injectable() +export class HashLocationStrategy extends LocationStrategy { + private _location: Location; + private _history: History; + + constructor() { + super(); + this._location = DOM.getLocation(); + this._history = DOM.getHistory(); + } + + onPopState(fn: EventListener): void { + DOM.getGlobalEventTarget('window').addEventListener('popstate', fn, false); + } + + getBaseHref(): string { return ''; } + + path(): string { return this._location.hash; } + + pushState(state: any, title: string, url: string) { + this._history.pushState(state, title, '#' + url); + } + + forward(): void { this._history.forward(); } + + back(): void { this._history.back(); } +}