b578bce0c20f48ee1808233843105d0db92ad297
larrym
  Wed May 2 14:17:34 2012 -0700
refactor loadingImg code so it can be reused in hgVarAnnogrator
diff --git src/hg/js/ajax.js src/hg/js/ajax.js
index 7329d8f..be6145b 100644
--- src/hg/js/ajax.js
+++ src/hg/js/ajax.js
@@ -449,15 +449,59 @@
     if($(buttOk).length == 1)
         $(buttOk).focus();
 }
 
 function scrapeVariable(html, name)
 {
 // scrape a variable defintion out of html (see jsHelper.c::jsPrintHash)
     var re = new RegExp("^// START " + name + "\\nvar " + name + " = ([\\S\\s]+);\\n// END " + name + "$", "m");
     var a = re.exec(html);
     var json;
     if(a && a[1]) {
         json = eval("(" + a[1] + ")");
     }
     return json;
 }
+
+// The loadingImage module helps you manage a loading image (for a slow upload; e.g. in hgCustom).
+
+var loadingImage = function ()
+{
+    // private vars
+    var imgEle, msgEle, statusMsg;
+
+    // private methods
+    var refreshLoadingImg = function()
+    {
+        // hack to make sure animation continues in IE after form submission
+        // See: http://stackoverflow.com/questions/774515/keep-an-animated-gif-going-after-form-submits
+        // and http://stackoverflow.com/questions/780560/animated-gif-in-ie-stopping
+        imgEle.attr('src', imgEle.attr('src'));
+    };
+
+    // public methods
+    return {
+        init: function(_imgEle, _msgEle, _msg)
+        {
+            // This should be called from the ready method; imgEle and msgEle should be jQuery objects
+            imgEle = _imgEle;
+            msgEle = _msgEle;
+            statusMsg = _msg;
+            // To make the loadingImg visible on FF, we have to make sure it's visible during page load (i.e. in html) otherwise it doesn't get shown by the submitClick code.
+            imgEle.hide();
+        },
+        run: function() {
+            msgEle.append(statusMsg);
+            if(navigator.userAgent.indexOf("Chrome") != -1) {
+                // In Chrome, gif animation and setTimeout's are stopped when the browser receives the first blank line/comment of the next page
+                // (basically, the current page is unloaded). I have found no way around this problem, so we just show a 
+                // simple "Processing..." message (we can't make that blink, b/c Chrome doesn't support blinking text).
+                // 
+                // (Surprisingly, this is NOT true for Safari, so this is apparently not a WebKit issue).
+                imgEle.replaceWith("<span id='loadingBlinker'>&nbsp;&nbsp;<b>Processing...</b></span>");
+            } else {
+                imgEle.show();
+                setTimeout(refreshLoadingImg, 1000);
+            }
+        }
+    }
+}();