4
4
*/ /** */
5
5
import { LocationConfig , LocationServices , UIRouter , ParamType , isDefined } from '@uirouter/core' ;
6
6
import { val , createProxyFunctions , removeFrom , isObject } from '@uirouter/core' ;
7
- import { ILocationService , ILocationProvider } from 'angular' ;
7
+ import { ILocationService , ILocationProvider , IWindowService } from 'angular' ;
8
8
9
9
/**
10
10
* Implements UI-Router LocationServices and LocationConfig using Angular 1's $location service
11
11
*/
12
12
export class Ng1LocationServices implements LocationConfig , LocationServices {
13
13
private $locationProvider : ILocationProvider ;
14
14
private $location : ILocationService ;
15
- private $sniffer ;
15
+ private $sniffer : any ;
16
+ private $browser : any ;
17
+ private $window : IWindowService ;
16
18
17
19
path ;
18
20
search ;
@@ -21,7 +23,8 @@ export class Ng1LocationServices implements LocationConfig, LocationServices {
21
23
port ;
22
24
protocol ;
23
25
host ;
24
- baseHref ;
26
+
27
+ private _baseHref : string ;
25
28
26
29
// .onChange() registry
27
30
private _urlListeners : Function [ ] = [ ] ;
@@ -67,27 +70,30 @@ export class Ng1LocationServices implements LocationConfig, LocationServices {
67
70
return html5Mode && this . $sniffer . history ;
68
71
}
69
72
73
+ baseHref ( ) {
74
+ return this . _baseHref || ( this . _baseHref = this . $browser . baseHref ( ) || this . $window . location . pathname ) ;
75
+ }
76
+
70
77
url ( newUrl ?: string , replace = false , state ?) {
71
78
if ( isDefined ( newUrl ) ) this . $location . url ( newUrl ) ;
72
79
if ( replace ) this . $location . replace ( ) ;
73
80
if ( state ) this . $location . state ( state ) ;
74
81
return this . $location . url ( ) ;
75
82
}
76
83
77
- _runtimeServices ( $rootScope , $location : ILocationService , $sniffer , $browser ) {
84
+ _runtimeServices ( $rootScope , $location : ILocationService , $sniffer , $browser , $window : IWindowService ) {
78
85
this . $location = $location ;
79
86
this . $sniffer = $sniffer ;
87
+ this . $browser = $browser ;
88
+ this . $window = $window ;
80
89
81
90
// Bind $locationChangeSuccess to the listeners registered in LocationService.onChange
82
91
$rootScope . $on ( '$locationChangeSuccess' , evt => this . _urlListeners . forEach ( fn => fn ( evt ) ) ) ;
83
92
const _loc = val ( $location ) ;
84
- const _browser = val ( $browser ) ;
85
93
86
94
// Bind these LocationService functions to $location
87
95
createProxyFunctions ( _loc , this , _loc , [ 'replace' , 'path' , 'search' , 'hash' ] ) ;
88
96
// Bind these LocationConfig functions to $location
89
97
createProxyFunctions ( _loc , this , _loc , [ 'port' , 'protocol' , 'host' ] ) ;
90
- // Bind these LocationConfig functions to $browser
91
- createProxyFunctions ( _browser , this , _browser , [ 'baseHref' ] ) ;
92
98
}
93
99
}
0 commit comments