[PATCH 08 of 20] hgweb: add makeRequest javascript function

Alexander Plavin alexander at plav.in
Fri Aug 9 18:57:33 UTC 2013


# HG changeset patch
# User Alexander Plavin <alexander at plav.in>
# Date 1376061238 -14400
#      Fri Aug 09 19:13:58 2013 +0400
# Node ID d4cdebd1a84f65faa6a21ad275cc9394b03cdef7
# Parent  f112e78a72dab869d57cad83ab40ece62163d517
hgweb: add makeRequest javascript function

This function performs an asynchronous HTTP request and calls provided
callbacks:
- onstart: request is sent
- onsuccess: response is received and it's valid XML
- onerror: some error occured
- oncomplete: after response is fully processed and all callbacks finished

diff -r f112e78a72da -r d4cdebd1a84f mercurial/templates/static/mercurial.js
--- a/mercurial/templates/static/mercurial.js	Fri Aug 09 15:01:33 2013 +0400
+++ b/mercurial/templates/static/mercurial.js	Fri Aug 09 19:13:58 2013 +0400
@@ -299,3 +299,48 @@
         return String(replacements[p1]);
     });
 }
+
+function makeRequest(url, method, onstart, onsuccess, onerror, oncomplete) {
+    if (window.XMLHttpRequest) { // Chromium, Mozilla, Safari, ...
+        xfr = new XMLHttpRequest();
+    } else if (window.ActiveXObject) { // old IE
+        try {
+            xfr = new ActiveXObject("Msxml2.XMLHTTP");
+        }
+        catch (e) {
+            try {
+                xfr = new ActiveXObject("Microsoft.XMLHTTP");
+            }
+            catch (e) {
+            }
+        }
+    }
+    if (!xfr) {
+        throw "AJAX not supported";
+    }
+
+    xfr.overrideMimeType('text/xml');
+    xfr.onreadystatechange = function() {
+        if (xfr.readyState === 4) {
+            try {
+                if (xfr.status === 200) {
+                    if (xfr.responseXML === null) {
+                        throw 'malformed XML response';
+                    }
+                    onsuccess(xfr.responseXML);
+                } else {
+                    throw 'server error';
+                }
+            } catch (e) {
+                onerror(typeof e == 'string' ? e : 'unknown error');
+            } finally {
+                oncomplete();
+            }
+        }
+    };
+
+    xfr.open(method, url);
+    xfr.send();
+    onstart();
+    return xfr;
+}



More information about the Mercurial-devel mailing list