# HG changeset patch
# User Alexander Plavin <alexander@plav.in>
# Date 1378459857 -14400
#      Fri Sep 06 13:30:57 2013 +0400
# Node ID 2228bd109706cda974a1338f5127e15d3036b559
# Parent  5bdc179e58c13d7d37e2c77af4904c1ef4edf0ef
hgweb: add makeRequest javascript function

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

diff --git a/mercurial/templates/static/mercurial.js b/mercurial/templates/static/mercurial.js
--- a/mercurial/templates/static/mercurial.js
+++ b/mercurial/templates/static/mercurial.js
@@ -304,3 +304,27 @@
         return String(replacements[p1]);
     });
 }
+
+function makeRequest(url, method, onstart, onsuccess, onerror, oncomplete) {
+    xfr = new XMLHttpRequest();
+    xfr.onreadystatechange = function() {
+        if (xfr.readyState === 4) {
+            try {
+                if (xfr.status === 200) {
+                    onsuccess(xfr.responseText);
+                } else {
+                    throw 'server error';
+                }
+            } catch (e) {
+                onerror(e);
+            } finally {
+                oncomplete();
+            }
+        }
+    };
+
+    xfr.open(method, url);
+    xfr.send();
+    onstart();
+    return xfr;
+}