changeset 5712:7a83cc907f68

simplify auto scroll initialization; fix bug in IE init discovered when using IE7 on pages with wide tables
author Roger Haase <crosseyedpenquin@yahoo.com>
date Fri, 23 Jul 2010 09:23:59 -0700
parents 58a9aa0d67bd
children 32811373ffa1
files MoinMoin/web/static/htdocs/common/js/common.js
diffstat 1 files changed, 23 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/web/static/htdocs/common/js/common.js	Mon Jul 05 23:21:21 2010 +0000
+++ b/MoinMoin/web/static/htdocs/common/js/common.js	Fri Jul 23 09:23:59 2010 -0700
@@ -655,6 +655,7 @@
 
 // run during page load when user may edit current page OR is viewing draft preview
 function setSpanTags(isPreview) {
+    var startTime = new Date();
     // find all the SPAN tags with an ID beginning with "line-"
     var spanTags = document.getElementsByTagName('span');
     var marks = [];
@@ -679,7 +680,7 @@
         var mark = marks[i];
         // skip span tags generated by embedded parsers
         if (i > skipTo) {
-            // split the ID into parts: looks line "line-22" or "line-22-1"
+            // split the ID into parts: looks like "line-22" or "line-22-1"
             var lineParts = mark.id.split('-');
             var line = lineParts[1];
             if (lineParts.length == 3) {
@@ -716,11 +717,12 @@
             }
         }
     }
-    if (autoScrollDebugOn)  {
+    if (autoScrollDebugOn && document.getElementById('content'))  {
         for (i = 0; i < marks.length-1; ++i) {
             marks[i].innerHTML = ' ' + marks[i].id + ' ';
             marks[i].style. color = "red";
         }
+        showStartStopTimes(startTime);
     }
 }
 
@@ -749,54 +751,33 @@
     }
 }
 
-// Now to resolve the problem of how to best execute scrollTextareaInit
-// -- We want to run as soon as DOM is loaded, perhaps many seconds before last big image is loaded
-// -- If we wait for body.onload, the user may see and doubleclick on text before we are ready 
-// -- If every browser supported DOMContentLoaded, we could do:
-//         document.addEventListener("DOMContentLoaded", scrollTextareaInit, false);
-// -- If we had jQuery, we could do:
-//         jQuery(scrollTextareaInit);
-// -- Another possibility is to add a bit of script near the end of the mypage.HTML, hoping the DOM is ready
-//         '<script type="text/javascript" language="javascript">scrollTextareaInit()</script>'
-// -- Our choice is to speed up most current browsers and do slow but sure for the rest:
-
-// run scrollTextareaInit one time;  this function will be called twice for almost all browsers,
-scrollTextareaInitComplete = 0;
-function runScrollTextareaInitOnce() {
-    // uncomment next line to test - most browsers will display this alert twice 
-    //~ alert('scrollTextareaInitComplete=' + scrollTextareaInitComplete);
-    if (scrollTextareaInitComplete) {
-        return;
-    }
-    scrollTextareaInitComplete = 1;
-    var startTime = new Date();
+// The DOM ready check for Internet Explorer
+function ieScrollCheck() {
+	try {
+		// If IE is used, use the trick by Diego Perini
+		document.documentElement.doScroll("left");
+	} catch( error ) {
+		setTimeout( ieScrollCheck, 1 );
+		return;
+	}
     scrollTextareaInit();
-    if (autoScrollDebugOn && document.getElementById('content')) {
-        showStartStopTimes(startTime);
-    }
 }
 
-// speed up most browsers -- run my function As Soon As Possible
-function runASAP(func) {
+// run auto scroll init As Soon As Possible -- prior to onload for modern browsers
+function runASAP() {
     if (document.addEventListener) { 
         // Firefox 3.6, Chrome 4.0.249.89, Safari for Windows 4.04, Opera 10.5beta, and maybe older versions
-        // schedule func to be run when DOM complete
-        document.addEventListener("DOMContentLoaded", func, false);
+        // schedule func to be run when DOM complete, usually before last image loaded
+        document.addEventListener("DOMContentLoaded", scrollTextareaInit, false);
     } else {
-        // trick discovered by Diego Perini to test for IE DOM complete
         if (document.documentElement.doScroll && window == window.top) {
-            try {
-                document.documentElement.doScroll("left");
-                // DOM is complete; run func now
-                func();
-            } catch(e) {
-                // wait and try again
-                setTimeout(arguments.callee, 1);
-            }
+            // IE 5-8 and not using frames
+            ieScrollCheck();
+        } else {
+            addLoadEvent(scrollTextareaInit);
         }
     }
 }
-runASAP(runScrollTextareaInitOnce);
-// ensure init will be run by obsolete browsers
-addLoadEvent(runScrollTextareaInitOnce);
+// auto scroll initialization starts here
+runASAP();