From 93080cae36e1bea653b39c85d9345afe798de59f Mon Sep 17 00:00:00 2001 From: Robert Messerle Date: Thu, 26 Feb 2015 07:46:00 -0800 Subject: [PATCH] fix(tooltip): fixes `md-direction` attribute functionality Closes #1673 --- src/components/tooltip/tooltip.js | 39 +++++++++++-------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/src/components/tooltip/tooltip.js b/src/components/tooltip/tooltip.js index 4077ead5e0a..1d3a3ddac21 100644 --- a/src/components/tooltip/tooltip.js +++ b/src/components/tooltip/tooltip.js @@ -149,25 +149,14 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $mdUtil, $mdThe function positionTooltip() { var tipRect = $mdUtil.offsetRect(element, tooltipParent); var parentRect = $mdUtil.offsetRect(parent, tooltipParent); - - // Default to bottom position if possible - var tipDirection = 'bottom'; - var newPosition = { - left: parentRect.left + parentRect.width / 2 - tipRect.width / 2, - top: parentRect.top + parentRect.height - }; - - // If element bleeds over left/right of the window, place it on the edge of the window. - newPosition.left = Math.min( - newPosition.left, - tooltipParent.prop('scrollWidth') - tipRect.width - TOOLTIP_WINDOW_EDGE_SPACE - ); - newPosition.left = Math.max(newPosition.left, TOOLTIP_WINDOW_EDGE_SPACE); - - // If element bleeds over the bottom of the window, place it above the parent. - if (newPosition.top + tipRect.height > tooltipParent.prop('scrollHeight')) { - newPosition.top = parentRect.top - tipRect.height; - tipDirection = 'top'; + var newPosition = getPosition(direction); + + // If the user provided a direction, just nudge the tooltip onto the screen + // Otherwise, recalculate based on 'top' since default is 'bottom' + if (direction) { + newPosition = fitInParent(newPosition); + } else if (newPosition.top > tooltipParent.prop('scrollHeight') - tipRect.height - TOOLTIP_WINDOW_EDGE_SPACE) { + newPosition = fitInParent(getPosition('top')); } element.css({top: newPosition.top + 'px', left: newPosition.left + 'px'}); @@ -190,12 +179,12 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $mdUtil, $mdThe }); } - function fitOnScreen (pos) { - var newPosition = {}; - newPosition.left = Math.min( pos.left, tooltipParent.prop('scrollWidth') - tipRect.width - TOOLTIP_WINDOW_EDGE_SPACE ); - newPosition.left = Math.max( pos.left, TOOLTIP_WINDOW_EDGE_SPACE ); - newPosition.top = Math.min( pos.top, tooltipParent.prop('scrollHeight') - tipRect.height - TOOLTIP_WINDOW_EDGE_SPACE ); - newPosition.top = Math.max( pos.top, TOOLTIP_WINDOW_EDGE_SPACE ); + function fitInParent (pos) { + var newPosition = { left: pos.left, top: pos.top }; + newPosition.left = Math.min( newPosition.left, tooltipParent.prop('scrollWidth') - tipRect.width - TOOLTIP_WINDOW_EDGE_SPACE ); + newPosition.left = Math.max( newPosition.left, TOOLTIP_WINDOW_EDGE_SPACE ); + newPosition.top = Math.min( newPosition.top, tooltipParent.prop('scrollHeight') - tipRect.height - TOOLTIP_WINDOW_EDGE_SPACE ); + newPosition.top = Math.max( newPosition.top, TOOLTIP_WINDOW_EDGE_SPACE ); return newPosition; }