mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
104 lines
2.6 KiB
JavaScript
104 lines
2.6 KiB
JavaScript
IonicModule
|
|
.controller('$ionicTabs', [
|
|
'$scope',
|
|
'$ionicHistory',
|
|
'$element',
|
|
function($scope, $ionicHistory, $element) {
|
|
var _selectedTab = null;
|
|
var self = this;
|
|
self.tabs = [];
|
|
|
|
self.selectedIndex = function() {
|
|
return self.tabs.indexOf(_selectedTab);
|
|
};
|
|
self.selectedTab = function() {
|
|
return _selectedTab;
|
|
};
|
|
|
|
self.add = function(tab) {
|
|
$ionicHistory.registerHistory(tab);
|
|
self.tabs.push(tab);
|
|
};
|
|
|
|
self.remove = function(tab) {
|
|
var tabIndex = self.tabs.indexOf(tab);
|
|
if (tabIndex === -1) {
|
|
return;
|
|
}
|
|
//Use a field like '$tabSelected' so developers won't accidentally set it in controllers etc
|
|
if (tab.$tabSelected) {
|
|
self.deselect(tab);
|
|
//Try to select a new tab if we're removing a tab
|
|
if (self.tabs.length === 1) {
|
|
//do nothing if there are no other tabs to select
|
|
} else {
|
|
//Select previous tab if it's the last tab, else select next tab
|
|
var newTabIndex = tabIndex === self.tabs.length - 1 ? tabIndex - 1 : tabIndex + 1;
|
|
self.select(self.tabs[newTabIndex]);
|
|
}
|
|
}
|
|
self.tabs.splice(tabIndex, 1);
|
|
};
|
|
|
|
self.deselect = function(tab) {
|
|
if (tab.$tabSelected) {
|
|
_selectedTab = null;
|
|
tab.$tabSelected = false;
|
|
(tab.onDeselect || angular.noop)();
|
|
}
|
|
};
|
|
|
|
self.select = function(tab, shouldEmitEvent) {
|
|
var tabIndex;
|
|
if (angular.isNumber(tab)) {
|
|
tabIndex = tab;
|
|
if (tabIndex >= self.tabs.length) return;
|
|
tab = self.tabs[tabIndex];
|
|
} else {
|
|
tabIndex = self.tabs.indexOf(tab);
|
|
}
|
|
|
|
if (arguments.length === 1) {
|
|
shouldEmitEvent = !!(tab.navViewName || tab.uiSref);
|
|
}
|
|
|
|
if (_selectedTab && _selectedTab.$historyId == tab.$historyId) {
|
|
if (shouldEmitEvent) {
|
|
$ionicHistory.goToHistoryRoot(tab.$historyId);
|
|
}
|
|
} else {
|
|
forEach(self.tabs, function(tab) {
|
|
self.deselect(tab);
|
|
});
|
|
|
|
_selectedTab = tab;
|
|
//Use a funny name like $tabSelected so the developer doesn't overwrite the var in a child scope
|
|
tab.$tabSelected = true;
|
|
(tab.onSelect || angular.noop)();
|
|
|
|
if (shouldEmitEvent) {
|
|
$scope.$emit('$ionicHistory.change', {
|
|
type: 'tab',
|
|
tabIndex: tabIndex,
|
|
historyId: tab.$historyId,
|
|
navViewName: tab.navViewName,
|
|
hasNavView: !!tab.navViewName,
|
|
title: tab.title,
|
|
url: tab.href,
|
|
uiSref: tab.uiSref
|
|
});
|
|
}
|
|
}
|
|
};
|
|
|
|
self.hasActiveScope = function() {
|
|
for (var x = 0; x < self.tabs.length; x++) {
|
|
if ($ionicHistory.isActiveScope(self.tabs[x])) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
};
|
|
|
|
}]);
|