Repository navigation
Expand file tree
/
Copy pathfilters.js
More file actions
87 lines (76 loc) · 3.55 KB
/
Copy pathfilters.js
File metadata and controls
87 lines (76 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/** @import { Picked } from "./authors.js" */
import { plural } from "./plural.js";
let filters = document.querySelector("#filters");
let applied = filters.querySelector(".applied");
let controls = filters.querySelector(".controls");
let fold = filters.querySelector(".fold");
const foldKey = "review-queue:filters-folded";
/** Folded on the first visit, then as the user left them */
let folded = localStorage.getItem(foldKey) !== "false";
setFolded(folded);
// A value unfolds the controls with the focus on its own, e.g. on Author for “Author foo”. A tap beside the values opens the filter.
// Author is disabled while bars stand in for the rows, and a disabled button takes no focus
applied.addEventListener("click", event => {
let control = document.getElementById(
event.target.closest("[data-for]")?.dataset.for ?? "filter",
);
setFolded(false);
(control.disabled ? fold : control).focus();
});
// No menu is open by then. A click on the fold button is a click outside the menu, and keyboard focus on the button is focus out of it. Both apply the menu
fold.addEventListener("click", () => setFolded(!folded));
/**
* Say on the folded line what the controls apply, e.g. “Filter label:foo Author bar 2 drafts hidden Exclude test”, or just “Filter”.
* @param {{ filter?: string | null, author?: Picked, drafts?: number, exclude?: string | null }} values - Only those whose control shows, e.g. no `exclude` in the author view. `drafts` counts the hidden ones, each a PR someone asked about
*/
export function showApplied ({ filter, author, drafts, exclude }) {
let values = [
filter && value("filter", "Filter", code(filter)),
author &&
value("author-button", author.key, author.faces.cloneNode(true), name(author.name)),
drafts && value("drafts-button", "", `${drafts} ${plural("draft", drafts)} hidden`),
exclude && value("display-button", "Exclude", code(exclude)),
].filter(Boolean);
// Nothing applied is the filter with nothing in it.
// Spaces between, so a screen reader reads “label:foo Author bar”, not “label:fooAuthor bar”
applied.replaceChildren(
...(values.length > 0 ? values : [value("filter", "Filter")]).flatMap((button, i) =>
i > 0 ? [" ", button] : [button]),
);
}
/** Show the folded line or the controls, name what the fold button does next, and remember it for the next visit. */
function setFolded (value) {
folded = value;
applied.hidden = !folded;
controls.hidden = folded;
fold.ariaExpanded = `${!folded}`;
fold.ariaLabel = fold.title = folded ? "Show filters" : "Hide filters";
try {
localStorage.setItem(foldKey, folded);
}
catch (error) {
// A full storage, e.g. of stored queues, loses only the fold on the next visit
console.warn("Couldn’t store the fold.", error);
}
}
/** An applied value, e.g. “Filter label:foo”, which unfolds to the control with this id. */
function value (id, key, ...content) {
let button = Object.assign(document.createElement("button"), { type: "button" });
button.dataset.for = id;
// Its name is what is applied, e.g. “Filter label:foo”, so a screen reader also hears what a press does, rather than guess it removes the value
button.setAttribute("aria-description", "Edit");
if (key) {
button.append(
Object.assign(document.createElement("span"), { className: "key", textContent: key }),
" ",
);
}
button.append(...content);
return button;
}
function code (text) {
return Object.assign(document.createElement("code"), { textContent: text });
}
function name (text) {
return Object.assign(document.createElement("span"), { className: "name", textContent: text });
}