mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-08 07:41:51 +08:00
Updated to use controller list not tabs
This is more inline with iOS style, where the controllers that show content drive the tabs, not the tab list.
This commit is contained in:
@ -6,57 +6,71 @@ TabBar.prototype = {};
|
||||
TabBarController = function(options) {
|
||||
this.tabBar = options.tabBar;
|
||||
|
||||
this.tabs = [];
|
||||
this.controllers = [];
|
||||
|
||||
// Bind or set our tabWillChange callback
|
||||
this.tabWillChange = options.tabWillChange || function(tab) {};
|
||||
this.tabChanged = options.tabChanged || function(tab) {};
|
||||
this.controllerWillChange = options.controllerWillChange || function(controller) {};
|
||||
this.controllerChanged = options.controllerChanged || function(controller) {};
|
||||
};
|
||||
|
||||
TabBarController.prototype = {
|
||||
selectTab: function(index) {
|
||||
selectController: function(index) {
|
||||
var shouldChange = true;
|
||||
|
||||
// Check if we should switch to this tab. This lets the app
|
||||
// cancel tab switches if the context isn't right, for example.
|
||||
if(this.tabWillChange) {
|
||||
if(this.tabWillChange(this.tabs[index], index) === false) {
|
||||
if(this.controllerWillChange) {
|
||||
if(this.controllerWillChange(this.controllers[index], index) === false) {
|
||||
shouldChange = false;
|
||||
}
|
||||
}
|
||||
|
||||
if(shouldChange) {
|
||||
this.selectedTab = this.tabs[index];
|
||||
this.tabChanged && this.tabChanged(this.selectedTab, index);
|
||||
this.setSelectedController(index);
|
||||
this.controllerChanged && this.controllerChanged(this.selectedController, this.selectedIndex);
|
||||
}
|
||||
},
|
||||
|
||||
// Force the selection of a controller at the given index
|
||||
setSelectedController: function(index) {
|
||||
this.selectedController = this.controllers[index];
|
||||
this.selectedIndex = index;
|
||||
},
|
||||
|
||||
_clearSelected: function() {
|
||||
this.selectedController = null;
|
||||
this.selectedIndex = -1;
|
||||
},
|
||||
|
||||
// Return the tab at the given index
|
||||
getTab: function(index) {
|
||||
return this.tabs[index];
|
||||
getController: function(index) {
|
||||
return this.controllers[index];
|
||||
},
|
||||
|
||||
// Return the current tab list
|
||||
getTabs: function() {
|
||||
return this.tabs;
|
||||
getControllers: function() {
|
||||
return this.controllers;
|
||||
},
|
||||
|
||||
// Get the currently selected tab
|
||||
getSelectedTab: function() {
|
||||
return this.selectedTab;
|
||||
getSelectedController: function() {
|
||||
return this.selectedController;
|
||||
},
|
||||
|
||||
// Add a tab
|
||||
addTab: function(tab) {
|
||||
this.tabs.push(tab);
|
||||
if(!this.selectedTab) {
|
||||
this.selectedTab = tab;
|
||||
addController: function(controller) {
|
||||
this.controllers.push(controller);
|
||||
|
||||
// If we don't have a selected controller yet, select the first one.
|
||||
if(!this.selectedController) {
|
||||
this.setSelectedController(0);
|
||||
}
|
||||
},
|
||||
|
||||
// Set the tabs and select the first
|
||||
setTabs: function(tabs) {
|
||||
this.tabs = tabs;
|
||||
this.selectTab(0);
|
||||
setControllers: function(controllers) {
|
||||
this.controllers = controllers;
|
||||
this._clearSelected();
|
||||
this.selectController(0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user