1
1
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
2
2
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3
3
4
- import { Log } from './Log.js' ;
4
+ import { Log } from './Log' ;
5
+ import { IWindow } from './IWindow' ;
5
6
6
7
const DefaultPopupFeatures = 'location=no,toolbar=no,zoom=no' ;
7
8
const DefaultPopupTarget = "_blank" ;
8
9
9
- export class CordovaPopupWindow {
10
+ export class CordovaPopupWindow implements IWindow {
11
+ private _promise : Promise < unknown > ;
12
+ private _resolve ! : ( value : unknown ) => void ;
13
+ private _reject ! : ( reason ?: any ) => void ;
14
+ private features : string ;
15
+ private target : string ;
16
+ private redirect_uri : string ;
17
+ private _popup : any ;
18
+ private _exitCallbackEvent ?: ( message : any ) => void ;
19
+ private _loadStartCallbackEvent ?: ( event : any ) => void ;
10
20
11
- constructor ( params ) {
21
+ constructor ( params : any ) {
12
22
this . _promise = new Promise ( ( resolve , reject ) => {
13
23
this . _resolve = resolve ;
14
24
this . _reject = reject ;
15
25
} ) ;
16
26
17
27
this . features = params . popupWindowFeatures || DefaultPopupFeatures ;
18
28
this . target = params . popupWindowTarget || DefaultPopupTarget ;
19
-
29
+
20
30
this . redirect_uri = params . startUrl ;
21
31
Log . debug ( "CordovaPopupWindow.ctor: redirect_uri: " + this . redirect_uri ) ;
22
32
}
23
33
24
- _isInAppBrowserInstalled ( cordovaMetadata ) {
34
+ _isInAppBrowserInstalled ( cordovaMetadata : any ) {
25
35
return [ "cordova-plugin-inappbrowser" , "cordova-plugin-inappbrowser.inappbrowser" , "org.apache.cordova.inappbrowser" ] . some ( function ( name ) {
26
36
return cordovaMetadata . hasOwnProperty ( name )
27
37
} )
28
38
}
29
-
30
- navigate ( params ) {
39
+
40
+ navigate ( params : any ) {
31
41
if ( ! params || ! params . url ) {
32
42
this . _error ( "No url provided" ) ;
33
43
} else {
44
+ // @ts -ignore
34
45
if ( ! window . cordova ) {
35
- return this . _error ( "cordova is undefined" )
46
+ this . _error ( "cordova is undefined" ) ;
47
+ return this . promise ;
36
48
}
37
-
49
+
50
+ // @ts -ignore
38
51
var cordovaMetadata = window . cordova . require ( "cordova/plugin_list" ) . metadata ;
39
52
if ( this . _isInAppBrowserInstalled ( cordovaMetadata ) === false ) {
40
- return this . _error ( "InAppBrowser plugin not found" )
53
+ this . _error ( "InAppBrowser plugin not found" ) ;
54
+ return this . promise ;
41
55
}
56
+
57
+ // @ts -ignore
42
58
this . _popup = cordova . InAppBrowser . open ( params . url , this . target , this . features ) ;
43
59
if ( this . _popup ) {
44
60
Log . debug ( "CordovaPopupWindow.navigate: popup successfully created" ) ;
45
-
46
- this . _exitCallbackEvent = this . _exitCallback . bind ( this ) ;
61
+
62
+ this . _exitCallbackEvent = this . _exitCallback . bind ( this ) ;
47
63
this . _loadStartCallbackEvent = this . _loadStartCallback . bind ( this ) ;
48
-
64
+
49
65
this . _popup . addEventListener ( "exit" , this . _exitCallbackEvent , false ) ;
50
66
this . _popup . addEventListener ( "loadstart" , this . _loadStartCallbackEvent , false ) ;
51
67
} else {
@@ -59,22 +75,22 @@ export class CordovaPopupWindow {
59
75
return this . _promise ;
60
76
}
61
77
62
- _loadStartCallback ( event ) {
78
+ _loadStartCallback ( event : any ) {
63
79
if ( event . url . indexOf ( this . redirect_uri ) === 0 ) {
64
80
this . _success ( { url : event . url } ) ;
65
- }
81
+ }
66
82
}
67
- _exitCallback ( message ) {
68
- this . _error ( message ) ;
83
+ _exitCallback ( message : string ) {
84
+ this . _error ( message ) ;
69
85
}
70
-
71
- _success ( data ) {
86
+
87
+ _success ( data : any ) {
72
88
this . _cleanup ( ) ;
73
89
74
90
Log . debug ( "CordovaPopupWindow: Successful response from cordova popup window" ) ;
75
91
this . _resolve ( data ) ;
76
92
}
77
- _error ( message ) {
93
+ _error ( message : string ) {
78
94
this . _cleanup ( ) ;
79
95
80
96
Log . error ( message ) ;
0 commit comments