Skip to content

Commit 170787e

Browse files
committed
feat(config): add a 'scrollableTabsetConfig' to ease the directive configuration
1 parent 8a0c75f commit 170787e

File tree

2 files changed

+115
-23
lines changed

2 files changed

+115
-23
lines changed

README.md

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,74 @@ angular-ui-tab-scroll
33

44
A scrollable tab plugin compatible with angular-ui bootstrap tabs.
55

6-
Dependencies
7-
============
6+
# Dependencies
7+
88
* Bootstrap CSS
99
* jQuery
1010
* AngularJS
1111
* angular-ui-bootstrap
1212

13-
Usage
14-
=====
13+
# Usage
1514

1615
* Include `angular-ui-tab-scroll.js` and `angular-ui-tab-scroll.css` in your page.
1716
* Add `ui.tab.scroll` to your angular module dependencies.
1817
* Wrap your `<tabset>` inside of `<scrollable-tabset>`, like so:
1918

2019
```html
21-
<scrollable-tabset show-tooltips="true" watch-expression="tabs">
22-
<tabset>
23-
<tab ng-repeat="x in tabs">...</tab>
24-
</tabset>
20+
<scrollable-tabset show-tooltips="true">
21+
<tabset>
22+
<tab ng-repeat="x in tabs">...</tab>
23+
</tabset>
2524
</scrollable-tabset>
2625
```
27-
Options
28-
=======
26+
27+
# Options
28+
2929
* `show-tooltips` - whether or not to show the side-tooltips
3030
* `tooltip-left` - which tooltip direction to use for the left tooltip (bottom, top, left, right) - defaults to bottom
3131
* `tooltip-right` - which tooltip direction to use for the right tooltip (bottom, top, left, right) - defaults to bottom
3232
* `tooltip-text-selector` - the selector for your tooltips, defaults to `*:not(:has("*:not(span)"))`
3333
* `scroll-left-icon` - the CSS class(es) to customize the left navigation button icon, defaults to `glyphicon glyphicon-chevron-left`
3434
* `scroll-right-icon` - the CSS class(es) to customize the right navigation button icon, defaults to `glyphicon glyphicon-chevron-right`
35+
36+
37+
These options can directly be set on each directive as **DOM attributes**.
38+
39+
40+
Example:
41+
42+
```
43+
<scrollable-tabset show-tooltips="true"
44+
tooltip-left="right"
45+
tooltip-right="left"
46+
scroll-left-icon="glyphicon glyphicon-chevron-left"
47+
scroll-right-icon="glyphicon glyphicon-chevron-right">
48+
<tabset>
49+
<tab ng-repeat="x in tabs">...</tab>
50+
</tabset>
51+
</scrollable-tabset>
52+
```
53+
54+
Or, they can be configured globally for all your `scrollable-tabset` directives, by using the **scrollableTabsetConfigProvider**, in the `config` section of your app.
55+
56+
Example:
57+
58+
```
59+
angular.module('yourApp', [])
60+
.config(['scrollableTabsetConfigProvider', function(scrollableTabsetConfigProvider){
61+
62+
scrollableTabsetConfigProvider.setShowTooltips(false);
63+
scrollableTabsetConfigProvider.setScrollLeftIcon('glyphicon glyphicon-chevron-left');
64+
scrollableTabsetConfigProvider.setScrollRightIcon('glyphicon glyphicon-chevron-right');
65+
//...set other properties here
66+
67+
}]);
68+
```
69+
Here is a working plunker : http://plnkr.co/edit/BheQyO7W9qXS0F6vZTlg?p=preview
70+
71+
This way, you can keep the directive usage simple in all your html templates!
72+
73+
74+
> **Important Note:** When an option is both defined at directive level and at config level, the value specified in the DOM **takes precedence over the one from the config**!.
75+
76+

