2015-06-22 12:14:19 -07:00
|
|
|
import {LocationStrategy} from './location_strategy';
|
2015-06-15 15:41:09 -07:00
|
|
|
import {StringWrapper, isPresent, CONST_EXPR} from 'angular2/src/facade/lang';
|
2015-04-21 11:23:23 -07:00
|
|
|
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
|
2015-07-17 12:14:48 -07:00
|
|
|
import {BaseException, isBlank} from 'angular2/src/facade/lang';
|
2015-06-15 15:41:09 -07:00
|
|
|
import {OpaqueToken, Injectable, Optional, Inject} from 'angular2/di';
|
|
|
|
|
|
|
|
export const appBaseHrefToken: OpaqueToken = CONST_EXPR(new OpaqueToken('locationHrefToken'));
|
2015-04-21 11:23:23 -07:00
|
|
|
|
2015-06-22 12:14:19 -07:00
|
|
|
/**
|
|
|
|
* This is the service that an application developer will directly interact with.
|
|
|
|
*
|
|
|
|
* Responsible for normalizing the URL against the application's base href.
|
|
|
|
* 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-05-29 14:58:41 -07:00
|
|
|
@Injectable()
|
2015-04-21 11:23:23 -07:00
|
|
|
export class Location {
|
2015-06-29 10:37:55 +02:00
|
|
|
private _subject: EventEmitter = new EventEmitter();
|
2015-05-29 14:58:41 -07:00
|
|
|
private _baseHref: string;
|
|
|
|
|
2015-06-22 12:14:19 -07:00
|
|
|
constructor(public _platformStrategy: LocationStrategy,
|
2015-06-15 15:41:09 -07:00
|
|
|
@Optional() @Inject(appBaseHrefToken) href?: string) {
|
2015-07-17 12:14:48 -07:00
|
|
|
var browserBaseHref = isPresent(href) ? href : this._platformStrategy.getBaseHref();
|
|
|
|
|
|
|
|
if (isBlank(browserBaseHref)) {
|
|
|
|
throw new BaseException(
|
|
|
|
`No base href set. Either provide a binding to "appBaseHrefToken" or add a base element.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._baseHref = stripTrailingSlash(stripIndexHtml(browserBaseHref));
|
2015-06-22 12:14:19 -07:00
|
|
|
this._platformStrategy.onPopState((_) => this._onPopState(_));
|
2015-04-21 11:23:23 -07:00
|
|
|
}
|
|
|
|
|
2015-05-29 14:58:41 -07:00
|
|
|
_onPopState(_): void { ObservableWrapper.callNext(this._subject, {'url': this.path()}); }
|
2015-04-21 11:23:23 -07:00
|
|
|
|
2015-06-22 12:14:19 -07:00
|
|
|
path(): string { return this.normalize(this._platformStrategy.path()); }
|
2015-05-06 18:28:24 -07:00
|
|
|
|
2015-06-22 12:14:19 -07:00
|
|
|
normalize(url: string): string {
|
|
|
|
return stripTrailingSlash(this._stripBaseHref(stripIndexHtml(url)));
|
|
|
|
}
|
2015-05-06 18:28:24 -07:00
|
|
|
|
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-06-22 12:14:19 -07:00
|
|
|
return stripTrailingSlash(this._addBaseHref(url));
|
2015-05-12 16:18:58 -07:00
|
|
|
}
|
|
|
|
|
2015-05-14 15:24:35 +02:00
|
|
|
_stripBaseHref(url: string): string {
|
2015-06-22 12:14:19 -07:00
|
|
|
if (this._baseHref.length > 0 && url.startsWith(this._baseHref)) {
|
|
|
|
return url.substring(this._baseHref.length);
|
2015-05-06 18:28:24 -07:00
|
|
|
}
|
|
|
|
return url;
|
2015-04-21 11:23:23 -07:00
|
|
|
}
|
|
|
|
|
2015-05-14 15:24:35 +02:00
|
|
|
_addBaseHref(url: string): string {
|
2015-06-22 12:14:19 -07:00
|
|
|
if (!url.startsWith(this._baseHref)) {
|
2015-05-12 16:18:58 -07:00
|
|
|
return this._baseHref + url;
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2015-05-29 14:58:41 -07:00
|
|
|
go(url: string): void {
|
2015-05-12 16:18:58 -07:00
|
|
|
var finalUrl = this.normalizeAbsolutely(url);
|
2015-06-22 12:14:19 -07:00
|
|
|
this._platformStrategy.pushState(null, '', finalUrl);
|
2015-04-21 11:23:23 -07:00
|
|
|
}
|
|
|
|
|
2015-06-22 12:14:19 -07:00
|
|
|
forward(): void { this._platformStrategy.forward(); }
|
2015-04-21 11:23:23 -07:00
|
|
|
|
2015-06-22 12:14:19 -07:00
|
|
|
back(): void { this._platformStrategy.back(); }
|
2015-04-21 11:23:23 -07:00
|
|
|
|
2015-05-14 15:24:35 +02:00
|
|
|
subscribe(onNext, onThrow = null, onReturn = 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-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;
|
|
|
|
}
|