1
1
import application = require( "application" ) ;
2
+ import { Injectable } from '@angular/core' ;
2
3
import { LocationStrategy } from '@angular/common' ;
3
- import { NgZone , ApplicationRef , Inject , forwardRef } from '@angular/core' ;
4
4
import { routerLog } from "../trace" ;
5
- import { topmost } from "ui/frame" ;
5
+ import { Frame , NavigationTransition } from "ui/frame" ;
6
+ import { isPresent } from '@angular/core/src/facade/lang' ;
6
7
7
- interface LocationState {
8
+ export interface NavigationOptions {
9
+ clearHistory ?: boolean ;
10
+ animated ?: boolean ;
11
+ transition ?: NavigationTransition ;
12
+ }
13
+
14
+ const defaultNavOptions : NavigationOptions = {
15
+ clearHistory : false ,
16
+ animated : true
17
+ } ;
18
+
19
+ export interface LocationState {
8
20
state : any ,
9
21
title : string ,
10
22
url : string ,
11
23
queryParams : string ,
12
24
isPageNavigation : boolean
13
25
}
14
26
27
+ @Injectable ( )
15
28
export class NSLocationStrategy extends LocationStrategy {
16
29
private states = new Array < LocationState > ( ) ;
17
30
private popStateCallbacks = new Array < ( _ : any ) => any > ( ) ;
18
31
19
32
private _isPageNavigationgBack = false ;
20
33
private _isPageNavigatingForward : boolean = false ;
34
+ private _currentNavigationOptions : NavigationOptions ;
21
35
22
- constructor ( ) {
36
+ constructor ( private frame : Frame ) {
23
37
super ( ) ;
24
38
routerLog ( "NSLocationStrategy.constructor()" ) ;
25
39
}
26
40
27
41
path ( ) : string {
28
- routerLog ( "NSLocationStrategy.path()" ) ;
29
42
let state = this . peekState ( ) ;
30
- return state ? state . url : "/" ;
43
+ const result = state ? state . url : "/" ;
44
+ routerLog ( "NSLocationStrategy.path(): " + result ) ;
45
+ return result ;
31
46
}
32
47
33
48
prepareExternalUrl ( internal : string ) : string {
@@ -42,7 +57,16 @@ export class NSLocationStrategy extends LocationStrategy {
42
57
}
43
58
44
59
pushStateInternal ( state : any , title : string , url : string , queryParams : string ) : void {
45
- let isNewPage = this . _isPageNavigatingForward ;
60
+ let isNewPage = this . _isPageNavigatingForward || this . states . length === 0 ;
61
+
62
+ const navOptions = this . _currentNavigationOptions || defaultNavOptions ;
63
+
64
+ if ( navOptions . clearHistory ) {
65
+ routerLog ( "NSLocationStrategy.pushStateInternal clearing states history" ) ;
66
+ this . states . length = 0 ;
67
+ }
68
+
69
+ this . _currentNavigationOptions = undefined ;
46
70
47
71
this . _isPageNavigatingForward = false ;
48
72
this . states . push ( {
@@ -55,19 +79,22 @@ export class NSLocationStrategy extends LocationStrategy {
55
79
}
56
80
57
81
replaceState ( state : any , title : string , url : string , queryParams : string ) : void {
58
- routerLog ( `NSLocationStrategy.replaceState state: ${ state } , title: ${ title } , url: ${ url } , queryParams: ${ queryParams } ` ) ;
59
-
60
82
if ( this . states . length > 0 ) {
61
- let oldState = this . states . pop ( ) ;
62
- routerLog ( `NSLocationStrategy.replaceState state poped: ${ oldState . state } , title: ${ oldState . title } , url: ${ oldState . url } , queryParams: ${ oldState . queryParams } ` ) ;
83
+ routerLog ( `NSLocationStrategy.replaceState changing exisitng state: ${ state } , title: ${ title } , url: ${ url } , queryParams: ${ queryParams } ` ) ;
84
+ const topState = this . peekState ( ) ;
85
+ topState . state = state ;
86
+ topState . title = title ;
87
+ topState . url = url ;
88
+ topState . queryParams = queryParams ;
89
+ }
90
+ else {
91
+ routerLog ( `NSLocationStrategy.replaceState pushing new state: ${ state } , title: ${ title } , url: ${ url } , queryParams: ${ queryParams } ` ) ;
92
+ this . pushStateInternal ( state , title , url , queryParams ) ;
63
93
}
64
-
65
- this . pushStateInternal ( state , title , url , queryParams ) ;
66
94
}
67
95
68
96
forward ( ) : void {
69
- routerLog ( "NSLocationStrategy.forward" ) ;
70
- throw new Error ( "Not implemented" ) ;
97
+ throw new Error ( "NSLocationStrategy.forward() - not implemented" ) ;
71
98
}
72
99
73
100
back ( ) : void {
@@ -76,21 +103,23 @@ export class NSLocationStrategy extends LocationStrategy {
76
103
// clear the stack until we get to a page navigation state
77
104
let state = this . states . pop ( ) ;
78
105
let count = 1 ;
79
- while ( ! state . isPageNavigation ) {
106
+
107
+ while ( ! ( state . isPageNavigation ) ) {
80
108
state = this . states . pop ( ) ;
81
109
count ++ ;
82
110
}
83
- routerLog ( "NSLocationStrategy.back() while navigating back. States popped: " + count )
111
+
112
+ routerLog ( "NSLocationStrategy.back() while navigating back. States popped: " + count ) ;
84
113
this . callPopState ( state , true ) ;
85
114
} else {
86
115
let state = this . peekState ( ) ;
87
116
if ( state . isPageNavigation ) {
88
117
// This was a page navigation - so navigate through frame.
89
- routerLog ( "NSLocationStrategy.back() while not navigating back but top state is page - will call frame.goback()" )
90
- topmost ( ) . goBack ( ) ;
118
+ routerLog ( "NSLocationStrategy.back() while not navigating back but top state is page - will call frame.goback()" ) ;
119
+ this . frame . goBack ( ) ;
91
120
} else {
92
121
// Nested navigation - just pop the state
93
- routerLog ( "NSLocationStrategy.back() while not navigating back but top state is not page - just pop" )
122
+ routerLog ( "NSLocationStrategy.back() while not navigating back but top state is not page - just pop" ) ;
94
123
this . callPopState ( this . states . pop ( ) , true ) ;
95
124
}
96
125
}
@@ -108,8 +137,8 @@ export class NSLocationStrategy extends LocationStrategy {
108
137
}
109
138
110
139
private callPopState ( state : LocationState , pop : boolean = true ) {
111
- var change = { url : state . url , pop : pop } ;
112
- for ( var fn of this . popStateCallbacks ) {
140
+ const change = { url : state . url , pop : pop } ;
141
+ for ( let fn of this . popStateCallbacks ) {
113
142
fn ( change ) ;
114
143
}
115
144
}
@@ -121,32 +150,53 @@ export class NSLocationStrategy extends LocationStrategy {
121
150
return null ;
122
151
}
123
152
153
+ public toString ( ) {
154
+ return this . states
155
+ . map ( ( v , i ) => `${ i } .[${ v . isPageNavigation ? "PAGE" : "INTERNAL" } ] "${ v . url } "` )
156
+ . reverse ( )
157
+ . join ( "\n" ) ;
158
+ }
159
+
124
160
// Methods for syncing with page navigation in PageRouterOutlet
125
- public beginBackPageNavigation ( ) {
161
+ public _beginBackPageNavigation ( ) {
126
162
routerLog ( "NSLocationStrategy.startGoBack()" ) ;
127
163
if ( this . _isPageNavigationgBack ) {
128
- throw new Error ( "Calling startGoBack while going back." )
164
+ throw new Error ( "Calling startGoBack while going back." ) ;
129
165
}
130
166
this . _isPageNavigationgBack = true ;
131
167
}
132
168
133
- public finishBackPageNavigation ( ) {
169
+ public _finishBackPageNavigation ( ) {
134
170
routerLog ( "NSLocationStrategy.finishBackPageNavigation()" ) ;
135
171
if ( ! this . _isPageNavigationgBack ) {
136
- throw new Error ( "Calling endGoBack while not going back." )
172
+ throw new Error ( "Calling endGoBack while not going back." ) ;
137
173
}
138
174
this . _isPageNavigationgBack = false ;
139
175
}
140
176
141
- public isPageNavigatingBack ( ) {
177
+ public _isPageNavigatingBack ( ) {
142
178
return this . _isPageNavigationgBack ;
143
179
}
144
180
145
- public navigateToNewPage ( ) {
181
+ public _beginPageNavigation ( ) : NavigationOptions {
146
182
routerLog ( "NSLocationStrategy.navigateToNewPage()" ) ;
147
183
if ( this . _isPageNavigatingForward ) {
148
- throw new Error ( "Calling navigateToNewPage while already navigating to new page." )
184
+ throw new Error ( "Calling navigateToNewPage while already navigating to new page." ) ;
149
185
}
186
+
150
187
this . _isPageNavigatingForward = true ;
188
+ return this . _currentNavigationOptions || defaultNavOptions ;
189
+ }
190
+
191
+ public _setNavigationOptions ( options : NavigationOptions ) {
192
+ this . _currentNavigationOptions = {
193
+ clearHistory : isPresent ( options . clearHistory ) ? options . clearHistory : false ,
194
+ animated : isPresent ( options . animated ) ? options . animated : true ,
195
+ transition : options . transition
196
+ } ;
197
+ }
198
+
199
+ public _getSatates ( ) : Array < LocationState > {
200
+ return this . states . slice ( ) ;
151
201
}
152
202
}
0 commit comments