// dashnav.js — arrow keys through the home board.
//
// In the filter box, ArrowDown and ArrowUp move a highlight through the
// repositories under it (the row with the name, not its event line) and
// Enter opens the highlighted one. With nothing
// highlighted Enter is left alone, so it submits the form as it always
// has. The highlighted row is the box's aria-activedescendant, so a
// screen reader follows it while focus stays in the box.
//
// The list is repainted as the reader types, and the highlight belongs
// to the rows it was made on: typing clears it, and a repaint that
// brings new rows brings none of its marks, so Enter never opens a row
// that is no longer there.
//
// Delegated from the document, like copy.js: the list is a patched
// fragment, and the box outlives every patch.
(function () {
"use strict";
var MARK = "data-hl", ID = "dash-hl";
function boxOf(ev) {
return ev.target.closest && ev.target.closest("[data-hook=dash-find-input]");
}
function rows() {
return Array.prototype.slice.call(document.querySelectorAll("[data-hook=board-row]"));
}
function marked(list) {
return list.findIndex(function (li) { return li.hasAttribute(MARK); });
}
function clear(box) {
rows().forEach(function (li) { li.removeAttribute(MARK); li.removeAttribute("id"); });
box.removeAttribute("aria-activedescendant");
}
document.addEventListener("input", function (ev) {
var box = boxOf(ev);
if (box) clear(box);
});
document.addEventListener("keydown", function (ev) {
var box = boxOf(ev);
if (!box || ev.isComposing || ev.keyCode === 229 || ev.altKey || ev.ctrlKey || ev.metaKey || ev.shiftKey) return;
var list = rows();
var at = marked(list);
if (at < 0) box.removeAttribute("aria-activedescendant");
if (ev.key === "Enter") {
var a = at >= 0 && list[at].querySelector("a.dash-title");
if (a) { ev.preventDefault(); a.click(); }
return;
}
if (ev.key !== "ArrowDown" && ev.key !== "ArrowUp") return;
// A row that is not on screen cannot be highlighted: nobody can see
// where the highlight went.
var shown = list.filter(function (li) { return li.offsetParent !== null; });
if (!shown.length) return;
ev.preventDefault();
var cur = marked(shown);
var next = ev.key === "ArrowDown" ? cur + 1 : (cur < 0 ? shown.length - 1 : cur - 1);
next = (next + shown.length) % shown.length;
clear(box);
shown[next].setAttribute(MARK, "");
shown[next].id = ID;
box.setAttribute("aria-activedescendant", ID);
shown[next].scrollIntoView({ block: "nearest" });
});
})();