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