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