This repository has been archived by the owner on Apr 29, 2021. It is now read-only.
forked from purplecabbage/phonegap-plugins
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
NativeControls.m
724 lines (619 loc) · 23.2 KB
/
NativeControls.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
//
// NativeControls.h
//
//
// Created by Jesse MacFadyen on 10-02-03.
// MIT Licensed
// Originally this code was developed my Michael Nachbaur
// Formerly -> PhoneGap :: UIControls.h
// Created by Michael Nachbaur on 13/04/09.
// Copyright 2009 Decaf Ninja Software. All rights reserved.
#import "NativeControls.h"
#import <QuartzCore/QuartzCore.h>
@implementation NativeControls
#ifndef __IPHONE_3_0
@synthesize webView;
#endif
-(CDVPlugin*) initWithWebView:(UIWebView*)theWebView
{
self = (NativeControls*)[super initWithWebView:theWebView];
if (self)
{
tabBarItems = [[NSMutableDictionary alloc] initWithCapacity:5];
originalWebViewBounds = theWebView.bounds;
}
return self;
}
- (void)dealloc
{
if (tabBar)
[tabBar release];
if (toolBar)
{
[toolBarTitle release];
[toolBarItems release];
[toolBar release];
}
[super dealloc];
}
#pragma mark -
#pragma mark TabBar
/**
* Create a native tab bar at either the top or the bottom of the display.
* @brief creates a tab bar
* @param arguments unused
* @param options unused
*/
- (void)createTabBar:(NSArray*)arguments withDict:(NSDictionary*)options
{
tabBar = [UITabBar new];
tabBar.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[tabBar sizeToFit];
tabBar.delegate = self;
tabBar.multipleTouchEnabled = NO;
tabBar.autoresizesSubviews = YES;
tabBar.hidden = YES;
tabBar.userInteractionEnabled = YES;
tabBar.opaque = YES;
self.webView.superview.autoresizesSubviews = YES;
/* Styling hints REF UIInterface.h
tabBar.alpha = 0.5;
tabBar.tintColor = [UIColor colorWithRed:1.000 green:0.000 blue:0.000 alpha:1.000];
*/
[self.webView.superview addSubview:tabBar];
}
/**
* Show the tab bar after its been created.
* @brief show the tab bar
* @param arguments unused
* @param options used to indicate options for where and how the tab bar should be placed
* - \c height integer indicating the height of the tab bar (default: \c 49)
* - \c position specifies whether the tab bar will be placed at the \c top or \c bottom of the screen (default: \c bottom)
*/
- (void)showTabBar:(NSArray*)arguments withDict:(NSDictionary*)options
{
if (!tabBar)
[self createTabBar:nil withDict:nil];
// if we are calling this again when its shown, reset
if (!tabBar.hidden) {
return;
}
CGFloat height = 0.0f;
BOOL atBottom = YES;
// CGRect offsetRect = [ [UIApplication sharedApplication] statusBarFrame];
if (options)
{
height = [[options objectForKey:@"height"] floatValue];
atBottom = [[options objectForKey:@"position"] isEqualToString:@"bottom"];
}
if(height == 0)
{
height = 49.0f;
atBottom = YES;
}
tabBar.hidden = NO;
CGRect webViewBounds = originalWebViewBounds;
CGRect tabBarBounds;
NSNotification* notif = [NSNotification notificationWithName:@"CDVLayoutSubviewAdded" 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
);
}
[tabBar setFrame:tabBarBounds];
[self.webView setFrame:webViewBounds];
}
/**
* Hide the tab bar
* @brief hide the tab bar
* @param arguments unused
* @param options unused
*/
- (void)hideTabBar:(NSArray*)arguments withDict:(NSDictionary*)options
{
if (!tabBar)
[self createTabBar:nil withDict:nil];
tabBar.hidden = YES;
NSNotification* notif = [NSNotification notificationWithName:@"CDVLayoutSubviewRemoved" object:tabBar];
[[NSNotificationQueue defaultQueue] enqueueNotification:notif postingStyle: NSPostASAP];
[self.webView setFrame:originalWebViewBounds];
}
/**
* 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.
*
* If the supplied image name is one of the labels listed below, then this method will construct a tab button
* using the standard system buttons. Note that if you use one of the system images, that the \c title you supply will be ignored.
* - <b>Tab Buttons</b>
* - tabButton:More
* - tabButton:Favorites
* - tabButton:Featured
* - tabButton:TopRated
* - tabButton:Recents
* - tabButton:Contacts
* - tabButton:History
* - tabButton:Bookmarks
* - tabButton:Search
* - tabButton:Downloads
* - tabButton:MostRecent
* - tabButton:MostViewed
* @brief create a tab bar item
* @param arguments Parameters used to create the tab bar
* -# \c name internal name to refer to this tab by
* -# \c title title text to show on the tab, or null if no text should be shown
* -# \c image image filename or internal identifier to show, or null if now image should be shown
* -# \c tag unique number to be used as an internal reference to this button
* @param options Options for customizing the individual tab item
* - \c badge value to display in the optional circular badge on the item; if nil or unspecified, the badge will be hidden
*/
- (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) {
UITabBarSystemItem systemItem = -1;
if ([imageName isEqualToString:@"tabButton:More"]) systemItem = UITabBarSystemItemMore;
if ([imageName isEqualToString:@"tabButton:Favorites"]) systemItem = UITabBarSystemItemFavorites;
if ([imageName isEqualToString:@"tabButton:Featured"]) systemItem = UITabBarSystemItemFeatured;
if ([imageName isEqualToString:@"tabButton:TopRated"]) systemItem = UITabBarSystemItemTopRated;
if ([imageName isEqualToString:@"tabButton:Recents"]) systemItem = UITabBarSystemItemRecents;
if ([imageName isEqualToString:@"tabButton:Contacts"]) systemItem = UITabBarSystemItemContacts;
if ([imageName isEqualToString:@"tabButton:History"]) systemItem = UITabBarSystemItemHistory;
if ([imageName isEqualToString:@"tabButton:Bookmarks"]) systemItem = UITabBarSystemItemBookmarks;
if ([imageName isEqualToString:@"tabButton:Search"]) systemItem = UITabBarSystemItemSearch;
if ([imageName isEqualToString:@"tabButton:Downloads"]) systemItem = UITabBarSystemItemDownloads;
if ([imageName isEqualToString:@"tabButton:MostRecent"]) systemItem = UITabBarSystemItemMostRecent;
if ([imageName isEqualToString:@"tabButton:MostViewed"]) systemItem = UITabBarSystemItemMostViewed;
if (systemItem != -1)
item = [[UITabBarItem alloc] initWithTabBarSystemItem:systemItem tag:tag];
}
if (item == nil) {
item = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:imageName] tag:tag];
}
if ([options objectForKey:@"badge"])
item.badgeValue = [options objectForKey:@"badge"];
[tabBarItems setObject:item forKey:name];
[item release];
}
/**
* Update an existing tab bar item to change its badge value.
* @brief update the badge value on an existing tab bar item
* @param arguments Parameters used to identify the tab bar item to update
* -# \c name internal name used to represent this item when it was created
* @param options Options for customizing the individual tab item
* - \c badge value to display in the optional circular badge on the item; if nil or unspecified, the badge will be hidden
*/
- (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"];
}
/**
* Show previously created items on the tab bar
* @brief show a list of tab bar items
* @param arguments the item names to be shown
* @param options dictionary of options, notable options including:
* - \c animate indicates that the items should animate onto the tab bar
* @see createTabBarItem
* @see createTabBar
*/
- (void)showTabBarItems:(NSArray*)arguments withDict:(NSDictionary*)options
{
if (!tabBar)
[self createTabBar:nil withDict:nil];
int i, count = [arguments count];
NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:count];
for (i = 0; i < count; i++) {
NSString *itemName = [arguments objectAtIndex:i];
UITabBarItem *item = [tabBarItems objectForKey:itemName];
if (item)
[items addObject:item];
}
BOOL animateItems = NO;
if ([options objectForKey:@"animate"])
animateItems = [(NSString*)[options objectForKey:@"animate"] boolValue];
[tabBar setItems:items animated:animateItems];
[items release];
}
/**
* Manually select an individual tab bar item, or nil for deselecting a currently selected tab bar item.
* @brief manually select a tab bar item
* @param arguments the name of the tab bar item to select
* @see createTabBarItem
* @see showTabBarItems
*/
- (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)
tabBar.selectedItem = item;
else
tabBar.selectedItem = nil;
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSString * jsCallBack = [NSString stringWithFormat:@"window.plugins.nativeControls.tabBarItemSelected(%d);", item.tag];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
}
#pragma mark -
#pragma mark ToolBar
/*********************************************************************************/
- (void)createToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
{
CGFloat height = 45.0f;
BOOL atTop = YES;
UIBarStyle style = UIBarStyleBlack;
//UIBarStyle style = UIBarStyleDefault;
NSDictionary* toolBarSettings = options;//[settings objectForKey:@"ToolBarSettings"];
if (toolBarSettings)
{
if ([toolBarSettings objectForKey:@"height"])
height = [[toolBarSettings objectForKey:@"height"] floatValue];
if ([toolBarSettings objectForKey:@"position"])
atTop = [[toolBarSettings objectForKey:@"position"] isEqualToString:@"top"];
#pragma unused(atTop)
NSString *styleStr = [toolBarSettings objectForKey:@"style"];
if ([styleStr isEqualToString:@"Default"])
style = UIBarStyleDefault;
else if ([styleStr isEqualToString:@"BlackOpaque"])
style = UIBarStyleBlackOpaque;//deprecated
else if ([styleStr isEqualToString:@"BlackTranslucent"])
style = UIBarStyleBlackTranslucent;//deprecated
}
CGRect webViewBounds = self.webView.bounds;
CGRect toolBarBounds = CGRectMake(
webViewBounds.origin.x,
webViewBounds.origin.y - 1.0f,
webViewBounds.size.width,
height
);
webViewBounds = CGRectMake(
webViewBounds.origin.x,
webViewBounds.origin.y + height,
webViewBounds.size.width,
webViewBounds.size.height - height
);
toolBar = [[UIToolbar alloc] initWithFrame:toolBarBounds];
[toolBar sizeToFit];
toolBar.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
toolBar.hidden = NO;
toolBar.multipleTouchEnabled = NO;
toolBar.autoresizesSubviews = YES;
toolBar.userInteractionEnabled = YES;
toolBar.barStyle = style; //set in line: 324 above UIBarStyle style = UIBarStyleBlack;
/* Styling hints REF UIInterface.h
toolBar.alpha = 0.5;
toolBar.tintColor = [UIColor colorWithRed:1.000 green:0.000 blue:0.000 alpha:1.000];
*/
[toolBar setFrame:toolBarBounds];
[self.webView setFrame:webViewBounds];
[self.webView.superview addSubview:toolBar];
}
- (void)resetToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
{
NSLog(@"about to reset toolBarItems");
toolBarItems = nil;
/*
if (toolBarItems)
{
[toolBarItems release];
}
*/
}
/**
* Hide the tool bar
* @brief hide the tool bar
* @param arguments unused
* @param options unused
*/
- (void)hideToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
{
if (!toolBar)
[self createToolBar:nil withDict:nil];
toolBar.hidden = YES;
NSNotification* notif = [NSNotification notificationWithName:@"CDVLayoutSubviewRemoved" object:toolBar];
[[NSNotificationQueue defaultQueue] enqueueNotification:notif postingStyle: NSPostASAP];
[self.webView setFrame:originalWebViewBounds];
}
- (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;
}
}
/**
* 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]
*
*/
- (void)createToolBarItem:(NSArray*)arguments withDict:(NSDictionary*)options
{
if (!toolBar)
{
[self createToolBar:nil withDict:nil];
}
if (!toolBarItems)
{
toolBarItems = [[NSMutableArray alloc] initWithCapacity:1];
}
NSString *tagId = [arguments objectAtIndex:0];
NSString *title = [arguments objectAtIndex:1];
NSString *imageName = nil;
if (arguments.count > 2)
{
imageName = [arguments objectAtIndex:2];
}
NSString *style;
if (arguments.count >= 4)
{
style = [arguments objectAtIndex:3];
}
else
{
style = @"UIBarButtonItemStylePlain";
}
UIBarButtonItemStyle useStyle;
if ([style isEqualToString:@"UIBarButtonItemStyleBordered"])
{
useStyle = UIBarButtonItemStyleBordered;
}
else if ([style isEqualToString:@"UIBarButtonItemStyleDone"])
{
useStyle = UIBarButtonItemStyleDone;
}
else
{
useStyle = UIBarButtonItemStylePlain;
}
UIBarButtonItem *item = nil;
if (imageName && [imageName length] > 0)
{
UIBarButtonSystemItem systemItem = -1;
if ([imageName isEqualToString:@"UIBarButtonSystemItemDone"])
{
systemItem = UIBarButtonSystemItemDone;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemCancel"])
{
systemItem = UIBarButtonSystemItemCancel;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemEdit"])
{
systemItem = UIBarButtonSystemItemEdit;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemSave"])
{
systemItem = UIBarButtonSystemItemSave;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemAdd"])
{
systemItem = UIBarButtonSystemItemAdd;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemFlexibleSpace"])
{
systemItem = UIBarButtonSystemItemFlexibleSpace;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemFixedSpace"])
{
systemItem = UIBarButtonSystemItemFixedSpace;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemCompose"])
{
systemItem = UIBarButtonSystemItemCompose;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemReply"])
{
systemItem = UIBarButtonSystemItemReply;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemAction"])
{
systemItem = UIBarButtonSystemItemAction;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemOrganize"])
{
systemItem = UIBarButtonSystemItemOrganize;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemBookmarks"])
{
systemItem = UIBarButtonSystemItemBookmarks;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemSearch"])
{
systemItem = UIBarButtonSystemItemSearch;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemRefresh"])
{
systemItem = UIBarButtonSystemItemRefresh;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemStop"])
{
systemItem = UIBarButtonSystemItemStop;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemCamera"])
{
systemItem = UIBarButtonSystemItemCamera;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemTrash"])
{
systemItem = UIBarButtonSystemItemTrash;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemPlay"])
{
systemItem = UIBarButtonSystemItemPlay;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemPause"])
{
systemItem = UIBarButtonSystemItemPause;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemRewind"])
{
systemItem = UIBarButtonSystemItemRewind;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemFastForward"])
{
systemItem = UIBarButtonSystemItemFastForward;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemUndo"])
{
systemItem = UIBarButtonSystemItemUndo;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemRedo"])
{
systemItem = UIBarButtonSystemItemRedo;
}
else if ([imageName isEqualToString:@"UIBarButtonSystemItemPageCurl"])
{
systemItem = UIBarButtonSystemItemPageCurl;
}
if (systemItem)
{
item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem target:self action:@selector(toolBarButtonTapped:)];
if ([imageName isEqualToString:@"UIBarButtonSystemItemFixedSpace"])
{
item.width = 14;
}
}
else
{
item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:imageName] style:useStyle target:self action:@selector(toolBarButtonTapped:)];
}
}
else
{
item = [[UIBarButtonItem alloc] initWithTitle:title style:useStyle target:self action:@selector(toolBarButtonTapped:)];
}
[toolBarItems insertObject:item atIndex:[tagId intValue]];
[item release];
}
- (void)showToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
{
if (!toolBar)
{
[self createToolBar:nil withDict:nil];
}
[toolBar setItems:toolBarItems animated:NO];
}
- (void) toolBarButtonTapped:(UIBarButtonItem *)button
{
int count = 0;
for (UIBarButtonItem* currentButton in toolBarItems)
{
if (currentButton == button) {
NSString * jsCallBack = [NSString stringWithFormat:@"window.plugins.nativeControls.toolBarButtonTapped(%d);", count];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
return;
}
count++;
}
}
#pragma mark -
#pragma mark ActionSheet
- (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
];
int count = [arguments count];
for(int n = 0; n < count; n++)
{
[ actionSheet addButtonWithTitle:[arguments objectAtIndex:n]];
}
if([options objectForKey:@"cancelButtonIndex"])
{
actionSheet.cancelButtonIndex = [[options objectForKey:@"cancelButtonIndex"] intValue];
}
if([options objectForKey:@"destructiveButtonIndex"])
{
actionSheet.destructiveButtonIndex = [[options objectForKey:@"destructiveButtonIndex"] intValue];
}
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;//UIActionSheetStyleBlackOpaque;
[actionSheet showInView:self.webView.superview];
[actionSheet release];
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSString * jsCallBack = [NSString stringWithFormat:@"window.plugins.nativeControls._onActionSheetDismissed(%d);", buttonIndex];
[self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
}
@end