Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Don't select tabs when being destroyed #2763

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/tabs/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ angular.module('ui.bootstrap.tabs', [])

ctrl.removeTab = function removeTab(tab) {
var index = tabs.indexOf(tab);
//Select a new tab if the tab to be removed is selected
if (tab.active && tabs.length > 1) {
//Select a new tab if the tab to be removed is selected and not destroying
if (tab.active && tabs.length > 1 && !destroyed) {
//If this is the last tab, select the previous tab. else, the next tab.
var newActiveIndex = index == tabs.length - 1 ? index - 1 : index + 1;
ctrl.select(tabs[newActiveIndex]);
}
tabs.splice(index, 1);
};

var destroyed;
$scope.$on('$destroy', function() {
destroyed = true;
});
}])

/**
Expand Down
39 changes: 39 additions & 0 deletions src/tabs/test/tabs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,45 @@ describe('tabs', function() {
expect(titles().eq(1)).toHaveClass('active');
expect(contents().eq(1)).toHaveClass('active');
}));

it('should not select tabs when being destroyed', inject(function($controller, $compile, $rootScope){
var selectList = [],
deselectList = [],
getTab = function(active){
return {
active: active,
select : function(){
selectList.push('select');
},
deselect : function(){
deselectList.push('deselect');
}
};
};

scope = $rootScope.$new();
scope.tabs = [
getTab(true),
getTab(false)
];
elm = $compile([
'<tabset>',
' <tab ng-repeat="t in tabs" active="t.active" select="t.select()" deselect="t.deselect()">',
' <tab-heading><b>heading</b> {{index}}</tab-heading>',
' content {{$index}}',
' </tab>',
'</tabset>'
].join('\n'))(scope);
scope.$apply();

// The first tab is selected the during the initial $digest.
expect(selectList.length).toEqual(1);

// Destroy the tabs - we should not trigger selection/deselection any more.
scope.$destroy();
expect(selectList.length).toEqual(1);
expect(deselectList.length).toEqual(0);
}));
});

describe('disabled', function() {
Expand Down