From 90661665150192691d646145f7502ecc8ca861da Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Fri, 6 Jun 2014 15:50:41 +0300 Subject: [PATCH] dialogs improved with neutral button and ConfirmResult for confirm and prompt dialogs --- ui/dialogs/dialogs.android.ts | 64 +++++++++++++++++++++++------------ ui/dialogs/dialogs.d.ts | 20 +++++++++++ ui/dialogs/dialogs.ios.ts | 50 +++++++++++++++++++++------ 3 files changed, 101 insertions(+), 33 deletions(-) diff --git a/ui/dialogs/dialogs.android.ts b/ui/dialogs/dialogs.android.ts index 5ab9342e6..d803b2d22 100644 --- a/ui/dialogs/dialogs.android.ts +++ b/ui/dialogs/dialogs.android.ts @@ -18,23 +18,39 @@ function createAlertDialog(options: dialogs.DialogOptions): android.app.AlertDia return alert; } -function addOkCancelButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options: dialogs.ConfirmOptions, - okCallback: Function, cancelCallback?: Function): void { - alert.setPositiveButton(options.okButtonName, new android.content.DialogInterface.OnClickListener({ - onClick: function (dialog: android.content.DialogInterface, id: number) { - dialog.cancel(); - okCallback(); - } - })); +function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options: dialogs.ConfirmOptions, + okCallback: Function, cancelCallback?: Function, neutralCallback?: Function): void { - alert.setNegativeButton(options.cancelButtonName, new android.content.DialogInterface.OnClickListener({ - onClick: function (dialog: android.content.DialogInterface, id: number) { - dialog.cancel(); - if (cancelCallback) { - cancelCallback(); + if (options.okButtonName) { + alert.setPositiveButton(options.okButtonName, new android.content.DialogInterface.OnClickListener({ + onClick: function (dialog: android.content.DialogInterface, id: number) { + dialog.cancel(); + okCallback(); } - } - })); + })); + } + + if (options.cancelButtonName) { + alert.setNegativeButton(options.cancelButtonName, new android.content.DialogInterface.OnClickListener({ + onClick: function (dialog: android.content.DialogInterface, id: number) { + dialog.cancel(); + if (cancelCallback) { + cancelCallback(); + } + } + })); + } + + if (options.otherButtonName) { + alert.setNeutralButton(options.otherButtonName, new android.content.DialogInterface.OnClickListener({ + onClick: function (dialog: android.content.DialogInterface, id: number) { + dialog.cancel(); + if (neutralCallback) { + neutralCallback(); + } + } + })); + } } export function alert(arg: any): promises.Promise { @@ -67,7 +83,7 @@ export function confirm(arg: any): promises.Promise { var alert = createAlertDialog(options); - addOkCancelButtonsToAlertDialog(alert, options, function () { d.resolve(true); }, function () { d.resolve(false); }); + addButtonsToAlertDialog(alert, options, function () { d.resolve(true); }, function () { d.resolve(false); }, function () { d.resolve(); }); alert.show(); @@ -78,8 +94,8 @@ export function confirm(arg: any): promises.Promise { return d.promise(); } -export function prompt(arg: any): promises.Promise { - var d = promises.defer(); +export function prompt(arg: any): promises.Promise { + var d = promises.defer(); try { var options = typeof arg === STRING ? { message: arg, title: ALERT, okButtonName: OK, cancelButtonName: CANCEL } : arg @@ -90,7 +106,11 @@ export function prompt(arg: any): promises.Promise { alert.setView(input); - addOkCancelButtonsToAlertDialog(alert, options, function () { d.resolve(input.getText().toString()); }); + var getText = function () { return input.getText().toString(); }; + + addButtonsToAlertDialog(alert, options, function () { d.resolve({ result: true, text: getText() }); }, + function () { d.resolve({ result: false, text: getText() }); }, + function () { d.resolve({ result: undefined, text: getText() }); }); alert.show(); @@ -105,7 +125,7 @@ export class Dialog { private _dialog: android.app.AlertDialog; private _android: android.app.AlertDialog.Builder; private _title: string; - private _view: view.View; + //private _view: view.View; constructor() { this._android = new android.app.AlertDialog.Builder(appmodule.android.foregroundActivity); @@ -122,14 +142,14 @@ export class Dialog { this._title = value; this.android.setTitle(this._title); } - + /* get view(): view.View { return this._view; } set view(value: view.View) { this._view = value; this.android.setView(this._view.android); - } + }*/ public show() { this._dialog = this.android.show(); diff --git a/ui/dialogs/dialogs.d.ts b/ui/dialogs/dialogs.d.ts index 97b69bda4..64ec483de 100644 --- a/ui/dialogs/dialogs.d.ts +++ b/ui/dialogs/dialogs.d.ts @@ -76,6 +76,11 @@ * Gets or sets the Cancel button name. */ cancelButtonName?: string; + + /** + * Gets or sets the Cancel button name. + */ + otherButtonName?: string; } /** @@ -88,6 +93,21 @@ defaultText?: string; } + /** + * Provides result data from the prompt dialog. + */ + interface PromptResult { + /** + * Gets or sets the prompt dialog boolean result. + */ + result: boolean; + + /** + * Gets or sets the text entered in the prompt dialog. + */ + text: string; + } + export class Dialog { /** * Shows the dialog. diff --git a/ui/dialogs/dialogs.ios.ts b/ui/dialogs/dialogs.ios.ts index 1a7bfdb61..b1e689fed 100644 --- a/ui/dialogs/dialogs.ios.ts +++ b/ui/dialogs/dialogs.ios.ts @@ -3,6 +3,7 @@ */ import promises = require("promises"); import dialogs = require("ui/dialogs"); +import view = require("ui/core/view"); var UIALERTVIEWDELEGATE = "UIAlertViewDelegate", STRING = "string", @@ -29,6 +30,20 @@ function createDelegate(callback) { return new delegateType; } +function addButtonsToAlertDialog(alert: UIKit.UIAlertView, options: dialogs.ConfirmOptions): void { + if (options.okButtonName) { + alert.addButtonWithTitle(options.okButtonName); + } + + if (options.cancelButtonName) { + alert.addButtonWithTitle(options.cancelButtonName); + } + + if (options.otherButtonName) { + alert.addButtonWithTitle(options.otherButtonName); + } +} + export function alert(arg: any): promises.Promise { var d = promises.defer(); try { @@ -36,7 +51,9 @@ export function alert(arg: any): promises.Promise { var alert = createUIAlertView(options); - alert.addButtonWithTitle(options.buttonName); + if (options.buttonName) { + alert.addButtonWithTitle(options.buttonName); + } // Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference. var delegate = createDelegate(function (view, index) { @@ -62,12 +79,11 @@ export function confirm(arg: any): promises.Promise { var alert = createUIAlertView(options); - alert.addButtonWithTitle(options.okButtonName); - alert.addButtonWithTitle(options.cancelButtonName); + addButtonsToAlertDialog(alert, options); // Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference. var delegate = createDelegate(function (view, index) { - d.resolve(index === 0); + d.resolve(index === 2 ? undefined : index === 0); // Remove the local variable for the delegate. delegate = undefined; }); @@ -83,24 +99,22 @@ export function confirm(arg: any): promises.Promise { return d.promise(); } -export function prompt(arg: any): promises.Promise { - var d = promises.defer(); +export function prompt(arg: any): 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); alert.alertViewStyle = UIKit.UIAlertViewStyle.UIAlertViewStylePlainTextInput; - alert.addButtonWithTitle(options.okButtonName); - alert.addButtonWithTitle(options.cancelButtonName); + + addButtonsToAlertDialog(alert, options); 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); - } + d.resolve({ result: index === 2 ? undefined : index === 0, text: textField.text }); // Remove the local variable for the delegate. delegate = undefined; }); @@ -118,6 +132,8 @@ export function prompt(arg: any): promises.Promise { export class Dialog { private _ios: UIKit.UIAlertView; + //private _view: view.View; + //private _nativeView: UIKit.UIView; constructor() { this._ios = new UIKit.UIAlertView(); @@ -133,6 +149,18 @@ export class Dialog { set title(value: string) { this.ios.title = value; } + /* + get view(): view.View { + return this._view; + } + set view(value: view.View) { + this._view = value; + this._nativeView = this._view.ios; + this._nativeView.removeFromSuperview(); + + // Not working on iOS7! + this.ios.addSubview(this._nativeView); + }*/ public show() { this.ios.show();