Skip to content

Instantly share code, notes, and snippets.

@onecrayon

onecrayon/dom.js Secret

Created November 19, 2012 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onecrayon/ae3109fe326c1f6ef247 to your computer and use it in GitHub Desktop.
Save onecrayon/ae3109fe326c1f6ef247 to your computer and use it in GitHub Desktop.
Extension to enyo.dom for Enyo 2
enyo.dom = {
// ...
//* Returns an object like `{top: 0, left: 0, bottom: 100, right: 100, height: 10, width: 10}` that represents the object's position relative to `relativeToNode`. Negative values mean part of the object is not visible. If you leave `relativeToNode` undefined, then the position will be relative to the viewport.
calcNodePosition: function(inNode, relativeToNode) {
// Parse upward and grab our positioning relative to the viewport
var top = 0,
left = 0,
node = inNode,
width = node.offsetWidth,
height = node.offsetHeight,
transformProp = enyo.dom.getStyleTransformProp(),
xregex = /translateX\((-?\d+)px\)/i,
yregex = /translateY\((-?\d+)px\)/i,
borderLeft = 0, borderTop = 0,
totalHeight = 0, totalWidth = 0;
if (relativeToNode) {
totalHeight = relativeToNode.offsetHeight;
totalWidth = relativeToNode.offsetWidth;
} else {
totalHeight = (document.body.parentNode.offsetHeight > this.getWindowHeight() ? this.getWindowHeight() - document.body.parentNode.scrollTop : document.body.parentNode.offsetHeight);
totalWidth = (document.body.parentNode.offsetWidth > this.getWindowWidth() ? this.getWindowWidth() - document.body.parentNode.scrollLeft : document.body.parentNode.offsetWidth);
}
if (node.offsetParent) {
do {
left += node.offsetLeft - (node.offsetParent ? node.offsetParent.scrollLeft : 0);
if (transformProp && xregex.test(node.style[transformProp])) {
left += parseInt(node.style[transformProp].replace(xregex, '$1'), 10);
}
top += node.offsetTop - (node.offsetParent ? node.offsetParent.scrollTop : 0);
if (transformProp && yregex.test(node.style[transformProp])) {
top += parseInt(node.style[transformProp].replace(yregex, '$1'), 10);
}
// Need to correct for borders if any exist on parent elements
if (node !== inNode) {
if (node.currentStyle) {
// Oh IE, we do so love working around your incompatibilities
borderLeft = parseInt(node.currentStyle.borderLeftWidth, 10);
borderTop = parseInt(node.currentStyle.borderTopWidth, 10);
} else if (window.getComputedStyle) {
borderLeft = parseInt(window.getComputedStyle(node, '').getPropertyValue('border-left-width'), 10);
borderTop = parseInt(window.getComputedStyle(node, '').getPropertyValue('border-top-width'), 10);
} else {
// No computed style options, so try the normal style object (much less robust)
borderLeft = parseInt(node.style.borderLeftWidth, 10);
borderTop = parseInt(node.style.borderTopWidth, 10);
}
if (borderLeft) {
left += borderLeft;
}
if (borderTop) {
top += borderTop;
}
}
} while ((node = node.offsetParent) && node !== relativeToNode);
}
return {
'top': top,
'left': left,
'bottom': totalHeight - top - height,
'right': totalWidth - left - width,
'height': height,
'width': width
};
},
// Can optionally include this stub function, if you think it's important to preserve the original naming
calcViewportPositionForNode: function(inNode) {
return this.calcNodePosition(inNode);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment