2015-06-22 12:14:19 -07:00
|
|
|
import {LocationStrategy} from './location_strategy';
|
2015-08-20 14:28:25 -07:00
|
|
|
import {StringWrapper, isPresent, CONST_EXPR} from 'angular2/src/core/facade/lang';
|
|
|
|
import {EventEmitter, ObservableWrapper} from 'angular2/src/core/facade/async';
|
2015-09-10 15:25:36 -07:00
|
|
|
import {isBlank} from 'angular2/src/core/facade/lang';
|
|
|
|
import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions';
|
2015-10-11 13:17:06 +02:00
|
|
|
import {OpaqueToken, Injectable, Optional, Inject} from 'angular2/angular2';
|
2015-06-15 15:41:09 -07:00
|
|
|
|
2015-09-21 17:31:31 -07:00
|
|
|
/**
|
|
|
|
* The `APP_BASE_HREF` token represents the base href to be used with the
|
|
|
|
* {@link PathLocationStrategy}.
|
|
|
|
*
|
2015-10-10 22:11:13 -07:00
|
|
|
* If you're using {@link PathLocationStrategy}, you must provide a provider to a string
|
2015-09-21 17:31:31 -07:00
|
|
|
* representing the URL prefix that should be preserved when generating and recognizing
|
|
|
|
* URLs.
|
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```
|
2015-10-11 07:41:19 -07:00
|
|
|
* import {Component} from 'angular2/angular2';
|
2015-10-10 22:11:13 -07:00
|
|
|
* import {ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig} from 'angular2/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 {
|
|
|
|
* // ...
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* bootstrap(AppCmp, [
|
2015-10-10 22:11:13 -07:00
|
|
|
* ROUTER_PROVIDERS,
|
2015-09-21 17:31:31 -07:00
|
|
|
* PathLocationStrategy,
|
2015-10-12 11:30:34 -07:00
|
|
|
* provide(APP_BASE_HREF, {useValue: '/my/app'})
|
2015-09-21 17:31:31 -07:00
|
|
|
* ]);
|
|
|
|
* ```
|
|
|
|
*/
|
2015-08-10 21:42:47 -07:00
|
|
|
export const APP_BASE_HREF: OpaqueToken = CONST_EXPR(new OpaqueToken('appBaseHref'));
|
2015-04-21 11:23:23 -07:00
|
|
|
|
2015-06-22 12:14:19 -07:00
|
|
|
/**
|
2015-09-21 17:31:31 -07:00
|
|
|
* `Location` is a service that applications can use to interact with a browser's URL.
|
|
|
|
* Depending on which {@link LocationStrategy} is used, `Location` will either persist
|
|
|
|
* to the URL's path or the URL's hash segment.
|
|
|
|
*
|
|
|
|
* Note: it's better to use {@link Router#navigate} service to trigger route changes. Use
|
|
|
|
* `Location` only if you need to interact with or create normalized URLs outside of
|
|
|
|
* routing.
|
2015-06-22 12:14:19 -07:00
|
|
|
*
|
2015-09-21 17:31:31 -07:00
|
|
|
* `Location` is responsible for normalizing the URL against the application's base href.
|
2015-06-22 12:14:19 -07:00
|
|
|
* A normalized URL is absolute from the URL host, includes the application's base href, and has no
|
|
|
|
* trailing slash:
|
|
|
|
* - `/my/app/user/123` is normalized
|
|
|
|
* - `my/app/user/123` **is not** normalized
|
|
|
|
* - `/my/app/user/123/` **is not** normalized
|
2015-09-21 17:31:31 -07:00
|
|
|
*
|
|
|
|
* ## Example
|
|
|
|
*
|
|
|
|
* ```
|
2015-10-11 07:41:19 -07:00
|
|
|
* import {Component} 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,
|
|
|
|
* Location
|
|
|
|
* } 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-10 22:11:13 -07:00
|
|
|
* bootstrap(AppCmp, [ROUTER_PROVIDERS]);
|
2015-09-21 17:31:31 -07:00
|
|
|
* ```
|
2015-06-22 12:14:19 -07:00
|
|
|
*/
|
2015-05-29 14:58:41 -07:00
|
|
|
@Injectable()
|
2015-04-21 11:23:23 -07:00
|
|
|
export class Location {
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-08-26 11:41:41 -07:00
|
|
|
_subject: EventEmitter = new EventEmitter();
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-08-26 11:41:41 -07:00
|
|
|
_baseHref: string;
|
2015-05-29 14:58:41 -07:00
|
|
|
|
2015-08-26 11:41:41 -07:00
|
|
|
constructor(public platformStrategy: LocationStrategy,
|
2015-08-10 21:42:47 -07:00
|
|
|
@Optional() @Inject(APP_BASE_HREF) href?: string) {
|
2015-08-26 11:41:41 -07:00
|
|
|
var browserBaseHref = isPresent(href) ? href : this.platformStrategy.getBaseHref();
|
2015-07-17 12:14:48 -07:00
|
|
|
|
|
|
|
if (isBlank(browserBaseHref)) {
|
|
|
|
throw new BaseException(
|
2015-10-10 22:11:13 -07:00
|
|
|
`No base href set. Either provide a provider for the APP_BASE_HREF token or add a base element to the document.`);
|
2015-07-17 12:14:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this._baseHref = stripTrailingSlash(stripIndexHtml(browserBaseHref));
|
2015-08-26 11:41:41 -07:00
|
|
|
this.platformStrategy.onPopState(
|
|
|
|
(_) => { ObservableWrapper.callNext(this._subject, {'url': this.path(), 'pop': true}); });
|
2015-04-21 11:23:23 -07:00
|
|
|
}
|
|
|
|
|
2015-09-21 17:31:31 -07:00
|
|
|
/**
|
|
|
|
* Returns the normalized URL path.
|
|
|
|
*/
|
2015-08-26 11:41:41 -07:00
|
|
|
path(): string { return this.normalize(this.platformStrategy.path()); }
|
2015-05-06 18:28:24 -07:00
|
|
|
|
2015-09-21 17:31:31 -07:00
|
|
|
/**
|
|
|
|
* Given a string representing a URL, returns the normalized URL path.
|
|
|
|
*/
|
2015-06-22 12:14:19 -07:00
|
|
|
normalize(url: string): string {
|
2015-08-26 11:41:41 -07:00
|
|
|
return stripTrailingSlash(_stripBaseHref(this._baseHref, stripIndexHtml(url)));
|
2015-06-22 12:14:19 -07:00
|
|
|
}
|
2015-05-06 18:28:24 -07:00
|
|
|
|
2015-09-21 17:31:31 -07:00
|
|
|
/**
|
|
|
|
* Given a string representing a URL, returns the normalized URL path.
|
|
|
|
* If the given URL doesn't begin with a leading slash (`'/'`), this method adds one
|
|
|
|
* before normalizing.
|
|
|
|
*/
|
2015-05-14 15:24:35 +02:00
|
|
|
normalizeAbsolutely(url: string): string {
|
2015-06-22 12:14:19 -07:00
|
|
|
if (!url.startsWith('/')) {
|
2015-05-12 16:18:58 -07:00
|
|
|
url = '/' + url;
|
|
|
|
}
|
2015-08-26 11:41:41 -07:00
|
|
|
return stripTrailingSlash(_addBaseHref(this._baseHref, url));
|
2015-05-12 16:18:58 -07:00
|
|
|
}
|
|
|
|
|
2015-09-21 17:31:31 -07:00
|
|
|
/**
|
|
|
|
* Changes the browsers URL to the normalized version of the given URL, and pushes a
|
|
|
|
* new item onto the platform's history.
|
|
|
|
*/
|
2015-09-23 00:10:26 -07:00
|
|
|
go(path: string, query: string = ''): void {
|
|
|
|
var absolutePath = this.normalizeAbsolutely(path);
|
|
|
|
this.platformStrategy.pushState(null, '', absolutePath, query);
|
2015-04-21 11:23:23 -07:00
|
|
|
}
|
|
|
|
|
2015-09-21 17:31:31 -07:00
|
|
|
/**
|
|
|
|
* Navigates forward in the platform's history.
|
|
|
|
*/
|
2015-08-26 11:41:41 -07:00
|
|
|
forward(): void { this.platformStrategy.forward(); }
|
2015-04-21 11:23:23 -07:00
|
|
|
|
2015-09-21 17:31:31 -07:00
|
|
|
/**
|
|
|
|
* Navigates back in the platform's history.
|
|
|
|
*/
|
2015-08-26 11:41:41 -07:00
|
|
|
back(): void { this.platformStrategy.back(); }
|
2015-04-21 11:23:23 -07:00
|
|
|
|
2015-09-21 17:31:31 -07:00
|
|
|
/**
|
|
|
|
* Subscribe to the platform's `popState` events.
|
|
|
|
*/
|
2015-07-07 20:03:00 -07:00
|
|
|
subscribe(onNext: (value: any) => void, onThrow: (exception: any) => void = null,
|
|
|
|
onReturn: () => void = null): void {
|
2015-04-21 11:23:23 -07:00
|
|
|
ObservableWrapper.subscribe(this._subject, onNext, onThrow, onReturn);
|
|
|
|
}
|
|
|
|
}
|
2015-05-06 18:28:24 -07:00
|
|
|
|
2015-08-26 11:41:41 -07:00
|
|
|
function _stripBaseHref(baseHref: string, url: string): string {
|
|
|
|
if (baseHref.length > 0 && url.startsWith(baseHref)) {
|
|
|
|
return url.substring(baseHref.length);
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
2015-05-06 18:28:24 -07:00
|
|
|
|
2015-08-26 11:41:41 -07:00
|
|
|
function _addBaseHref(baseHref: string, url: string): string {
|
|
|
|
if (!url.startsWith(baseHref)) {
|
|
|
|
return baseHref + url;
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
2015-05-06 18:28:24 -07:00
|
|
|
|
2015-05-14 15:24:35 +02:00
|
|
|
function stripIndexHtml(url: string): string {
|
2015-06-22 12:14:19 -07:00
|
|
|
if (/\/index.html$/g.test(url)) {
|
|
|
|
// '/index.html'.length == 11
|
|
|
|
return url.substring(0, url.length - 11);
|
2015-05-06 18:28:24 -07:00
|
|
|
}
|
2015-06-22 12:14:19 -07:00
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
function stripTrailingSlash(url: string): string {
|
|
|
|
if (/\/$/g.test(url)) {
|
|
|
|
url = url.substring(0, url.length - 1);
|
2015-06-12 12:57:35 -07:00
|
|
|
}
|
2015-05-06 18:28:24 -07:00
|
|
|
return url;
|
|
|
|
}
|