Skip to content

Commit d6c21f5

Browse files
committed
fix(mdTooltip): Tooltip position and watcher issue.
Fixes angular#10162 - When the position was changed dynamically the resulting tooltip's panel would retain the position classes on it's panelEl. Now when creating and recreating the tooltip with a different position class due to a position change, the panel will grab that tracked panel and update its configuration object so that the new position class can be used. Fixes angular#10157 - There was a `mdDirection` watcher that was deleted due to a rebase/timing issue when moving to the `mdPanel` API. Reference to previous PR: angular#7822.
1 parent 0b72ab9 commit d6c21f5

3 files changed

Lines changed: 42 additions & 36 deletions

File tree

src/components/panel/panel.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,9 +910,12 @@ MdPanelService.prototype.create = function(config) {
910910
config = config || {};
911911

912912
// If the passed-in config contains an ID and the ID is within _trackedPanels,
913-
// return the tracked panel.
913+
// return the tracked panel after updating its config with the passed in
914+
// config.
914915
if (angular.isDefined(config.id) && this._trackedPanels[config.id]) {
915-
return this._trackedPanels[config.id];
916+
var trackedPanel = this._trackedPanels[config.id];
917+
angular.extend(trackedPanel.config, config);
918+
return trackedPanel;
916919
}
917920

918921
this._config = {

src/components/tooltip/tooltip.js

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,19 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $interpolate,
7878
var parent = $mdUtil.getParentWithPointerEvents(element);
7979
var debouncedOnResize = $$rAF.throttle(updatePosition);
8080
var mouseActive = false;
81-
var origin, position, panelPosition, panelRef, autohide, showTimeout,
82-
elementFocusedOnWindowBlur = null;
81+
var origin, position, panelPosition, panelAnimation, panelRef, autohide,
82+
showTimeout, elementFocusedOnWindowBlur, template = null;
83+
var id = 'tooltip-' + $mdUtil.nextUid();
84+
var attachTo = angular.element(document.body);
85+
if (parent && parent[0]) {
86+
panelAnimation = $mdPanel.newPanelAnimation()
87+
.openFrom(parent)
88+
.closeTo(parent)
89+
.withAnimation({
90+
open: 'md-show',
91+
close: 'md-hide'
92+
});
93+
}
8394

8495
// Set defaults
8596
setDefaults();
@@ -114,10 +125,12 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $interpolate,
114125
function updatePosition() {
115126
setDefaults();
116127

117-
// If the panel has already been created, remove the current origin
118-
// class from the panel element.
128+
// If the panel has already been created, remove all possible origin
129+
// classes from the panelEl.
119130
if (panelRef && panelRef.panelEl) {
120-
panelRef.panelEl.removeClass(origin);
131+
angular.forEach(TOOLTIP_DIRECTIONS, function(value, key) {
132+
panelRef.panelEl.removeClass('md-origin-' + key);
133+
});
121134
}
122135

123136
// Set the panel element origin class based off of the current
@@ -261,6 +274,8 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $interpolate,
261274
!scope.visibleWatcher ) {
262275
scope.visibleWatcher = scope.$watch('mdVisible',
263276
onVisibleChanged);
277+
} else if (mutation.attributeName === 'md-direction') {
278+
updatePosition(scope.mdDirection);
264279
}
265280
});
266281
});
@@ -356,35 +371,23 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $interpolate,
356371
if (!element[0].textContent.trim()) {
357372
throw new Error('Text for the tooltip has not been provided. ' +
358373
'Please include text within the mdTooltip element.');
374+
} else {
375+
template = element[0].textContent.trim();
359376
}
360377

361-
if (!panelRef) {
362-
var id = 'tooltip-' + $mdUtil.nextUid();
363-
var attachTo = angular.element(document.body);
364-
var content = element.html().trim();
365-
var panelAnimation = $mdPanel.newPanelAnimation()
366-
.openFrom(parent)
367-
.closeTo(parent)
368-
.withAnimation({
369-
open: 'md-show',
370-
close: 'md-hide'
371-
});
372-
373-
var panelConfig = {
374-
id: id,
375-
attachTo: attachTo,
376-
template: content,
377-
propagateContainerEvents: true,
378-
panelClass: 'md-tooltip ' + origin,
379-
animation: panelAnimation,
380-
position: panelPosition,
381-
zIndex: scope.mdZIndex,
382-
focusOnOpen: false
383-
};
384-
385-
panelRef = $mdPanel.create(panelConfig);
386-
}
387-
378+
var panelConfig = {
379+
id: id,
380+
attachTo: attachTo,
381+
template: template,
382+
propagateContainerEvents: true,
383+
panelClass: 'md-tooltip ' + origin,
384+
animation: panelAnimation,
385+
position: panelPosition,
386+
zIndex: scope.mdZIndex,
387+
focusOnOpen: false
388+
};
389+
390+
panelRef = $mdPanel.create(panelConfig);
388391
panelRef.open().then(function() {
389392
panelRef.panelEl.attr('role', 'tooltip');
390393
});

src/components/tooltip/tooltip.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('MdTooltip Component', function() {
2424
// Make sure to remove/cleanup after each test.
2525
element.remove();
2626
var scope = element && element.scope();
27-
scope && scope.$destroy;
27+
scope && scope.$destroy();
2828
element = undefined;
2929
});
3030

@@ -102,7 +102,7 @@ describe('MdTooltip Component', function() {
102102

103103
expect(element.attr('aria-label')).toBe('test 2');
104104
});
105-
105+
106106
it('should not interpolate interpolated values', function() {
107107
buildTooltip(
108108
'<md-button>' +

0 commit comments

Comments
 (0)