Patchwork [08,of,20] hgweb: add makeRequest javascript function

login
register
mail settings
Submitter Alexander Plavin
Date Aug. 9, 2013, 6:57 p.m.
Message ID <d4cdebd1a84f65faa6a2.1376074653@debian-alexander.dolgopa>
Download mbox | patch
Permalink /patch/2115/
State Superseded
Commit 2228bd109706cda974a1338f5127e15d3036b559
Headers show

Comments

Alexander Plavin - Aug. 9, 2013, 6:57 p.m.
# HG changeset patch
# User Alexander Plavin <alexander@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
Laurens Holst - Aug. 10, 2013, 1:06 p.m.
Op 09-08-13 20:57, Alexander Plavin schreef:
> # HG changeset patch
> # User Alexander Plavin <alexander@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) {
> +            }
> +        }
> +    }

Internet Explorer supports XMLHttpRequest() natively since version 7. So 
all this ActiveXObject stuff is not needed.

> +    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;
> +}
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel
>
Alexander Plavin - Aug. 12, 2013, 5:44 p.m.
10.08.2013, 17:07, "Laurens Holst" <laurens.nospam@grauw.nl>:
> Op 09-08-13 20:57, Alexander Plavin schreef:
>
>>  # HG changeset patch
>>  # User Alexander Plavin <alexander@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) {
>>  +            }
>>  +        }
>>  +    }
>
> Internet Explorer supports XMLHttpRequest() natively since version 7. So
> all this ActiveXObject stuff is not needed.

Heh, didn't know about and just took this piece of code (which creates xfr) from SO :)

>
>>  +    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;
>>  +}
>>  _______________________________________________
>>  Mercurial-devel mailing list
>>  Mercurial-devel@selenic.com
>>  http://selenic.com/mailman/listinfo/mercurial-devel
>
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel

Patch

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;
+}