From 360cd5ca950f8f837de8cc436a63e1edda4f0334 Mon Sep 17 00:00:00 2001 From: Andy Joslin Date: Mon, 8 Jul 2013 15:45:50 -0400 Subject: [PATCH] fix(tabs): make tabs added with active=true be selected --- src/tabs/tabs.js | 13 +++---------- src/tabs/test/tabsSpec.js | 22 ++++++++++++---------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/tabs/tabs.js b/src/tabs/tabs.js index 202fecafd8..d5b9ed12ae 100644 --- a/src/tabs/tabs.js +++ b/src/tabs/tabs.js @@ -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); } }; @@ -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; @@ -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; diff --git a/src/tabs/test/tabsSpec.js b/src/tabs/test/tabsSpec.js index 2068e1117f..d0f0d9d842 100644 --- a/src/tabs/test/tabsSpec.js +++ b/src/tabs/test/tabsSpec.js @@ -284,8 +284,8 @@ describe('tabs', function() { }); describe('tabset controller', function() { - function mockTab() { - return { active: false }; + function mockTab(isActive) { + return { active: !!isActive }; } var ctrl; @@ -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); + }); }); }); @@ -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');