|
| 1 | +import {Injectable} from "angular2/core"; |
| 2 | +import {PlatformLocation, LocationStrategy, PathLocationStrategy} from "angular2/router"; |
| 3 | +import {services} from "../common/coreservices"; |
| 4 | +import {isDefined} from "../common/predicates"; |
| 5 | + |
| 6 | +@Injectable() |
| 7 | +export class UiLocationStrategy { |
| 8 | + private hashPrefix: string = '!'; |
| 9 | + |
| 10 | + constructor(private locationStrategy: LocationStrategy, private _platformLocation: PlatformLocation) { |
| 11 | + } |
| 12 | + |
| 13 | + init() { |
| 14 | + let loc = <any> services.location; |
| 15 | + |
| 16 | + loc.hash = () => this._platformLocation.hash; |
| 17 | + loc.path = this.locationStrategy.path; |
| 18 | + loc.search = () => location.search; |
| 19 | + loc.url = (url) => { |
| 20 | + if(url) { |
| 21 | + location.hash = url; |
| 22 | + } |
| 23 | + return loc.path(); |
| 24 | + }; |
| 25 | + loc.replace = this.locationStrategy.replaceState; |
| 26 | + // should we use location.onPopState instead ? https://github.com/angular/angular/blob/d272f96e23f379e1b565435b3af010138e710ab9/modules/angular2/src/router/location/hash_location_strategy.ts#L61 |
| 27 | + loc.onChange = this._platformLocation.onHashChange; |
| 28 | + |
| 29 | + let locCfg = <any> services.locationConfig; |
| 30 | + |
| 31 | + locCfg.port = () => location.port; |
| 32 | + locCfg.protocol = () => location.protocol; |
| 33 | + locCfg.host = () => location.host; |
| 34 | + locCfg.baseHref = this.locationStrategy.getBaseHref; |
| 35 | + locCfg.html5Mode = () => this.locationStrategy instanceof PathLocationStrategy; // does it work ? |
| 36 | + locCfg.hashPrefix = (newprefix: string): string => { |
| 37 | + if(isDefined(newprefix)) { |
| 38 | + this.hashPrefix = newprefix; |
| 39 | + } |
| 40 | + return this.hashPrefix; |
| 41 | + }; |
| 42 | + } |
| 43 | +} |
0 commit comments