11import application = require( "application" ) ;
2+ import { Injectable } from '@angular/core' ;
23import { LocationStrategy } from '@angular/common' ;
3- import { NgZone , ApplicationRef , Inject , forwardRef } from '@angular/core' ;
44import { 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' ;
67
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 {
820 state : any ,
921 title : string ,
1022 url : string ,
1123 queryParams : string ,
1224 isPageNavigation : boolean
1325}
1426
27+ @Injectable ( )
1528export class NSLocationStrategy extends LocationStrategy {
1629 private states = new Array < LocationState > ( ) ;
1730 private popStateCallbacks = new Array < ( _ : any ) => any > ( ) ;
1831
1932 private _isPageNavigationgBack = false ;
2033 private _isPageNavigatingForward : boolean = false ;
34+ private _currentNavigationOptions : NavigationOptions ;
2135
22- constructor ( ) {
36+ constructor ( private frame : Frame ) {
2337 super ( ) ;
2438 routerLog ( "NSLocationStrategy.constructor()" ) ;
2539 }
2640
2741 path ( ) : string {
28- routerLog ( "NSLocationStrategy.path()" ) ;
2942 let state = this . peekState ( ) ;
30- return state ? state . url : "/" ;
43+ const result = state ? state . url : "/" ;
44+ routerLog ( "NSLocationStrategy.path(): " + result ) ;
45+ return result ;
3146 }
3247
3348 prepareExternalUrl ( internal : string ) : string {
@@ -42,7 +57,16 @@ export class NSLocationStrategy extends LocationStrategy {
4257 }
4358
4459 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 ;
4670
4771 this . _isPageNavigatingForward = false ;
4872 this . states . push ( {
@@ -55,19 +79,22 @@ export class NSLocationStrategy extends LocationStrategy {
5579 }
5680
5781 replaceState ( state : any , title : string , url : string , queryParams : string ) : void {
58- routerLog ( `NSLocationStrategy.replaceState state: ${ state } , title: ${ title } , url: ${ url } , queryParams: ${ queryParams } ` ) ;
59-
6082 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 ) ;
6393 }
64-
65- this . pushStateInternal ( state , title , url , queryParams ) ;
6694 }
6795
6896 forward ( ) : void {
69- routerLog ( "NSLocationStrategy.forward" ) ;
70- throw new Error ( "Not implemented" ) ;
97+ throw new Error ( "NSLocationStrategy.forward() - not implemented" ) ;
7198 }
7299
73100 back ( ) : void {
@@ -76,21 +103,23 @@ export class NSLocationStrategy extends LocationStrategy {
76103 // clear the stack until we get to a page navigation state
77104 let state = this . states . pop ( ) ;
78105 let count = 1 ;
79- while ( ! state . isPageNavigation ) {
106+
107+ while ( ! ( state . isPageNavigation ) ) {
80108 state = this . states . pop ( ) ;
81109 count ++ ;
82110 }
83- routerLog ( "NSLocationStrategy.back() while navigating back. States popped: " + count )
111+
112+ routerLog ( "NSLocationStrategy.back() while navigating back. States popped: " + count ) ;
84113 this . callPopState ( state , true ) ;
85114 } else {
86115 let state = this . peekState ( ) ;
87116 if ( state . isPageNavigation ) {
88117 // 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 ( ) ;
91120 } else {
92121 // 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" ) ;
94123 this . callPopState ( this . states . pop ( ) , true ) ;
95124 }
96125 }
@@ -108,8 +137,8 @@ export class NSLocationStrategy extends LocationStrategy {
108137 }
109138
110139 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 ) {
113142 fn ( change ) ;
114143 }
115144 }
@@ -121,32 +150,54 @@ export class NSLocationStrategy extends LocationStrategy {
121150 return null ;
122151 }
123152
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+
124160 // Methods for syncing with page navigation in PageRouterOutlet
125- public beginBackPageNavigation ( ) {
161+ public _beginBackPageNavigation ( ) {
126162 routerLog ( "NSLocationStrategy.startGoBack()" ) ;
127163 if ( this . _isPageNavigationgBack ) {
128- throw new Error ( "Calling startGoBack while going back." )
164+ throw new Error ( "Calling startGoBack while going back." ) ;
129165 }
130166 this . _isPageNavigationgBack = true ;
131167 }
132168
133- public finishBackPageNavigation ( ) {
169+ public _finishBackPageNavigation ( ) {
134170 routerLog ( "NSLocationStrategy.finishBackPageNavigation()" ) ;
135171 if ( ! this . _isPageNavigationgBack ) {
136- throw new Error ( "Calling endGoBack while not going back." )
172+ throw new Error ( "Calling endGoBack while not going back." ) ;
137173 }
138174 this . _isPageNavigationgBack = false ;
139175 }
140176
141- public isPageNavigatingBack ( ) {
177+ public _isPageNavigatingBack ( ) {
142178 return this . _isPageNavigationgBack ;
143179 }
144180
145- public navigateToNewPage ( ) {
181+ public _beginPageNavigation ( ) : NavigationOptions {
146182 routerLog ( "NSLocationStrategy.navigateToNewPage()" ) ;
147183 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." ) ;
149185 }
186+
187+
150188 this . _isPageNavigatingForward = true ;
189+ return this . _currentNavigationOptions || defaultNavOptions ;
190+ }
191+
192+ public _setNavigationOptions ( options : NavigationOptions ) {
193+ this . _currentNavigationOptions = {
194+ clearHistory : isPresent ( options . clearHistory ) ? options . clearHistory : false ,
195+ animated : isPresent ( options . animated ) ? options . animated : true ,
196+ transition : options . transition
197+ } ;
198+ }
199+
200+ public _getSatates ( ) : Array < LocationState > {
201+ return this . states . slice ( ) ;
151202 }
152203}
0 commit comments