Skip to content
Permalink
Browse files

Added multiple button support and event / callback support to alerts

  • Loading branch information...
NachoMan committed May 27, 2009
1 parent 7b64d09 commit ce44b0760a2acd2d422075c8e5b56fa1fa086f0f
@@ -11,10 +11,14 @@
#import <AudioToolbox/AudioServices.h>
#import "PhoneGapCommand.h"

@interface Notification : PhoneGapCommand {
@interface Notification : PhoneGapCommand <UIAlertViewDelegate> {
UIAlertView* openURLAlert;
NSInteger alertCallbackId;
}

- (void)alert:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

- (void)activityStart:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
- (void)activityStop:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
- (void)vibrate:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@@ -10,22 +10,72 @@

@implementation Notification

/**
* Show a native alert window, with one or two buttons. Depending on the options given, it can customize
* the title, button labels, and can even be issued a callback to be invoked when a button is clicked.
*
* Additionally this command will issue a DOMEvent on the \c document element within JavaScript when a button
* is clicked. The \c alertClosed event will be fired, with \c buttonIndex and \c buttonLabel properties on
* the supplied event object.
*
* @brief show a native alert window
* @param arguments The message to display in the alert window
* @param options dictionary of options, notable options including:
* - \c title {String} title text
* - \c okLabel {String=OK} label of the OK button
* - \c cancelLabel {String} optional label for a second Cancel button
* - \c onClose {Integer} callback ID used to invoke a function in JavaScript when the alert window closes
* @see alertView:clickedButtonAtIndex
*/
- (void)alert:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSString* message = [arguments objectAtIndex:0];
NSString* title = [options objectForKey:@"title"];
NSString* button = [options objectForKey:@"buttonLabel"];
if (openURLAlert) {
NSLog(@"Cannot open an alert when one already exists");
return;
}

NSString* message = [arguments objectAtIndex:0];
NSString* title = [options objectForKey:@"title"];
NSString* okButton = [options objectForKey:@"okLabel"];
NSString* cancelButton = [options objectForKey:@"cancelLabel"];
NSInteger onCloseId = [(NSString*)[options objectForKey:@"onClose"] integerValue];

if (!title)
title = @"Alert";
if (!button)
button = @"OK";
if (!okButton)
okButton = @"OK";
if (onCloseId)
alertCallbackId = onCloseId;

openURLAlert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:okButton
otherButtonTitles:nil];
if (cancelButton)
[openURLAlert addButtonWithTitle:cancelButton];

UIAlertView *openURLAlert = [[UIAlertView alloc]
initWithTitle:title
message:message delegate:nil cancelButtonTitle:button otherButtonTitles:nil];
[openURLAlert show];
[openURLAlert release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonLabel = [alertView buttonTitleAtIndex:buttonIndex];
if (alertCallbackId) {
NSString *buttonIndexStr = [NSString stringWithFormat:@"%d", buttonIndex];
NSArray *arguments = [NSArray arrayWithObjects:buttonIndexStr, buttonLabel, nil];
[self fireCallback:alertCallbackId withArguments:arguments];
alertCallbackId = 0;
}
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"(function(){ "
"var e = document.createEvent('Events'); "
"e.initEvent('alertClosed', 'false', 'false'); "
"e.buttonIndex = %d; "
"e.buttonLabel = \"%@\"; "
"document.dispatchEvent(e); "
"})()", buttonIndex, [buttonLabel stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]]];
[openURLAlert release];
openURLAlert = nil;
}

- (void)activityStart:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
@@ -8,6 +8,7 @@

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <JSON/JSON.h>

