2016-06-23 12:47:54 -04: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
|
|
|
|
*/
|
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {Injectable} from '@angular/core';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {LocationStrategy} from '../index';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {EventEmitter, ObservableWrapper} from '../src/facade/async';
|
|
|
|
|
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.
|
2016-07-20 20:51:21 -04:00
|
|
|
*
|
|
|
|
* @stable
|
2015-12-03 18:49:09 -05: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-12-15 12:42:27 -05:00
|
|
|
ObservableWrapper.callEmit(this._subject, new _MockPopStateEvent(this.path()));
|
2015-06-15 18:41:09 -04:00
|
|
|
}
|
|
|
|
|
2016-06-25 14:51:15 -04:00
|
|
|
path(includeHash: boolean = false): string { return this.internalPath; }
|
2015-06-15 18:41:09 -04:00
|
|
|
|
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) {}
|
|
|
|
}
|