From b4e7a8f08ec85f3cdff06b866d696df7ea97664c Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Mon, 9 Jun 2014 12:05:31 +0300 Subject: [PATCH] dialogs refactored --- ui/dialogs/dialogs.android.ts | 38 ++++++++++++---------------- ui/dialogs/dialogs.d.ts | 40 ++++++++--------------------- ui/dialogs/dialogs.ios.ts | 47 +++++++++++++++++------------------ 3 files changed, 49 insertions(+), 76 deletions(-) diff --git a/ui/dialogs/dialogs.android.ts b/ui/dialogs/dialogs.android.ts index d803b2d22..58553419b 100644 --- a/ui/dialogs/dialogs.android.ts +++ b/ui/dialogs/dialogs.android.ts @@ -11,18 +11,18 @@ var STRING = "string", OK = "OK", CANCEL = "Cancel"; -function createAlertDialog(options: dialogs.DialogOptions): android.app.AlertDialog.Builder { +function createAlertDialog(message: string, options: dialogs.DialogOptions): android.app.AlertDialog.Builder { var alert = new android.app.AlertDialog.Builder(appmodule.android.foregroundActivity); - alert.setTitle(options.title); - alert.setMessage(options.message); + alert.setTitle(options && options.title ? options.title : ""); + alert.setMessage(message); return alert; } function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options: dialogs.ConfirmOptions, okCallback: Function, cancelCallback?: Function, neutralCallback?: Function): void { - if (options.okButtonName) { - alert.setPositiveButton(options.okButtonName, new android.content.DialogInterface.OnClickListener({ + if (options.okButtonText) { + alert.setPositiveButton(options.okButtonText, new android.content.DialogInterface.OnClickListener({ onClick: function (dialog: android.content.DialogInterface, id: number) { dialog.cancel(); okCallback(); @@ -30,8 +30,8 @@ function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options })); } - if (options.cancelButtonName) { - alert.setNegativeButton(options.cancelButtonName, new android.content.DialogInterface.OnClickListener({ + if (options.cancelButtonText) { + alert.setNegativeButton(options.cancelButtonText, new android.content.DialogInterface.OnClickListener({ onClick: function (dialog: android.content.DialogInterface, id: number) { dialog.cancel(); if (cancelCallback) { @@ -41,8 +41,8 @@ function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options })); } - if (options.otherButtonName) { - alert.setNeutralButton(options.otherButtonName, new android.content.DialogInterface.OnClickListener({ + if (options.otherButtonText) { + alert.setNeutralButton(options.otherButtonText, new android.content.DialogInterface.OnClickListener({ onClick: function (dialog: android.content.DialogInterface, id: number) { dialog.cancel(); if (neutralCallback) { @@ -53,14 +53,12 @@ function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options } } -export function alert(arg: any): promises.Promise { +export function alert(message: string, options = { title: ALERT, buttonText: OK }): promises.Promise { var d = promises.defer(); try { - var options = typeof arg === STRING ? { message: arg, title: ALERT, buttonName: OK } : arg + var alert = createAlertDialog(message, options); - var alert = createAlertDialog(options); - - alert.setPositiveButton(options.buttonName, new android.content.DialogInterface.OnClickListener({ + alert.setPositiveButton(options.buttonText, new android.content.DialogInterface.OnClickListener({ onClick: function (dialog: android.content.DialogInterface, id: number) { dialog.cancel(); d.resolve(); @@ -76,12 +74,10 @@ export function alert(arg: any): promises.Promise { return d.promise(); } -export function confirm(arg: any): promises.Promise { +export function confirm(message: string, options = { title: ALERT, okButtonText: OK, cancelButtonText: CANCEL }): promises.Promise { var d = promises.defer(); try { - var options = typeof arg === STRING ? { message: arg, title: ALERT, okButtonName: OK, cancelButtonName: CANCEL } : arg - - var alert = createAlertDialog(options); + var alert = createAlertDialog(message, options); addButtonsToAlertDialog(alert, options, function () { d.resolve(true); }, function () { d.resolve(false); }, function () { d.resolve(); }); @@ -94,12 +90,10 @@ export function confirm(arg: any): promises.Promise { return d.promise(); } -export function prompt(arg: any): promises.Promise { +export function prompt(message: string, options = { title: ALERT, okButtonText: OK, cancelButtonText: CANCEL, defaultText: "" }): promises.Promise { var d = promises.defer(); try { - var options = typeof arg === STRING ? { message: arg, title: ALERT, okButtonName: OK, cancelButtonName: CANCEL } : arg - - var alert = createAlertDialog(options); + var alert = createAlertDialog(message, options); var input = new android.widget.EditText(appmodule.android.context); input.setText(options.defaultText ? options.defaultText : ""); diff --git a/ui/dialogs/dialogs.d.ts b/ui/dialogs/dialogs.d.ts index 64ec483de..cc7b42bee 100644 --- a/ui/dialogs/dialogs.d.ts +++ b/ui/dialogs/dialogs.d.ts @@ -5,48 +5,28 @@ /** * The alert() method displays an alert box with a specified message. * @param message Specifies the text to display in the alert box. + * @param options Specifies the options for the alert box. Optional. */ - function alert(message: string): promises.Promise; - - /** - * The alert() method displays an alert box with a specified options. - * @param options Specifies the options for the alert box. - */ - function alert(options: AlertOptions): promises.Promise; + function alert(message: string, options?: AlertOptions): promises.Promise; /** * The confirm() method displays a dialog box with a specified message. * @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): promises.Promise; - - /** - * The confirm() method displays a dialog box with a specified message. - * @param options Specifies the options for the confirm box. - */ - function confirm(options: ConfirmOptions): promises.Promise; + function confirm(message: string, options?: ConfirmOptions): promises.Promise; /** * 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): promises.Promise; - - /** - * 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): promises.Promise; + function prompt(message: string, options?: PromptOptions): promises.Promise; /** * Provides options for the dialog. */ interface DialogOptions { - /** - * Gets or sets the alert message. - */ - message: string; - /** * Gets or sets the alert title. */ @@ -60,7 +40,7 @@ /** * Gets or sets the button name. */ - buttonName?: string; + buttonText?: string; } /** @@ -70,17 +50,17 @@ /** * Gets or sets the OK button name. */ - okButtonName?: string; + okButtonText?: string; /** * Gets or sets the Cancel button name. */ - cancelButtonName?: string; + cancelButtonText?: string; /** * Gets or sets the Cancel button name. */ - otherButtonName?: string; + otherButtonText?: string; } /** diff --git a/ui/dialogs/dialogs.ios.ts b/ui/dialogs/dialogs.ios.ts index b1e689fed..983f42e04 100644 --- a/ui/dialogs/dialogs.ios.ts +++ b/ui/dialogs/dialogs.ios.ts @@ -7,20 +7,22 @@ import view = require("ui/core/view"); var UIALERTVIEWDELEGATE = "UIAlertViewDelegate", STRING = "string", + PROMPT = "Prompt", + CONFIRM = "Confirm", ALERT = "Alert", OK = "OK", CANCEL = "Cancel"; -function createUIAlertView(options: dialogs.DialogOptions): UIKit.UIAlertView { +function createUIAlertView(message: string, options: dialogs.DialogOptions): UIKit.UIAlertView { var alert = new UIKit.UIAlertView(); - alert.title = options.title; - alert.message = options.message; + alert.title = options && options.title ? options.title : ""; + alert.message = message; return alert; } function createDelegate(callback) { var delegateType = Foundation.NSObject.extends({}, {}).implements({ - protocol: UIALERTVIEWDELEGATE, + protocol: "UIAlertViewDelegate", implementation: { alertViewClickedButtonAtIndex: function (view, index) { callback(view, index); @@ -31,28 +33,29 @@ function createDelegate(callback) { } function addButtonsToAlertDialog(alert: UIKit.UIAlertView, options: dialogs.ConfirmOptions): void { - if (options.okButtonName) { - alert.addButtonWithTitle(options.okButtonName); + if (!options) + return; + + if (options.okButtonText) { + alert.addButtonWithTitle(options.okButtonText); } - if (options.cancelButtonName) { - alert.addButtonWithTitle(options.cancelButtonName); + if (options.cancelButtonText) { + alert.addButtonWithTitle(options.cancelButtonText); } - if (options.otherButtonName) { - alert.addButtonWithTitle(options.otherButtonName); + if (options.otherButtonText) { + alert.addButtonWithTitle(options.otherButtonText); } } -export function alert(arg: any): promises.Promise { +export function alert(message: string, options = { title: ALERT, buttonText: OK }): promises.Promise { var d = promises.defer(); try { - var options = typeof arg === STRING ? { message: arg, title: ALERT, buttonName: OK } : arg + var alert = createUIAlertView(message, options); - var alert = createUIAlertView(options); - - if (options.buttonName) { - alert.addButtonWithTitle(options.buttonName); + if (options.buttonText) { + alert.addButtonWithTitle(options.buttonText); } // Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference. @@ -72,12 +75,10 @@ export function alert(arg: any): promises.Promise { return d.promise(); } -export function confirm(arg: any): promises.Promise { +export function confirm(message: string, options = { title: CONFIRM, okButtonText: OK, cancelButtonText: CANCEL }): promises.Promise { var d = promises.defer(); try { - var options = typeof arg === STRING ? { message: arg, title: ALERT, okButtonName: OK, cancelButtonName: CANCEL } : arg - - var alert = createUIAlertView(options); + var alert = createUIAlertView(message, options); addButtonsToAlertDialog(alert, options); @@ -99,12 +100,10 @@ export function confirm(arg: any): promises.Promise { return d.promise(); } -export function prompt(arg: any): promises.Promise { +export function prompt(message: string, options = { title: PROMPT, okButtonText: OK, cancelButtonText: CANCEL, defaultText: "" }): promises.Promise { var d = promises.defer(); try { - var options = typeof arg === STRING ? { message: arg, title: ALERT, okButtonName: OK, cancelButtonName: CANCEL } : arg - - var alert = createUIAlertView(options); + var alert = createUIAlertView(message, options); alert.alertViewStyle = UIKit.UIAlertViewStyle.UIAlertViewStylePlainTextInput; addButtonsToAlertDialog(alert, options);