Added retry after ajax request timeout, preparation for client-side
[fa-stable.git] / js / utils.js
index 98733d667d84f2542f72b59f3d5f1f1e4d99b23d..b8055c997b83d40f3600f62b6c75f1a967e3532b 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)
        {