forked from ui-router/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locationService.ts
32 lines (27 loc) · 1.02 KB
/
locationService.ts
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
/** @module ng2 */
/** */
import { UIRouter, BaseLocationServices, parseUrl } from "ui-router-core";
import { LocationStrategy } from "@angular/common";
/** A `LocationServices` that uses the browser hash "#" to get/set the current location */
export class Ng2LocationServices extends BaseLocationServices {
constructor(router: UIRouter, private _locationStrategy: LocationStrategy) {
super(router, true);
this._locationStrategy.onPopState(this._listener);
}
_get() {
return this._locationStrategy.path(true)
.replace(this._locationStrategy.getBaseHref().replace(/\/$/, ''), '');
}
_set(state: any, title: string, url: string, replace: boolean): any {
let { path, search, hash } = parseUrl(url);
let urlWithHash = path + (hash ? "#" + hash : "");
if (replace) {
this._locationStrategy.replaceState(state, title, urlWithHash, search);
} else {
this._locationStrategy.pushState(state, title, urlWithHash, search);
}
}
dispose(router: UIRouter) {
super.dispose(router);
}
}