From patchwork Wed Sep 18 18:33:16 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [2,of,5] hgweb: add makeRequest javascript function From: Alexander Plavin X-Patchwork-Id: 2519 Message-Id: <9ae2d8d0823a34439d94.1379529196@debian-alexander.dolgopa> To: mercurial-devel@selenic.com Date: Wed, 18 Sep 2013 22:33:16 +0400 # HG changeset patch # User Alexander Plavin # Date 1378459857 -14400 # Fri Sep 06 13:30:57 2013 +0400 # Node ID 9ae2d8d0823a34439d94335b42aea155531850f4 # Parent 1a20a7cc2b08c3b720ac02df815905e01276a90a 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 -r 1a20a7cc2b08 -r 9ae2d8d0823a mercurial/templates/static/mercurial.js --- a/mercurial/templates/static/mercurial.js Fri Sep 06 13:30:57 2013 +0400 +++ b/mercurial/templates/static/mercurial.js Fri Sep 06 13:30:57 2013 +0400 @@ -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; +}