From 879c44be4af48a4e4bd025031e492817337d41ef Mon Sep 17 00:00:00 2001 From: Calvin Froedge Date: Tue, 1 Oct 2013 14:46:49 -1000 Subject: [PATCH] feat(tooltip): Update tooltip placement after height changes --- src/tooltip/tooltip.js | 111 +++++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 42 deletions(-) diff --git a/src/tooltip/tooltip.js b/src/tooltip/tooltip.js index b75124eb17..3148b83d09 100644 --- a/src/tooltip/tooltip.js +++ b/src/tooltip/tooltip.js @@ -143,7 +143,10 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap hide(); }); } - + + // Listener for height, which we will unset on hide + var heightListener = false; + // Show the tooltip popup element. function show() { var position, @@ -175,49 +178,69 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap } // Get the position of the directive element. - position = appendToBody ? $position.offset( element ) : $position.position( element ); + function calculatePosition () { + position = appendToBody ? $position.offset( element ) : $position.position( element ); - // Get the height and width of the tooltip so we can center it. - ttWidth = tooltip.prop( 'offsetWidth' ); - ttHeight = tooltip.prop( 'offsetHeight' ); - - // Calculate the tooltip's top and left coordinates to center it with - // this directive. - switch ( scope.tt_placement ) { - case 'right': - ttPosition = { - top: position.top + position.height / 2 - ttHeight / 2, - left: position.left + position.width - }; - break; - case 'bottom': - ttPosition = { - top: position.top + position.height, - left: position.left + position.width / 2 - ttWidth / 2 - }; - break; - case 'left': - ttPosition = { - top: position.top + position.height / 2 - ttHeight / 2, - left: position.left - ttWidth - }; - break; - default: - ttPosition = { - top: position.top - ttHeight, - left: position.left + position.width / 2 - ttWidth / 2 - }; - break; - } - - ttPosition.top += 'px'; - ttPosition.left += 'px'; + // Get the height and width of the tooltip so we can center it. + ttWidth = tooltip.prop( 'offsetWidth' ); + ttHeight = tooltip.prop( 'offsetHeight' ); + } + calculatePosition(); - // Now set the calculated positioning. - tooltip.css( ttPosition ); - - // And show the tooltip. - scope.tt_isOpen = true; + // Calculate the tooltip's top and left coordinates to center it with + // this directive + function calculatePlacement () { + switch ( scope.tt_placement ) { + case 'right': + ttPosition = { + top: position.top + position.height / 2 - ttHeight / 2, + left: position.left + position.width + }; + break; + case 'bottom': + ttPosition = { + top: position.top + position.height, + left: position.left + position.width / 2 - ttWidth / 2 + }; + break; + case 'left': + ttPosition = { + top: position.top + position.height / 2 - ttHeight / 2, + left: position.left - ttWidth + }; + break; + default: + ttPosition = { + top: position.top - ttHeight, + left: position.left + position.width / 2 - ttWidth / 2 + }; + break; + } + } + + function makePlacement () { + calculatePosition(); + calculatePlacement(); + console.log('making placement', ttPosition.top, ttPosition.left); + + ttPosition.top += 'px'; + ttPosition.left += 'px'; + + // Now set the calculated positioning. + tooltip.css( ttPosition ); + + // And show the tooltip. + scope.tt_isOpen = true; + } + makePlacement(); + + //Set a listener which will change the height + heightListener = setInterval(function() { + //If calculated position does not match current position, adjust the height + if( position.top != $position.offset( element ).top ){ + makePlacement(); + } + }, 100); } // Hide the tooltip popup element. @@ -236,6 +259,10 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap } else { tooltip.remove(); } + + //Unset heightListener to avoid memory leaks + clearInterval(heightListener); + heightListener = false; } /**