From d2355b556ae67cea9594ea838bd2eb050cffb647 Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Tue, 3 Jun 2014 15:38:19 +0300 Subject: [PATCH] confirm implemented for ios --- ui/dialogs/dialogs.d.ts | 41 ++++++++++++++++++++--- ui/dialogs/dialogs.ios.ts | 68 ++++++++++++++++++++++++++++++--------- 2 files changed, 89 insertions(+), 20 deletions(-) diff --git a/ui/dialogs/dialogs.d.ts b/ui/dialogs/dialogs.d.ts index f22674102..d7edd24a6 100644 --- a/ui/dialogs/dialogs.d.ts +++ b/ui/dialogs/dialogs.d.ts @@ -13,10 +13,16 @@ function alert(options: AlertOptions): promises.Promise; /** - * The confirm() method displays a dialog box with a specified message, along with an OK and a Cancel button. + * The confirm() method displays a dialog box with a specified message. * @param message Specifies the text to display in the confirm box. */ - function confirm(message: string): void; + 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; /** * The prompt() method displays a dialog box that prompts the visitor for input. @@ -35,13 +41,38 @@ message: string; /** - * Gets or sets the alert message. + * Gets or sets the alert title. */ title?: string; /** - * Gets or sets the request headers in JSON format. + * Gets or sets the button name. */ - buttonName?: any; + buttonName?: string; + } + + /** + * Provides options for the alert. + */ + interface ConfirmOptions { + /** + * Gets or sets the alert message. + */ + message: string; + + /** + * Gets or sets the alert title. + */ + title?: string; + + /** + * Gets or sets the OK button name. + */ + okButtonName?: string; + + /** + * Gets or sets the Cancel button name. + */ + cancelButtonName?: string; } } \ No newline at end of file diff --git a/ui/dialogs/dialogs.ios.ts b/ui/dialogs/dialogs.ios.ts index e0c4ca353..37520e1fc 100644 --- a/ui/dialogs/dialogs.ios.ts +++ b/ui/dialogs/dialogs.ios.ts @@ -3,27 +3,41 @@ */ import promises = require("promises"); +function createUIAlertView(options) { + var alert = new UIKit.UIAlertView(); + alert.title = options.title; + alert.message = options.message; + return alert; +} + +function createDelegate(callback) { + var delegateType = Foundation.NSObject.extends({}, {}).implements({ + protocol: "UIAlertViewDelegate", + implementation: { + alertViewClickedButtonAtIndex: function (view, index) { + callback(view, index); + } + } + }); + return new delegateType; +} + export function alert(arg: any): promises.Promise { var d = promises.defer(); try { var options = typeof arg === "string" ? { message: arg, title: "Alert", buttonName: "OK" } : arg - var alert = new UIKit.UIAlertView(); - alert.title = options.title; - alert.message = options.message; + + var alert = createUIAlertView(options); + alert.addButtonWithTitle(options.buttonName); - var delegateType = Foundation.NSObject.extends({}, {}).implements({ - protocol: "UIAlertViewDelegate", - implementation: { - alertViewClickedButtonAtIndex: function (view, index) { - d.resolve(); - // Remove the local variable for the delegate. - delegate = undefined; - } - } - }); // Assign first to local variable, otherwise it will be garbage collected since delegate is weak reference. - var delegate = new delegateType(); + var delegate = createDelegate(function (view, index) { + d.resolve(); + // Remove the local variable for the delegate. + delegate = undefined; + }); + alert.delegate = delegate; alert.show(); @@ -34,8 +48,32 @@ export function alert(arg: any): promises.Promise { return d.promise(); } -export function confirm(message: string): void { +export function confirm(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.addButtonWithTitle(options.okButtonName); + alert.addButtonWithTitle(options.cancelButtonName); + + // 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); + // Remove the local variable for the delegate. + delegate = undefined; + }); + + alert.delegate = delegate; + + alert.show(); + + } catch (ex) { + d.reject(ex); + } + + return d.promise(); } export function prompt(text: string, defaultText?: string): void {