// fold.js — folding the files of a diff.
//
// Each file is a <details>, so it folds with no script; this adds what
// needs one. "Collapse all" and "Expand all" act on every file on the
// page, and an address that points into a folded file opens it, because
// a browser does not open a <details> to reach an anchor inside it.
//
// Delegated from the document, like copy.js: the check-in page's diff
// and each later window of it arrive as patched fragments.
(function () {
"use strict";
document.addEventListener("click", function (ev) {
var b = ev.target.closest && ev.target.closest("[data-hook=diff-collapse-all],[data-hook=diff-expand-all]");
if (!b) return;
var open = b.getAttribute("data-hook") === "diff-expand-all";
document.querySelectorAll("details.filediff-fold").forEach(function (d) { d.open = open; });
});
// Opens whatever folds hide the address's target, then scrolls to it:
// the browser's own jump landed on a hidden element. A file's own
// anchor (#f-path) sits on the section around its fold, so that opens
// too. Answers whether the target exists yet.
function reveal() {
// A file's anchor keeps its path escaped (f-docs%2Fhooks.md), so
// the address is tried as written before it is tried decoded.
var id = location.hash.slice(1), t = id && document.getElementById(id);
if (!t && id) {
try { t = document.getElementById(decodeURIComponent(id)); } catch (e) { /* not decodable */ }
}
if (!t) return false;
var opened = false;
var own = t.querySelector(":scope > details.filediff-fold");
if (own && !own.open) { own.open = true; opened = true; }
for (var p = t.parentElement; p; p = p.parentElement) {
if (p.tagName === "DETAILS" && !p.open) { p.open = true; opened = true; }
}
if (opened) t.scrollIntoView();
return true;
}
window.addEventListener("hashchange", reveal);
if (!reveal() && location.hash) {
// The check-in page's diff arrives after the page; wait for the
// target to appear, but not forever.
var watch = new MutationObserver(function () { if (reveal()) watch.disconnect(); });
watch.observe(document.body, { childList: true, subtree: true });
setTimeout(function () { watch.disconnect(); }, 15000);
}
})();