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

Commit a75a85d

Browse files
just-borisajoslin
authored andcommitted
feat(tabs): Add feature to select tabs through active attribute binding
1 parent 06a60e7 commit a75a85d

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

src/tabs/docs/demo.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<div ng-controller="TabsDemoCtrl">
22
<tabs>
33
<pane heading="Static title">Static content</pane>
4-
<pane ng-repeat="pane in panes" heading="{{pane.title}}">{{pane.content}}</pane>
4+
<pane ng-repeat="pane in panes" heading="{{pane.title}}" active="pane.active">{{pane.content}}</pane>
55
</tabs>
6+
<div class="row-fluid">
7+
<button class="btn" ng-click="panes[0].active = true">Select second tab</button>
8+
<button class="btn" ng-click="panes[1].active = true">Select third tab</button>
9+
</div>
610
</div>

src/tabs/tabs.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ angular.module('ui.bootstrap.tabs', [])
22
.controller('TabsController', ['$scope', '$element', function($scope, $element) {
33
var panes = $scope.panes = [];
44

5-
$scope.select = function selectPane(pane) {
5+
this.select = $scope.select = function selectPane(pane) {
66
angular.forEach(panes, function(pane) {
77
pane.selected = false;
88
});
@@ -35,7 +35,7 @@ angular.module('ui.bootstrap.tabs', [])
3535
replace: true
3636
};
3737
})
38-
.directive('pane', function() {
38+
.directive('pane', ['$parse', function($parse) {
3939
return {
4040
require: '^tabs',
4141
restrict: 'EA',
@@ -44,6 +44,26 @@ angular.module('ui.bootstrap.tabs', [])
4444
heading:'@'
4545
},
4646
link: function(scope, element, attrs, tabsCtrl) {
47+
var getSelected, setSelected;
48+
scope.selected = false;
49+
if (attrs.active) {
50+
getSelected = $parse(attrs.active);
51+
setSelected = getSelected.assign;
52+
scope.$watch(
53+
function watchSelected() {return getSelected(scope.$parent);},
54+
function updateSelected(value) {scope.selected = value;}
55+
);
56+
scope.selected = getSelected ? getSelected(scope.$parent) : false;
57+
}
58+
scope.$watch('selected', function(selected) {
59+
if(selected) {
60+
tabsCtrl.select(scope);
61+
}
62+
if(setSelected) {
63+
setSelected(scope.$parent, selected);
64+
}
65+
});
66+
4767
tabsCtrl.addPane(scope);
4868
scope.$on('$destroy', function() {
4969
tabsCtrl.removePane(scope);
@@ -52,4 +72,4 @@ angular.module('ui.bootstrap.tabs', [])
5272
templateUrl: 'template/tabs/pane.html',
5373
replace: true
5474
};
55-
});
75+
}]);

src/tabs/test/tabsSpec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,60 @@ describe('tabs', function() {
8888
});
8989

9090

91+
describe('remote selection', function() {
92+
var elm, scope;
93+
94+
// load the tabs code
95+
beforeEach(module('ui.bootstrap.tabs'));
96+
97+
// load the templates
98+
beforeEach(module('template/tabs/tabs.html', 'template/tabs/pane.html'));
99+
100+
beforeEach(inject(function($rootScope, $compile) {
101+
// we might move this tpl into an html file as well...
102+
elm = angular.element(
103+
'<div>' +
104+
'<tabs>' +
105+
'<pane ng-repeat="pane in panes" active="pane.active" heading="pane.title">' +
106+
'{{pane.content}}}' +
107+
'</pane>' +
108+
'</tabs>' +
109+
'</div>'
110+
);
111+
scope = $rootScope;
112+
scope.panes = [
113+
{ title:"Dynamic Title 1", content:"Dynamic content 1", active:true},
114+
{ title:"Dynamic Title 2", content:"Dynamic content 2" }
115+
];
116+
117+
$compile(elm)(scope);
118+
scope.$digest();
119+
}));
120+
121+
it('should handle select attribute when select/deselect', function() {
122+
var titles = elm.find('ul.nav-tabs li');
123+
scope.$apply('panes[1].active=true');
124+
expect(titles.eq(1)).toHaveClass('active');
125+
126+
titles.eq(0).find('a').click();
127+
128+
expect(scope.panes[1].active).toBe(false);
129+
});
130+
131+
it('should select last active tab when multiple panes evaluate to active=true', function() {
132+
var titles = elm.find('ul.nav-tabs li');
133+
scope.$apply('panes[0].active=true;panes[1].active=true');
134+
expect(titles.eq(1)).toHaveClass('active');
135+
});
136+
137+
it('should deselect all panes when all atrributes set to false', function() {
138+
var titles = elm.find('ul.nav-tabs li');
139+
scope.$apply('panes[0].active=false');
140+
expect(titles.eq(0)).not.toHaveClass('active');
141+
expect(titles.eq(1)).not.toHaveClass('active');
142+
});
143+
});
144+
91145
describe('tabs controller', function() {
92146
var scope, ctrl;
93147

0 commit comments

Comments
 (0)