2015-12-03 12:06:42 -05:00
|
|
|
import {Injectable} from 'angular2/src/core/di';
|
2015-11-06 20:34:07 -05:00
|
|
|
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
|
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 03:31:20 -04:00
|
|
|
import {LocationStrategy} from 'angular2/platform/common';
|
2015-06-15 18:41:09 -04:00
|
|
|
|
2015-06-22 15:14:19 -04:00
|
|
|
|
2015-12-03 18:49:09 -05:00
|
|
|
/**
|
|
|
|
* A mock implementation of {@link LocationStrategy} that allows tests to fire simulated
|
|
|
|
* location events.
|
|
|
|
*/
|
2015-12-03 12:06:42 -05:00
|
|
|
@Injectable()
|
2015-06-22 15:14:19 -04:00
|
|
|
export class MockLocationStrategy extends LocationStrategy {
|
2015-06-15 18:41:09 -04:00
|
|
|
internalBaseHref: string = '/';
|
|
|
|
internalPath: string = '/';
|
|
|
|
internalTitle: string = '';
|
2015-08-28 14:29:19 -04:00
|
|
|
urlChanges: string[] = [];
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-10-24 21:48:43 -04:00
|
|
|
_subject: EventEmitter<any> = new EventEmitter();
|
2015-06-15 18:41:09 -04:00
|
|
|
constructor() { super(); }
|
|
|
|
|
2015-07-07 23:03:00 -04:00
|
|
|
simulatePopState(url: string): void {
|
2015-06-15 18:41:09 -04:00
|
|
|
this.internalPath = url;
|
2015-12-15 12:42:27 -05:00
|
|
|
ObservableWrapper.callEmit(this._subject, new _MockPopStateEvent(this.path()));
|
2015-06-15 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
path(): string { return this.internalPath; }
|
|
|
|
|
2015-11-17 13:47:59 -05:00
|
|
|
prepareExternalUrl(internal: string): string {
|
|
|
|
if (internal.startsWith('/') && this.internalBaseHref.endsWith('/')) {
|
|
|
|
return this.internalBaseHref + internal.substring(1);
|
|
|
|
}
|
|
|
|
return this.internalBaseHref + internal;
|
|
|
|
}
|
2015-10-26 09:57:41 -04:00
|
|
|
|
2015-09-23 03:10:26 -04:00
|
|
|
pushState(ctx: any, title: string, path: string, query: string): void {
|
2015-06-15 18:41:09 -04:00
|
|
|
this.internalTitle = title;
|
2015-09-23 03:10:26 -04:00
|
|
|
|
|
|
|
var url = path + (query.length > 0 ? ('?' + query) : '');
|
2015-06-15 18:41:09 -04:00
|
|
|
this.internalPath = url;
|
2015-11-17 13:47:59 -05:00
|
|
|
|
2015-07-15 20:18:06 -04:00
|
|
|
var externalUrl = this.prepareExternalUrl(url);
|
|
|
|
this.urlChanges.push(externalUrl);
|
2015-06-15 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2015-12-07 19:05:57 -05:00
|
|
|
replaceState(ctx: any, title: string, path: string, query: string): void {
|
|
|
|
this.internalTitle = title;
|
|
|
|
|
|
|
|
var url = path + (query.length > 0 ? ('?' + query) : '');
|
|
|
|
this.internalPath = url;
|
|
|
|
|
|
|
|
var externalUrl = this.prepareExternalUrl(url);
|
|
|
|
this.urlChanges.push('replace: ' + externalUrl);
|
|
|
|
}
|
|
|
|
|
2015-07-07 23:03:00 -04:00
|
|
|
onPopState(fn: (value: any) => void): void { ObservableWrapper.subscribe(this._subject, fn); }
|
2015-06-15 18:41:09 -04:00
|
|
|
|
|
|
|
getBaseHref(): string { return this.internalBaseHref; }
|
2015-07-28 23:19:56 -04:00
|
|
|
|
|
|
|
back(): void {
|
|
|
|
if (this.urlChanges.length > 0) {
|
|
|
|
this.urlChanges.pop();
|
|
|
|
var nextUrl = this.urlChanges.length > 0 ? this.urlChanges[this.urlChanges.length - 1] : '';
|
|
|
|
this.simulatePopState(nextUrl);
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 17:48:17 -04:00
|
|
|
|
|
|
|
forward(): void { throw 'not implemented'; }
|
2015-06-15 18:41:09 -04:00
|
|
|
}
|
2015-12-07 19:05:57 -05:00
|
|
|
|
2015-12-15 12:42:27 -05:00
|
|
|
class _MockPopStateEvent {
|
2015-12-07 19:05:57 -05:00
|
|
|
pop: boolean = true;
|
|
|
|
type: string = 'popstate';
|
|
|
|
constructor(public newUrl: string) {}
|
|
|
|
}
|