[PATCH 3 of 6 V2] hgweb: add ajaxScrollInit skeleton

Alexander Plavin alexander at plav.in
Sat Aug 17 17:28:58 CDT 2013


# HG changeset patch
# User Alexander Plavin <alexander at plav.in>
# Date 1376754486 -14400
#      Sat Aug 17 19:48:06 2013 +0400
# Node ID 474146e53438e832a7a1b3817273569f03d25aee
# Parent  4e3c3eaff5b224d6aa16a41f112c330341fedf97
hgweb: add ajaxScrollInit skeleton

This piece of code handles onscroll event and makes request to load next page
of entries, but doesn't actually add the loaded entries to the DOM tree yet.

diff -r 4e3c3eaff5b2 -r 474146e53438 mercurial/templates/static/mercurial.js
--- a/mercurial/templates/static/mercurial.js	Sat Aug 17 15:41:33 2013 +0400
+++ b/mercurial/templates/static/mercurial.js	Sat Aug 17 19:48:06 2013 +0400
@@ -328,3 +328,45 @@
     onstart();
     return xfr;
 }
+
+function ajaxScrollInit(urlFormat,
+                        nextHash,
+                        nextHashRegex,
+                        containerSelector,
+                        messageFormat) {
+    updateInitiated = false;
+    container = document.querySelector(containerSelector);
+
+    function scrollHandler() {
+        if (updateInitiated) {
+            return;
+        }
+
+        var scrollHeight = document.documentElement.scrollHeight;
+        var clientHeight = document.documentElement.clientHeight;
+        var scrollTop = document.body.scrollTop
+            || document.documentElement.scrollTop;
+
+        if (scrollHeight - (scrollTop + clientHeight) < 50) {
+            updateInitiated = true;
+
+            makeRequest(
+                format(urlFormat, {hash: nextHash}),
+                'GET',
+                function onstart() {
+                },
+                function onsuccess(htmlText) {
+                },
+                function onerror(errorText) {
+                },
+                function oncomplete() {
+                    updateInitiated = false;
+                }
+            );
+        }
+    }
+
+    window.addEventListener('scroll', scrollHandler);
+    window.addEventListener('resize', scrollHandler);
+    scrollHandler();
+}


More information about the Mercurial-devel mailing list