1
1
import { Injectable , Injector } from '@angular/core' ;
2
2
import { Title } from '@angular/platform-browser' ;
3
3
4
- import { Config } from '../../config/config' ;
5
4
import { ClickBlock } from '../../util/click-block' ;
5
+ import { Config } from '../../config/config' ;
6
+ import { NavController } from '../nav/nav-controller' ;
6
7
import { Platform } from '../../platform/platform' ;
8
+ import { Tabs } from '../tabs/tabs' ;
7
9
8
10
9
11
/**
@@ -15,24 +17,17 @@ export class App {
15
17
private _scrollTime : number = 0 ;
16
18
private _title : string = '' ;
17
19
private _titleSrv : Title = new Title ( ) ;
18
- private _rootNav : any = null ;
20
+ private _rootNav : NavController = null ;
19
21
private _appInjector : Injector ;
20
22
21
23
constructor (
22
24
private _config : Config ,
23
25
private _clickBlock : ClickBlock ,
24
- platform : Platform
26
+ private _platform : Platform
25
27
) {
26
- platform . backButton . subscribe ( ( ) => {
27
- let activeNav = this . getActiveNav ( ) ;
28
- if ( activeNav ) {
29
- if ( activeNav . length ( ) === 1 ) {
30
- platform . exitApp ( ) ;
31
- } else {
32
- activeNav . pop ( ) ;
33
- }
34
- }
35
- } ) ;
28
+ // listen for hardware back button events
29
+ // register this back button action with a default priority
30
+ _platform . registerBackButtonAction ( this . navPop . bind ( this ) ) ;
36
31
}
37
32
38
33
/**
@@ -100,7 +95,7 @@ export class App {
100
95
/**
101
96
* @private
102
97
*/
103
- getActiveNav ( ) : any {
98
+ getActiveNav ( ) : NavController {
104
99
var nav = this . _rootNav || null ;
105
100
var activeChildNav : any ;
106
101
@@ -118,7 +113,7 @@ export class App {
118
113
/**
119
114
* @private
120
115
*/
121
- getRootNav ( ) : any {
116
+ getRootNav ( ) : NavController {
122
117
return this . _rootNav ;
123
118
}
124
119
@@ -129,6 +124,72 @@ export class App {
129
124
this . _rootNav = nav ;
130
125
}
131
126
127
+ /**
128
+ * @private
129
+ */
130
+ navPop ( ) : Promise < any > {
131
+ // function used to climb up all parent nav controllers
132
+ function navPop ( nav : any ) : Promise < any > {
133
+ if ( nav ) {
134
+ if ( nav . length && nav . length ( ) > 1 ) {
135
+ // this nav controller has more than one view
136
+ // pop the current view on this nav and we're done here
137
+ console . debug ( 'app, goBack pop nav' ) ;
138
+ return nav . pop ( ) ;
139
+
140
+ } else if ( nav . previousTab ) {
141
+ // FYI, using "nav instanceof Tabs" throws a Promise runtime error for whatever reason, idk
142
+ // this is a Tabs container
143
+ // see if there is a valid previous tab to go to
144
+ let prevTab = nav . previousTab ( true ) ;
145
+ if ( prevTab ) {
146
+ console . debug ( 'app, goBack previous tab' ) ;
147
+ nav . select ( prevTab ) ;
148
+ return Promise . resolve ( ) ;
149
+ }
150
+ }
151
+
152
+ // try again using the parent nav (if there is one)
153
+ return navPop ( nav . parent ) ;
154
+ }
155
+
156
+ // nerp, never found nav that could pop off a view
157
+ return null ;
158
+ }
159
+
160
+ // app must be enabled and there must be a
161
+ // root nav controller for go back to work
162
+ if ( this . _rootNav && this . isEnabled ( ) ) {
163
+
164
+ // first check if the root navigation has any overlays
165
+ // opened in it's portal, like alert/actionsheet/popup
166
+ let portal = this . _rootNav . getPortal && this . _rootNav . getPortal ( ) ;
167
+ if ( portal && portal . length ( ) > 0 ) {
168
+ // there is an overlay view in the portal
169
+ // let's pop this one off to go back
170
+ console . debug ( 'app, goBack pop overlay' ) ;
171
+ return portal . pop ( ) ;
172
+ }
173
+
174
+ // next get the active nav, check itself and climb up all
175
+ // of its parent navs until it finds a nav that can pop
176
+ let navPromise = navPop ( this . getActiveNav ( ) ) ;
177
+ if ( navPromise === null ) {
178
+ // no views to go back to
179
+ // let's exit the app
180
+ if ( this . _config . getBoolean ( 'navExitApp' , true ) ) {
181
+ console . debug ( 'app, goBack exitApp' ) ;
182
+ this . _platform . exitApp ( ) ;
183
+ }
184
+
185
+ } else {
186
+ return navPromise ;
187
+ }
188
+ }
189
+
190
+ return Promise . resolve ( ) ;
191
+ }
192
+
132
193
/**
133
194
* @private
134
195
*/
0 commit comments