Skip to content

Commit 07fcf99

Browse files
committed
updating to support dynamic tabs / watch expressions
1 parent 4f3d00a commit 07fcf99

File tree

3 files changed

+41
-39
lines changed

3 files changed

+41
-39
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ Usage
1818
* Wrap your `<tabset>` inside of `<scrollable-tabset>`, like so:
1919

2020
```html
21-
<scrollable-tabset show-tooltips="true">
21+
<scrollable-tabset show-tooltips="true" watch-expression="tabs">
2222
<tabset>
23-
<tab ng-repeat="x in y">...</tab>
23+
<tab ng-repeat="x in tabs">...</tab>
2424
</tabset>
2525
</scrollable-tabset>
2626
```
2727

28-
You can turn tooltips on and off with the `show-tooltips` attribute.
28+
You can turn tooltips on and off with the `show-tooltips` attribute, and if your tabs aren't static, you may want to specify `watch-expression`.

angular-ui-tab-scroll.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@
6767
.ui-tabs-scrollable .right-nav-button {
6868
right: 0;
6969
border-top-right-radius: 4px!important;
70-
}
70+
}

angular-ui-tab-scroll.js

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ angular.module('ui.tab.scroll', [])
1212
}
1313
}
1414

15-
var unbindFunctions = function(el, cache) {
16-
if(!cache) return;
17-
el.off('mousedown', cache.mouseDown);
18-
el.off('mouseup', cache.mouseUp);
19-
};
20-
2115
var bindHoldFunctionTo = function(element, fn) {
2216

17+
//get rid of the previous scroll function
18+
element.unbind('mousedown');
19+
element.unbind('mouseup');
20+
2321
var isHolding = false;
2422

2523
var mouseDown = function() {
@@ -31,7 +29,7 @@ angular.module('ui.tab.scroll', [])
3129
if(isHolding) {
3230
fn();
3331

34-
if($(element).is(":disabled")) {
32+
if(element.is(':disabled')) {
3533
cancelId();
3634
}
3735
}
@@ -45,17 +43,17 @@ angular.module('ui.tab.scroll', [])
4543

4644
element.on('mousedown', mouseDown);
4745
element.on('mouseup', mouseUp);
48-
49-
return {mouseDown: mouseDown, mouseUp: mouseUp};
50-
5146
};
5247

5348
return {
5449
restrict: 'AE',
5550
transclude: true,
51+
5652
scope: {
57-
showTooltips: "="
53+
showTooltips: '=',
54+
watchExpression: '='
5855
},
56+
5957
template: [
6058
'<div class="ui-tabs-scrollable">',
6159
'<button ng-hide="hideButtons" ng-disabled="disableLeft()" class="btn nav-button left-nav-button" tooltip-placement="bottom" tooltip-html-unsafe="{{tooltipLeftContent()}}">',
@@ -67,11 +65,8 @@ angular.module('ui.tab.scroll', [])
6765
'</button>',
6866
'</div>'
6967
].join(''),
70-
link: function($scope, $el) {
7168

72-
$scope.currentOffset = 0;
73-
$scope.leftFunction = null;
74-
$scope.rightFunction = null;
69+
link: function($scope, $el) {
7570

7671
$scope.toTheLeftHTML = '';
7772
$scope.toTheRightHTML = '';
@@ -99,31 +94,38 @@ angular.module('ui.tab.scroll', [])
9994

10095
$scope.toTheLeft = function() {
10196
if(!$scope.tabContainer) return;
97+
10298
var nodes = [];
10399
$scope.tabContainer.find(selector).each(function(index, node) {
100+
104101
var nodeObj = $(node);
105-
var nodeContainer = nodeObj.parentsUntil("ul");
102+
var nodeContainer = nodeObj.parentsUntil('ul');
106103

107104
if(nodeContainer.offset().left > 0) return;
108105

109106
nodes.push(nodeObj.html());
107+
110108
});
109+
111110
$scope.toTheLeftHTML = nodes.join('<br>');
112111
};
113112

114113
$scope.toTheRight = function() {
115114
if(!$scope.tabContainer) return;
115+
116116
var nodes = [];
117117
$scope.tabContainer.find(selector).each(function(index, node) {
118-
var nodeObj = $(node);
119-
var nodeContainer = nodeObj.parentsUntil("ul");
120118

119+
var nodeObj = $(node);
120+
var nodeContainer = nodeObj.parentsUntil('ul');
121121
var nodeWidth = nodeContainer.offset().left;
122-
122+
123123
if(nodeWidth < $scope.tabWidth) return;
124124

125125
nodes.push(nodeObj.html());
126+
126127
});
128+
127129
$scope.toTheRightHTML = nodes.join('<br>');
128130
};
129131

@@ -134,15 +136,15 @@ angular.module('ui.tab.scroll', [])
134136

135137
var generateScrollFunction = function(el, offset) {
136138
return function() {
137-
$scope.currentOffset = Math.min($scope.tabContainerWidth, Math.max(0, el.scrollLeft += offset));
139+
el.scrollLeft += offset;
138140
$scope.recalcSides();
139141
};
140142
};
141143

142144
var init = function() {
143-
var $leftNav = $el.find(".left-nav-button");
144-
var $rightNav = $el.find(".right-nav-button");
145-
var $tabs = $scope.tabContainer = $el.find(".spacer").find("ul.nav.nav-tabs");
145+
var $leftNav = $el.find('.left-nav-button');
146+
var $rightNav = $el.find('.right-nav-button');
147+
var $tabs = $scope.tabContainer = $el.find('.spacer').find('ul.nav.nav-tabs');
146148

147149
var tabContainerWidth = $scope.tabContainerWidth = $tabs[0].scrollWidth;
148150
var tabWidth = $scope.tabWidth = $tabs.width();
@@ -151,24 +153,24 @@ angular.module('ui.tab.scroll', [])
151153

152154
$scope.hideButtons = tabContainerWidth === tabWidth;
153155

154-
$scope.leftFunction = generateScrollFunction(realTabs, -tabScrollWidth);
155-
$scope.rightfunction = generateScrollFunction(realTabs, tabScrollWidth);
156-
157-
unbindFunctions($leftNav, $scope.leftFunctionCache);
158-
unbindFunctions($rightNav, $scope.rightFunctionCache);
159-
160-
$scope.leftFunctionCache = bindHoldFunctionTo($leftNav, $scope.leftFunction);
161-
$scope.rightfunctionCache = bindHoldFunctionTo($rightNav, $scope.rightfunction);
156+
bindHoldFunctionTo($leftNav, generateScrollFunction(realTabs, -tabScrollWidth));
157+
bindHoldFunctionTo($rightNav, generateScrollFunction(realTabs, tabScrollWidth));
162158

163159
$scope.recalcSides();
164160
};
165161

166-
$timeout(init, 0);
167-
168-
$(window).on('resize', function() {
162+
var initAndApply = function() {
169163
init();
170164
$scope.$apply();
171-
});
165+
};
166+
167+
//hello my friend jake weary
168+
$(window).on('resize', initAndApply);
169+
170+
//even if one doesn't exist, we can still initialize w/ this
171+
$scope.$watch(function(){return $scope.watchExpression;}, function(newVal, oldVal) {
172+
$timeout(initAndApply, 0);
173+
}, true);
172174

173175
}
174176
};

0 commit comments

Comments
 (0)