[bugzilla-gnome-org-extensions] Add scrollToElement() utility function



commit 6101d11013f681603e9847a29a5ca4f8550c5c7d
Author: Owen W. Taylor <otaylor fishsoup net>
Date:   Sun Oct 4 17:13:15 2009 -0400

    Add scrollToElement() utility function
    
    Add a function to scroll to a specified element; factor out the
    code for determining an element's relative to document.body
    into a getElementPosition() utility function.

 js/splinter.js |   28 +++++++++++++++++++++-------
 1 files changed, 21 insertions(+), 7 deletions(-)
---
diff --git a/js/splinter.js b/js/splinter.js
index 4597678..bde528a 100644
--- a/js/splinter.js
+++ b/js/splinter.js
@@ -474,17 +474,31 @@ function EL(element, cls, text) {
     return e;
 }
 
+function getElementPosition(element) {
+    var left = element.offsetLeft;
+    var top = element.offsetTop;
+    var parent = element.offsetParent;
+    while (parent != document.body) {
+        left += parent.offsetLeft;
+        top += parent.offsetTop;
+        parent = parent.offsetParent;
+    }
+
+    return [left, top];
+}
+
+function scrollToElement(element) {
+    var pos = getElementPosition(element);
+    var yCenter = pos[1] + element.offsetHeight / 2;
+    window.scrollTo(0, yCenter - window.innerHeight / 2);
+}
+
 function onRowDblClick(e) {
     var file = $(this).data('patchFile');
 
     if (file.status == Patch.CHANGED) {
-        var leftX = this.offsetLeft;
-        var parent = this.offsetParent;
-        while (parent != document.body) {
-            leftX += parent.offsetLeft;
-            parent = parent.offsetParent;
-        }
-        var delta = e.pageX - (leftX + this.offsetWidth/2);
+        var pos = getElementPosition(this);
+        var delta = e.pageX - (pos[0] + this.offsetWidth/2);
         var type;
         if (delta < - 20)
         type = Patch.REMOVED;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]