Repository navigation
Expand file tree
/
Copy pathlocation.js
More file actions
77 lines (63 loc) · 1.97 KB
/
Copy pathlocation.js
File metadata and controls
77 lines (63 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {BrowserLocation} from './browser_location';
import {StringWrapper} from 'angular2/src/facade/lang';
import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async';
export class Location {
_subject:EventEmitter;
_browserLocation:BrowserLocation;
_baseHref:string;
constructor(browserLocation:BrowserLocation) {
this._subject = new EventEmitter();
this._browserLocation = browserLocation;
this._baseHref = stripIndexHtml(this._browserLocation.getBaseHref());
this._browserLocation.onPopState((_) => this._onPopState(_));
}
_onPopState(_): void {
ObservableWrapper.callNext(this._subject, {
'url': this.path()
});
}
path(): string {
return this.normalize(this._browserLocation.path());
}
normalize(url: string): string {
return this._stripBaseHref(stripIndexHtml(url));
}
normalizeAbsolutely(url: string): string {
if (url[0] != '/') {
url = '/' + url;
}
return this._addBaseHref(url);
}
_stripBaseHref(url: string): string {
if (this._baseHref.length > 0 && StringWrapper.startsWith(url, this._baseHref)) {
return StringWrapper.substring(url, this._baseHref.length);
}
return url;
}
_addBaseHref(url: string): string {
if (!StringWrapper.startsWith(url, this._baseHref)) {
return this._baseHref + url;
}
return url;
}
go(url:string): void {
var finalUrl = this.normalizeAbsolutely(url);
this._browserLocation.pushState(null, '', finalUrl);
}
forward(): void {
this._browserLocation.forward();
}
back(): void {
this._browserLocation.back();
}
subscribe(onNext, onThrow = null, onReturn = null): void {
ObservableWrapper.subscribe(this._subject, onNext, onThrow, onReturn);
}
}
function stripIndexHtml(url: string): string {
// '/index.html'.length == 11
if (url.length > 10 && StringWrapper.substring(url, url.length - 11) == '/index.html') {
return StringWrapper.substring(url, 0, url.length - 11);
}
return url;
}