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

Commit 220e7b6

Browse files
lgalfasoajoslin
authored andcommitted
feat(tabs): add the ability to set the direction of the tabs
Add the ability to set the direction of the tabs, the possible values are 'right', 'left' and 'below'. If no direction is defined the tabs are rendered on the top as they do now Closes #659
1 parent d9e5bc9 commit 220e7b6

File tree

4 files changed

+87
-7
lines changed

4 files changed

+87
-7
lines changed

src/tabs/tabs.js

+31-3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ function TabsetCtrl($scope, $element) {
5656
* Tabset is the outer container for the tabs directive
5757
*
5858
* @param {boolean=} vertical Whether or not to use vertical styling for the tabs.
59+
* @param {string=} direction What direction the tabs should be rendered. Available:
60+
* 'right', 'left', 'below'.
5961
*
6062
* @example
6163
<example module="ui.bootstrap">
@@ -77,12 +79,19 @@ function TabsetCtrl($scope, $element) {
7779
restrict: 'EA',
7880
transclude: true,
7981
replace: true,
82+
require: '^tabset',
8083
scope: {},
8184
controller: 'TabsetController',
8285
templateUrl: 'template/tabs/tabset.html',
83-
link: function(scope, element, attrs) {
84-
scope.vertical = angular.isDefined(attrs.vertical) ? scope.$eval(attrs.vertical) : false;
85-
scope.type = angular.isDefined(attrs.type) ? scope.$parent.$eval(attrs.type) : 'tabs';
86+
compile: function(elm, attrs, transclude) {
87+
return function(scope, element, attrs, tabsetCtrl) {
88+
scope.vertical = angular.isDefined(attrs.vertical) ? scope.$eval(attrs.vertical) : false;
89+
scope.type = angular.isDefined(attrs.type) ? scope.$parent.$eval(attrs.type) : 'tabs';
90+
scope.direction = angular.isDefined(attrs.direction) ? scope.$parent.$eval(attrs.direction) : 'top';
91+
scope.tabsAbove = (scope.direction != 'below');
92+
tabsetCtrl.$scope = scope;
93+
tabsetCtrl.$transcludeFn = transclude;
94+
};
8695
}
8796
};
8897
})
@@ -284,5 +293,24 @@ function($parse, $http, $templateCache, $compile) {
284293
}
285294
}])
286295

296+
.directive('tabsetTitles', function($http) {
297+
return {
298+
restrict: 'A',
299+
require: '^tabset',
300+
templateUrl: 'template/tabs/tabset-titles.html',
301+
replace: true,
302+
link: function(scope, elm, attrs, tabsetCtrl) {
303+
if (!scope.$eval(attrs.tabsetTitles)) {
304+
elm.remove();
305+
} else {
306+
//now that tabs location has been decided, transclude the tab titles in
307+
tabsetCtrl.$transcludeFn(tabsetCtrl.$scope.$parent, function(node) {
308+
elm.append(node);
309+
});
310+
}
311+
}
312+
};
313+
})
314+
287315
;
288316

src/tabs/test/tabsSpec.js

+51-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
describe('tabs', function() {
2-
beforeEach(module('ui.bootstrap.tabs', 'template/tabs/tabset.html', 'template/tabs/tab.html'));
2+
beforeEach(module('ui.bootstrap.tabs', 'template/tabs/tabset.html', 'template/tabs/tab.html', 'template/tabs/tabset-titles.html'));
33

44
var elm, scope;
55
function titles() {
@@ -493,6 +493,56 @@ describe('tabs', function() {
493493
});
494494
});
495495

496+
describe('direction', function() {
497+
it('should not have `tab-left`, `tab-right` nor `tabs-below` classes if the direction is undefined', inject(function($compile, $rootScope) {
498+
scope = $rootScope.$new();
499+
scope.direction = undefined;
500+
501+
elm = $compile('<tabset direction="direction"></tabset>')(scope);
502+
scope.$apply();
503+
expect(elm).not.toHaveClass('tabs-left');
504+
expect(elm).not.toHaveClass('tabs-right');
505+
expect(elm).not.toHaveClass('tabs-below');
506+
expect(elm.find('.nav + .tab-content').length).toBe(1);
507+
}));
508+
509+
it('should only have the `tab-left` direction class if the direction is "left"', inject(function($compile, $rootScope) {
510+
scope = $rootScope.$new();
511+
scope.direction = 'left';
512+
513+
elm = $compile('<tabset direction="direction"></tabset>')(scope);
514+
scope.$apply();
515+
expect(elm).toHaveClass('tabs-left');
516+
expect(elm).not.toHaveClass('tabs-right');
517+
expect(elm).not.toHaveClass('tabs-below');
518+
expect(elm.find('.nav + .tab-content').length).toBe(1);
519+
}));
520+
521+
it('should only have the `tab-right direction class if the direction is "right"', inject(function($compile, $rootScope) {
522+
scope = $rootScope.$new();
523+
scope.direction = 'right';
524+
525+
elm = $compile('<tabset direction="direction"></tabset>')(scope);
526+
scope.$apply();
527+
expect(elm).not.toHaveClass('tabs-left');
528+
expect(elm).toHaveClass('tabs-right');
529+
expect(elm).not.toHaveClass('tabs-below');
530+
expect(elm.find('.nav + .tab-content').length).toBe(1);
531+
}));
532+
533+
it('should only have the `tab-below direction class if the direction is "below"', inject(function($compile, $rootScope) {
534+
scope = $rootScope.$new();
535+
scope.direction = 'below';
536+
537+
elm = $compile('<tabset direction="direction"></tabset>')(scope);
538+
scope.$apply();
539+
expect(elm).not.toHaveClass('tabs-left');
540+
expect(elm).not.toHaveClass('tabs-right');
541+
expect(elm).toHaveClass('tabs-below');
542+
expect(elm.find('.tab-content + .nav').length).toBe(1);
543+
}));
544+
});
545+
496546
//https://github.com/angular-ui/bootstrap/issues/524
497547
describe('child compilation', function() {
498548

template/tabs/tabset-titles.html

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<ul class="nav {{type && 'nav-' + type}}" ng-class="{'nav-stacked': vertical}">
2+
</ul>

template/tabs/tabset.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

2-
<div class="tabbable">
3-
<ul class="nav {{type && 'nav-' + type}}" ng-class="{'nav-stacked': vertical}" ng-transclude>
4-
</ul>
2+
<div class="tabbable" ng-class="{'tabs-right': direction == 'right', 'tabs-left': direction == 'left', 'tabs-below': direction == 'below'}">
3+
<div tabset-titles="tabsAbove"></div>
54
<div class="tab-content">
65
<div class="tab-pane"
76
ng-repeat="tab in tabs"
87
ng-class="{active: tab.active}"
98
tab-content-transclude="tab">
109
</div>
1110
</div>
11+
<div tabset-titles="!tabsAbove"></div>
1212
</div>

0 commit comments

Comments
 (0)