prompt implemented for iOS

This commit is contained in:
Vladimir Enchev
2014-06-03 15:54:50 +03:00
parent d2355b556a
commit d01c22ff2a
2 changed files with 53 additions and 14 deletions

View File

@ -27,14 +27,19 @@
/** /**
* The prompt() method displays a dialog box that prompts the visitor for input. * The prompt() method displays a dialog box that prompts the visitor for input.
* @param text The text to display in the dialog box. * @param text The text to display in the dialog box.
* @param defaultText The default input value.
*/ */
function prompt(text: string, defaultText?: string): void; function prompt(text: string, defaultText?: string): void;
/**
* The prompt() method displays a dialog box that prompts the visitor for input.
* @param options The options for the dialog box.
*/
function prompt(options: PromptOptions): void;
/** /**
* Provides options for the alert. * Provides options for the alert.
*/ */
interface AlertOptions { interface DialogOptions {
/** /**
* Gets or sets the alert message. * Gets or sets the alert message.
*/ */
@ -44,7 +49,12 @@
* Gets or sets the alert title. * Gets or sets the alert title.
*/ */
title?: string; title?: string;
}
/**
* Provides options for the alert.
*/
interface AlertOptions extends DialogOptions {
/** /**
* Gets or sets the button name. * Gets or sets the button name.
*/ */
@ -54,17 +64,7 @@
/** /**
* Provides options for the alert. * Provides options for the alert.
*/ */
interface ConfirmOptions { interface ConfirmOptions extends DialogOptions {
/**
* Gets or sets the alert message.
*/
message: string;
/**
* Gets or sets the alert title.
*/
title?: string;
/** /**
* Gets or sets the OK button name. * Gets or sets the OK button name.
*/ */
@ -75,4 +75,14 @@
*/ */
cancelButtonName?: string; cancelButtonName?: string;
} }
/**
* Provides options for the alert.
*/
interface PromptOptions extends ConfirmOptions {
/**
* Gets or sets the default text.
*/
defaultText?: string;
}
} }

View File

@ -76,6 +76,35 @@ export function confirm(arg: any): promises.Promise<boolean> {
return d.promise(); return d.promise();
} }
export function prompt(text: string, defaultText?: string): void { export function prompt(arg: any): promises.Promise<string> {
var d = promises.defer<string>();
try {
var options = typeof arg === "string" ? { message: arg, title: "Alert", okButtonName: "OK", cancelButtonName: "Cancel" } : arg
var alert = createUIAlertView(options);
alert.alertViewStyle = UIKit.UIAlertViewStyle.UIAlertViewStylePlainTextInput;
alert.addButtonWithTitle(options.okButtonName);
alert.addButtonWithTitle(options.cancelButtonName);
var textField = alert.textFieldAtIndex(0);
textField.text = options.defaultText ? options.defaultText : "";
// Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference.
var delegate = createDelegate(function (view, index) {
if (index === 0) {
d.resolve(textField.text);
}
// Remove the local variable for the delegate.
delegate = undefined;
});
alert.delegate = delegate;
alert.show();
} catch (ex) {
d.reject(ex);
}
return d.promise();
} }