| 
									
										
										
										
											2015-11-19 15:09:34 -08:00
										 |  |  | import {DOM} from 'angular2/src/platform/dom/dom_adapter'; | 
					
						
							| 
									
										
										
										
											2015-11-23 10:18:04 -08:00
										 |  |  | import {Injectable} from 'angular2/core'; | 
					
						
							| 
									
										
										
										
											2015-09-23 00:10:26 -07:00
										 |  |  | import {LocationStrategy, normalizeQueryParams} from './location_strategy'; | 
					
						
							| 
									
										
										
										
											2015-11-06 17:34:07 -08:00
										 |  |  | import {EventListener, History, Location} from 'angular2/src/facade/browser'; | 
					
						
							| 
									
										
										
										
											2015-06-15 14:34:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 17:31:31 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * `HashLocationStrategy` is a {@link LocationStrategy} used to configure the | 
					
						
							|  |  |  |  * {@link Location} service to represent its state in the | 
					
						
							|  |  |  |  * [hash fragment](https://en.wikipedia.org/wiki/Uniform_Resource_Locator#Syntax)
 | 
					
						
							|  |  |  |  * of the browser's URL. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * For instance, if you call `location.go('/foo')`, the browser's URL will become | 
					
						
							|  |  |  |  * `example.com#/foo`. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-10-19 15:37:32 +01:00
										 |  |  |  * ### Example | 
					
						
							| 
									
										
										
										
											2015-09-21 17:31:31 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * ```
 | 
					
						
							| 
									
										
										
										
											2015-10-29 07:30:05 -05:00
										 |  |  |  * import {Component, provide} from 'angular2/angular2'; | 
					
						
							| 
									
										
										
										
											2015-09-21 17:31:31 -07:00
										 |  |  |  * import { | 
					
						
							|  |  |  |  *   ROUTER_DIRECTIVES, | 
					
						
							| 
									
										
										
										
											2015-10-10 22:11:13 -07:00
										 |  |  |  *   ROUTER_PROVIDERS, | 
					
						
							| 
									
										
										
										
											2015-09-21 17:31:31 -07:00
										 |  |  |  *   RouteConfig, | 
					
						
							| 
									
										
										
										
											2015-10-29 07:30:05 -05:00
										 |  |  |  *   Location, | 
					
						
							|  |  |  |  *   LocationStrategy, | 
					
						
							|  |  |  |  *   HashLocationStrategy | 
					
						
							| 
									
										
										
										
											2015-09-21 17:31:31 -07:00
										 |  |  |  * } from 'angular2/router'; | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-10-11 07:41:19 -07:00
										 |  |  |  * @Component({directives: [ROUTER_DIRECTIVES]}) | 
					
						
							| 
									
										
										
										
											2015-09-21 17:31:31 -07:00
										 |  |  |  * @RouteConfig([ | 
					
						
							|  |  |  |  *  {...}, | 
					
						
							|  |  |  |  * ]) | 
					
						
							|  |  |  |  * class AppCmp { | 
					
						
							|  |  |  |  *   constructor(location: Location) { | 
					
						
							|  |  |  |  *     location.go('/foo'); | 
					
						
							|  |  |  |  *   } | 
					
						
							|  |  |  |  * } | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-10-29 07:30:05 -05:00
										 |  |  |  * bootstrap(AppCmp, [ | 
					
						
							|  |  |  |  *   ROUTER_PROVIDERS, | 
					
						
							|  |  |  |  *   provide(LocationStrategy, {useClass: HashLocationStrategy}) | 
					
						
							|  |  |  |  * ]); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:31:31 -07:00
										 |  |  |  * ```
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-06-15 14:34:14 -07:00
										 |  |  | @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 ''; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-08 15:08:17 -07:00
										 |  |  |   path(): string { | 
					
						
							|  |  |  |     // the hash value is always prefixed with a `#`
 | 
					
						
							|  |  |  |     // and if it is empty then it will stay empty
 | 
					
						
							| 
									
										
										
										
											2015-07-09 11:05:07 -07:00
										 |  |  |     var path = this._location.hash; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Dart will complain if a call to substring is
 | 
					
						
							|  |  |  |     // executed with a position value that extends the
 | 
					
						
							|  |  |  |     // length of string.
 | 
					
						
							| 
									
										
										
										
											2015-09-23 00:10:26 -07:00
										 |  |  |     return (path.length > 0 ? path.substring(1) : path) + | 
					
						
							|  |  |  |            normalizeQueryParams(this._location.search); | 
					
						
							| 
									
										
										
										
											2015-07-08 15:08:17 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-06-15 14:34:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-26 13:57:41 +00:00
										 |  |  |   prepareExternalUrl(internal: string): string { | 
					
						
							|  |  |  |     return internal.length > 0 ? ('#' + internal) : internal; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-23 00:10:26 -07:00
										 |  |  |   pushState(state: any, title: string, path: string, queryParams: string) { | 
					
						
							|  |  |  |     var url = path + normalizeQueryParams(queryParams); | 
					
						
							|  |  |  |     if (url.length == 0) { | 
					
						
							|  |  |  |       url = this._location.pathname; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2015-10-26 13:57:41 +00:00
										 |  |  |       url = this.prepareExternalUrl(url); | 
					
						
							| 
									
										
										
										
											2015-09-23 00:10:26 -07:00
										 |  |  |     } | 
					
						
							|  |  |  |     this._history.pushState(state, title, url); | 
					
						
							| 
									
										
										
										
											2015-06-15 14:34:14 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   forward(): void { this._history.forward(); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   back(): void { this._history.back(); } | 
					
						
							|  |  |  | } |