Permalink
Browse files

Added ejecta.getText(); switched buttons for ejecta.openURL()

  • Loading branch information...
1 parent f840c3d commit 6968031ab3b12d1ad6d43ff18765be09fa8cdb0a @phoboslab committed Oct 28, 2012
Showing with 55 additions and 5 deletions.
  1. +6 −0 Classes/Ejecta/EJBindingEjectaCore.h
  2. +49 −5 Classes/Ejecta/EJBindingEjectaCore.m
@@ -1,8 +1,14 @@
#import <Foundation/Foundation.h>
#import "EJBindingBase.h"
+enum {
+ kEJCoreAlertViewOpenURL = 1,
+ kEJCoreAlertViewGetText
+};
+
@interface EJBindingEjectaCore : EJBindingBase {
NSString * urlToOpen;
+ JSObjectRef getTextCallback;
}
@end
@@ -5,6 +5,14 @@
@implementation EJBindingEjectaCore
+- (void)dealloc {
+ [urlToOpen release];
+ if( getTextCallback ) {
+ JSValueUnprotect([EJApp instance].jsGlobalContext, getTextCallback);
+ }
+ [super dealloc];
+}
+
EJ_BIND_FUNCTION(log, ctx, argc, argv ) {
if( argc < 1 ) return NULL;
@@ -48,7 +56,8 @@ @implementation EJBindingEjectaCore
urlToOpen = [url retain];
NSString * confirm = JSValueToNSString( ctx, argv[1] );
- UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Open Browser?" message:confirm delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
+ UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Open Browser?" message:confirm delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
+ alert.tag = kEJCoreAlertViewOpenURL;
[alert show];
[alert release];
}
@@ -58,12 +67,47 @@ @implementation EJBindingEjectaCore
return NULL;
}
+EJ_BIND_FUNCTION(getText, ctx, argc, argv) {
+ if( argc < 3 ) { return NULL; }
+
+ NSString * title = JSValueToNSString(ctx, argv[0]);
+ NSString * message = JSValueToNSString(ctx, argv[1]);
+
+ if( getTextCallback ) {
+ JSValueUnprotect(ctx, getTextCallback);
+ }
+ getTextCallback = JSValueToObject(ctx, argv[2], NULL);
+ JSValueProtect(ctx, getTextCallback);
+
+ UIAlertView * alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self
+ cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
+ alert.alertViewStyle = UIAlertViewStylePlainTextInput;
+ alert.tag = kEJCoreAlertViewGetText;
+ [alert show];
+ [alert release];
+ return NULL;
+}
+
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)index {
- if( index == 0 ) {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlToOpen]];
+ if( alertView.tag == kEJCoreAlertViewOpenURL ) {
+ if( index == 1 ) {
+ [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlToOpen]];
+ }
+ [urlToOpen release];
+ urlToOpen = nil;
+ }
+
+ else if( alertView.tag == kEJCoreAlertViewGetText ) {
+ NSString * text = @"";
+ if( index == 1 ) {
+ text = [[alertView textFieldAtIndex:0] text];
+ }
+ JSValueRef params[] = { NSStringToJSValue([EJApp instance].jsGlobalContext, text) };
+ [[EJApp instance] invokeCallback:getTextCallback thisObject:NULL argc:1 argv:params];
+
+ JSValueUnprotect([EJApp instance].jsGlobalContext, getTextCallback);
+ getTextCallback = NULL;
}
- [urlToOpen release];
- urlToOpen = nil;
}

0 comments on commit 6968031

Please sign in to comment.