1
1
const Vue = require ( './nativescript-vue' )
2
2
3
+ const SCENARIO = 'login'
4
+
3
5
Vue . config . silent = false
4
6
Vue . config . debug = true
5
7
@@ -14,7 +16,7 @@ const FrameRouter = function install(Vue, options) {
14
16
data : {
15
17
routes : options . routes ,
16
18
path : '/' , // current path
17
- stack : [ ] ,
19
+ stack : [ '/' ] ,
18
20
data : null ,
19
21
20
22
pending : false
@@ -31,10 +33,15 @@ const FrameRouter = function install(Vue, options) {
31
33
this . data = this . pending . data
32
34
33
35
this . pending = false
36
+ navigatorLog (
37
+ ` *** NAVIGATOR::_confirmPathChange::canGoBack = ${ this . frameVM . nativeView . canGoBack ( ) } ***`
38
+ )
34
39
} ,
35
- _setPending ( path , clear = false , data ) {
40
+ _setPending ( path , clear = false , data , replaceStack ) {
36
41
navigatorLog (
37
- ` *** NAVIGATOR::_setPending(path = ${ path } , clear = ${ clear } , data = ${ data } ) ***`
42
+ ` *** NAVIGATOR::_setPending(path = ${ path } , clear = ${ clear } , data = ${ data } , replaceStack = ${
43
+ replaceStack ? replaceStack . join ( ) : undefined
44
+ } ) ***`
38
45
)
39
46
40
47
let stack = this . stack . slice ( )
@@ -46,7 +53,7 @@ const FrameRouter = function install(Vue, options) {
46
53
47
54
this . pending = {
48
55
path,
49
- stack,
56
+ stack : replaceStack ? replaceStack : stack ,
50
57
data
51
58
}
52
59
} ,
@@ -83,13 +90,63 @@ const FrameRouter = function install(Vue, options) {
83
90
navigatorLog ( ` *** NAVIGATOR::back(notify = ${ notify } ) ***` )
84
91
85
92
if ( notify ) {
86
- this . stack . pop ( )
87
- this . path = this . stack [ this . stack . length - 1 ] || '/'
93
+ if ( ! this . pending ) {
94
+ this . stack . pop ( )
95
+ this . path = this . stack [ this . stack . length - 1 ]
96
+
97
+ if ( ! this . path ) {
98
+ this . path = '/'
99
+ this . stack = [ '/' ]
100
+ }
101
+ } else {
102
+ this . _confirmPathChange ( )
103
+ }
88
104
} else {
105
+ const replaceStack = this . stack . slice ( )
106
+ replaceStack . pop ( )
107
+
108
+ this . _setPending (
109
+ this . stack [ this . stack . length - 1 ] || '/' ,
110
+ false ,
111
+ null ,
112
+ replaceStack
113
+ )
89
114
this . $navigateBack ( {
90
115
frame : '__navigator_frame__'
91
116
} )
92
117
}
118
+ } ,
119
+ /**
120
+ * Tries to find the most recent entry matching the provided path in the backstack
121
+ * and navigates back to it, if the path is not found a navigation will occur with
122
+ * clearing the backStack
123
+ * @param path
124
+ */
125
+ backTo ( path ) {
126
+ navigatorLog ( ` *** NAVIGATOR::backTo(path = ${ path } ) ***` )
127
+
128
+ const entry = navigator . frameVM . nativeView . backStack . find ( entry => {
129
+ return entry . entry . path === path
130
+ } )
131
+ entry &&
132
+ navigatorLog ( ` *** NAVIGATOR::backTo::FOUND_BACKSTACK_ENTRY ***` )
133
+
134
+ if ( entry ) {
135
+ const index = this . stack
136
+ . slice ( )
137
+ . reverse ( )
138
+ . findIndex ( p => p === path )
139
+ const actualIndex = this . stack . length - index
140
+ const replaceStack = this . stack . slice ( )
141
+ replaceStack . splice ( actualIndex )
142
+ this . _setPending ( path , false , null , replaceStack )
143
+ this . $navigateBack ( {
144
+ frame : '__navigator_frame__' ,
145
+ entry : entry . entry
146
+ } )
147
+ } else {
148
+ this . replace ( path )
149
+ }
93
150
}
94
151
}
95
152
} )
@@ -109,6 +166,7 @@ const FrameRouter = function install(Vue, options) {
109
166
Vue . component ( 'FrameRouter' , {
110
167
created ( ) {
111
168
this . rendered = false
169
+ navigator . frameVM = this
112
170
} ,
113
171
render ( h ) {
114
172
if ( ! this . rendered ) {
@@ -170,25 +228,86 @@ const DetailsPage = {
170
228
<Button @tap="$navigator.replace('/details')" text="Replace to details" />
171
229
172
230
<Button @tap="$navigator.back()" text="Go back" />
231
+ <Button @tap="$navigator.backTo('/')" text="Go back to Home" />
173
232
</StackLayout>
174
233
</Page>
175
234
`
176
235
}
177
236
178
- Vue . use ( FrameRouter , {
179
- routes : [
180
- { path : '/' , component : HomePage } ,
181
- { path : '/details' , component : DetailsPage }
182
- ] ,
183
- debug : true
184
- } )
237
+ // LOGIN SCENARIO
185
238
186
- new Vue ( {
187
- template : `
239
+ if ( SCENARIO === 'login' ) {
240
+ const applicationSettings = require ( 'tns-core-modules/application-settings' )
241
+ const LoginSCN__LoadingPage = {
242
+ data ( ) {
243
+ return {
244
+ isLoggedIn : applicationSettings . getBoolean ( 'isLoggedIn' , false )
245
+ }
246
+ } ,
247
+ template : `
248
+ <Page @loaded="$navigator.replace(isLoggedIn ? '/home' : '/login')" actionBarHidden="true" />
249
+ `
250
+ }
251
+ const LoginSCN__HomePage = {
252
+ template : `
253
+ <Page>
254
+ <StackLayout>
255
+ <Button text="Do something (nav to home again...)" @tap="$navigator.push('/home')"/>
256
+ <Button text="Log out" @tap="logout"/>
257
+ </StackLayout>
258
+ </Page>
259
+ ` ,
260
+ methods : {
261
+ logout ( ) {
262
+ applicationSettings . setBoolean ( 'isLoggedIn' , false )
263
+ this . $navigator . replace ( '/login' )
264
+ }
265
+ }
266
+ }
267
+ const LoginSCN__LoginPage = {
268
+ template : `
269
+ <Page>
270
+ <StackLayout>
271
+ <Button text="Login" @tap="login"/>
272
+ </StackLayout>
273
+ </Page>
274
+ ` ,
275
+ methods : {
276
+ login ( ) {
277
+ applicationSettings . setBoolean ( 'isLoggedIn' , true )
278
+ this . $navigator . replace ( '/home' )
279
+ }
280
+ }
281
+ }
282
+
283
+ Vue . use ( FrameRouter , {
284
+ routes : [
285
+ { path : '/' , component : LoginSCN__LoadingPage } ,
286
+ { path : '/home' , component : LoginSCN__HomePage } ,
287
+ { path : '/login' , component : LoginSCN__LoginPage }
288
+ ] ,
289
+ debug : false
290
+ } )
291
+
292
+ new Vue ( {
293
+ template : `<FrameRouter :transition="$navigator.path === '/' ? { name: 'fade', duration: 1 } : 'slide'"/>`
294
+ } ) . $start ( )
295
+ } else {
296
+ Vue . use ( FrameRouter , {
297
+ routes : [
298
+ { path : '/' , component : HomePage } ,
299
+ { path : '/details' , component : DetailsPage }
300
+ ] ,
301
+ debug : false
302
+ } )
303
+
304
+ new Vue ( {
305
+ template : `
188
306
<GridLayout rows="*, auto, auto">
189
307
<FrameRouter row="0"/>
190
308
<label :text="$navigator.$data.path" row="1" />
191
309
<label :text="JSON.stringify($navigator.$data.stack, null, 2)" textWrap="true" row="2" />
192
310
</GridLayout>
193
311
`
194
- } ) . $start ( )
312
+ } ) . $start ( )
313
+ }
0 commit comments