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

tab selection from controllers #87

Closed
wants to merge 3 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
6 changes: 5 additions & 1 deletion src/tabs/docs/demo.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<div ng-controller="TabsDemoCtrl">
<tabs>
<pane heading="Static title">Static content</pane>
<pane ng-repeat="pane in panes" heading="{{pane.title}}">{{pane.content}}</pane>
<pane ng-repeat="pane in panes" heading="{{pane.title}}" active="pane.active">{{pane.content}}</pane>
</tabs>
<div class="row-fluid">
<button class="btn" ng-click="panes[0].active = true">Select second tab</button>
<button class="btn" ng-click="panes[1].active = true">Select third tab</button>
</div>
</div>
26 changes: 23 additions & 3 deletions src/tabs/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ angular.module('ui.bootstrap.tabs', [])
.controller('TabsController', ['$scope', '$element', function($scope, $element) {
var panes = $scope.panes = [];

$scope.select = function selectPane(pane) {
this.select = $scope.select = function selectPane(pane) {
angular.forEach(panes, function(pane) {
pane.selected = false;
});
Expand Down Expand Up @@ -35,7 +35,7 @@ angular.module('ui.bootstrap.tabs', [])
replace: true
};
})
.directive('pane', function() {
.directive('pane', ['$parse', function($parse) {
return {
require: '^tabs',
restrict: 'E',
Expand All @@ -44,6 +44,26 @@ angular.module('ui.bootstrap.tabs', [])
heading:'@'
},
link: function(scope, element, attrs, tabsCtrl) {
var getSelected, setSelected;
scope.selected = false;
if (attrs.active) {
getSelected = $parse(attrs.active);
setSelected = getSelected.assign;
scope.$watch(
function watchSelected() {return getSelected(scope.$parent);},
function updateSelected(value) {scope.selected = value;}
);
scope.selected = getSelected ? getSelected(scope.$parent) : false;
}
scope.$watch('selected', function(selected) {
if(selected) {
tabsCtrl.select(scope);
}
if(setSelected) {
setSelected(scope.$parent, selected);
}
});

tabsCtrl.addPane(scope);
scope.$on('$destroy', function() {
tabsCtrl.removePane(scope);
Expand All @@ -52,4 +72,4 @@ angular.module('ui.bootstrap.tabs', [])
templateUrl: 'template/tabs/pane.html',
replace: true
};
});
}]);
54 changes: 54 additions & 0 deletions src/tabs/test/tabsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,60 @@ describe('tabs', function() {
});


describe('remote selection', function() {
var elm, scope;

// load the tabs code
beforeEach(module('ui.bootstrap.tabs'));

// load the templates
beforeEach(module('template/tabs/tabs.html', 'template/tabs/pane.html'));

beforeEach(inject(function($rootScope, $compile) {
// we might move this tpl into an html file as well...
elm = angular.element(
'<div>' +
'<tabs>' +
'<pane ng-repeat="pane in panes" active="pane.active" heading="pane.title">' +
'{{pane.content}}}' +
'</pane>' +
'</tabs>' +
'</div>'
);
scope = $rootScope;
scope.panes = [
{ title:"Dynamic Title 1", content:"Dynamic content 1", active:true},
{ title:"Dynamic Title 2", content:"Dynamic content 2" }
];

$compile(elm)(scope);
scope.$digest();
}));

it('should handle select attribute when select/deselect', function() {
var titles = elm.find('ul.nav-tabs li');
scope.$apply('panes[1].active=true');
expect(titles.eq(1)).toHaveClass('active');

titles.eq(0).find('a').click();

expect(scope.panes[1].active).toBe(false);
});

it('should select last active tab when multiple panes evaluate to active=true', function() {
var titles = elm.find('ul.nav-tabs li');
scope.$apply('panes[0].active=true;panes[1].active=true');
expect(titles.eq(1)).toHaveClass('active');
});

it('should deselect all panes when all atrributes set to false', function() {
var titles = elm.find('ul.nav-tabs li');
scope.$apply('panes[0].active=false');
expect(titles.eq(0)).not.toHaveClass('active');
expect(titles.eq(1)).not.toHaveClass('active');
});
});

describe('tabs controller', function() {
var scope, ctrl;

Expand Down