2017-02-09 17:10:00 -05: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
|
|
|
|
*/
|
|
|
|
|
2017-02-10 19:29:30 -05:00
|
|
|
import {LocationChangeEvent, LocationChangeListener, PlatformLocation} from '@angular/common';
|
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {Subject} from 'rxjs/Subject';
|
2017-02-09 17:10:00 -05:00
|
|
|
import * as url from 'url';
|
|
|
|
|
|
|
|
import {scheduleMicroTask} from './facade/lang';
|
2017-02-10 19:29:30 -05:00
|
|
|
import {getDOM} from './private_import_platform-browser';
|
2017-02-09 17:10:00 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Server-side implementation of URL state. Implements `pathname`, `search`, and `hash`
|
|
|
|
* but not the state stack.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class ServerPlatformLocation implements PlatformLocation {
|
|
|
|
private _path: string = '/';
|
|
|
|
private _search: string = '';
|
|
|
|
private _hash: string = '';
|
|
|
|
private _hashUpdate = new Subject<LocationChangeEvent>();
|
|
|
|
|
2017-02-10 19:29:30 -05:00
|
|
|
getBaseHrefFromDOM(): string { return getDOM().getBaseHref(); }
|
|
|
|
|
2017-02-09 17:10:00 -05:00
|
|
|
onPopState(fn: LocationChangeListener): void {
|
|
|
|
// No-op: a state stack is not implemented, so
|
|
|
|
// no events will ever come.
|
|
|
|
}
|
2017-02-10 19:29:30 -05:00
|
|
|
|
|
|
|
onHashChange(fn: LocationChangeListener): void { this._hashUpdate.subscribe(fn); }
|
2017-02-09 17:10:00 -05:00
|
|
|
|
|
|
|
get pathname(): string { return this._path; }
|
|
|
|
get search(): string { return this._search; }
|
|
|
|
get hash(): string { return this._hash; }
|
|
|
|
|
2017-02-10 19:29:30 -05:00
|
|
|
get url(): string { return `${this.pathname}${this.search}${this.hash}`; }
|
2017-02-09 17:10:00 -05:00
|
|
|
|
|
|
|
private setHash(value: string, oldUrl: string) {
|
|
|
|
if (this._hash === value) {
|
|
|
|
// Don't fire events if the hash has not changed.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._hash = value;
|
|
|
|
const newUrl = this.url;
|
2017-02-10 19:29:30 -05:00
|
|
|
scheduleMicroTask(
|
|
|
|
() => this._hashUpdate.next({ type: 'hashchange', oldUrl, newUrl } as LocationChangeEvent));
|
2017-02-09 17:10:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
replaceState(state: any, title: string, newUrl: string): void {
|
|
|
|
const oldUrl = this.url;
|
|
|
|
const parsedUrl = url.parse(newUrl, true);
|
|
|
|
this._path = parsedUrl.path;
|
|
|
|
this._search = parsedUrl.search;
|
|
|
|
this.setHash(parsedUrl.hash, oldUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
pushState(state: any, title: string, newUrl: string): void {
|
|
|
|
this.replaceState(state, title, newUrl);
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:29:30 -05:00
|
|
|
forward(): void { throw new Error('Not implemented'); }
|
2017-02-09 17:10:00 -05:00
|
|
|
|
2017-02-10 19:29:30 -05:00
|
|
|
back(): void { throw new Error('Not implemented'); }
|
2017-02-09 17:10:00 -05:00
|
|
|
}
|