@interface PhoneGapCommand : NSObject {
UIWebView* webView;
@@ -78,7 +78,6 @@ -(NSURL*) getLocalFileFor:(NSString*)filename
NSString *filePath = [mainBundle pathForResource:(NSString*)[filenameParts objectAtIndex:0]
ofType:(NSString*)[filenameParts objectAtIndex:1]
inDirectory:directoryStr];
NSLog(@"File: %@", filePath);
if (filePath == nil) {
NSLog(@"Can't find filename %@ in the app bundle", filename);
return nil;
@@ -30,7 +30,7 @@
}
function customAlert(){
navigator.notification.alert("Custom alert", "Custom title", "Yup!");
navigator.notification.alert("Custom alert", "Custom title", "Yup!", "Nope", { onClose: function(index, label) { debug.log("alert's onClick callback called on button " + index + " (" + label + ")") } });
}
function beep(){
@@ -206,11 +206,13 @@
deviceInfo();
document.addEventListener('startOrientationChange', function(e) { debug.log("Orientation changing to " + e.orientation); }, false);
document.addEventListener('stopOrientationChange', function(e) { debug.log("Orientation changed from " + e.orientation); }, false);
document.addEventListener('alertClosed', function(e) { debug.log("Alert box closed when user clicked button " + e.buttonIndex + " having title " + e.buttonLabel); }, false);
});
</script>
</head>
<body id="stage" class="theme">
<!--
<a href="javascript:addContact(true)">New Contact (GUI)</a>
<br />
<a href="javascript:addContact(false)">New Contact (API)</a>
@@ -229,6 +231,7 @@
<br />
<a href="javascript:contactsCount()">Contacts Count</a>
<br />
-->
<div id="Page1">
<h2>this file is located at iphone/www/index.html</h2>
<div id="info" class="Panel">
@@ -8,13 +8,17 @@ Notification.prototype.beep = function(count, volume) {
new Media('beep.wav').play();
};

Notification.prototype.alert = function(message, title, buttonLabel) {
var options = {};
if (title) options.title = title;
if (buttonLabel) options.buttonLabel = buttonLabel;
Notification.prototype.alert = function(message, title, okLabel, cancelLabel, options) {
var args = {};
if (title) args.title = title;
if (okLabel) args.okLabel = okLabel;
if (cancelLabel) args.cancelLabel = cancelLabel;
if (typeof(options) == 'object' && "onClose" in options)
args.onClose = PhoneGap.registerCallback(options.onClose);

debug.log(args);
if (PhoneGap.available)
PhoneGap.exec('Notification.alert', message, options);
PhoneGap.exec('Notification.alert', message, args);
else
alert(message);
};
@@ -9,9 +9,13 @@ function Notification() {
* Open a native alert dialog, with a customizable title and button text.
* @param {String} message Message to print in the body of the alert
* @param {String} [title="Alert"] Title of the alert dialog (default: Alert)
* @param {String} [buttonLabel="OK"] Label of the close button (default: OK)
* @param {String} [okLabel="OK"] Label of the first button (default: OK)
* @param {String} [cancelLabel] Label of the optional second button
* @param {Object} [options] Options to pass to the alert:
* - \c onClose {Function} callback handler to be called when the alert is closed; called with an Event
* object, with the \c buttonIndex and \c buttonLabel properties indicating which button was clicked.
*/
Notification.prototype.alert = function(message, title, buttonLabel) {
Notification.prototype.alert = function(message, title, buttonLabel, cancelLabel, options) {
// Default is to use a browser alert; this will use "index.html" as the title though
alert(message);
};
@@ -106,10 +106,8 @@ PhoneGap.unregisterCallback = function(id) {
* @returns {Object} returns whatever the callback returns, or \c null if the function has been removed.
*/
PhoneGap.invokeCallback = function(id, args) {
if (typeof(args) != 'array')
args = [];
var idx = id - 1;
if (typeof(PhoneGap.callbacks[idx]) != 'function')
return null;
return PhoneGap.callbacks[idx](args);
return PhoneGap.callbacks[idx].apply(null, args);
}

0 comments on commit ce44b07

Please sign in to comment.
You can’t perform that action at this time.