-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
410 lines (391 loc) · 17.5 KB
/
Copy pathindex.html
File metadata and controls
410 lines (391 loc) · 17.5 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Teaching Matrix — Editor</title>
<link rel="stylesheet" href="matrix.css">
<script src="matrix-common.js"></script>
</head>
<body>
<header class="app">
<h1>📊 Teaching Matrix</h1>
<nav class="tabs">
<button id="tab-edit" class="active">View & Edit</button>
<button id="tab-track">My Requests</button>
</nav>
<span class="who" id="who"></span>
</header>
<main>
<!-- connection / sign-in -->
<section class="card" id="connect-card">
<div class="row">
<strong>Repository:</strong> <span id="repo-label" class="note">detecting…</span>
<button class="btn" id="repo-change">change</button>
<span style="flex:1"></span>
<input type="password" id="token-input" placeholder="GitHub personal access token" size="34">
<button class="btn primary" id="signin">Sign in</button>
<button class="btn" id="signout" hidden>Sign out</button>
</div>
<div class="note" style="margin-top:6px">
Create a <b>fine-grained personal access token</b> at
GitHub → Settings → Developer settings → Fine-grained tokens with
<b>Resource owner = the organization that owns this repo</b> (not your personal account),
scoped to this repository, with <b>Contents: Read & write</b> and <b>Pull requests: Read & write</b>.
If the organization doesn't appear as a resource owner, it hasn't enabled fine-grained tokens —
use a <b>classic token</b> (scope: <code>repo</code>) instead. The token stays in your browser
(localStorage) and is only ever sent to api.github.com.
</div>
<div id="connect-msg"></div>
</section>
<!-- EDIT TAB -->
<div id="pane-edit">
<section class="card">
<div class="row">
<label>Matrix: <select id="dataset"></select></label>
<input type="text" id="filter" placeholder="filter topics…" size="24">
<button class="btn" id="add-course">+ Add course</button>
<button class="btn" id="add-topic">+ Add topic</button>
<button class="btn" id="undo" disabled title="Undo last change (Cmd/Ctrl+Z)">↩ Undo</button>
<span style="flex:1"></span>
<span class="legend">
<span><span class="chip" style="background:var(--e)"></span>E emphasized</span>
<span><span class="chip" style="background:var(--d)"></span>D developed</span>
<span><span class="chip" style="background:var(--i)"></span>I introduced</span>
<span>click a cell to cycle · blank → I → D → E</span>
</span>
</div>
</section>
<div class="matrix-wrap" id="matrix-container"><div style="padding:30px" class="note">Sign in (or view a public repo) to load the matrix…</div></div>
<section class="card" id="changes-card" hidden>
<div class="row">
<strong id="changes-count"></strong>
<span style="flex:1"></span>
<button class="btn" id="discard-all">Discard all</button>
<button class="btn primary" id="submit">Submit as Pull Request…</button>
</div>
<ul class="changes" id="changes-list"></ul>
<div id="submit-msg"></div>
</section>
</div>
<!-- TRACK TAB -->
<div id="pane-track" hidden>
<section class="card">
<div class="row">
<strong>Change requests</strong>
<label><input type="checkbox" id="mine-only" checked> only mine</label>
<label><select id="pr-state"><option value="open">open</option><option value="all">all</option></select></label>
<button class="btn" id="refresh-prs">Refresh</button>
</div>
</section>
<div class="prlist" id="pr-list"><div class="note">Sign in to see your requests.</div></div>
</div>
</main>
<dialog id="dlg-submit">
<h3 style="margin-top:0">Submit change request</h3>
<div class="note" id="dlg-summary"></div>
<div class="row"><input type="text" id="pr-title" size="42" placeholder="Title"></div>
<div class="row"><input type="text" id="pr-note" size="42" placeholder="Optional note for the maintainers"></div>
<div class="row">
<span style="flex:1"></span>
<button class="btn" id="dlg-cancel">Cancel</button>
<button class="btn primary" id="dlg-go">Create pull request</button>
</div>
<div id="dlg-msg"></div>
</dialog>
<script>
"use strict";
let cfg, me = null, ds = null; // config, signed-in user, active dataset
let base = null, work = null; // base matrix (from repo), working copy
let baseSha = null, baseCommitSha = null;
const edits = new Map(); // "key\x1fcol" -> new value (cell edits only)
const newRows = new Set(), newCols = new Set();
const undoStack = []; // [{type:"edit"|"workcell"|"addcol"|"addrow", ...}]
const $ = id => document.getElementById(id);
const msg = (id, text, cls) => { $(id).innerHTML = ""; if (text) $(id).append(el("div", { class: cls || "note" }, text)); };
/* ---------- init ---------- */
(async function init() {
cfg = await loadConfig();
$("repo-label").textContent = cfg.owner && cfg.repo ? `${cfg.owner}/${cfg.repo} @ ${cfg.baseBranch}` : "not configured";
for (const d of cfg.datasets) $("dataset").append(el("option", { value: d.id }, d.label));
ds = cfg.datasets[0];
GH.token = Settings.token;
if (GH.token) { $("token-input").value = "••••••••••••"; await signIn(true); }
else await loadMatrix(); // public repos work read-only without a token
})();
$("repo-change").onclick = () => {
const v = prompt("Repository (owner/repo):", cfg.owner && cfg.repo ? `${cfg.owner}/${cfg.repo}` : "");
if (v === null) return;
Settings.repoOverride = v.trim();
location.reload();
};
$("signin").onclick = () => signIn(false);
$("signout").onclick = () => { Settings.token = ""; location.reload(); };
async function signIn(silent) {
const t = $("token-input").value.trim();
if (!silent && t && !t.startsWith("•")) { GH.token = t; Settings.token = t; }
try {
me = await GH.user();
$("who").textContent = "";
$("who").append(el("img", { src: me.avatar_url, width: "22", height: "22", style: "border-radius:50%" }), me.login);
$("signin").hidden = true; $("signout").hidden = false;
msg("connect-msg", "");
await loadMatrix();
} catch (e) {
if (!silent) msg("connect-msg", "Sign-in failed: " + e.message, "err");
GH.token = null;
}
}
/* ---------- matrix load & render ---------- */
$("dataset").onchange = async () => {
if (edits.size + newRows.size + newCols.size > 0 &&
!confirm("Switching matrices discards your unsubmitted edits. Continue?")) {
$("dataset").value = ds.id; return;
}
ds = cfg.datasets.find(d => d.id === $("dataset").value);
clearEdits(); await loadMatrix();
};
async function loadMatrix() {
if (!cfg.owner || !cfg.repo) { $("matrix-container").innerHTML = '<div style="padding:30px" class="err">Repository not configured — click "change".</div>'; return; }
try {
const br = await GH.branch(cfg.owner, cfg.repo, cfg.baseBranch);
baseCommitSha = br.commit.sha;
const f = await GH.fileAt(cfg.owner, cfg.repo, ds.path, cfg.baseBranch);
baseSha = f.sha;
base = toMatrix(CSV.parse(f.text), ds.keyCols);
work = cloneMatrix(base);
render();
} catch (e) {
$("matrix-container").innerHTML = "";
$("matrix-container").append(el("div", { style: "padding:30px" },
el("div", { class: "err" }, "Could not load matrix: " + e.message),
el("div", { class: "note" }, "For a private repository you must sign in with a token first.")));
}
}
function render() {
const table = renderMatrix($("matrix-container"), work, {
editable: !!me, edits, newRows, newCols,
onCell: cycleCell,
});
applyFilter(table);
refreshChangesPanel();
$("undo").disabled = undoStack.length === 0;
}
$("filter").oninput = () => { const t = document.querySelector("#matrix-container table"); if (t) applyFilter(t); };
function applyFilter(table) {
const q = $("filter").value.trim().toLowerCase();
for (const tr of table.querySelectorAll("tbody tr")) {
if (tr.classList.contains("cat-row")) continue;
tr.hidden = q && !tr.textContent.toLowerCase().includes(q);
}
}
function cycleCell(key, col) {
const ek = key + "\x1f" + col;
const orig = cellValue(base, key, col);
const cur = edits.has(ek) ? edits.get(ek) : cellValue(work, key, col);
const order = cfg.levels;
const next = order[(order.indexOf(cur) + 1) % order.length];
// new rows/cols live directly in `work`; existing cells go through `edits`
if (newRows.has(key) || newCols.has(col)) {
undoStack.push({ type: "workcell", key, col, prev: cellValue(work, key, col) });
setCell(work, key, col, next);
} else {
undoStack.push({ type: "edit", ek, prev: edits.has(ek) ? edits.get(ek) : null });
if (next === orig) edits.delete(ek);
else edits.set(ek, next);
}
render();
}
function cellValue(m, key, col) {
const r = m.rows.find(r => rowKey(m, r) === key);
const i = m.header.indexOf(col);
return r && i >= 0 ? (r[i] || "").trim() : "";
}
function setCell(m, key, col, v) {
const r = m.rows.find(r => rowKey(m, r) === key);
const i = m.header.indexOf(col);
if (r && i >= 0) r[i] = v;
}
/* ---------- add course / topic ---------- */
$("add-course").onclick = () => {
if (!me) return alert("Sign in first.");
const name = prompt('New course column name (e.g. "ASDS 6307"):');
if (!name) return;
const n = name.trim();
if (work.header.includes(n)) return alert("That column already exists.");
work.header.push(n);
for (const r of work.rows) r.push("");
newCols.add(n);
undoStack.push({ type: "addcol", name: n });
render();
};
$("add-topic").onclick = () => {
if (!me) return alert("Sign in first.");
const cat = prompt("Category (exactly as shown on a category bar, or a new one):");
if (!cat) return;
const keyParts = [cat.trim()];
for (let i = 1; i < work.keyN; i++) {
const label = work.header[i];
const v = prompt(`${label}:`);
if (!v) return;
keyParts.push(v.trim());
}
const row = [...keyParts, ...Array(work.header.length - work.keyN).fill("")];
const key = rowKey(work, row);
if (work.rows.some(r => rowKey(work, r) === key)) return alert("That topic already exists.");
// insert at end of its category group (or append if new category)
let at = work.rows.length;
for (let i = work.rows.length - 1; i >= 0; i--)
if (work.rows[i][0] === keyParts[0]) { at = i + 1; break; }
work.rows.splice(at, 0, row);
newRows.add(key);
undoStack.push({ type: "addrow", key });
render();
};
/* ---------- changes panel ---------- */
function currentDiff() {
const target = cloneMatrix(work);
for (const [ek, v] of edits) {
// rowKey itself contains \x1f separators, so split on the LAST one (col names can't contain it)
const i = ek.lastIndexOf("\x1f");
setCell(target, ek.slice(0, i), ek.slice(i + 1), v);
}
return { target, diff: diffMatrices(base, target) };
}
function refreshChangesPanel() {
const { diff } = currentDiff();
const n = diff.cells.length + diff.addedRows.length + diff.addedCols.length;
$("changes-card").hidden = n === 0;
if (!n) return;
$("changes-count").textContent = `${n} pending change${n > 1 ? "s" : ""}`;
const ul = $("changes-list"); ul.textContent = "";
for (const c of diff.addedCols) ul.append(el("li", {}, `+ course column “${c}”`));
for (const rname of diff.addedRows) ul.append(el("li", {}, `+ topic “${rname}”`));
for (const c of diff.cells) {
const li = el("li", {}, `${c.label} × ${c.col}: ${c.old || "·"} → ${c.new || "·"}`);
const ek = c.key + "\x1f" + c.col;
if (edits.has(ek)) li.append(el("span", { class: "revert", title: "revert", onclick: () => { edits.delete(ek); render(); } }, "✕"));
ul.append(li);
}
}
$("discard-all").onclick = () => { if (confirm("Discard all pending edits?")) { clearEdits(); work = cloneMatrix(base); render(); } };
function clearEdits() { edits.clear(); newRows.clear(); newCols.clear(); undoStack.length = 0; }
/* ---------- undo ---------- */
function undo() {
const op = undoStack.pop();
if (!op) return;
if (op.type === "edit") {
if (op.prev === null) edits.delete(op.ek);
else edits.set(op.ek, op.prev);
} else if (op.type === "workcell") {
setCell(work, op.key, op.col, op.prev);
} else if (op.type === "addcol") {
const i = work.header.indexOf(op.name);
if (i >= 0) { work.header.splice(i, 1); for (const r of work.rows) r.splice(i, 1); }
newCols.delete(op.name);
} else if (op.type === "addrow") {
const i = work.rows.findIndex(r => rowKey(work, r) === op.key);
if (i >= 0) work.rows.splice(i, 1);
newRows.delete(op.key);
}
render();
}
$("undo").onclick = undo;
document.addEventListener("keydown", e => {
if ((e.metaKey || e.ctrlKey) && !e.shiftKey && e.key.toLowerCase() === "z") {
const t = e.target.tagName;
if (t === "INPUT" || t === "TEXTAREA") return; // don't hijack text-field undo
e.preventDefault(); undo();
}
});
/* ---------- submit PR ---------- */
$("submit").onclick = () => {
if (!me) return alert("Sign in first.");
const { diff } = currentDiff();
const n = diff.cells.length + diff.addedRows.length + diff.addedCols.length;
$("dlg-summary").textContent = `${n} change(s) to ${ds.label} will be proposed on a new branch and opened as a pull request against ${cfg.baseBranch}.`;
$("pr-title").value = `Matrix update: ${summaryTitle(diff)}`;
msg("dlg-msg", "");
$("dlg-submit").showModal();
};
function summaryTitle(diff) {
const bits = [];
if (diff.addedCols.length) bits.push(`add ${diff.addedCols.join(", ")}`);
if (diff.addedRows.length) bits.push(`${diff.addedRows.length} new topic(s)`);
if (diff.cells.length) bits.push(`${diff.cells.length} cell(s)`);
return bits.join(" · ") || "no-op";
}
$("dlg-cancel").onclick = () => $("dlg-submit").close();
$("dlg-go").onclick = async () => {
const { target, diff } = currentDiff();
const title = $("pr-title").value.trim() || "Teaching matrix update";
const note = $("pr-note").value.trim();
const branch = cfg.branchPrefix + me.login + "-" + Date.now().toString(36);
$("dlg-go").disabled = true;
msg("dlg-msg", "Creating branch…");
try {
await GH.createBranch(cfg.owner, cfg.repo, branch, baseCommitSha);
msg("dlg-msg", "Committing CSV…");
await GH.putFile(cfg.owner, cfg.repo, ds.path, branch, CSV.serialize(matrixRows(target)), title, baseSha);
msg("dlg-msg", "Opening pull request…");
let body = changesToMarkdown(ds.label, diff);
if (note) body = `> ${note}\n\n` + body;
body += `\n\n<!-- tm-dataset:${ds.id} -->`;
const pr = await GH.createPR(cfg.owner, cfg.repo, branch, cfg.baseBranch, title, body);
$("dlg-submit").close();
clearEdits();
await loadMatrix();
msg("submit-msg", "");
$("submit-msg").append(el("div", { class: "ok" }, "Pull request created: "),
el("a", { href: pr.html_url, target: "_blank" }, `#${pr.number} — ${pr.title}`));
showTab("track"); refreshPRs();
} catch (e) {
if (e.status === 403 || e.status === 404) {
$("dlg-msg").innerHTML = "";
$("dlg-msg").append(
el("div", { class: "err" }, "GitHub rejected the write (" + e.message + "). Most common causes:"),
el("ul", { class: "note" },
el("li", {}, "Your fine-grained token's Resource owner is your personal account — for this org-owned repo it must be the ", el("b", {}, "organization"), ". Re-create the token and pick the org under \u201cResource owner\u201d."),
el("li", {}, "The organization hasn't enabled fine-grained tokens (org Settings \u2192 Third-party Access \u2192 Personal access tokens). A ", el("b", {}, "classic token"), " with the \u201crepo\u201d scope works regardless."),
el("li", {}, "You aren't a collaborator on the repository — ask a maintainer to add you with Write access.")));
} else msg("dlg-msg", "Failed: " + e.message, "err");
} finally { $("dlg-go").disabled = false; }
};
/* ---------- track tab ---------- */
function showTab(which) {
$("pane-edit").hidden = which !== "edit";
$("pane-track").hidden = which !== "track";
$("tab-edit").classList.toggle("active", which === "edit");
$("tab-track").classList.toggle("active", which === "track");
}
$("tab-edit").onclick = () => showTab("edit");
$("tab-track").onclick = () => { showTab("track"); refreshPRs(); };
$("refresh-prs").onclick = refreshPRs;
$("mine-only").onchange = refreshPRs;
$("pr-state").onchange = refreshPRs;
async function refreshPRs() {
const box = $("pr-list");
box.innerHTML = ""; box.append(el("div", {}, el("span", { class: "spinner" }), " loading…"));
try {
const state = $("pr-state").value;
let prs = await GH.listPRs(cfg.owner, cfg.repo, state === "all" ? "all" : "open");
prs = prs.filter(p => p.head.ref.startsWith(cfg.branchPrefix));
if ($("mine-only").checked && me) prs = prs.filter(p => p.user.login === me.login);
box.textContent = "";
if (!prs.length) { box.append(el("div", { class: "note" }, "No change requests found.")); return; }
for (const pr of prs) {
const card = el("div", { class: "pr" },
el("h3", {}, prBadge(pr), " ", el("a", { href: pr.html_url, target: "_blank" }, `#${pr.number} ${pr.title}`)),
el("div", { class: "meta" }, `by ${pr.user.login} · opened ${fmtDate(pr.created_at)}` + (pr.merged_at ? ` · merged ${fmtDate(pr.merged_at)}` : "")));
box.append(card);
prStatus(cfg.owner, cfg.repo, pr).then(rs => {
if (rs) card.querySelector("h3").append(" ", el("span", { class: "badge review" }, rs));
});
}
} catch (e) {
box.textContent = ""; box.append(el("div", { class: "err" }, "Could not load PRs: " + e.message));
}
}
</script>
</body>
</html>