Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 8ac4fa1

Browse files
author
Robert Messerle
committed
fix(tabs): updating dynamic tab labels will now be reflected
Related to #2837
1 parent 5e47b52 commit 8ac4fa1

File tree

5 files changed

+54
-35
lines changed

5 files changed

+54
-35
lines changed

src/components/tabs/js/tabDirective.js

+29-25
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,34 @@ function MdTab () {
5959
return {
6060
require: '^?mdTabs',
6161
terminal: true,
62+
template: function (element, attr) {
63+
var label = getLabel(),
64+
body = getTemplate();
65+
return '' +
66+
'<md-tab-label>' + label + '</md-tab-label>' +
67+
'<md-tab-body>' + body + '</md-tab-body>';
68+
function getLabel () {
69+
return getLabelAttribute() || getLabelElement() || getElementContents();
70+
function getLabelAttribute () { return attr.label; }
71+
function getLabelElement () {
72+
var label = element.find('md-tab-label');
73+
if (label.length) return label.remove().html();
74+
}
75+
function getElementContents () {
76+
var html = element.html();
77+
element.empty();
78+
return html;
79+
}
80+
}
81+
function getTemplate () {
82+
var content = element.find('md-tab-body'),
83+
template = content.length ? content.html() : attr.label ? element.html() : '';
84+
if (content.length) content.remove();
85+
else if (attr.label) element.empty();
86+
return template;
87+
}
88+
},
6289
scope: {
63-
label: '@',
6490
active: '=?mdActive',
6591
disabled: '=?ngDisabled',
6692
select: '&?mdOnSelect',
@@ -77,8 +103,8 @@ function MdTab () {
77103
scope: scope,
78104
parent: scope.$parent,
79105
index: index,
80-
template: getTemplate(),
81-
label: getLabel()
106+
template: element.find('md-tab-body').html(),
107+
label: element.find('md-tab-label').html()
82108
}, index);
83109

84110
scope.select = scope.select || angular.noop;
@@ -97,27 +123,5 @@ function MdTab () {
97123
);
98124
scope.$on('$destroy', function () { ctrl.removeTab(data); });
99125

100-
function getLabel () {
101-
var label = attr.label || (element.find('md-tab-label')[0] || element[0]).innerHTML;
102-
return getLabelAttribute() || getLabelElement() || getElementContents();
103-
function getLabelAttribute () { return attr.label; }
104-
function getLabelElement () {
105-
var label = element.find('md-tab-label');
106-
if (label.length) return label.remove().html();
107-
}
108-
function getElementContents () {
109-
var html = element.html();
110-
element.empty();
111-
return html;
112-
}
113-
}
114-
115-
function getTemplate () {
116-
var content = element.find('md-tab-body'),
117-
template = content.length ? content.html() : attr.label ? element.html() : null;
118-
if (content.length) content.remove();
119-
else if (attr.label) element.empty();
120-
return template;
121-
}
122126
}
123127
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
angular
2+
.module('material.components.tabs')
3+
.directive('mdTabLabel', MdTabLabel);
4+
5+
function MdTabLabel () {
6+
return { terminal: true };
7+
}
8+

src/components/tabs/js/tabsController.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,16 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
4848
$scope.$watch('$mdTabsCtrl.focusIndex', handleFocusIndexChange);
4949
$scope.$watch('$mdTabsCtrl.offsetLeft', handleOffsetChange);
5050
$scope.$watch('$mdTabsCtrl.hasContent', handleHasContent);
51-
angular.element($window).on('resize', function () { $scope.$apply(handleWindowResize); });
52-
$timeout(updateInkBarStyles, 0, false);
51+
angular.element($window).on('resize', handleWindowResize);
52+
angular.element(elements.paging).on('DOMSubtreeModified', updateInkBarStyles);
5353
$timeout(updateHeightFromContent, 0, false);
5454
$timeout(adjustOffset);
55+
$scope.$on('$destroy', cleanup);
56+
}
57+
58+
function cleanup () {
59+
angular.element($window).off('resize', handleWindowResize);
60+
angular.element(elements.paging).off('DOMSubtreeModified', updateInkBarStyles);
5561
}
5662

5763
//-- Change handlers
@@ -100,7 +106,7 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
100106
handleResizeWhenVisible.watcher = null;
101107

102108
//-- we have to trigger our own $apply so that the DOM bindings will update
103-
$scope.$apply(handleWindowResize);
109+
handleWindowResize();
104110
}
105111
}, 0, false);
106112
});
@@ -159,9 +165,11 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
159165
}
160166

161167
function handleWindowResize () {
162-
ctrl.lastSelectedIndex = $scope.selectedIndex;
163-
ctrl.offsetLeft = fixOffset(ctrl.offsetLeft);
164-
$timeout(updateInkBarStyles, 0, false);
168+
$scope.$apply(function () {
169+
ctrl.lastSelectedIndex = $scope.selectedIndex;
170+
ctrl.offsetLeft = fixOffset(ctrl.offsetLeft);
171+
$timeout(updateInkBarStyles, 0, false);
172+
});
165173
}
166174

167175
function removeTab (tabData) {
@@ -175,7 +183,6 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
175183
ctrl.tabs[$scope.selectedIndex] && ctrl.tabs[$scope.selectedIndex].scope.select();
176184
}
177185
$timeout(function () {
178-
updateInkBarStyles();
179186
ctrl.offsetLeft = fixOffset(ctrl.offsetLeft);
180187
});
181188
}
@@ -270,7 +277,6 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
270277
});
271278
$scope.selectedIndex = ctrl.tabs.indexOf(selectedItem);
272279
ctrl.focusIndex = ctrl.tabs.indexOf(focusItem);
273-
$timeout(updateInkBarStyles, 0, false);
274280
}
275281

276282
function incrementSelectedIndex (inc, focus) {
@@ -339,6 +345,7 @@ function MdTabsController ($scope, $element, $window, $timeout, $mdConstant, $md
339345
}
340346

341347
function updateInkBarStyles () {
348+
if (!elements.tabs[$scope.selectedIndex]) return;
342349
if (!ctrl.tabs.length) return queue.push(updateInkBarStyles);
343350
//-- if the element is not visible, we will not be able to calculate sizes until it is
344351
//-- we should treat that as a resize event rather than just updating the ink bar

src/components/tabs/js/tabsDirective.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function MdTabs ($mdTheming, $mdUtil, $compile) {
9595
stretchTabs: '@?mdStretchTabs'
9696
},
9797
template: function (element, attr) {
98-
var content = attr["$mdTabsTemplate"] = element.html();
98+
attr["$mdTabsTemplate"] = element.html();
9999
return '\
100100
<md-tabs-wrapper ng-class="{ \'md-stretch-tabs\': $mdTabsCtrl.shouldStretchTabs() }">\
101101
<md-tab-data></md-tab-data>\

src/components/tabs/js/templateDirective.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function MdTemplate ($compile, $mdUtil, $timeout) {
2020
$compile(element.contents())(compileScope);
2121
return $timeout(handleScope);
2222
function handleScope () {
23-
scope.$watch('connected', function (value) { value ? reconnect() : disconnect(); });
23+
scope.$watch('connected', function (value) { value === false ? disconnect() : reconnect(); });
2424
scope.$on('$destroy', reconnect);
2525
}
2626
function disconnect () {

0 commit comments

Comments
 (0)