forked from johnsam/Titanium-Mobile-custom-tab-bar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomTabBar.js
executable file
·116 lines (92 loc) · 3.12 KB
/
customTabBar.js
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
CustomTabBar = function(settings) {
//tabGroup -- Required that the user set this!!!
settings.width = (typeof settings.width == 'undefined') ? 64 : settings.width; //64 is default for 5 icons, w/equal share
settings.height = (typeof settings.height == 'undefined') ? 40 : settings.height;
var tabBarItems = [];
var defaultImages = [];
var defineSettings = function() {
// watch the tab for change
assignClick(settings.tabBar);
for(var i = 0; i < settings.tabBar.tabs.length; i++)
{
var iconWidth = settings.tabBar.tabs[i].iconWidth == undefined ? 320 / settings.tabBar.tabs.length : settings.tabBar.tabs[i+1].width;
var iconHeight = settings.tabBar.tabs[i].iconHeight == undefined ? 40 : settings.tabBar.tabs[i+1].height;
defaultImages[i] = settings.tabBar.tabs[i].icon;
var pos = i;
// Go through each item and create an imageView
tabBarItems[i] = Titanium.UI.createImageView({
width: iconWidth,
height: iconHeight,
top: -8,
left: (320 / settings.tabBar.tabs.length) * pos
});
if(settings.tabBar.tabs[i].custom != undefined)
{
// image is the default image
tabBarItems[i].image = settings.tabBar.tabs[i].icon;
// clear out the default to prevent some odd coloring
settings.tabBar.tabs[i].icon = null;
}
else
{
tabBarItems[i].image = 'clear.png';
}
// Pass the item number (used later for changing tabs)
tabBarItems[i].pos = i;
// Add to the container window
customTabBar.add(tabBarItems[i]);
}
};
var assignClick = function(tabGroup) {
tabGroup.addEventListener('focus', function(e) {
var pos = e.index;
for(var i = 0; i < settings.tabBar.tabs.length; i++)
{
if(settings.tabBar.tabs[i].custom != undefined)
{
// image is the selected image
tabBarItems[i].image = defaultImages[i];
}
}
if(settings.tabBar.tabs[pos] != undefined && settings.tabBar.tabs[pos].selectedIcon != undefined) {
tabBarItems[pos].image = settings.tabBar.tabs[pos].selectedIcon;
}
});
};
// Create the container for our tab items
var customTabBar = Ti.UI.createWindow({
height: settings.height,
bottom: 0,
touchEnabled: false
});
var slideOut = function(speed) {
speed = speed || 300;
var hideAnimation = Titanium.UI.createAnimation({
bottom: -50,
duration: speed
});
customTabBar.animate(hideAnimation);
settings.tabBar.animate(hideAnimation);
};
var slideIn = function(speed) {
speed = speed || 300;
var showAnimation = Titanium.UI.createAnimation({
bottom: 0,
duration: speed
});
customTabBar.animate(showAnimation);
settings.tabBar.animate(showAnimation);
};
// Display the container and it's items
customTabBar.open();
// Set the first item as current :)
defineSettings();
if(settings.tabBar.tabs[0].custom != undefined)
{
tabBarItems[0].image = settings.tabBar.tabs[0].selectedIcon;
}
return {
hide: function(speed) { slideOut(speed); },
show: function(speed) { slideIn(speed); }
};
};