Patchwork [3,of,6,V2] hgweb: add ajaxScrollInit skeleton

login
register
mail settings
Submitter Alexander Plavin
Date Aug. 17, 2013, 10:28 p.m.
Message ID <474146e53438e832a7a1.1376778538@debian-alexander.dolgopa>
Download mbox | patch
Permalink /patch/2204/
State Deferred
Headers show

Comments

Alexander Plavin - Aug. 17, 2013, 10:28 p.m.
# HG changeset patch
# User Alexander Plavin <alexander@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.

Patch

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