angular-ui-tab-scroll.js

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,59 @@
11
angular.module('ui.tab.scroll', [])
2+
.provider('scrollableTabsetConfig', function(){
3+
4+
//the default options
5+
var defaultConfig = {
6+
showTooltips: true,
7+
8+
tooltipLeft: 'bottom',
9+
tooltipRight: 'bottom',
10+
11+
//select the innermost child that isn't a span
12+
//this way we cover getting <tab-heading> and <tab heading=''>
13+
//but leave other markup out of it, unless it's a span (commonly an icon)
14+
tooltipTextSelector: '*:not(:has("*:not(span)"))',
15+
16+
scrollLeftIcon: 'glyphicon glyphicon-chevron-left',
17+
scrollRightIcon: 'glyphicon glyphicon-chevron-right'
18+
};
19+
20+
var config = angular.extend({}, defaultConfig);
21+
22+
return {
23+
setShowTooltips : function(value){
24+
config.showTooltips = value;
25+
},
26+
setTooltipLeft : function(value){
27+
config.tooltipLeft = value;
28+
},
29+
setTooltipRight : function(value){
30+
config.tooltipRight = value;
31+
},
32+
setTooltipTextSelector : function(value){
33+
config.tooltipTextSelector = value;
34+
},
35+
setScrollLeftIcon : function(value){
36+
config.scrollLeftIcon = value;
37+
},
38+
setScrollRightIcon : function(value){
39+
config.scrollRightIcon = value;
40+
},
41+
$get: function(){
42+
return {
43+
showTooltips: config.showTooltips,
44+
tooltipLeft: config.tooltipLeft,
45+
tooltipRight: config.tooltipRight,
46+
tooltipTextSelector: config.tooltipTextSelector,
47+
scrollLeftIcon: config.scrollLeftIcon,
48+
scrollRightIcon: config.scrollRightIcon
49+
};
50+
}
51+
};
52+
}
53+
)
254
.directive('scrollableTabset', [
3-
'$window', '$interval', '$timeout',
4-
function($window, $interval, $timeout) {
55+
'scrollableTabsetConfig', '$window', '$interval', '$timeout',
56+
function(scrollableTabsetConfig, $window, $interval, $timeout) {
557

658
var timeoutId = null;
759

@@ -75,6 +127,8 @@ angular.module('ui.tab.scroll', [])
75127
$scope.toTheLeftHTML = '';
76128
$scope.toTheRightHTML = '';
77129

130+
var showTooltips = angular.isDefined($scope.showTooltips)? $scope.showTooltips : scrollableTabsetConfig.showTooltips;
131+
78132
$scope.disableLeft = function() {
79133
return !$scope.toTheLeftHTML;
80134
};
@@ -84,36 +138,32 @@ angular.module('ui.tab.scroll', [])
84138
};
85139

86140
$scope.tooltipLeftContent = function() {
87-
return $scope.showTooltips ? $scope.toTheLeftHTML : '';
141+
return showTooltips ? $scope.toTheLeftHTML : '';
88142
};
89143

90144
$scope.tooltipRightContent = function() {
91-
return $scope.showTooltips ? $scope.toTheRightHTML : '';
145+
return showTooltips ? $scope.toTheRightHTML : '';
92146
};
93147

94148
$scope.tooltipLeftDirection = function() {
95-
return $scope.tooltipLeft ? $scope.tooltipLeft : 'bottom';
149+
return $scope.tooltipLeft ? $scope.tooltipLeft : scrollableTabsetConfig.tooltipLeft;
96150
};
97151

98152
$scope.tooltipRightDirection = function() {
99-
return $scope.tooltipRight ? $scope.tooltipRight : 'bottom';
153+
return $scope.tooltipRight ? $scope.tooltipRight : scrollableTabsetConfig.tooltipRight;
100154
};
101155

102156
$scope.scrollLeftIconClass = function() {
103-
return $scope.scrollLeftIcon ? $scope.scrollLeftIcon : 'glyphicon glyphicon-chevron-left';
157+
return $scope.scrollLeftIcon ? $scope.scrollLeftIcon : scrollableTabsetConfig.scrollLeftIcon;
104158
};
105159

106160
$scope.scrollRightIconClass = function() {
107-
return $scope.scrollRightIcon ? $scope.scrollRightIcon : 'glyphicon glyphicon-chevron-right';
161+
return $scope.scrollRightIcon ? $scope.scrollRightIcon : scrollableTabsetConfig.scrollRightIcon;
108162
};
109163

110-
//select the innermost child that isn't a span
111-
//this way we cover getting <tab-heading> and <tab heading=''>
112-
//but leave other markup out of it, unless it's a span (commonly an icon)
113-
var selector = '*:not(:has("*:not(span)"))';
114164

115165
$scope.getSelector = function() {
116-
return $scope.tooltipTextSelector ? $scope.tooltipTextSelector : selector;
166+
return $scope.tooltipTextSelector ? $scope.tooltipTextSelector : scrollableTabsetConfig.tooltipTextSelector;
117167
};
118168

119169
$scope.toTheLeft = function() {

0 commit comments

Comments
 (0)