Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
234 lines (213 loc) · 8.65 KB
/
Copy pathindex.js
File metadata and controls
234 lines (213 loc) · 8.65 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { describe, github } from "./github.js";
import { forget, storedQueues } from "./queues.js";
import { unmark } from "./row.js";
import { offerRecent } from "./recent.js";
import { showDisplay } from "./display.js";
import { passFocus } from "./menu.js";
import { href, normalizeParams, paramNames, pickNames, viewKey, viewOf } from "./view.js";
import { showNotice } from "./status.js";
import { onScreen, pick, showQueue } from "./screen.js";
import { load, loading } from "./load.js";
let table = document.querySelector("#queue");
let status = document.querySelector("#status");
let progress = document.querySelector("progress");
let signIn = document.querySelector("#sign-in");
let refresh = document.querySelector("#refresh");
let signOut = document.querySelector("#sign-out");
let controls = document.querySelector("#controls");
let tokenForm = document.querySelector("#token-form");
let repoForm = document.querySelector("#repo-form");
let strip = document.querySelector("#summary");
let filterForm = document.querySelector("#narrow");
let authorForm = document.querySelector("#authors");
let displayForm = document.querySelector("#display");
let draftsForm = document.querySelector("#drafts");
let forms = [
repoForm,
filterForm,
authorForm,
displayForm,
draftsForm,
...strip.querySelectorAll("form"),
];
// Opening a GitHub link of a marked row, e.g. its PR, is seeing what changed in it.
// GitHub links open in a new tab, so `[target]` leaves out links that filter the queue, e.g. its author.
// A middle click opens a link too. A right click opens a menu instead
for (let type of ["click", "auxclick"]) {
table.tBodies[0].addEventListener(type, event => {
let tr = event.target.closest("a[target]")?.closest("tr");
if (tr && event.button < 2) {
unmark(tr);
}
});
}
signIn
.querySelector(":scope > button")
.addEventListener("click", () =>
github.login().catch(error => showNotice(signIn, "Couldn’t sign in", describe(error))));
tokenForm.addEventListener("submit", event => {
event.preventDefault();
// Madata stores and validates the token as it does after the popup, then fires mv-login, which login() waits for
github
.login({ accessToken: tokenForm.elements.token.value })
.catch(error => showNotice(signIn, "Couldn’t sign in", describe(error)));
});
// A token that works but can’t read the repo keeps the sign-in hidden, so signing out is the way to replace it
signOut.addEventListener("click", async () => {
await github.logout();
// A load that ends before the reload would store its queue again
loading?.abort();
forget();
location.reload();
});
// The forms submit their fields as URL parameters, so the URL they open can be copied and reused.
// A form has fields only for what it changes, and keeps the rest of the URL, e.g. the filter form its `repo`.
// Capturing, this runs before a form’s own listener, e.g. the author list’s, which then replaces the URL’s `authors`
for (let form of forms) {
form.addEventListener(
"formdata",
({ formData }) => {
let params = new URLSearchParams(location.search);
for (let name of paramNames) {
if (!formData.has(name) && params.has(name)) {
formData.set(name, params.get(name));
}
}
normalizeParams(formData);
// An empty field means its default, e.g. the signed-in user: ?repo=foo/bar, not ?repo=foo/bar&user=
for (let [name, value] of [...formData]) {
if (!value) {
formData.delete(name);
}
}
},
{ capture: true },
);
}
// The repos and users of the stored queues, e.g. those opened yesterday. Forgetting one forgets its queues
// NOTE: The same repo in another case, e.g. `Foo/Bar`, shows twice.
// Clearing the repos forgets every queue, but the user field shows its chevron until it is opened
for (let name of ["repo", "user"]) {
offerRecent(repoForm.elements[name], {
values: () => [
...new Set(
storedQueues()
.map(({ view }) => view[name])
.filter(Boolean),
),
],
remove: value => forget(view => view[name] === value),
});
}
// An empty filter field submits no `filter`
filterForm.querySelector(".cancel").addEventListener("click", event => {
filterForm.elements.filter.value = "";
passFocus(event.currentTarget, filterForm.elements.filter);
filterForm.requestSubmit();
});
refresh.addEventListener("click", () => {
if (!loading) {
load();
}
});
// A typed or shared URL can repeat the view’s qualifiers in its filter, e.g. `?role=author&filter=author:foo`.
// The rewrite runs before the navigate listener, which would load the URL a second time
let current = new URLSearchParams(location.search);
let normalized = new URLSearchParams(current);
normalizeParams(normalized);
// A repo or user that its field would reject, e.g. `foo/bar/issues`, names none, so it reaches neither GitHub’s URLs nor the page
for (let field of [repoForm.elements.repo, repoForm.elements.user]) {
field.value = normalized.get(field.name) ?? "";
if (field.validity.patternMismatch) {
normalized.delete(field.name);
}
}
// Encoded as the page’s links and forms encode it, e.g. `repo=foo%2Fbar`, so a link to the view on screen has its URL
let search = `${normalized}` && `?${normalized}`;
if (search !== location.search) {
history.replaceState(null, "", `${location.pathname}${search}${location.hash}`);
}
// A filter, a role switch or another repo loads in place, so what it doesn’t change stays put, e.g. the focus in the filter field.
// Without the Navigation API, or to or from the page that asks for a repo, the page reloads.
// Going back to that page then shows it, not just its URL, and leaving it brings back the controls it hides
window.navigation?.addEventListener("navigate", event => {
let next = new URL(event.destination.url).searchParams;
let current = new URLSearchParams(location.search);
if (
!event.canIntercept ||
event.hashChange ||
event.navigationType === "reload" ||
!current.get("repo") ||
!next.get("repo")
) {
return;
}
// A link to the URL on screen changes nothing, e.g. a label already in the filter or the picked author.
// A form submitted again loads anew, e.g. Enter with the same terms
// NOTE: A label typed in another form, e.g. `label:bug`, searches again, as its link quotes it: `label:"bug"`
if (event.sourceElement?.localName === "a" && `${next}` === `${current}`) {
event.intercept({ focusReset: "manual", scroll: "manual" });
return;
}
// The same view submitted again, e.g. Enter with the same terms, loads it again
let onlyPicks =
viewKey(viewOf(next)) === viewKey(viewOf(current)) &&
pickNames.some(name => next.get(name) !== current.get(name));
// The handler runs once the URL has changed. Focus and scroll stay where they were, e.g. in the filter field
event.intercept({
focusReset: "manual",
scroll: "manual",
handler: () => showView({ onlyPicks }),
});
});
showView();
/**
* Show what the URL asks for, e.g. `?repo=foo/bar&filter=label:baz`.
* @param {{ onlyPicks?: boolean }} [options] - Whether the URL changed only what hides rows, e.g. its `authors` after a pick in the list
*/
function showView ({ onlyPicks } = {}) {
let params = new URLSearchParams(location.search);
let view = viewOf(params);
let { role, filter, exclude } = view;
pick({
authors: role === "author" ? [] : (params.get("authors")?.split(",").filter(Boolean) ?? []),
drafts: params.get("drafts"),
});
// As defaults, so a reset, e.g. Esc in a form of the strip, goes back to what the URL says
for (let form of forms) {
for (let field of form.elements) {
if (field.name) {
field.defaultValue = params.get(field.name) ?? "";
}
}
// An open menu keeps what is changed in it, e.g. on a step back, as it does while a refresh ends
if (!form.matches(":popover-open")) {
form.reset();
}
}
// Each role opens the same queue, filter and all, from the other side
// The other role’s qualifier in the filter, e.g. `author:foo`, names its user
for (let link of strip.querySelectorAll(".roles a")) {
link.href = href({ role: link.dataset.role });
link.ariaCurrent = (link.dataset.role === "author") === (role === "author") ? "page" : null;
}
filterForm.querySelector(".cancel").hidden = !filter;
showDisplay(exclude);
if (view.repo) {
// Other picks of the view on screen need no load, e.g. other authors. Their rows are on screen already
if (onlyPicks && onScreen(view)) {
return showQueue();
}
// The load of this view in flight shows the picks once its search ends
if (onlyPicks && loading) {
return;
}
return load();
}
// The page’s first script hides the controls before the first paint, but it still sees a repo that the URL drops, e.g. `foo/bar/issues`
controls.hidden = true;
// The form’s heading says what to do
status.hidden = true;
progress.hidden = true;
repoForm.hidden = false;
}