2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
|
*/
|
|
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
import {Inject, Injectable, Optional} from '@angular/core';
|
|
|
|
|
|
2016-05-31 15:22:59 -07:00
|
|
|
import {isPresent} from '../facade/lang';
|
2016-04-28 17:50:03 -07:00
|
|
|
|
2016-04-08 00:31:20 -07:00
|
|
|
import {Location} from './location';
|
2016-06-08 16:38:52 -07:00
|
|
|
import {APP_BASE_HREF, LocationStrategy} from './location_strategy';
|
2016-08-23 14:26:31 -07:00
|
|
|
import {LocationChangeListener, PlatformLocation} from './platform_location';
|
2016-06-08 16:38:52 -07:00
|
|
|
|
2016-04-28 17:50:03 -07:00
|
|
|
|
2015-06-15 14:34:14 -07:00
|
|
|
|
2015-09-21 17:31:31 -07:00
|
|
|
/**
|
2016-09-09 16:54:26 -07:00
|
|
|
* @whatItDoes Use URL hash for storing application location data.
|
|
|
|
|
* @description
|
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
|
|
|
*
|
2016-09-09 16:54:26 -07:00
|
|
|
* {@example common/location/ts/hash_location_component.ts region='LocationComponent'}
|
2016-05-27 11:24:05 -07:00
|
|
|
*
|
|
|
|
|
* @stable
|
2015-09-21 17:31:31 -07:00
|
|
|
*/
|
2015-06-15 14:34:14 -07:00
|
|
|
@Injectable()
|
|
|
|
|
export class HashLocationStrategy extends LocationStrategy {
|
2015-11-23 10:47:06 -08:00
|
|
|
private _baseHref: string = '';
|
2016-06-08 16:38:52 -07:00
|
|
|
constructor(
|
|
|
|
|
private _platformLocation: PlatformLocation,
|
|
|
|
|
@Optional() @Inject(APP_BASE_HREF) _baseHref?: string) {
|
2015-06-15 14:34:14 -07:00
|
|
|
super();
|
2015-11-23 10:47:06 -08:00
|
|
|
if (isPresent(_baseHref)) {
|
|
|
|
|
this._baseHref = _baseHref;
|
|
|
|
|
}
|
2015-06-15 14:34:14 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-23 14:26:31 -07:00
|
|
|
onPopState(fn: LocationChangeListener): void {
|
2015-12-07 16:05:57 -08:00
|
|
|
this._platformLocation.onPopState(fn);
|
|
|
|
|
this._platformLocation.onHashChange(fn);
|
|
|
|
|
}
|
2015-06-15 14:34:14 -07:00
|
|
|
|
2015-11-23 10:47:06 -08:00
|
|
|
getBaseHref(): string { return this._baseHref; }
|
2015-06-15 14:34:14 -07:00
|
|
|
|
2016-06-25 11:51:15 -07:00
|
|
|
path(includeHash: boolean = false): string {
|
2015-07-08 15:08:17 -07:00
|
|
|
// the hash value is always prefixed with a `#`
|
|
|
|
|
// and if it is empty then it will stay empty
|
2016-11-12 14:08:58 +01:00
|
|
|
let path = this._platformLocation.hash;
|
2016-02-25 16:18:55 -08:00
|
|
|
if (!isPresent(path)) path = '#';
|
2015-07-09 11:05:07 -07:00
|
|
|
|
2016-07-30 21:44:04 -07:00
|
|
|
return path.length > 0 ? path.substring(1) : path;
|
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 {
|
2016-11-12 14:08:58 +01:00
|
|
|
const url = Location.joinWithSlash(this._baseHref, internal);
|
2015-11-23 10:47:06 -08:00
|
|
|
return url.length > 0 ? ('#' + url) : url;
|
2015-10-26 13:57:41 +00:00
|
|
|
}
|
|
|
|
|
|
2015-09-23 00:10:26 -07:00
|
|
|
pushState(state: any, title: string, path: string, queryParams: string) {
|
2016-11-12 14:08:58 +01:00
|
|
|
let url = this.prepareExternalUrl(path + Location.normalizeQueryParams(queryParams));
|
2015-09-23 00:10:26 -07:00
|
|
|
if (url.length == 0) {
|
2015-11-23 10:47:06 -08:00
|
|
|
url = this._platformLocation.pathname;
|
2015-09-23 00:10:26 -07:00
|
|
|
}
|
2015-11-23 10:47:06 -08:00
|
|
|
this._platformLocation.pushState(state, title, url);
|
2015-06-15 14:34:14 -07:00
|
|
|
}
|
|
|
|
|
|
2015-12-07 16:05:57 -08:00
|
|
|
replaceState(state: any, title: string, path: string, queryParams: string) {
|
2016-11-12 14:08:58 +01:00
|
|
|
let url = this.prepareExternalUrl(path + Location.normalizeQueryParams(queryParams));
|
2015-12-07 16:05:57 -08:00
|
|
|
if (url.length == 0) {
|
|
|
|
|
url = this._platformLocation.pathname;
|
|
|
|
|
}
|
|
|
|
|
this._platformLocation.replaceState(state, title, url);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-23 10:47:06 -08:00
|
|
|
forward(): void { this._platformLocation.forward(); }
|
2015-06-15 14:34:14 -07:00
|
|
|
|
2015-11-23 10:47:06 -08:00
|
|
|
back(): void { this._platformLocation.back(); }
|
2015-06-15 14:34:14 -07:00
|
|
|
}
|