Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit ace7bc6

Browse files
lanterndevJosh David Miller
authored and
Josh David Miller
committed
feat(tooltip): Added mouse placement option
Adds a mouse() method to the $position service API to return the current mouse position. The $tooltip API has been changed to allow using this value to set the position of the tooltip element. The top left corner of the element will be at the cursor position.
1 parent 56fd692 commit ace7bc6

File tree

6 files changed

+38
-11
lines changed

6 files changed

+38
-11
lines changed

src/popover/docs/demo.html

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ <h4>Positional</h4>
1313
<button popover-placement="left" popover="On the Left!" class="btn">Left</button>
1414
<button popover-placement="right" popover="On the Right!" class="btn">Right</button>
1515
<button popover-placement="bottom" popover="On the Bottom!" class="btn">Bottom</button>
16+
<button popover-placement="mouse" popover="relative to mouse" class="btn">Mouse</button>
1617
</div>
1718
<div>
1819
<h4>Triggers</h4>

src/popover/docs/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ will display:
99

1010
- `popover-title`: A string to display as a fancy title.
1111
- `popover-placement`: Where to place it? Defaults to "top", but also accepts
12-
"bottom", "left", or "right".
12+
"bottom", "left", "right", or "mouse".
1313
- `popover-animation`: Should it fade in and out? Defaults to "true".
1414
- `popover-popup-delay`: For how long should the user have to have the mouse
1515
over the element before the popover shows (in milliseconds)? Defaults to 0.

src/position/position.js

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ angular.module('ui.bootstrap.position', [])
88
*/
99
.factory('$position', ['$document', '$window', function ($document, $window) {
1010

11+
var mouseX, mouseY;
12+
13+
$document.bind('mousemove', function mouseMoved(event) {
14+
mouseX = event.pageX;
15+
mouseY = event.pageY;
16+
});
17+
1118
function getStyle(el, cssprop) {
1219
if (el.currentStyle) { //IE
1320
return el.currentStyle[cssprop];
@@ -74,6 +81,13 @@ angular.module('ui.bootstrap.position', [])
7481
top: boundingClientRect.top + ($window.pageYOffset || $document[0].body.scrollTop),
7582
left: boundingClientRect.left + ($window.pageXOffset || $document[0].body.scrollLeft)
7683
};
84+
},
85+
86+
/**
87+
* Provides the coordinates of the mouse
88+
*/
89+
mouse: function () {
90+
return {x: mouseX, y: mouseY};
7791
}
7892
};
7993
}]);

src/tooltip/docs/demo.html

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
nunc sed velit dignissim sodales ut eu sem integer vitae. Turpis egestas
1313
<a><span tooltip-placement="bottom" tooltip="On the Bottom!">bottom</span></a>
1414
pharetra convallis posuere morbi leo urna,
15+
<a><span tooltip-placement="mouse" tooltip="relative to mouse">mouse</span></a>
16+
blah blah blah,
1517
<a><span tooltip-animation="false" tooltip="I don't fade. :-(">fading</span></a>
1618
at elementum eu, facilisis sed odio morbi quis commodo odio. In cursus
1719
<a><span tooltip-popup-delay='1000' tooltip='appears with delay'>delayed</span></a> turpis massa tincidunt dui ut.

src/tooltip/docs/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The tooltip directives provide several optional attributes to control how they
1111
will display:
1212

1313
- `tooltip-placement`: Where to place it? Defaults to "top", but also accepts
14-
"bottom", "left", or "right".
14+
"bottom", "left", "right", or "mouse".
1515
- `tooltip-animation`: Should it fade in and out? Defaults to "true".
1616
- `tooltip-popup-delay`: For how long should the user have to have the mouse
1717
over the element before the tooltip shows (in milliseconds)? Defaults to 0.

src/tooltip/tooltip.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -180,32 +180,42 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position' ] )
180180
// Calculate the tooltip's top and left coordinates to center it with
181181
// this directive.
182182
switch ( scope.tt_placement ) {
183+
case 'mouse':
184+
var mousePos = $position.mouse();
185+
ttPosition = {
186+
top: mousePos.y,
187+
left: mousePos.x
188+
};
189+
break;
183190
case 'right':
184191
ttPosition = {
185-
top: (position.top + position.height / 2 - ttHeight / 2) + 'px',
186-
left: (position.left + position.width) + 'px'
192+
top: position.top + position.height / 2 - ttHeight / 2,
193+
left: position.left + position.width
187194
};
188195
break;
189196
case 'bottom':
190197
ttPosition = {
191-
top: (position.top + position.height) + 'px',
192-
left: (position.left + position.width / 2 - ttWidth / 2) + 'px'
198+
top: position.top + position.height,
199+
left: position.left + position.width / 2 - ttWidth / 2
193200
};
194201
break;
195202
case 'left':
196203
ttPosition = {
197-
top: (position.top + position.height / 2 - ttHeight / 2) + 'px',
198-
left: (position.left - ttWidth) + 'px'
204+
top: position.top + position.height / 2 - ttHeight / 2,
205+
left: position.left - ttWidth
199206
};
200207
break;
201208
default:
202209
ttPosition = {
203-
top: (position.top - ttHeight) + 'px',
204-
left: (position.left + position.width / 2 - ttWidth / 2) + 'px'
210+
top: position.top - ttHeight,
211+
left: position.left + position.width / 2 - ttWidth / 2
205212
};
206213
break;
207214
}
208-
215+
216+
ttPosition.top += 'px';
217+
ttPosition.left += 'px';
218+
209219
// Now set the calculated positioning.
210220
tooltip.css( ttPosition );
211221

0 commit comments

Comments
 (0)