Skip to content

Commit

Permalink
[Luis] add extra functionality to getOffset to add the bordersize
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Silva committed Oct 21, 2014
1 parent c524451 commit 0f11515
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
21 changes: 17 additions & 4 deletions lib/util/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,25 +240,38 @@ function clearSelection(win) {
* Calculates an element's total top and left offset from the document edge.
*
* @param {Element} el the element for which position needs to be returned
* @param {includeBorder} if is to include the border width
* @return {Object} vector object with properties topOffset and leftOffset
*/
function getElementOffset(el) {
function getElementOffset(el, includeBorder) {
var yPos, xPos;

yPos = el.offsetTop;
xPos = el.offsetLeft;
el = el.offsetParent;

while (el != null) {
yPos += el.offsetTop;
xPos += el.offsetLeft;
yPos += el.offsetTop + getBorder(el, 'Height', includeBorder);
xPos += el.offsetLeft + getBorder(el, 'Width', includeBorder);
el = el.offsetParent;
}
}

return { topOffset: yPos, leftOffset: xPos };
}


function getBorder(el, type, includeBorder) {
if (includeBorder) {
var side = (type == 'Height') ? 'top' : 'left',
styles = window.getComputedStyle(el),
sideValue = styles.getPropertyCSSValue('border-' + side + '-width');

if (sideValue) return sideValue.getFloatValue(sideValue.CSS_PX);
}
return 0;
}


/**
* Removes element from the document
*
Expand Down
21 changes: 17 additions & 4 deletions milo.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13359,25 +13359,38 @@ function clearSelection(win) {
* Calculates an element's total top and left offset from the document edge.
*
* @param {Element} el the element for which position needs to be returned
* @param {includeBorder} if is to include the border width
* @return {Object} vector object with properties topOffset and leftOffset
*/
function getElementOffset(el) {
function getElementOffset(el, includeBorder) {
var yPos, xPos;

yPos = el.offsetTop;
xPos = el.offsetLeft;
el = el.offsetParent;

while (el != null) {
yPos += el.offsetTop;
xPos += el.offsetLeft;
yPos += el.offsetTop + getBorder(el, 'Height', includeBorder);
xPos += el.offsetLeft + getBorder(el, 'Width', includeBorder);
el = el.offsetParent;
}
}

return { topOffset: yPos, leftOffset: xPos };
}


function getBorder(el, type, includeBorder) {
if (includeBorder) {
var side = (type == 'Height') ? 'top' : 'left',
styles = window.getComputedStyle(el),
sideValue = styles.getPropertyCSSValue('border-' + side + '-width');

if (sideValue) return sideValue.getFloatValue(sideValue.CSS_PX);
}
return 0;
}


/**
* Removes element from the document
*
Expand Down

0 comments on commit 0f11515

Please sign in to comment.