-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ng2.urlRouter): HTML5 PushState support
- Wire up HTML5 PushState and HashBang modes using Hash and PathLocationStrategy from angular2 rc.0+ Closes #2688
- Loading branch information
1 parent
79d4fd7
commit 9842fb7
Showing
5 changed files
with
90 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import {HashLocationStrategy, PlatformLocation, LocationStrategy} from "@angular/common"; | ||
import {Injectable} from "@angular/core"; | ||
|
||
import {services} from "../common/coreservices"; | ||
import {isDefined} from "../common/predicates"; | ||
import {applyPairs} from "../common/common"; | ||
import {beforeAfterSubstr} from "../common/strings"; | ||
|
||
const splitOnHash = beforeAfterSubstr("#"); | ||
const splitOnEquals = beforeAfterSubstr("="); | ||
const splitOnQuestionMark = beforeAfterSubstr("?"); | ||
|
||
@Injectable() | ||
export class UIRouterLocation { | ||
isHashBang: boolean; | ||
hashPrefix: string = ""; | ||
|
||
constructor( | ||
public locationStrategy: LocationStrategy, | ||
public platformLocation: PlatformLocation | ||
) { | ||
this.isHashBang = locationStrategy instanceof HashLocationStrategy; | ||
} | ||
|
||
init() { | ||
let loc = <any> services.location; | ||
let locSt = this.locationStrategy; | ||
|
||
if (this.isHashBang) { | ||
loc.hash = () => | ||
splitOnHash(splitOnHash(this.platformLocation.hash)[1])[1]; | ||
} else { | ||
loc.hash = () => | ||
splitOnHash(this.platformLocation.hash)[1]; | ||
} | ||
|
||
loc.path = () => | ||
splitOnHash(splitOnQuestionMark(locSt.path())[0])[0]; | ||
|
||
loc.search = () => { | ||
let queryString = splitOnHash(splitOnQuestionMark(locSt.path())[1])[0]; | ||
return queryString.split("&").map(kv => splitOnEquals(kv)).reduce(applyPairs, {}); | ||
}; | ||
|
||
loc.url = (url) => { | ||
if(isDefined(url)) { | ||
let split = splitOnQuestionMark(url); | ||
locSt.pushState(null, null, split[0], split[1]); | ||
} | ||
return locSt.path() | ||
}; | ||
|
||
loc.replace = () => { | ||
console.log(new Error('$location.replace() not impl')) | ||
}; | ||
|
||
loc.onChange = cb => locSt.onPopState(cb); | ||
|
||
let locCfg = <any> services.locationConfig; | ||
|
||
locCfg.port = () => null; | ||
locCfg.protocol = () => null; | ||
locCfg.host = () => null; | ||
locCfg.baseHref = () => locSt.getBaseHref(); | ||
locCfg.html5Mode = () => !this.isHashBang; | ||
locCfg.hashPrefix = (newprefix: string): string => { | ||
if(isDefined(newprefix)) { | ||
this.hashPrefix = newprefix; | ||
} | ||
return this.hashPrefix; | ||
}; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters