1
+ import { DOM } from "angular2/src/platform/dom/dom_adapter" ;
1
2
import { Injectable } from "angular2/core" ;
2
- import { PlatformLocation , LocationStrategy , PathLocationStrategy } from "angular2/router" ;
3
3
import { services } from "../common/coreservices" ;
4
4
import { isDefined } from "../common/predicates" ;
5
5
6
+ const beforeAfterSubstr = char => str => {
7
+ if ( ! str ) return [ "" , "" ] ;
8
+ let idx = str . indexOf ( char ) ;
9
+ if ( idx === - 1 ) return [ str , "" ] ;
10
+ return [ str . substr ( 0 , idx ) , str . substr ( idx + 1 ) ] ;
11
+ } ;
12
+
13
+ const splitHash = beforeAfterSubstr ( "#" ) ;
14
+ const trimHashVal = ( str ) => str ? str . replace ( / ^ # / , "" ) : "" ;
15
+
16
+
17
+ export abstract class UrlStrategy {
18
+ abstract init ( ) ;
19
+ }
20
+
6
21
@Injectable ( )
7
- export class UiLocationStrategy {
22
+ export class HashUrlStrategy extends UrlStrategy {
8
23
private hashPrefix : string = '!' ;
9
24
10
- constructor ( private locationStrategy : LocationStrategy , private _platformLocation : PlatformLocation ) {
11
- }
12
-
13
25
init ( ) {
14
26
let loc = < any > services . location ;
27
+ let location : Location = DOM . getLocation ( ) ;
15
28
16
- loc . hash = ( ) => this . _platformLocation . hash ;
17
- loc . path = this . locationStrategy . path ;
29
+ loc . hash = ( ) => splitHash ( trimHashVal ( location . hash ) ) [ 1 ] ;
30
+ loc . path = ( ) => splitHash ( trimHashVal ( location . hash ) ) [ 0 ] ;
18
31
loc . search = ( ) => location . search ;
19
32
loc . url = ( url ) => {
20
- if ( url ) {
33
+ if ( isDefined ( url ) ) {
21
34
location . hash = url ;
22
35
}
23
36
return loc . path ( ) ;
24
37
} ;
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 ;
38
+ loc . replace = ( ) => { console . log ( new Error ( '$location.replace() not impl' ) ) } ;
39
+ loc . onChange = cb => window . addEventListener ( "hashchange" , cb , false ) ;
28
40
29
41
let locCfg = < any > services . locationConfig ;
30
42
31
43
locCfg . port = ( ) => location . port ;
32
44
locCfg . protocol = ( ) => location . protocol ;
33
45
locCfg . host = ( ) => location . host ;
34
- locCfg . baseHref = this . locationStrategy . getBaseHref ;
35
- locCfg . html5Mode = ( ) => this . locationStrategy instanceof PathLocationStrategy ; // does it work ?
46
+ locCfg . baseHref = ( ) => null ;
47
+ locCfg . html5Mode = ( ) => false ;
36
48
locCfg . hashPrefix = ( newprefix : string ) : string => {
37
49
if ( isDefined ( newprefix ) ) {
38
50
this . hashPrefix = newprefix ;
@@ -41,3 +53,51 @@ export class UiLocationStrategy {
41
53
} ;
42
54
}
43
55
}
56
+
57
+
58
+ @Injectable ( )
59
+ export class HTML5UrlStrategy extends UrlStrategy {
60
+ baseHref : string ;
61
+
62
+ init ( ) {
63
+ let loc = < any > services . location ;
64
+ let location : Location = DOM . getLocation ( ) ;
65
+ let history : History = DOM . getHistory ( ) ;
66
+ this . baseHref = DOM . getBaseHref ( ) || "" ;
67
+
68
+ loc . hash = ( ) =>
69
+ trimHashVal ( location . hash ) ;
70
+ loc . path = ( ) => {
71
+ let path = location . pathname ;
72
+ let idx = path . indexOf ( this . baseHref ) ;
73
+ if ( idx !== 0 ) throw new Error ( `current url: ${ path } does not start with <base> tag ${ this . baseHref } ` ) ;
74
+ return path . substr ( this . baseHref . length ) ;
75
+ } ;
76
+
77
+ loc . search = ( ) =>
78
+ location . search ;
79
+ loc . url = ( url ) => {
80
+ if ( isDefined ( url ) && url !== loc . url ( ) ) {
81
+ history . pushState ( null , null , this . baseHref + url ) ;
82
+ }
83
+ let hash = loc . hash ( ) ;
84
+ return loc . path ( ) + ( hash ? "#" + hash : "" ) ;
85
+ } ;
86
+ loc . replace = ( ) => { console . log ( new Error ( '$location.replace() not impl' ) ) } ;
87
+ loc . onChange = cb => window . addEventListener ( "popstate" , cb , false ) ;
88
+
89
+ let locCfg = < any > services . locationConfig ;
90
+
91
+ locCfg . port = ( ) => location . port ;
92
+ locCfg . protocol = ( ) => location . protocol ;
93
+ locCfg . host = ( ) => location . host ;
94
+ locCfg . baseHref = ( baseHref ?) => {
95
+ if ( isDefined ( baseHref ) ) {
96
+ this . baseHref = baseHref ;
97
+ }
98
+ return this . baseHref
99
+ } ;
100
+ locCfg . html5Mode = ( ) => true ;
101
+ locCfg . hashPrefix = ( ) => null ;
102
+ }
103
+ }
0 commit comments