@@ -24,6 +24,7 @@ export interface LocationState {
24
24
queryParams : string ;
25
25
segmentGroup : UrlSegmentGroup ;
26
26
isPageNavigation : boolean ;
27
+ isModalNavigation : boolean ;
27
28
}
28
29
29
30
@Injectable ( )
@@ -33,9 +34,13 @@ export class NSLocationStrategy extends LocationStrategy {
33
34
private currentOutlet : string ;
34
35
private popStateCallbacks = new Array < ( _ : any ) => any > ( ) ;
35
36
37
+ private _isModalClosing = false ;
36
38
private _isPageNavigationBack = false ;
37
39
private _currentNavigationOptions : NavigationOptions ;
38
40
41
+
42
+ public _isModalNavigation = false ;
43
+
39
44
constructor ( private frameService : FrameService ) {
40
45
super ( ) ;
41
46
routerLog ( "NSLocationStrategy.constructor()" ) ;
@@ -87,7 +92,8 @@ export class NSLocationStrategy extends LocationStrategy {
87
92
url : url ,
88
93
queryParams : queryParams ,
89
94
segmentGroup : rootOutlets [ outletName ] ,
90
- isPageNavigation : isNewPage
95
+ isPageNavigation : isNewPage ,
96
+ isModalNavigation : false
91
97
} ) ;
92
98
}
93
99
} ) ;
@@ -130,7 +136,22 @@ export class NSLocationStrategy extends LocationStrategy {
130
136
}
131
137
132
138
back ( ) : void {
133
- if ( this . _isPageNavigationBack ) {
139
+ if ( this . _isModalClosing ) {
140
+ const states = this . statesByOutlet [ this . currentOutlet ] ;
141
+ // We are closing modal view
142
+ // clear the stack until we get to the page that opened the modal view
143
+ let state = states . pop ( ) ;
144
+ let count = 1 ;
145
+
146
+ while ( ! ( state . isModalNavigation ) ) {
147
+ state = states . pop ( ) ;
148
+ count ++ ;
149
+ }
150
+
151
+ states . push ( state ) ;
152
+ routerLog ( "NSLocationStrategy.back() while closing modal. States popped: " + count ) ;
153
+ this . callPopState ( state , true ) ;
154
+ } else if ( this . _isPageNavigationBack ) {
134
155
const states = this . statesByOutlet [ this . currentOutlet ] ;
135
156
// We are navigating to the previous page
136
157
// clear the stack until we get to a page navigation state
@@ -178,6 +199,7 @@ export class NSLocationStrategy extends LocationStrategy {
178
199
179
200
private callPopState ( state : LocationState , pop : boolean = true ) {
180
201
const urlSerializer = new DefaultUrlSerializer ( ) ;
202
+ this . currentUrlTree . root . children [ this . currentOutlet ] = state . segmentGroup ;
181
203
const url = urlSerializer . serialize ( this . currentUrlTree ) ;
182
204
const change = { url : url , pop : pop } ;
183
205
for ( let fn of this . popStateCallbacks ) {
@@ -199,11 +221,12 @@ export class NSLocationStrategy extends LocationStrategy {
199
221
Object . keys ( this . statesByOutlet ) . forEach ( outletName => {
200
222
const outletStates = this . statesByOutlet [ outletName ] ;
201
223
const outletLog = outletStates
202
- . map ( ( v , i ) => `${ i } .[${ v . isPageNavigation ? "PAGE" : "INTERNAL" } ] "${ v . segmentGroup . toString ( ) } "` )
224
+ // tslint:disable-next-line:max-line-length
225
+ . map ( ( v , i ) => `${ outletName } .${ i } .[${ v . isPageNavigation ? "PAGE" : "INTERNAL" } ].[${ v . isModalNavigation ? "MODAL" : "BASE" } ] "${ v . segmentGroup . toString ( ) } "` )
203
226
. reverse ( ) ;
204
227
205
- result . push ( ` ${ outletName } :` ) ;
206
- result = result . concat ( result , outletLog ) ;
228
+
229
+ result = result . concat ( outletLog ) ;
207
230
} ) ;
208
231
209
232
return result . join ( "\n" ) ;
@@ -231,6 +254,33 @@ export class NSLocationStrategy extends LocationStrategy {
231
254
return this . _isPageNavigationBack ;
232
255
}
233
256
257
+ public _beginModalNavigation ( ) : void {
258
+ routerLog ( "NSLocationStrategy._beginModalNavigation()" ) ;
259
+ const lastState = this . peekState ( this . currentOutlet ) ;
260
+
261
+ if ( lastState ) {
262
+ lastState . isModalNavigation = true ;
263
+ }
264
+
265
+ this . _isModalNavigation = true ;
266
+ }
267
+
268
+ public _beginCloseModalNavigation ( ) : void {
269
+ routerLog ( "NSLocationStrategy.startCloseModal()" ) ;
270
+ if ( this . _isModalClosing ) {
271
+ throw new Error ( "Calling startCloseModal while closing modal." ) ;
272
+ }
273
+ this . _isModalClosing = true ;
274
+ }
275
+
276
+ public _finishCloseModalNavigation ( ) {
277
+ routerLog ( "NSLocationStrategy.finishCloseModalNavigation()" ) ;
278
+ if ( ! this . _isModalClosing ) {
279
+ throw new Error ( "Calling startCloseModal while not closing modal." ) ;
280
+ }
281
+ this . _isModalClosing = false ;
282
+ }
283
+
234
284
public _beginPageNavigation ( name : string ) : NavigationOptions {
235
285
routerLog ( "NSLocationStrategy._beginPageNavigation()" ) ;
236
286
const lastState = this . peekState ( name ) ;
0 commit comments