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

Commit

Permalink
fix(tabs): make tabs added with active=true be selected
Browse files Browse the repository at this point in the history
  • Loading branch information
ajoslin committed Jul 8, 2013
1 parent 224bc2f commit 360cd5c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
13 changes: 3 additions & 10 deletions src/tabs/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function TabsetCtrl($scope, $element) {

ctrl.addTab = function addTab(tab) {
tabs.push(tab);
if (tabs.length == 1) {
if (tabs.length == 1 || tab.active) {
ctrl.select(tab);
}
};
Expand Down Expand Up @@ -186,16 +186,11 @@ function($parse, $http, $templateCache, $compile) {
compile: function(elm, attrs, transclude) {
return function postLink(scope, elm, attrs, tabsetCtrl) {
var getActive, setActive;
scope.active = false; // default value
if (attrs.active) {
getActive = $parse(attrs.active);
setActive = getActive.assign;
scope.$parent.$watch(getActive, function updateActive(value) {
if ( !!value && scope.disabled ) {
setActive(scope.$parent, false); // Prevent active assignment
} else {
scope.active = !!value;
}
scope.active = !!value;
});
} else {
setActive = getActive = angular.noop;
Expand Down Expand Up @@ -226,13 +221,11 @@ function($parse, $http, $templateCache, $compile) {
scope.$on('$destroy', function() {
tabsetCtrl.removeTab(scope);
});
//If the tabset sets this tab to active, set the parent scope's active
//binding too. We do this so the watch for the parent's initial active
//value won't overwrite what is initially set by the tabset
if (scope.active) {
setActive(scope.$parent, true);
}


//We need to transclude later, once the content container is ready.
//when this link happens, we're inside a tab heading.
scope.$transcludeFn = transclude;
Expand Down
22 changes: 12 additions & 10 deletions src/tabs/test/tabsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ describe('tabs', function() {
});

describe('tabset controller', function() {
function mockTab() {
return { active: false };
function mockTab(isActive) {
return { active: !!isActive };
}

var ctrl;
Expand Down Expand Up @@ -355,6 +355,16 @@ describe('tabs', function() {
ctrl.addTab(tab2);
expect(tab1.active).toBe(true);
});

it('should select a tab added that\'s already active', function() {
var tab1 = mockTab(), tab2 = mockTab(true);
ctrl.addTab(tab1);
expect(tab1.active).toBe(true);

ctrl.addTab(tab2);
expect(tab1.active).toBe(false);
expect(tab2.active).toBe(true);
});
});
});

Expand Down Expand Up @@ -434,14 +444,6 @@ describe('tabs', function() {
expectTabActive(scope.tabs[2]);
});

it('should not switch active when setting active=true', function() {
scope.$apply('tabs[2].active = true');
expectTabActive(scope.tabs[2]);

scope.$apply('tabs[3].active = true');
expectTabActive(scope.tabs[2]);
});

it('should toggle between states', function() {
expect(titles().eq(3)).toHaveClass('disabled');
scope.$apply('tabs[3].disabled = false');
Expand Down

0 comments on commit 360cd5c

Please sign in to comment.