|
1 | | -import {DOM} from 'angular2/src/platform/dom/dom_adapter'; |
2 | | -import {Injectable} from 'angular2/core'; |
3 | | -import {LocationStrategy, normalizeQueryParams} from './location_strategy'; |
4 | | -import {EventListener, History, Location} from 'angular2/src/facade/browser'; |
| 1 | +import {Injectable, Inject, Optional} from 'angular2/core'; |
| 2 | +import { |
| 3 | + LocationStrategy, |
| 4 | + joinWithSlash, |
| 5 | + APP_BASE_HREF, |
| 6 | + normalizeQueryParams |
| 7 | +} from './location_strategy'; |
| 8 | +import {EventListener} from 'angular2/src/facade/browser'; |
| 9 | +import {isPresent} from 'angular2/src/facade/lang'; |
| 10 | +import {PlatformLocation} from './platform_location'; |
5 | 11 |
|
6 | 12 | /** |
7 | 13 | * `HashLocationStrategy` is a {@link LocationStrategy} used to configure the |
@@ -43,48 +49,45 @@ import {EventListener, History, Location} from 'angular2/src/facade/browser'; |
43 | 49 | */ |
44 | 50 | @Injectable() |
45 | 51 | export class HashLocationStrategy extends LocationStrategy { |
46 | | - private _location: Location; |
47 | | - private _history: History; |
48 | | - |
49 | | - constructor() { |
| 52 | + private _baseHref: string = ''; |
| 53 | + constructor(private _platformLocation: PlatformLocation, |
| 54 | + @Optional() @Inject(APP_BASE_HREF) _baseHref?: string) { |
50 | 55 | super(); |
51 | | - this._location = DOM.getLocation(); |
52 | | - this._history = DOM.getHistory(); |
| 56 | + if (isPresent(_baseHref)) { |
| 57 | + this._baseHref = _baseHref; |
| 58 | + } |
53 | 59 | } |
54 | 60 |
|
55 | | - onPopState(fn: EventListener): void { |
56 | | - DOM.getGlobalEventTarget('window').addEventListener('popstate', fn, false); |
57 | | - } |
| 61 | + onPopState(fn: EventListener): void { this._platformLocation.onPopState(fn); } |
58 | 62 |
|
59 | | - getBaseHref(): string { return ''; } |
| 63 | + getBaseHref(): string { return this._baseHref; } |
60 | 64 |
|
61 | 65 | path(): string { |
62 | 66 | // the hash value is always prefixed with a `#` |
63 | 67 | // and if it is empty then it will stay empty |
64 | | - var path = this._location.hash; |
| 68 | + var path = this._platformLocation.hash; |
65 | 69 |
|
66 | 70 | // Dart will complain if a call to substring is |
67 | 71 | // executed with a position value that extends the |
68 | 72 | // length of string. |
69 | 73 | return (path.length > 0 ? path.substring(1) : path) + |
70 | | - normalizeQueryParams(this._location.search); |
| 74 | + normalizeQueryParams(this._platformLocation.search); |
71 | 75 | } |
72 | 76 |
|
73 | 77 | prepareExternalUrl(internal: string): string { |
74 | | - return internal.length > 0 ? ('#' + internal) : internal; |
| 78 | + var url = joinWithSlash(this._baseHref, internal); |
| 79 | + return url.length > 0 ? ('#' + url) : url; |
75 | 80 | } |
76 | 81 |
|
77 | 82 | pushState(state: any, title: string, path: string, queryParams: string) { |
78 | | - var url = path + normalizeQueryParams(queryParams); |
| 83 | + var url = this.prepareExternalUrl(path + normalizeQueryParams(queryParams)); |
79 | 84 | if (url.length == 0) { |
80 | | - url = this._location.pathname; |
81 | | - } else { |
82 | | - url = this.prepareExternalUrl(url); |
| 85 | + url = this._platformLocation.pathname; |
83 | 86 | } |
84 | | - this._history.pushState(state, title, url); |
| 87 | + this._platformLocation.pushState(state, title, url); |
85 | 88 | } |
86 | 89 |
|
87 | | - forward(): void { this._history.forward(); } |
| 90 | + forward(): void { this._platformLocation.forward(); } |
88 | 91 |
|
89 | | - back(): void { this._history.back(); } |
| 92 | + back(): void { this._platformLocation.back(); } |
90 | 93 | } |
0 commit comments