confirm implemented for ios

This commit is contained in:
Vladimir Enchev
2014-06-03 15:38:19 +03:00
parent 8440509a48
commit d2355b556a
2 changed files with 89 additions and 20 deletions

View File

@@ -13,10 +13,16 @@
function alert(options: AlertOptions): promises.Promise<any>;
/**
* 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<boolean>;
/**
* 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<boolean>;
/**
* 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;
}
}

View File

@@ -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<any> {
var d = promises.defer<any>();
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<any> {
return d.promise();
}
export function confirm(message: string): void {
export function confirm(arg: any): promises.Promise<boolean> {
var d = promises.defer<boolean>();
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 {