Merged changes in main branch up to v.2.1.2
[fa-stable.git] / js / utils.js
index 25b6e19a70a59b23611ffb509e581bfd6624fca2..d051b10f1a900a1a9d1bb4e478cbfe218b7f4715 100644 (file)
@@ -8,6 +8,12 @@
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
+function set_mark(img) {
+       var box = document.getElementById('ajaxmark');
+       if(img) box.src = user.theme+'images/'+ img;
+       box.style.visibility = img ? 'visible' : 'hidden'
+}
+
 //
 //     JsHttpRequest class extensions.
 //
 //             if form parameter exists also form values are submited, otherwise
 //             request is directed to current location 
 // 
-    JsHttpRequest.request= function(trigger, form) {
-               var mark = document.getElementById('ajaxmark');
-               if(mark) mark.style.visibility = 'visible';
+JsHttpRequest.request= function(trigger, form, tout) {
+       tout = tout | 3000;     // default timeout value
+       set_mark(tout>5000 ? 'progressbar.gif' : 'ajax-loader.gif');
+       JsHttpRequest._request(trigger, form, tout, 2);
+}
+
+JsHttpRequest._request = function(trigger, form, tout, retry) {
+
                if (trigger.tagName=='A') {
                        var content = {};
                        var upload = 0;
 
                if (!form) url = url.substring(0, url.indexOf('?'));
                
-               if (!submitObj) 
+               if (!submitObj) {
                        content[trigger] = 1;
-                       
+                       }
                }
                        // this is to avoid caching problems
                content['_random'] = Math.random()*1234567;
+       
+               var tcheck = setTimeout(
+                       function() {
+                               for(var id in JsHttpRequest.PENDING)  {
+                                       var call = JsHttpRequest.PENDING[id];
+                                       if (call != false) {
+                                       if (call._ldObj.xr) // needed for gecko
+                                               call._ldObj.xr.onreadystatechange = function(){};
+                                       call.abort(); // why this doesn't kill request in firebug?
+//                                             call._ldObj.xr.abort();
+                                               delete JsHttpRequest.PENDING[id];
+                                       }
+                               }
+                               set_mark(retry ? 'ajax-loader2.gif':'warning.png' );
+                               if(retry)
+                                       JsHttpRequest._request(trigger, form, tout, retry-1);
+                       }, tout );
 
         JsHttpRequest.query(
             (upload ? "form." : "")+"POST "+url, // force form loader
                                  errors = errors+'<br>Unknown ajax function: '+cmd;
                        }
                  }
-
+                if(tcheck)
+                  JsHttpRequest.clearTimeout(tcheck);
         // Write errors to the debug div.
                  document.getElementById('msgbox').innerHTML = errors;
-                 var mark = document.getElementById('ajaxmark');
-                 if(mark) mark.style.visibility = 'hidden';
+                 set_mark();
 
                  Behaviour.apply();
 
                        }
                }
             },
-            false  // do not disable caching
+               false  // do not disable caching
         );
-    }
+       }
        // collect all form input values plus inp trigger value
        JsHttpRequest.formInputs = function(inp, objForm, upload)
        {
                var q = {};
 
                if (typeof(inp) == "string")
-                       submitObj = document.getElementsByName(inp)[0];
-               else
-                       submitObj = inp;
+                       submitObj = document.getElementsByName(inp)[0]||inp;
                
                objForm = objForm || (submitObj && submitObj.form);
 
@@ -275,9 +301,10 @@ function element_pos(e) {
                var offsetParent = e.offsetParent;
                var parentNode = e.parentNode;
 
-               while (offsetParent !== null) {
+               while (offsetParent !== null && offsetParent.style.display != 'none') {
                        res.x += offsetParent.offsetLeft;
                        res.y += offsetParent.offsetTop;
+                       // the second case is for IE6/7 in some doctypes
                        if (offsetParent != document.body && offsetParent != document.documentElement) {
                                res.x -= offsetParent.scrollLeft;
                                res.y -= offsetParent.scrollTop;
@@ -296,6 +323,6 @@ function element_pos(e) {
                }
        }
        // parentNode has style.display set to none
-       if (parentNode!=document.documentElement) return null;
+       if (parentNode != document.documentElement) return null;
        return res;
 }
\ No newline at end of file