Patchwork [1,of,6] hgweb: adopt child nodes in ajaxScrollInit on /graph pages too

login
register
mail settings
Submitter Anton Shestakov
Date Dec. 4, 2017, 12:40 p.m.
Message ID <c90d696319a8e9c45185.1512391238@neuro>
Download mbox | patch
Permalink /patch/25917/
State Accepted
Headers show

Comments

Anton Shestakov - Dec. 4, 2017, 12:40 p.m.
# HG changeset patch
# User Anton Shestakov <av6@dwimlabs.net>
# Date 1512109040 -28800
#      Fri Dec 01 14:17:20 2017 +0800
# Node ID c90d696319a8e9c451853109b851ab8389b450e9
# Parent  8feef8ef8389a3b544e0a74624f1efc3a8d85d35
# EXP-Topic hgweb-more-info
hgweb: adopt child nodes in ajaxScrollInit on /graph pages too

ajaxScrollInit is a function that loads more elements (e.g. changelog entries)
when browser window is scrolled down to the bottom of the page. It basically
fetches the next page from the server as HTML, finds container element in that
document and "adopts" (essentially, moves) all its child nodes to the container
in the current document.

Currently, hgweb doesn't render any changesets on /graph page (everything is
done in JavaScript), so there are no children to adopt. But there will be soon,
so let's create a reusable function that does it.

Hardcoding #graphnodes selector is suboptimal, but graph code already does this
in two other places.

Patch

diff --git a/mercurial/templates/static/mercurial.js b/mercurial/templates/static/mercurial.js
--- a/mercurial/templates/static/mercurial.js
+++ b/mercurial/templates/static/mercurial.js
@@ -339,6 +339,18 @@  function appendFormatHTML(element, forma
     element.insertAdjacentHTML('beforeend', format(formatStr, replacements));
 }
 
+function adoptChildren(from, to) {
+    var nodes = from.children;
+    var curClass = 'c' + Date.now();
+    while (nodes.length) {
+        var node = nodes[0];
+        node = document.adoptNode(node);
+        node.classList.add(curClass);
+        to.appendChild(node);
+    }
+    process_dates('.' + curClass);
+}
+
 function ajaxScrollInit(urlFormat,
                         nextPageVar,
                         nextPageVarGet,
@@ -382,6 +394,8 @@  function ajaxScrollInit(urlFormat,
                     appendFormatHTML(container, messageFormat, message);
                 },
                 function onsuccess(htmlText) {
+                    var doc = docFromHTML(htmlText);
+
                     if (mode === 'graph') {
                         var graph = window.graph;
                         var sizes = htmlText.match(/^\s*<canvas id="graph" width="(\d+)" height="(\d+)"><\/canvas>$/m);
@@ -398,18 +412,10 @@  function ajaxScrollInit(urlFormat,
                             nextPageVar = undefined;
                         }
                         graph.reset();
+                        adoptChildren(doc.querySelector('#graphnodes'), container.querySelector('#graphnodes'));
                         graph.render(data);
                     } else {
-                        var doc = docFromHTML(htmlText);
-                        var nodes = doc.querySelector(containerSelector).children;
-                        var curClass = 'c' + Date.now();
-                        while (nodes.length) {
-                            var node = nodes[0];
-                            node = document.adoptNode(node);
-                            node.classList.add(curClass);
-                            container.appendChild(node);
-                        }
-                        process_dates('.' + curClass);
+                        adoptChildren(doc.querySelector(containerSelector), container);
                     }
 
                     nextPageVar = nextPageVarGet(htmlText, nextPageVar);