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
|
|
|
|
refactor(Location): out of router and into platform/common
closes https://github.com/angular/angular/issues/4943
BREAKING CHANGE:
`Location` and other related providers have been moved out of `router` and into `platform/common`. `BrowserPlatformLocation` is not meant to be used directly however advanced configurations may use it via the following import change.
Before:
```
import {
PlatformLocation,
Location,
LocationStrategy,
HashLocationStrategy,
PathLocationStrategy,
APP_BASE_HREF}
from 'angular2/router';
import {BrowserPlatformLocation} from 'angular2/src/router/location/browser_platform_location';
```
After:
```
import {
PlatformLocation,
Location,
LocationStrategy,
HashLocationStrategy,
PathLocationStrategy,
APP_BASE_HREF}
from 'angular2/platform/common';
import {BrowserPlatformLocation} from 'angular2/src/platform/browser/location/browser_platform_location';
```
Closes #7962
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';
|
|
|
|
|
import {PlatformLocation, UrlChangeListener} from './platform_location';
|
|
|
|
|
|
2016-04-28 17:50:03 -07:00
|
|
|
|
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
|
|
|
*
|
|
|
|
|
* ```
|
2016-04-28 17:50:03 -07:00
|
|
|
* import {Component, provide} from '@angular/core';
|
2015-09-21 17:31:31 -07:00
|
|
|
* import {
|
2015-10-29 07:30:05 -05:00
|
|
|
* Location,
|
|
|
|
|
* LocationStrategy,
|
|
|
|
|
* HashLocationStrategy
|
2016-04-28 17:50:03 -07:00
|
|
|
* } from '@angular/common';
|
refactor(Location): out of router and into platform/common
closes https://github.com/angular/angular/issues/4943
BREAKING CHANGE:
`Location` and other related providers have been moved out of `router` and into `platform/common`. `BrowserPlatformLocation` is not meant to be used directly however advanced configurations may use it via the following import change.
Before:
```
import {
PlatformLocation,
Location,
LocationStrategy,
HashLocationStrategy,
PathLocationStrategy,
APP_BASE_HREF}
from 'angular2/router';
import {BrowserPlatformLocation} from 'angular2/src/router/location/browser_platform_location';
```
After:
```
import {
PlatformLocation,
Location,
LocationStrategy,
HashLocationStrategy,
PathLocationStrategy,
APP_BASE_HREF}
from 'angular2/platform/common';
import {BrowserPlatformLocation} from 'angular2/src/platform/browser/location/browser_platform_location';
```
Closes #7962
2016-04-08 00:31:20 -07:00
|
|
|
* import {
|
|
|
|
|
* ROUTER_DIRECTIVES,
|
|
|
|
|
* ROUTER_PROVIDERS,
|
|
|
|
|
* RouteConfig
|
2016-04-28 17:50:03 -07:00
|
|
|
* } from '@angular/router';
|
2015-09-21 17:31:31 -07:00
|
|
|
*
|
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,
|
2016-06-02 17:30:40 -07:00
|
|
|
* {provide: LocationStrategy, useClass: HashLocationStrategy}
|
2015-10-29 07:30:05 -05:00
|
|
|
* ]);
|
2015-09-21 17:31:31 -07:00
|
|
|
* ```
|
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-01-21 09:58:28 -08:00
|
|
|
onPopState(fn: UrlChangeListener): 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
|
|
|
|
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-11-23 10:47:06 -08:00
|
|
|
var path = this._platformLocation.hash;
|
2016-02-25 16:18:55 -08:00
|
|
|
if (!isPresent(path)) path = '#';
|
2015-07-09 11:05:07 -07:00
|
|
|
|
|
|
|
|
// Dart will complain if a call to substring is
|
|
|
|
|
// executed with a position value that extends the
|
|
|
|
|
// length of string.
|
2016-02-25 16:18:55 -08: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 {
|
refactor(Location): out of router and into platform/common
closes https://github.com/angular/angular/issues/4943
BREAKING CHANGE:
`Location` and other related providers have been moved out of `router` and into `platform/common`. `BrowserPlatformLocation` is not meant to be used directly however advanced configurations may use it via the following import change.
Before:
```
import {
PlatformLocation,
Location,
LocationStrategy,
HashLocationStrategy,
PathLocationStrategy,
APP_BASE_HREF}
from 'angular2/router';
import {BrowserPlatformLocation} from 'angular2/src/router/location/browser_platform_location';
```
After:
```
import {
PlatformLocation,
Location,
LocationStrategy,
HashLocationStrategy,
PathLocationStrategy,
APP_BASE_HREF}
from 'angular2/platform/common';
import {BrowserPlatformLocation} from 'angular2/src/platform/browser/location/browser_platform_location';
```
Closes #7962
2016-04-08 00:31:20 -07:00
|
|
|
var 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) {
|
refactor(Location): out of router and into platform/common
closes https://github.com/angular/angular/issues/4943
BREAKING CHANGE:
`Location` and other related providers have been moved out of `router` and into `platform/common`. `BrowserPlatformLocation` is not meant to be used directly however advanced configurations may use it via the following import change.
Before:
```
import {
PlatformLocation,
Location,
LocationStrategy,
HashLocationStrategy,
PathLocationStrategy,
APP_BASE_HREF}
from 'angular2/router';
import {BrowserPlatformLocation} from 'angular2/src/router/location/browser_platform_location';
```
After:
```
import {
PlatformLocation,
Location,
LocationStrategy,
HashLocationStrategy,
PathLocationStrategy,
APP_BASE_HREF}
from 'angular2/platform/common';
import {BrowserPlatformLocation} from 'angular2/src/platform/browser/location/browser_platform_location';
```
Closes #7962
2016-04-08 00:31:20 -07:00
|
|
|
var 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) {
|
refactor(Location): out of router and into platform/common
closes https://github.com/angular/angular/issues/4943
BREAKING CHANGE:
`Location` and other related providers have been moved out of `router` and into `platform/common`. `BrowserPlatformLocation` is not meant to be used directly however advanced configurations may use it via the following import change.
Before:
```
import {
PlatformLocation,
Location,
LocationStrategy,
HashLocationStrategy,
PathLocationStrategy,
APP_BASE_HREF}
from 'angular2/router';
import {BrowserPlatformLocation} from 'angular2/src/router/location/browser_platform_location';
```
After:
```
import {
PlatformLocation,
Location,
LocationStrategy,
HashLocationStrategy,
PathLocationStrategy,
APP_BASE_HREF}
from 'angular2/platform/common';
import {BrowserPlatformLocation} from 'angular2/src/platform/browser/location/browser_platform_location';
```
Closes #7962
2016-04-08 00:31:20 -07:00
|
|
|
var 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
|
|
|
}
|