2016-06-23 16:47:54 +00: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 23:38:52 +00:00
|
|
|
import {Inject, Injectable, Optional} from '@angular/core';
|
|
|
|
|
|
2016-05-31 22:22:59 +00:00
|
|
|
import {BaseException} from '../facade/exceptions';
|
2016-06-08 23:38:52 +00:00
|
|
|
import {isBlank} from '../facade/lang';
|
2016-04-29 00:50:03 +00: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 07:31:20 +00:00
|
|
|
import {Location} from './location';
|
2016-06-08 23:38:52 +00:00
|
|
|
import {APP_BASE_HREF, LocationStrategy} from './location_strategy';
|
|
|
|
|
import {PlatformLocation, UrlChangeListener} from './platform_location';
|
|
|
|
|
|
2015-05-29 21:58:41 +00:00
|
|
|
|
2015-09-22 00:31:31 +00:00
|
|
|
/**
|
|
|
|
|
* `PathLocationStrategy` is a {@link LocationStrategy} used to configure the
|
|
|
|
|
* {@link Location} service to represent its state in the
|
|
|
|
|
* [path](https://en.wikipedia.org/wiki/Uniform_Resource_Locator#Syntax) of the
|
|
|
|
|
* browser's URL.
|
|
|
|
|
*
|
2015-10-05 01:11:31 +00:00
|
|
|
* `PathLocationStrategy` is the default binding for {@link LocationStrategy}
|
2015-10-11 05:11:13 +00:00
|
|
|
* provided in {@link ROUTER_PROVIDERS}.
|
2015-10-05 01:11:31 +00:00
|
|
|
*
|
2016-07-14 22:00:50 +00:00
|
|
|
* If you're using `PathLocationStrategy`, you must provide a {@link APP_BASE_HREF}
|
|
|
|
|
* or add a base element to the document. This URL prefix that will be preserved
|
|
|
|
|
* when generating and recognizing URLs.
|
2015-09-22 00:31:31 +00:00
|
|
|
*
|
|
|
|
|
* For instance, if you provide an `APP_BASE_HREF` of `'/my/app'` and call
|
|
|
|
|
* `location.go('/foo')`, the browser's URL will become
|
|
|
|
|
* `example.com/my/app/foo`.
|
|
|
|
|
*
|
2016-07-14 22:00:50 +00:00
|
|
|
* Similarly, if you add `<base href='/my/app'/>` to the document and call
|
|
|
|
|
* `location.go('/foo')`, the browser's URL will become
|
|
|
|
|
* `example.com/my/app/foo`.
|
2016-05-27 18:24:05 +00:00
|
|
|
*
|
|
|
|
|
* @stable
|
2015-09-22 00:31:31 +00:00
|
|
|
*/
|
2015-05-29 21:58:41 +00:00
|
|
|
@Injectable()
|
2015-08-24 19:13:51 +00:00
|
|
|
export class PathLocationStrategy extends LocationStrategy {
|
2015-05-29 21:58:41 +00:00
|
|
|
private _baseHref: string;
|
|
|
|
|
|
2016-06-08 23:38:52 +00:00
|
|
|
constructor(
|
|
|
|
|
private _platformLocation: PlatformLocation,
|
|
|
|
|
@Optional() @Inject(APP_BASE_HREF) href?: string) {
|
2015-06-22 19:14:19 +00:00
|
|
|
super();
|
2015-11-17 18:47:59 +00:00
|
|
|
|
|
|
|
|
if (isBlank(href)) {
|
2015-11-23 18:47:06 +00:00
|
|
|
href = this._platformLocation.getBaseHrefFromDOM();
|
2015-11-17 18:47:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isBlank(href)) {
|
|
|
|
|
throw new BaseException(
|
|
|
|
|
`No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document.`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this._baseHref = href;
|
2015-05-29 21:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
2016-01-21 17:58:28 +00:00
|
|
|
onPopState(fn: UrlChangeListener): void {
|
2015-11-23 18:47:06 +00:00
|
|
|
this._platformLocation.onPopState(fn);
|
|
|
|
|
this._platformLocation.onHashChange(fn);
|
2015-05-29 21:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getBaseHref(): string { return this._baseHref; }
|
|
|
|
|
|
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 07:31:20 +00:00
|
|
|
prepareExternalUrl(internal: string): string {
|
|
|
|
|
return Location.joinWithSlash(this._baseHref, internal);
|
|
|
|
|
}
|
2015-10-26 13:57:41 +00:00
|
|
|
|
2016-06-25 18:51:15 +00:00
|
|
|
path(includeHash: boolean = false): string {
|
|
|
|
|
const pathname = this._platformLocation.pathname +
|
2016-06-08 23:38:52 +00:00
|
|
|
Location.normalizeQueryParams(this._platformLocation.search);
|
2016-06-25 18:51:15 +00:00
|
|
|
const hash = this._platformLocation.hash;
|
|
|
|
|
return hash && includeHash ? `${pathname}${hash}` : pathname;
|
2015-11-23 18:47:06 +00:00
|
|
|
}
|
2015-05-29 21:58:41 +00:00
|
|
|
|
2015-09-23 07:10:26 +00:00
|
|
|
pushState(state: any, title: string, url: 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 07:31:20 +00:00
|
|
|
var externalUrl = this.prepareExternalUrl(url + Location.normalizeQueryParams(queryParams));
|
2015-11-23 18:47:06 +00:00
|
|
|
this._platformLocation.pushState(state, title, externalUrl);
|
2015-09-23 07:10:26 +00:00
|
|
|
}
|
2015-05-29 21:58:41 +00:00
|
|
|
|
2015-12-08 00:05:57 +00:00
|
|
|
replaceState(state: any, title: string, url: 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 07:31:20 +00:00
|
|
|
var externalUrl = this.prepareExternalUrl(url + Location.normalizeQueryParams(queryParams));
|
2015-12-08 00:05:57 +00:00
|
|
|
this._platformLocation.replaceState(state, title, externalUrl);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-23 18:47:06 +00:00
|
|
|
forward(): void { this._platformLocation.forward(); }
|
2015-05-29 21:58:41 +00:00
|
|
|
|
2015-11-23 18:47:06 +00:00
|
|
|
back(): void { this._platformLocation.back(); }
|
2015-05-29 21:58:41 +00:00
|
|
|
}
|