changeset 19746:9ad7dd9574a9

hgweb: add ajaxScrollInit function, which does the ajax requests and processing This function should be correctly called on a page, otherwise there is no effect. When called, it makes ajax requests for the next portion of changesets when the user scrolls to the end. Also, when the monitor is high so that the default amount of changesets isn't enough to fill it, multiple portions are loaded if needed after the page load.
author Alexander Plavin <alexander@plav.in>
date Wed, 18 Sep 2013 22:44:12 +0400
parents 22a70f31e3e9
children da3808bcfbfa
files mercurial/templates/static/mercurial.js
diffstat 1 files changed, 58 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templates/static/mercurial.js	Wed Sep 18 14:36:19 2013 -0500
+++ b/mercurial/templates/static/mercurial.js	Wed Sep 18 22:44:12 2013 +0400
@@ -345,3 +345,61 @@
 function appendFormatHTML(element, formatStr, replacements) {
     element.insertAdjacentHTML('beforeend', format(formatStr, replacements));
 }
+
+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;
+
+            if (!nextHash) {
+                return;
+            }
+
+            makeRequest(
+                format(urlFormat, {hash: nextHash}),
+                'GET',
+                function onstart() {
+                },
+                function onsuccess(htmlText) {
+                    var m = htmlText.match(nextHashRegex);
+                    nextHash = m ? m[1] : null;
+
+                    var doc = docFromHTML(htmlText);
+                    var nodes = doc.querySelector(containerSelector).children;
+                    while (nodes.length) {
+                        var node = nodes[0];
+                        node = document.adoptNode(node);
+                        container.appendChild(node);
+                    }
+                    process_dates();
+                },
+                function onerror(errorText) {
+                },
+                function oncomplete() {
+                    updateInitiated = false;
+                    scrollHandler();
+                }
+            );
+        }
+    }
+
+    window.addEventListener('scroll', scrollHandler);
+    window.addEventListener('resize', scrollHandler);
+    scrollHandler();
+}