Skip to content

Commit 9749248

Browse files
committed
Fix next/prev file navigation to follow tree order
1 parent ed3fe0b commit 9749248

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

gcovr-templates/html/gcovr.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
initSidebarResize();
1111
initMobileMenu();
1212
initFileTree();
13+
initNavOverride();
1314
initBreadcrumbs();
1415
initSearch();
1516
initSorting();
@@ -413,7 +414,8 @@
413414
collapseSingleChildDirs(window.GCOVR_TREE_DATA);
414415
deduplicateTree(window.GCOVR_TREE_DATA);
415416
renderTree(treeContainer, window.GCOVR_TREE_DATA);
416-
// Re-run breadcrumbs and search now that the tree exists
417+
// Re-run dependent init now that the tree exists
418+
initNavOverride();
417419
initBreadcrumbs();
418420
initSearch();
419421
})
@@ -1297,6 +1299,45 @@
12971299
});
12981300
}
12991301

1302+
// ===========================================
1303+
// Nav Override (prev/next follows tree order)
1304+
// ===========================================
1305+
1306+
function initNavOverride() {
1307+
if (!window.GCOVR_TREE_DATA) return;
1308+
1309+
var navPrevs = document.querySelectorAll('.nav-prev');
1310+
var navNexts = document.querySelectorAll('.nav-next');
1311+
if (navPrevs.length === 0 && navNexts.length === 0) return;
1312+
1313+
// DFS-flatten tree to collect file links in sidebar order
1314+
function collectLinks(nodes) {
1315+
var links = [];
1316+
for (var i = 0; i < nodes.length; i++) {
1317+
var node = nodes[i];
1318+
if (node.isDirectory && node.children && node.children.length > 0) {
1319+
links = links.concat(collectLinks(node.children));
1320+
} else if (!node.isDirectory && node.link) {
1321+
links.push(node.link);
1322+
}
1323+
}
1324+
return links;
1325+
}
1326+
1327+
var fileLinks = collectLinks(window.GCOVR_TREE_DATA);
1328+
if (fileLinks.length === 0) return;
1329+
1330+
var currentPage = window.location.pathname.split('/').pop() || 'index.html';
1331+
var idx = fileLinks.indexOf(currentPage);
1332+
if (idx === -1) return;
1333+
1334+
var prev = idx > 0 ? fileLinks[idx - 1] : 'index.html';
1335+
var next = idx < fileLinks.length - 1 ? fileLinks[idx + 1] : 'index.html';
1336+
1337+
navPrevs.forEach(function(el) { el.setAttribute('href', prev); });
1338+
navNexts.forEach(function(el) { el.setAttribute('href', next); });
1339+
}
1340+
13001341
// ===========================================
13011342
// TLA Navigation (HIT/MIS/PAR links)
13021343
// ===========================================

0 commit comments

Comments
 (0)