alert. confirm and prompt options combined. defaultText now part of the confirm method signature

This commit is contained in:
Vladimir Enchev
2014-06-09 14:24:10 +03:00
parent 947c35d6b4
commit 63768427ba
3 changed files with 26 additions and 39 deletions

View File

@ -7,6 +7,8 @@ import appmodule = require("application");
import view = require("ui/core/view");
var STRING = "string",
PROMPT = "Prompt",
CONFIRM = "Confirm",
ALERT = "Alert",
OK = "OK",
CANCEL = "Cancel";
@ -18,7 +20,7 @@ function createAlertDialog(message: string, options: dialogs.DialogOptions): and
return alert;
}
function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options: dialogs.ConfirmOptions,
function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options: dialogs.DialogButtonOptions,
okCallback: Function, cancelCallback?: Function, neutralCallback?: Function): void {
if (options.okButtonText) {
@ -41,8 +43,8 @@ function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options
}));
}
if (options.otherButtonText) {
alert.setNeutralButton(options.otherButtonText, new android.content.DialogInterface.OnClickListener({
if (options.neutralButtonText) {
alert.setNeutralButton(options.neutralButtonText, new android.content.DialogInterface.OnClickListener({
onClick: function (dialog: android.content.DialogInterface, id: number) {
dialog.cancel();
if (neutralCallback) {
@ -53,12 +55,12 @@ function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options
}
}
export function alert(message: string, options = { title: ALERT, buttonText: OK }): promises.Promise<void> {
export function alert(message: string, options = { title: ALERT, okButtonText: OK }): promises.Promise<void> {
var d = promises.defer<void>();
try {
var alert = createAlertDialog(message, options);
alert.setPositiveButton(options.buttonText, new android.content.DialogInterface.OnClickListener({
alert.setPositiveButton(options.okButtonText, new android.content.DialogInterface.OnClickListener({
onClick: function (dialog: android.content.DialogInterface, id: number) {
dialog.cancel();
d.resolve();
@ -74,7 +76,7 @@ export function alert(message: string, options = { title: ALERT, buttonText: OK
return d.promise();
}
export function confirm(message: string, options = { title: ALERT, okButtonText: OK, cancelButtonText: CANCEL }): promises.Promise<boolean> {
export function confirm(message: string, options = { title: CONFIRM, okButtonText: OK, cancelButtonText: CANCEL }): promises.Promise<boolean> {
var d = promises.defer<boolean>();
try {
var alert = createAlertDialog(message, options);
@ -90,13 +92,13 @@ export function confirm(message: string, options = { title: ALERT, okButtonText:
return d.promise();
}
export function prompt(message: string, options = { title: ALERT, okButtonText: OK, cancelButtonText: CANCEL, defaultText: "" }): promises.Promise<dialogs.PromptResult> {
export function prompt(message: string, defaultText?: string, options = { title: PROMPT, okButtonText: OK, cancelButtonText: CANCEL }): promises.Promise<dialogs.PromptResult> {
var d = promises.defer<dialogs.PromptResult>();
try {
var alert = createAlertDialog(message, options);
var input = new android.widget.EditText(appmodule.android.context);
input.setText(options.defaultText ? options.defaultText : "");
input.setText(defaultText ? defaultText : "");
alert.setView(input);

View File

@ -14,14 +14,14 @@
* @param message Specifies the text to display in the confirm box.
* @param options Specifies the options for the confirm box. Optional.
*/
function confirm(message: string, options?: ConfirmOptions): promises.Promise<boolean>;
function confirm(message: string, options?: DialogButtonOptions): promises.Promise<boolean>;
/**
* The prompt() method displays a dialog box that prompts the visitor for input.
* @param message The text to display in the dialog box.
* @param options The options for the dialog box. Optional.
*/
function prompt(message: string, options?: PromptOptions): promises.Promise<string>;
function prompt(message: string, defaultText?: string, options?: DialogButtonOptions): promises.Promise<string>;
/**
* Provides options for the dialog.
@ -38,39 +38,24 @@
*/
interface AlertOptions extends DialogOptions {
/**
* Gets or sets the button name.
* Gets or sets the OK button text.
*/
buttonText?: string;
okButtonText?: string;
}
/**
* Provides options for the confirm.
*/
interface ConfirmOptions extends DialogOptions {
interface DialogButtonOptions extends AlertOptions {
/**
* Gets or sets the OK button name.
*/
okButtonText?: string;
/**
* Gets or sets the Cancel button name.
* Gets or sets the Cancel button text.
*/
cancelButtonText?: string;
/**
* Gets or sets the Cancel button name.
* Gets or sets the neutral button text.
*/
otherButtonText?: string;
}
/**
* Provides options for the prompt.
*/
interface PromptOptions extends ConfirmOptions {
/**
* Gets or sets the default text.
*/
defaultText?: string;
neutralButtonText?: string;
}
/**

View File

@ -32,7 +32,7 @@ function createDelegate(callback) {
return new delegateType;
}
function addButtonsToAlertDialog(alert: UIKit.UIAlertView, options: dialogs.ConfirmOptions): void {
function addButtonsToAlertDialog(alert: UIKit.UIAlertView, options: dialogs.DialogButtonOptions): void {
if (!options)
return;
@ -44,18 +44,18 @@ function addButtonsToAlertDialog(alert: UIKit.UIAlertView, options: dialogs.Conf
alert.addButtonWithTitle(options.cancelButtonText);
}
if (options.otherButtonText) {
alert.addButtonWithTitle(options.otherButtonText);
if (options.neutralButtonText) {
alert.addButtonWithTitle(options.neutralButtonText);
}
}
export function alert(message: string, options = { title: ALERT, buttonText: OK }): promises.Promise<void> {
export function alert(message: string, options = { title: ALERT, okButtonText: OK }): promises.Promise<void> {
var d = promises.defer<void>();
try {
var alert = createUIAlertView(message, options);
if (options.buttonText) {
alert.addButtonWithTitle(options.buttonText);
if (options.okButtonText) {
alert.addButtonWithTitle(options.okButtonText);
}
// Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference.
@ -100,7 +100,7 @@ export function confirm(message: string, options = { title: CONFIRM, okButtonTe
return d.promise();
}
export function prompt(message: string, options = { title: PROMPT, okButtonText: OK, cancelButtonText: CANCEL, defaultText: "" }): promises.Promise<dialogs.PromptResult> {
export function prompt(message: string, defaultText?: string, options = { title: PROMPT, okButtonText: OK, cancelButtonText: CANCEL, defaultText: "" }): promises.Promise<dialogs.PromptResult> {
var d = promises.defer<dialogs.PromptResult>();
try {
var alert = createUIAlertView(message, options);
@ -109,7 +109,7 @@ export function prompt(message: string, options = { title: PROMPT, okButtonText:
addButtonsToAlertDialog(alert, options);
var textField = alert.textFieldAtIndex(0);
textField.text = options.defaultText ? options.defaultText : "";
textField.text = defaultText ? defaultText : "";
// Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference.
var delegate = createDelegate(function (view, index) {