@@ -5,26 +5,51 @@ import promises = require("promises");
55import dialogs = require( "ui/dialogs" ) ;
66import appmodule = require( "application" ) ;
77
8+ var STRING = "string" ,
9+ ALERT = "Alert" ,
10+ OK = "OK" ,
11+ CANCEL = "Cancel" ;
12+
813function createAlertDialog ( options : dialogs . DialogOptions ) : android . app . AlertDialog . Builder {
914 var alert = new android . app . AlertDialog . Builder ( appmodule . android . currentActivity ) ;
1015 alert . setTitle ( options . title ) ;
1116 alert . setMessage ( options . message ) ;
1217 return alert ;
1318}
1419
20+ function addOkCancelButtonsToAlertDialog ( alert : android . app . AlertDialog . Builder , options : dialogs . ConfirmOptions ,
21+ okCallback : Function , cancelCallback ?: Function ) : void {
22+ alert . setPositiveButton ( options . okButtonName , new android . content . DialogInterface . OnClickListener ( {
23+ onClick : function ( dialog : android . content . DialogInterface , id : number ) {
24+ dialog . cancel ( ) ;
25+ okCallback ( ) ;
26+ }
27+ } ) ) ;
28+
29+ alert . setNegativeButton ( options . cancelButtonName , new android . content . DialogInterface . OnClickListener ( {
30+ onClick : function ( dialog : android . content . DialogInterface , id : number ) {
31+ dialog . cancel ( ) ;
32+ if ( cancelCallback ) {
33+ cancelCallback ( ) ;
34+ }
35+ }
36+ } ) ) ;
37+ }
38+
1539export function alert ( arg : any ) : promises . Promise < void > {
1640 var d = promises . defer < void > ( ) ;
1741 try {
18- var options = typeof arg === "string" ? { message : arg , title : "Alert" , buttonName : "OK" } : arg
42+ var options = typeof arg === STRING ? { message : arg , title : ALERT , buttonName : OK } : arg
1943
2044 var alert = createAlertDialog ( options ) ;
21- /*
45+
2246 alert . setPositiveButton ( options . buttonName , new android . content . DialogInterface . OnClickListener ( {
2347 onClick : function ( dialog : android . content . DialogInterface , id : number ) {
2448 dialog . cancel ( ) ;
49+ d . resolve ( ) ;
2550 }
2651 } ) ) ;
27- */
52+
2853 alert . show ( ) ;
2954
3055 } catch ( ex ) {
@@ -37,10 +62,12 @@ export function alert(arg: any): promises.Promise<void> {
3762export function confirm ( arg : any ) : promises . Promise < boolean > {
3863 var d = promises . defer < boolean > ( ) ;
3964 try {
40- var options = typeof arg === "string" ? { message : arg , title : "Alert" , okButtonName : "OK" , cancelButtonName : "Cancel" } : arg
65+ var options = typeof arg === STRING ? { message : arg , title : ALERT , okButtonName : OK , cancelButtonName : CANCEL } : arg
4166
4267 var alert = createAlertDialog ( options ) ;
4368
69+ addOkCancelButtonsToAlertDialog ( alert , options , function ( ) { d . resolve ( true ) ; } , function ( ) { d . resolve ( false ) ; } ) ;
70+
4471 alert . show ( ) ;
4572
4673 } catch ( ex ) {
@@ -53,10 +80,17 @@ export function confirm(arg: any): promises.Promise<boolean> {
5380export function prompt ( arg : any ) : promises . Promise < string > {
5481 var d = promises . defer < string > ( ) ;
5582 try {
56- var options = typeof arg === "string" ? { message : arg , title : "Alert" , okButtonName : "OK" , cancelButtonName : "Cancel" } : arg
83+ var options = typeof arg === STRING ? { message : arg , title : ALERT , okButtonName : OK , cancelButtonName : CANCEL } : arg
5784
5885 var alert = createAlertDialog ( options ) ;
5986
87+ var input = new android . widget . EditText ( appmodule . android . context ) ;
88+ input . setText ( options . defaultText ? options . defaultText : "" ) ;
89+
90+ alert . setView ( input ) ;
91+
92+ addOkCancelButtonsToAlertDialog ( alert , options , function ( ) { d . resolve ( input . getText ( ) . toString ( ) ) ; } ) ;
93+
6094 alert . show ( ) ;
6195
6296 } catch ( ex ) {
0 commit comments