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';
|
2015-06-22 15:14:19 -04:00
|
|
|
import {LocationStrategy} from 'angular2/src/router/location_strategy';
|
2015-06-15 18:41:09 -04:00
|
|
|
|
2015-06-22 15:14:19 -04:00
|
|
|
|
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-11-16 02:58:59 -05:00
|
|
|
ObservableWrapper.callEmit(this._subject, null);
|
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-06-15 18:41:09 -04:00
|
|
|
simulateUrlPop(pathname: string): void {
|
2015-11-16 02:58:59 -05:00
|
|
|
ObservableWrapper.callEmit(this._subject, {'url': pathname});
|
2015-06-15 18:41:09 -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
|
|
|
|
|
|
|
var external = this.prepareExternalUrl(url);
|
|
|
|
this.urlChanges.push(external);
|
2015-06-15 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|