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

feat(tooltip) - auto positioning #1224

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions src/position/position.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
angular.module('ui.bootstrap.position', [])
angular.module( 'ui.bootstrap.position', [] )

/**
* A set of utility methods that can be use to retrieve position of DOM elements.
* It is meant to be used where we need to absolute-position DOM elements in
* relation to other, existing elements (this is the case for tooltips, popovers,
* typeahead suggestions etc.).
*/
.factory('$position', ['$document', '$window', function ($document, $window) {
.factory( '$position', ['$document', '$window', function ( $document, $window ) {

function getStyle(el, cssprop) {
function getStyle( el, cssprop ) {
if (el.currentStyle) { //IE
return el.currentStyle[cssprop];
} else if ($window.getComputedStyle) {
return $window.getComputedStyle(el)[cssprop];
return $window.getComputedStyle( el )[cssprop];
}
// finally try and get inline style
return el.style[cssprop];
Expand All @@ -22,18 +22,18 @@ angular.module('ui.bootstrap.position', [])
* Checks if a given element is statically positioned
* @param element - raw DOM element
*/
function isStaticPositioned(element) {
return (getStyle(element, "position") || 'static' ) === 'static';
function isStaticPositioned( element ) {
return (getStyle( element, "position" ) || 'static' ) === 'static';
}

/**
* returns the closest, non-statically positioned parentOffset of a given element
* @param element
*/
var parentOffsetEl = function (element) {
var parentOffsetEl = function ( element ) {
var docDomEl = $document[0];
var offsetParent = element.offsetParent || docDomEl;
while (offsetParent && offsetParent !== docDomEl && isStaticPositioned(offsetParent) ) {
while (offsetParent && offsetParent !== docDomEl && isStaticPositioned( offsetParent )) {
offsetParent = offsetParent.offsetParent;
}
return offsetParent || docDomEl;
Expand All @@ -44,19 +44,19 @@ angular.module('ui.bootstrap.position', [])
* Provides read-only equivalent of jQuery's position function:
* http://api.jquery.com/position/
*/
position: function (element) {
var elBCR = this.offset(element);
position: function ( element ) {
var elBCR = this.offset( element );
var offsetParentBCR = { top: 0, left: 0 };
var offsetParentEl = parentOffsetEl(element[0]);
var offsetParentEl = parentOffsetEl( element[0] );
if (offsetParentEl != $document[0]) {
offsetParentBCR = this.offset(angular.element(offsetParentEl));
offsetParentBCR = this.offset( angular.element( offsetParentEl ) );
offsetParentBCR.top += offsetParentEl.clientTop - offsetParentEl.scrollTop;
offsetParentBCR.left += offsetParentEl.clientLeft - offsetParentEl.scrollLeft;
}

return {
width: element.prop('offsetWidth'),
height: element.prop('offsetHeight'),
width: element.prop( 'offsetWidth' ),
height: element.prop( 'offsetHeight' ),
top: elBCR.top - offsetParentBCR.top,
left: elBCR.left - offsetParentBCR.left
};
Expand All @@ -66,14 +66,23 @@ angular.module('ui.bootstrap.position', [])
* Provides read-only equivalent of jQuery's offset function:
* http://api.jquery.com/offset/
*/
offset: function (element) {
offset: function ( element ) {
var boundingClientRect = element[0].getBoundingClientRect();
return {
width: element.prop('offsetWidth'),
height: element.prop('offsetHeight'),
width: element.prop( 'offsetWidth' ),
height: element.prop( 'offsetHeight' ),
top: boundingClientRect.top + ($window.pageYOffset || $document[0].body.scrollTop || $document[0].documentElement.scrollTop),
left: boundingClientRect.left + ($window.pageXOffset || $document[0].body.scrollLeft || $document[0].documentElement.scrollLeft)
left: boundingClientRect.left + ($window.pageXOffset || $document[0].body.scrollLeft || $document[0].documentElement.scrollLeft)
};
},

viewport: function () {
return {
pageYOffset: $window.pageYOffset || $document[0].body.scrollTop || $document[0].documentElement.scrollTop,
pageXOffset: $window.pageXOffset || $document[0].body.scrollLeft || $document[0].documentElement.scrollLeft,
innerWidth: $window.innerWidth || $document.documentElement.clientWidth || $document.body.clientWidth,
innerHeight: $window.innerHeight || $document.documentElement.clientHeight || $document.body.clientHeight
};
}
};
}]);
}] );
Loading