Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ToolBar support added to the JS layer

  • Loading branch information...
commit f455a8746610d8447f67c89b1db7f3a549a82f28 1 parent 03f3119
@emile818 emile818 authored
View
6 iPhone/NativeControls/NativeControls.h
@@ -23,11 +23,11 @@
@interface NativeControls : PGPlugin <UITabBarDelegate, UIActionSheetDelegate> {
UITabBar* tabBar;
NSMutableDictionary* tabBarItems;
-
+
UIToolbar* toolBar;
UIBarButtonItem* toolBarTitle;
NSMutableArray* toolBarItems;
-
+
CGRect originalWebViewBounds;
}
@@ -41,6 +41,8 @@
- (void)updateTabBarItem:(NSArray*)arguments withDict:(NSDictionary*)options;
- (void)selectTabBarItem:(NSArray*)arguments withDict:(NSDictionary*)options;
+
+
/* Tool Bar methods
*/
- (void)createToolBar:(NSArray*)arguments withDict:(NSDictionary*)options;
View
250 iPhone/NativeControls/NativeControls.js
@@ -1,10 +1,12 @@
+// JS ::::::::
+
/*
-// This code is adapted from the work of:
-// Created by Michael Nachbaur on 13/04/09.
-// Copyright 2009 Decaf Ninja Software. All rights reserved.
-// MIT licensed
-*/
-
+ // This code is adapted from the work of:
+ // Created by Michael Nachbaur on 13/04/09.
+ // Copyright 2009 Decaf Ninja Software. All rights reserved.
+ // MIT licensed
+ */
+
/**
* This class exposes mobile phone interface controls to JavaScript, such as
* native tab and tool bars, etc.
@@ -12,17 +14,22 @@
*/
function NativeControls() {
this.tabBarTag = 0;
+ this.toolBarIndexes = 0;
+
this.tabBarCallbacks = {};
+ this.toolBarCallbacks = {};
+
+ this.tappedToolBarItem = null;
this.selectedTabBarItem = null;
}
-
+
/**
* Create a native tab bar that can have tab buttons added to it which can respond to events.
*/
NativeControls.prototype.createTabBar = function() {
PhoneGap.exec("NativeControls.createTabBar");
};
-
+
/**
* Show a tab bar. The tab bar has to be created first.
* @param {Object} [options] Options indicating how the tab bar should be shown:
@@ -33,7 +40,7 @@ NativeControls.prototype.showTabBar = function(options) {
if (!options) options = {'position' : 'bottom'};
PhoneGap.exec("NativeControls.showTabBar", options);
};
-
+
/**
* Hide a tab bar. The tab bar has to be created first.
*/
@@ -42,7 +49,7 @@ NativeControls.prototype.hideTabBar = function(animate) {
animate = true;
PhoneGap.exec("NativeControls.hideTabBar", { animate: animate });
};
-
+
/**
* Create a new tab bar item for use on a previously created tab bar. Use ::showTabBarItems to show the new item on the tab bar.
*
@@ -69,16 +76,16 @@ NativeControls.prototype.hideTabBar = function(animate) {
* - \c badge value to display in the optional circular badge on the item; if null or unspecified, the badge will be hidden
*/
NativeControls.prototype.createTabBarItem = function(name, label, image, options) {
-
- var tag = this.tabBarTag++;
+
+ var tag = this.tabBarTag++;
if (options && 'onSelect' in options && typeof(options['onSelect']) == 'function') {
this.tabBarCallbacks[tag] = {'onSelect':options.onSelect,'name':name};
//delete options.onSelect;
}
-
+
PhoneGap.exec("NativeControls.createTabBarItem", name, label, image, tag, options);
};
-
+
/**
* Update an existing tab bar item to change its badge value.
* @param {String} name internal name used to represent this item when it was created
@@ -89,7 +96,7 @@ NativeControls.prototype.updateTabBarItem = function(name, options) {
if (!options) options = {};
PhoneGap.exec("NativeControls.updateTabBarItem", name, options);
};
-
+
/**
* Show previously created items on the tab bar
* @param {String} arguments... the item names to be shown
@@ -105,8 +112,8 @@ NativeControls.prototype.showTabBarItems = function() {
}
PhoneGap.exec.apply(this, parameters);
};
-
-
+
+
/**
* Function to detect currently selected tab bar item
* @see createTabBarItem
@@ -115,8 +122,8 @@ NativeControls.prototype.showTabBarItems = function() {
NativeControls.prototype.getSelectedTabBarItem = function() {
return this.selectedTabBarItem;
};
-
-
+
+
/**
* Manually select an individual tab bar item, or nil for deselecting a currently selected tab bar item.
* @param {String} tabName the name of the tab to select, or null if all tabs should be deselected
@@ -126,98 +133,177 @@ NativeControls.prototype.getSelectedTabBarItem = function() {
NativeControls.prototype.selectTabBarItem = function(tab) {
PhoneGap.exec("NativeControls.selectTabBarItem", tab);
};
-
+
/**
* Function called when a tab bar item has been selected.
* @param {Number} tag the tag number for the item that has been selected
*/
-NativeControls.prototype.tabBarItemSelected = function(tag)
+NativeControls.prototype.tabBarItemSelected = function(tag)
{
- this.selectedTabBarItem = tag;
+ this.selectedTabBarItem = tag;
if (typeof(this.tabBarCallbacks[tag].onSelect) == 'function')
this.tabBarCallbacks[tag].onSelect(this.tabBarCallbacks[tag].name);
};
-
+
+
+
+
/**
* Create a toolbar.
*/
-NativeControls.prototype.createToolBar = function()
+NativeControls.prototype.createToolBar = function()
{
PhoneGap.exec("NativeControls.createToolBar");
};
-
/**
* Function called when a tab bar item has been selected.
* @param {String} title the title to set within the toolbar
*/
-NativeControls.prototype.setToolBarTitle = function(title)
+NativeControls.prototype.setToolBarTitle = function(title)
{
PhoneGap.exec("NativeControls.setToolBarTitle", title);
};
-
-
-
+/*
+ * Added by Emile khattar: emile818@gmail.com emile@sign.al
+ * @ 2011-07-08 , 5.00 AM
+ */
+/**
+ * Set toolBarItems = nil;
+ */
+NativeControls.prototype.resetToolBar = function() {
+ PhoneGap.exec("NativeControls.resetToolBar");
+};
+/**
+ * Hide the tool bar
+ * @brief hide the tool bar
+ */
+NativeControls.prototype.hideToolBar = function() {
+ PhoneGap.exec("NativeControls.hideToolBar");
+};
+
+/**
+ * Show the tool bar ( re-render elements )
+ * @brief Show the tool bar
+ */
+NativeControls.prototype.showToolBar = function() {
+ PhoneGap.exec("NativeControls.showToolBar");
+};
+
+/**
+ * Set the toolbar title
+ * @param: title
+ */
+NativeControls.prototype.setToolBarTitle = function(title) {
+ PhoneGap.exec("NativeControls.setToolBarTitle" , title );
+};
+
+
+/**
+ * Create a new tool bar button item for use on a previously created tool bar. Use ::showToolBar to show the new item on the tool bar.
+ *
+ * If the supplied image name is one of the labels listed below, then this method will construct a button
+ * using the standard system buttons. Note that if you use one of the system images, that the title you supply will be ignored.
+ *
+ * <b>Tool Bar Buttons</b>
+ * UIBarButtonSystemItemDone
+ * UIBarButtonSystemItemCancel
+ * UIBarButtonSystemItemEdit
+ * UIBarButtonSystemItemSave
+ * UIBarButtonSystemItemAdd
+ * UIBarButtonSystemItemFlexibleSpace
+ * UIBarButtonSystemItemFixedSpace
+ * UIBarButtonSystemItemCompose
+ * UIBarButtonSystemItemReply
+ * UIBarButtonSystemItemAction
+ * UIBarButtonSystemItemOrganize
+ * UIBarButtonSystemItemBookmarks
+ * UIBarButtonSystemItemSearch
+ * UIBarButtonSystemItemRefresh
+ * UIBarButtonSystemItemStop
+ * UIBarButtonSystemItemCamera
+ * UIBarButtonSystemItemTrash
+ * UIBarButtonSystemItemPlay
+ * UIBarButtonSystemItemPause
+ * UIBarButtonSystemItemRewind
+ * UIBarButtonSystemItemFastForward
+ * UIBarButtonSystemItemUndo, // iOS 3.0 and later
+ * UIBarButtonSystemItemRedo, // iOS 3.0 and later
+ * UIBarButtonSystemItemPageCurl, // iOS 4.0 and later
+ * @param {String} name internal name to refer to this tab by
+ * @param {String} [title] title text to show on the button, or null if no text should be shown
+ * @param {String} [image] image filename or internal identifier to show, or null if now image should be shown
+ * @param {Object} [options] Options for customizing the individual tab item [no option available at this time - this is for future proofing]
+ *
+ */
+NativeControls.prototype.createToolBarItem = function(name , title , image , options) {
+ var toolBarIndex = this.toolBarIndexes++;
+ if (options && 'onTap' in options && typeof(options['onTap']) == 'function') {
+ this.toolBarCallbacks[toolBarIndex] = {'onTap':options.onTap,'name':name};
+ //delete options.onSelect;
+ }
+ //modify the NativeControls.m to change the options quickly
+ // the instance name on the plugin can be passed with option for now it is hardcode in objc // Emile
+ PhoneGap.exec("NativeControls.createToolBarItem" , name , title , image , options );
+};
+
+/**
+ * Function called when a tool bar item has been tapped.
+ * @param {Number} tag the tag number for the item that has been selected
+ */
+NativeControls.prototype.toolBarButtonTapped = function(tag)
+{
+ this.tappedToolBarItem = tag;
+ if (typeof(this.toolBarCallbacks[tag].onTap) == 'function')
+ this.toolBarCallbacks[tag].onTap(this.toolBarCallbacks[tag].name);
+};
+
+
+
+
+
NativeControls.prototype.createActionSheet = function(buttonTitles,actionSheetTitle,cancelButtonIndex,destructiveButtonIndex)
{
- var options = {};
-
- if(actionSheetTitle != null)
- {
- options.title = actionSheetTitle;
- }
- if(cancelButtonIndex != null)
- {
- options.cancelButtonIndex = cancelButtonIndex;
- }
- if(destructiveButtonIndex != null)
- {
- options.destructiveButtonIndex = destructiveButtonIndex;
- }
-
- var params = [ "NativeControls.createActionSheet",options ];
- for (var i = 0; i < buttonTitles.length; i++)
- {
+ var options = {};
+
+ if(actionSheetTitle != null)
+ {
+ options.title = actionSheetTitle;
+ }
+ if(cancelButtonIndex != null)
+ {
+ options.cancelButtonIndex = cancelButtonIndex;
+ }
+ if(destructiveButtonIndex != null)
+ {
+ options.destructiveButtonIndex = destructiveButtonIndex;
+ }
+
+ var params = [ "NativeControls.createActionSheet",options ];
+ for (var i = 0; i < buttonTitles.length; i++)
+ {
params.push(buttonTitles[i]);
}
PhoneGap.exec.apply(this, params);
-
- this.actionSheetDelegate = {};
- return this.actionSheetDelegate;
+
+ this.actionSheetDelegate = {};
+ return this.actionSheetDelegate;
}
-
+
+
NativeControls.prototype._onActionSheetDismissed = function(index)
{
- this.actionSheetDelegate.onActionSheetDismissed(index);
-}
-
-PhoneGap.addConstructor(function()
-{
- if(!window.plugins)
- {
- window.plugins = {};
- }
- window.plugins.nativeControls = new NativeControls();
-});
-
-function StatusBar()
-{
-
+ this.actionSheetDelegate.onActionSheetDismissed(index);
}
-
-StatusBar.prototype.setHidden = function(bHide)
+
+
+
+NativeControls.prototype.setStatusBarVisibilty = function(bHide)
{
- PhoneGap.exec("StatusBar.setHidden",bHide);
+ PhoneGap.exec("StatusBar.setHidden",bHide);
}
-
-PhoneGap.addConstructor(
-
- function()
- {
- if (typeof window.plugins == "undefined")
- window.plugins = {};
-
- if (typeof window.plugins.statusBar == "undefined")
- window.plugins.statusBar = new StatusBar();
-
- }
- );
+
+
+if(!window.plugins)
+ window.plugins = {};
+
+ window.plugins.nativeControls = new NativeControls();
View
138 iPhone/NativeControls/NativeControls.m
@@ -87,11 +87,11 @@ - (void)showTabBar:(NSArray*)arguments withDict:(NSDictionary*)options
if (!tabBar.hidden) {
return;
}
-
+
CGFloat height = 0.0f;
BOOL atBottom = YES;
-// CGRect offsetRect = [ [UIApplication sharedApplication] statusBarFrame];
+ // CGRect offsetRect = [ [UIApplication sharedApplication] statusBarFrame];
if (options)
{
@@ -105,43 +105,43 @@ - (void)showTabBar:(NSArray*)arguments withDict:(NSDictionary*)options
}
tabBar.hidden = NO;
- CGRect webViewBounds = originalWebViewBounds;
- CGRect tabBarBounds;
+ CGRect webViewBounds = originalWebViewBounds;
+ CGRect tabBarBounds;
NSNotification* notif = [NSNotification notificationWithName:@"PGLayoutSubviewAdded" object:tabBar];
[[NSNotificationQueue defaultQueue] enqueueNotification:notif postingStyle: NSPostASAP];
- if (atBottom)
- {
- tabBarBounds = CGRectMake(
- webViewBounds.origin.x,
- webViewBounds.origin.y + webViewBounds.size.height - height,
- webViewBounds.size.width,
- height
- );
- webViewBounds = CGRectMake(
- webViewBounds.origin.x,
- webViewBounds.origin.y,
- webViewBounds.size.width,
- webViewBounds.size.height - height
- );
- }
- else
- {
- tabBarBounds = CGRectMake(
- webViewBounds.origin.x,
- webViewBounds.origin.y,
- webViewBounds.size.width,
- height
- );
- webViewBounds = CGRectMake(
- webViewBounds.origin.x,
- webViewBounds.origin.y + height,
- webViewBounds.size.width,
- webViewBounds.size.height - height
- );
- }
-
+ if (atBottom)
+ {
+ tabBarBounds = CGRectMake(
+ webViewBounds.origin.x,
+ webViewBounds.origin.y + webViewBounds.size.height - height,
+ webViewBounds.size.width,
+ height
+ );
+ webViewBounds = CGRectMake(
+ webViewBounds.origin.x,
+ webViewBounds.origin.y,
+ webViewBounds.size.width,
+ webViewBounds.size.height - height
+ );
+ }
+ else
+ {
+ tabBarBounds = CGRectMake(
+ webViewBounds.origin.x,
+ webViewBounds.origin.y,
+ webViewBounds.size.width,
+ height
+ );
+ webViewBounds = CGRectMake(
+ webViewBounds.origin.x,
+ webViewBounds.origin.y + height,
+ webViewBounds.size.width,
+ webViewBounds.size.height - height
+ );
+ }
+
[tabBar setFrame:tabBarBounds];
@@ -198,12 +198,12 @@ - (void)createTabBarItem:(NSArray*)arguments withDict:(NSDictionary*)options
{
if (!tabBar)
[self createTabBar:nil withDict:nil];
-
+
NSString *name = [arguments objectAtIndex:0];
NSString *title = [arguments objectAtIndex:1];
NSString *imageName = [arguments objectAtIndex:2];
int tag = [[arguments objectAtIndex:3] intValue];
-
+
UITabBarItem *item = nil;
if ([imageName length] > 0) {
UIBarButtonSystemItem systemItem = -1;
@@ -226,7 +226,7 @@ - (void)createTabBarItem:(NSArray*)arguments withDict:(NSDictionary*)options
if (item == nil) {
item = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:imageName] tag:tag];
}
-
+
if ([options objectForKey:@"badge"])
item.badgeValue = [options objectForKey:@"badge"];
@@ -247,11 +247,11 @@ - (void)updateTabBarItem:(NSArray*)arguments withDict:(NSDictionary*)options
{
if (!tabBar)
[self createTabBar:nil withDict:nil];
-
+
NSString *name = [arguments objectAtIndex:0];
UITabBarItem *item = [tabBarItems objectForKey:name];
if (item)
- item.badgeValue = [options objectForKey:@"badge"];
+ item.badgeValue = [options objectForKey:@"bad ge"];
}
@@ -296,7 +296,7 @@ - (void)selectTabBarItem:(NSArray*)arguments withDict:(NSDictionary*)options
{
if (!tabBar)
[self createTabBar:nil withDict:nil];
-
+
NSString *itemName = [arguments objectAtIndex:0];
UITabBarItem *item = [tabBarItems objectForKey:itemName];
if (item)
@@ -322,7 +322,7 @@ - (void)createToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
CGFloat height = 45.0f;
BOOL atTop = YES;
UIBarStyle style = UIBarStyleBlackOpaque;
-
+
NSDictionary* toolBarSettings = options;//[settings objectForKey:@"ToolBarSettings"];
if (toolBarSettings)
{
@@ -342,14 +342,14 @@ - (void)createToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
else if ([styleStr isEqualToString:@"BlackTranslucent"])
style = UIBarStyleBlackTranslucent;
}
-
+
CGRect webViewBounds = self.webView.bounds;
CGRect toolBarBounds = CGRectMake(
- webViewBounds.origin.x,
- webViewBounds.origin.y - 1.0f,
- webViewBounds.size.width,
- height
- );
+ webViewBounds.origin.x,
+ webViewBounds.origin.y - 1.0f,
+ webViewBounds.size.width,
+ height
+ );
webViewBounds = CGRectMake(
webViewBounds.origin.x,
webViewBounds.origin.y + height,
@@ -364,10 +364,10 @@ - (void)createToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
toolBar.userInteractionEnabled = YES;
toolBar.barStyle = style;
-
+
[toolBar setFrame:toolBarBounds];
[self.webView setFrame:webViewBounds];
-
+
[self.webView.superview addSubview:toolBar];
}
@@ -376,10 +376,10 @@ - (void)resetToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
NSLog(@"about to reset toolBarItems");
toolBarItems = nil;
/*
- if (toolBarItems)
- {
- [toolBarItems release];
- }
+ if (toolBarItems)
+ {
+ [toolBarItems release];
+ }
*/
}
@@ -407,11 +407,15 @@ - (void)setToolBarTitle:(NSArray*)arguments withDict:(NSDictionary*)options
{
if (!toolBar)
[self createToolBar:nil withDict:nil];
-
+
NSString *title = [arguments objectAtIndex:0];
+
+
if (!toolBarTitle) {
+ NSLog(@"not : %@", title);
toolBarTitle = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(toolBarTitleClicked)];
} else {
+ NSLog(@"is: %@", title);
toolBarTitle.title = title;
}
}
@@ -464,7 +468,7 @@ - (void)createToolBarItem:(NSArray*)arguments withDict:(NSDictionary*)options
{
toolBarItems = [[NSMutableArray alloc] initWithCapacity:1];
}
-
+
NSString *tagId = [arguments objectAtIndex:0];
NSString *title = [arguments objectAtIndex:1];
NSString *imageName;
@@ -483,7 +487,7 @@ - (void)createToolBarItem:(NSArray*)arguments withDict:(NSDictionary*)options
{
style = @"UIBarButtonItemStylePlain";
}
-
+
UIBarButtonItemStyle useStyle;
@@ -499,7 +503,7 @@ - (void)createToolBarItem:(NSArray*)arguments withDict:(NSDictionary*)options
{
useStyle = UIBarButtonItemStylePlain;
}
-
+
UIBarButtonItem *item = nil;
if ([imageName length] > 0)
{
@@ -637,7 +641,7 @@ - (void)showToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
- (void) toolBarButtonTapped:(UIBarButtonItem *)button
{
int count = 0;
-
+
for (UIBarButtonItem* currentButton in toolBarItems)
{
if (currentButton == button) {
@@ -657,15 +661,15 @@ - (void)createActionSheet:(NSArray*)arguments withDict:(NSDictionary*)options
{
NSString* title = [options objectForKey:@"title"];
-
+
UIActionSheet* actionSheet = [ [UIActionSheet alloc ]
- initWithTitle:title
- delegate:self
- cancelButtonTitle:nil
- destructiveButtonTitle:nil
- otherButtonTitles:nil
- ];
+ initWithTitle:title
+ delegate:self
+ cancelButtonTitle:nil
+ destructiveButtonTitle:nil
+ otherButtonTitles:nil
+ ];
int count = [arguments count];
for(int n = 0; n < count; n++)
@@ -689,7 +693,6 @@ - (void)createActionSheet:(NSArray*)arguments withDict:(NSDictionary*)options
}
-
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSString * jsCallBack = [NSString stringWithFormat:@"window.plugins.nativeControls._onActionSheetDismissed(%d);", buttonIndex];
@@ -697,4 +700,5 @@ - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSIn
}
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.