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.
This commit is contained in:
Anton Shestakov 2017-12-01 14:17:20 +08:00
parent ed55294406
commit 43f716d8d5

View File

@ -339,6 +339,18 @@ function appendFormatHTML(element, formatStr, replacements) {
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);