pi-durable-bot 0.8.5 → 0.8.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/README.md +12 -4
- package/dist/agent/agent.js +47 -6
- package/dist/agent/agent.js.map +1 -1
- package/dist/agent/models.js +73 -1
- package/dist/agent/models.js.map +1 -1
- package/dist/agent/warming.js +115 -0
- package/dist/agent/warming.js.map +1 -0
- package/dist/bot/bot.js +1 -0
- package/dist/bot/bot.js.map +1 -1
- package/dist/bot/prompt.js +18 -12
- package/dist/bot/prompt.js.map +1 -1
- package/dist/cli/ai.js +3 -0
- package/dist/cli/ai.js.map +1 -1
- package/dist/main.js +3 -2
- package/dist/main.js.map +1 -1
- package/dist/pages/login-page.js +4 -0
- package/dist/pages/login-page.js.map +1 -1
- package/dist/pages/login.js +2 -1
- package/dist/pages/login.js.map +1 -1
- package/docs/for-agents.md +1 -3
- package/kit/fonts/Inter-LICENSE.txt +93 -0
- package/kit/fonts/InterVariable-latin.woff2 +0 -0
- package/kit/login/login.js +1 -0
- package/kit/pots-ui.js +546 -0
- package/kit/pots.css +1850 -198
- package/kit/pots.js +65 -3
- package/package.json +1 -1
- package/skills/pages/SKILL.md +198 -9
- package/skills/pages/shot.mjs +106 -0
package/kit/pots-ui.js
ADDED
|
@@ -0,0 +1,546 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Pots page kit: pots-ui.js. Optional moving parts for pages, offline, no libraries. Link it from a page as
|
|
3
|
+
* <script src="../../__pots/kit/pots-ui.js" defer></script>, with pots.css.
|
|
4
|
+
* Every part starts from plain HTML that reads well without this script:
|
|
5
|
+
* <table data-pots-chart="bar|stack|line"> a chart drawn from the table; the table stays one tap away
|
|
6
|
+
* bar: first column = labels, second = values; data-marks="380 You; 300 Her" draws reference lines;
|
|
7
|
+
* a row's class (pots-good, pots-warn, pots-bad, pots-muted) colours its bar.
|
|
8
|
+
* stack: parts of a whole in one bar with a legend. line: rows are the x axis, each number column a line.
|
|
9
|
+
* data-max (bar) and data-min (line) fix the scale; data-pots-show="…" names the button that shows the numbers.
|
|
10
|
+
* <div data-pots-tabs> with children <section data-tab="Name"> tabs; data-pots-tabs="phone": tabs on phones only.
|
|
11
|
+
* A panel with data-current starts open. box.potsSelect(i) opens one from your own script.
|
|
12
|
+
* <table data-pots-sort> click a column header to sort.
|
|
13
|
+
* A cell's data-value, when it has one, is the value to sort and chart by; else the cell's text, read as a number
|
|
14
|
+
* when the whole cell is one (in the page's language: "1,234.5" in English, "1.234,5" in German), or as text.
|
|
15
|
+
* <input data-pots-filter="#list"> hides rows, or [data-pots-item] elements, that do not contain the text.
|
|
16
|
+
* <nav class="pots-toc"> marks the section on screen.
|
|
17
|
+
* every <table> goes in a <div class="pots-scroll">, so a wide one scrolls inside it.
|
|
18
|
+
* the page gets a light / dark switch at its top corner; window.PotsTheme.set("light").
|
|
19
|
+
* It sets only textContent and CSS custom properties, never HTML from text, so it works under the private-page CSP.
|
|
20
|
+
* Loaded twice, or a part called twice on one element, it does the work once. A part that fails leaves its plain HTML.
|
|
21
|
+
*/
|
|
22
|
+
(function () {
|
|
23
|
+
"use strict";
|
|
24
|
+
if (window.PotsUI) return;
|
|
25
|
+
// The page's language, when the browser knows it; numbers and sorting follow it.
|
|
26
|
+
const lang = (() => {
|
|
27
|
+
try {
|
|
28
|
+
return Intl.getCanonicalLocales(document.documentElement.lang || [])[0];
|
|
29
|
+
} catch {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
})();
|
|
33
|
+
const T = { data: /^zh/i.test(lang || "") ? "看数据" : "Show the numbers" };
|
|
34
|
+
const SVG = "http://www.w3.org/2000/svg";
|
|
35
|
+
|
|
36
|
+
// A cell is a number when all of it is one: a sign, a currency or "about", the digits with the page's separators,
|
|
37
|
+
// and a short unit ("€1,234.50", "−3.5 kg", "约 380 克", "12,50 €" in French). "Zebra 1" and "1,50" on an English page are text.
|
|
38
|
+
const NUM = (() => {
|
|
39
|
+
let group = ",", decimal = ".";
|
|
40
|
+
try {
|
|
41
|
+
for (const p of new Intl.NumberFormat(lang).formatToParts(12345.6)) {
|
|
42
|
+
if (p.type === "group") group = p.value;
|
|
43
|
+
if (p.type === "decimal") decimal = p.value;
|
|
44
|
+
}
|
|
45
|
+
} catch {}
|
|
46
|
+
const esc = (c) => c.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
47
|
+
const g = /\s/.test(group) ? "[\\s\\u00a0\\u202f]" : group === "," ? "[,,]" : /['’]/.test(group) ? "['’]" : esc(group);
|
|
48
|
+
return new RegExp("^([-+−]?)(?:\\p{Sc}|[~≈约])?\\s?([-+−]?)(\\d{1,3}(?:" + g + "\\d{3})+|\\d+)(?:" + esc(decimal) + "(\\d+))?\\s?(?:%|‰|\\p{Sc}|[\\p{L}°µ/²³]{1,4})?$", "u");
|
|
49
|
+
})();
|
|
50
|
+
function num(text) {
|
|
51
|
+
const m = String(text).trim().match(NUM);
|
|
52
|
+
if (!m) return NaN;
|
|
53
|
+
const v = parseFloat(m[3].replace(/\D/g, "") + (m[4] ? "." + m[4] : ""));
|
|
54
|
+
return /[-−]/.test(m[1] + m[2]) ? -v : v;
|
|
55
|
+
}
|
|
56
|
+
// A cell's value: its data-value when it has one, else its text read as a number (NaN when it is not one).
|
|
57
|
+
function cellValue(c) {
|
|
58
|
+
const d = c.getAttribute("data-value");
|
|
59
|
+
if (d == null) return num(c.textContent);
|
|
60
|
+
return d.trim() === "" ? NaN : Number(d);
|
|
61
|
+
}
|
|
62
|
+
function extent(values) {
|
|
63
|
+
let lo = Infinity, hi = -Infinity;
|
|
64
|
+
for (const v of values)
|
|
65
|
+
if (!isNaN(v)) {
|
|
66
|
+
if (v < lo) lo = v;
|
|
67
|
+
if (v > hi) hi = v;
|
|
68
|
+
}
|
|
69
|
+
return [lo, hi];
|
|
70
|
+
}
|
|
71
|
+
// Each part runs once on an element, also when this file is loaded twice; the mark is shared through the symbol.
|
|
72
|
+
const DONE = Symbol.for("pots-ui.done");
|
|
73
|
+
function once(e, part) {
|
|
74
|
+
const done = e[DONE] || (e[DONE] = new Set());
|
|
75
|
+
if (done.has(part)) return false;
|
|
76
|
+
done.add(part);
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
function el(tag, cls, text) {
|
|
80
|
+
const e = document.createElement(tag);
|
|
81
|
+
if (cls) e.className = cls;
|
|
82
|
+
if (text != null) e.textContent = text;
|
|
83
|
+
return e;
|
|
84
|
+
}
|
|
85
|
+
function svg(tag, attrs, text) {
|
|
86
|
+
const e = document.createElementNS(SVG, tag);
|
|
87
|
+
for (const k in attrs) e.setAttribute(k, attrs[k]);
|
|
88
|
+
if (text != null) e.textContent = text;
|
|
89
|
+
return e;
|
|
90
|
+
}
|
|
91
|
+
function pct(v, max) {
|
|
92
|
+
return max > 0 ? Math.max(0, Math.min(100, (v / max) * 100)).toFixed(2) + "%" : "0%";
|
|
93
|
+
}
|
|
94
|
+
function fmt(v) {
|
|
95
|
+
return new Intl.NumberFormat(lang, { maximumFractionDigits: 2 }).format(v);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/* ---- Tabs ---- */
|
|
99
|
+
let tabSeq = 0;
|
|
100
|
+
const FOCUSABLE = "a[href], button, input, select, textarea, summary, iframe, [tabindex], [contenteditable]";
|
|
101
|
+
function tabs(box) {
|
|
102
|
+
const panels = [...box.children].filter((c) => c.hasAttribute("data-tab"));
|
|
103
|
+
if (panels.length < 2 || !once(box, "tabs")) return;
|
|
104
|
+
const id = "pots-tab-" + ++tabSeq;
|
|
105
|
+
const bar = el("div", "pots-tabbar");
|
|
106
|
+
bar.setAttribute("role", "tablist");
|
|
107
|
+
const buttons = panels.map((p, i) => {
|
|
108
|
+
const b = el("button", null, p.getAttribute("data-tab"));
|
|
109
|
+
b.type = "button";
|
|
110
|
+
b.setAttribute("role", "tab");
|
|
111
|
+
if (!p.id) p.id = id + "-" + i;
|
|
112
|
+
b.id = p.id + "-tab";
|
|
113
|
+
b.setAttribute("aria-controls", p.id);
|
|
114
|
+
p.setAttribute("role", "tabpanel");
|
|
115
|
+
p.setAttribute("aria-labelledby", b.id);
|
|
116
|
+
b.addEventListener("click", () => select(i, true));
|
|
117
|
+
bar.append(b);
|
|
118
|
+
return b;
|
|
119
|
+
});
|
|
120
|
+
// A panel with nothing to focus in it is a stop for the keyboard itself, while it is a tab.
|
|
121
|
+
const plain = panels.filter((p) => !p.querySelector(FOCUSABLE));
|
|
122
|
+
const phone = box.getAttribute("data-pots-tabs") === "phone" && window.matchMedia ? matchMedia("(max-width: 719.98px)") : null;
|
|
123
|
+
function stops() {
|
|
124
|
+
plain.forEach((p) => (!phone || phone.matches ? (p.tabIndex = 0) : p.removeAttribute("tabindex")));
|
|
125
|
+
}
|
|
126
|
+
stops();
|
|
127
|
+
if (phone) phone.addEventListener("change", stops);
|
|
128
|
+
bar.addEventListener("keydown", (e) => {
|
|
129
|
+
const i = buttons.indexOf(document.activeElement);
|
|
130
|
+
if (i < 0) return;
|
|
131
|
+
// The arrow keys go the way the tabs read: right to left, the next tab is on the left.
|
|
132
|
+
const next = getComputedStyle(bar).direction === "rtl" ? "ArrowLeft" : "ArrowRight";
|
|
133
|
+
const prev = next === "ArrowRight" ? "ArrowLeft" : "ArrowRight";
|
|
134
|
+
const j = e.key === next ? i + 1 : e.key === prev ? i - 1 : e.key === "Home" ? 0 : e.key === "End" ? buttons.length - 1 : -2;
|
|
135
|
+
if (j === -2) return;
|
|
136
|
+
e.preventDefault();
|
|
137
|
+
const k = (j + buttons.length) % buttons.length;
|
|
138
|
+
select(k, true);
|
|
139
|
+
buttons[k].focus();
|
|
140
|
+
});
|
|
141
|
+
function select(i, byUser) {
|
|
142
|
+
panels.forEach((p, k) => {
|
|
143
|
+
p.classList.toggle("pots-off", k !== i);
|
|
144
|
+
buttons[k].setAttribute("aria-selected", String(k === i));
|
|
145
|
+
buttons[k].tabIndex = k === i ? 0 : -1;
|
|
146
|
+
});
|
|
147
|
+
if (byUser) buttons[i].scrollIntoView({ block: "nearest", inline: "nearest" });
|
|
148
|
+
box.dispatchEvent(new CustomEvent("pots-tab", { detail: { index: i, panel: panels[i] } }));
|
|
149
|
+
}
|
|
150
|
+
box.insertBefore(bar, panels[0]);
|
|
151
|
+
const start = panels.findIndex((p) => p.hasAttribute("data-current"));
|
|
152
|
+
select(start < 0 ? 0 : start, false);
|
|
153
|
+
box.potsSelect = (i) => select(i, false);
|
|
154
|
+
}
|
|
155
|
+
// A form that is sent with a field to fill in a closed tab: the browser can not show a hidden field, so it would
|
|
156
|
+
// stop without a word. The first such field of a check opens its tab, and the browser then shows it.
|
|
157
|
+
let checking = false;
|
|
158
|
+
function opener(e) {
|
|
159
|
+
if (checking || !(e.target instanceof Element)) return;
|
|
160
|
+
checking = true;
|
|
161
|
+
setTimeout(() => (checking = false));
|
|
162
|
+
for (let p = e.target.closest(".pots-off"); p; p = p.parentElement && p.parentElement.closest(".pots-off")) {
|
|
163
|
+
const box = p.parentElement;
|
|
164
|
+
if (box && box.potsSelect) box.potsSelect([...box.children].filter((c) => c.hasAttribute("data-tab")).indexOf(p));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/* ---- Charts ---- */
|
|
169
|
+
function chart(table) {
|
|
170
|
+
if (!once(table, "chart")) return;
|
|
171
|
+
const type = table.getAttribute("data-pots-chart") || "bar";
|
|
172
|
+
const head = table.tHead && table.tHead.rows[0] ? [...table.tHead.rows[0].cells].map((c) => c.textContent.trim()) : [];
|
|
173
|
+
const body = table.tBodies[0] || table;
|
|
174
|
+
const rows = [...body.rows].filter((r) => r.cells.length > 1 && r.parentElement !== table.tHead);
|
|
175
|
+
const data = rows.map((r) => ({
|
|
176
|
+
label: r.cells[0].textContent.trim(),
|
|
177
|
+
cls: r.className,
|
|
178
|
+
cells: [...r.cells].slice(1).map((c) => ({ text: c.textContent.trim(), v: cellValue(c) })),
|
|
179
|
+
}));
|
|
180
|
+
const fig = el("figure", "pots-chart pots-" + type + (table.classList.contains("pots-wide") ? " pots-wide" : ""));
|
|
181
|
+
if (table.caption) fig.append(el("figcaption", null, table.caption.textContent.trim()));
|
|
182
|
+
table.before(fig);
|
|
183
|
+
// The table goes in first, folded, so a long one is not laid out while the chart measures its width.
|
|
184
|
+
const more = el("details", "pots-data");
|
|
185
|
+
more.append(el("summary", null, table.getAttribute("data-pots-show") || T.data), table);
|
|
186
|
+
fig.append(more);
|
|
187
|
+
try {
|
|
188
|
+
if (type === "stack") stack(fig, data);
|
|
189
|
+
else if (type === "line") line(fig, data, head, table);
|
|
190
|
+
else bars(fig, data, table);
|
|
191
|
+
} catch (e) {
|
|
192
|
+
// The table stays as it was.
|
|
193
|
+
fig.replaceWith(table);
|
|
194
|
+
table[DONE].delete("chart");
|
|
195
|
+
throw e;
|
|
196
|
+
}
|
|
197
|
+
fig.append(more);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function bars(fig, data, table) {
|
|
201
|
+
const marks = (table.getAttribute("data-marks") || "")
|
|
202
|
+
.split(/[;;]/)
|
|
203
|
+
.map((s) => s.trim())
|
|
204
|
+
.filter(Boolean)
|
|
205
|
+
.map((s) => {
|
|
206
|
+
// The number first, then its name: "380 You", "€10,000 Budget", "10 000 Budget" in French.
|
|
207
|
+
const m = s.match(/^((?:[~≈约]\s?)?[^\p{L}]*\d)\s*(.*)$/u);
|
|
208
|
+
return m ? { v: num(m[1]), label: m[2] || s } : { v: NaN, label: s };
|
|
209
|
+
});
|
|
210
|
+
const max = parseFloat(table.getAttribute("data-max")) || Math.max(0, extent(data.map((d) => (d.cells[0] ? d.cells[0].v : NaN)))[1], extent(marks.map((m) => m.v))[1]);
|
|
211
|
+
const ul = el("ul", "pots-hbars");
|
|
212
|
+
if (marks.length) {
|
|
213
|
+
const li = el("li", "pots-marks");
|
|
214
|
+
li.setAttribute("aria-hidden", "true");
|
|
215
|
+
const tr = el("span", "pots-track");
|
|
216
|
+
marks.forEach((m) => {
|
|
217
|
+
// Past the middle, the name gives way towards the start.
|
|
218
|
+
const s = el("span", parseFloat(pct(m.v, max)) > 50 ? "pots-end" : null);
|
|
219
|
+
s.append(el("span", null, m.label));
|
|
220
|
+
s.style.setProperty("--v", pct(m.v, max));
|
|
221
|
+
tr.append(s);
|
|
222
|
+
});
|
|
223
|
+
li.append(tr);
|
|
224
|
+
ul.append(li);
|
|
225
|
+
}
|
|
226
|
+
data.forEach((d) => {
|
|
227
|
+
const li = el("li", d.cls);
|
|
228
|
+
const c = d.cells[0] || { text: "", v: NaN };
|
|
229
|
+
li.append(el("span", "pots-l", d.label), el("span", "pots-v", c.text));
|
|
230
|
+
const tr = el("span", "pots-track");
|
|
231
|
+
if (!isNaN(c.v)) {
|
|
232
|
+
const f = el("span", "pots-fill");
|
|
233
|
+
f.style.setProperty("--v", pct(c.v, max));
|
|
234
|
+
tr.append(f);
|
|
235
|
+
}
|
|
236
|
+
marks.forEach((m) => {
|
|
237
|
+
const k = el("span", "pots-mark");
|
|
238
|
+
k.style.setProperty("--v", pct(m.v, max));
|
|
239
|
+
tr.append(k);
|
|
240
|
+
});
|
|
241
|
+
li.append(tr);
|
|
242
|
+
ul.append(li);
|
|
243
|
+
});
|
|
244
|
+
fig.append(ul);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function stack(fig, data) {
|
|
248
|
+
const items = data.filter((d) => d.cells[0] && d.cells[0].v > 0);
|
|
249
|
+
const total = items.reduce((s, d) => s + d.cells[0].v, 0);
|
|
250
|
+
const barEl = el("div", "pots-stackbar");
|
|
251
|
+
barEl.setAttribute("role", "img");
|
|
252
|
+
barEl.setAttribute("aria-label", items.map((d) => d.label + " " + d.cells[0].text).join(", "));
|
|
253
|
+
const legend = el("ul", "pots-legend");
|
|
254
|
+
items.forEach((d, i) => {
|
|
255
|
+
const s = el("span", "pots-s" + ((i % 7) + 1));
|
|
256
|
+
s.style.setProperty("--v", pct(d.cells[0].v, total));
|
|
257
|
+
barEl.append(s);
|
|
258
|
+
const li = el("li");
|
|
259
|
+
li.append(el("i", "pots-s" + ((i % 7) + 1)), el("span", null, d.label), el("span", "pots-v", d.cells[0].text + " · " + Math.round((d.cells[0].v / total) * 100) + "%"));
|
|
260
|
+
legend.append(li);
|
|
261
|
+
});
|
|
262
|
+
fig.append(barEl, legend);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function line(fig, data, head, table) {
|
|
266
|
+
const box = el("div", "pots-linebox");
|
|
267
|
+
fig.append(box);
|
|
268
|
+
const series = (data[0] ? data[0].cells : []).map((_, s) => ({ name: head[s + 1] || "", pts: data.map((d) => (d.cells[s] ? d.cells[s].v : NaN)) }));
|
|
269
|
+
let [min, hi] = extent(series.map((s) => extent(s.pts)).flat());
|
|
270
|
+
if (!isFinite(min)) return;
|
|
271
|
+
const fixed = parseFloat(table.getAttribute("data-min"));
|
|
272
|
+
// From zero, unless the values sit far from it; then from just below the lowest, so the change shows.
|
|
273
|
+
let lo = !isNaN(fixed) ? fixed : min > 0 && hi - min < min ? min - (hi - min) * 0.25 : Math.min(0, min);
|
|
274
|
+
// All values equal, or a data-min above them: a scale from zero.
|
|
275
|
+
if (!(hi > lo)) {
|
|
276
|
+
lo = Math.min(0, min);
|
|
277
|
+
hi = Math.max(0, hi);
|
|
278
|
+
if (hi === lo) hi = lo + 1;
|
|
279
|
+
}
|
|
280
|
+
const step = niceStep((hi - lo) / 4);
|
|
281
|
+
lo = Math.floor(lo / step) * step;
|
|
282
|
+
hi = Math.ceil(hi / step) * step;
|
|
283
|
+
// A few ticks, each at lo plus a whole number of steps, so a step too small to add to a big value still ends.
|
|
284
|
+
const ticks = Math.min(12, Math.round((hi - lo) / step));
|
|
285
|
+
// Many points: in each run of k, the lowest and the highest, so a spike stays and the path stays small.
|
|
286
|
+
const run = Math.ceil(data.length / 500);
|
|
287
|
+
const shown = series.map((se) => {
|
|
288
|
+
if (run < 2) return se.pts.map((v, i) => i);
|
|
289
|
+
const out = [];
|
|
290
|
+
for (let a = 0; a < se.pts.length; a += run) {
|
|
291
|
+
let lo2 = -1, hi2 = -1;
|
|
292
|
+
for (let i = a; i < Math.min(a + run, se.pts.length); i++) {
|
|
293
|
+
const v = se.pts[i];
|
|
294
|
+
if (isNaN(v)) continue;
|
|
295
|
+
if (lo2 < 0 || v < se.pts[lo2]) lo2 = i;
|
|
296
|
+
if (hi2 < 0 || v > se.pts[hi2]) hi2 = i;
|
|
297
|
+
}
|
|
298
|
+
if (lo2 >= 0) out.push(...(lo2 === hi2 ? [lo2] : lo2 < hi2 ? [lo2, hi2] : [hi2, lo2]));
|
|
299
|
+
}
|
|
300
|
+
return out;
|
|
301
|
+
});
|
|
302
|
+
let last = 0;
|
|
303
|
+
function render() {
|
|
304
|
+
const W = Math.round(box.clientWidth);
|
|
305
|
+
if (!W || W === last) return;
|
|
306
|
+
last = W;
|
|
307
|
+
const H = Math.max(180, Math.min(300, Math.round(W * 0.45)));
|
|
308
|
+
const L = 8 + 7 * Math.max(fmt(lo).length, fmt(hi).length), R = 12, Tp = 10, B = 28;
|
|
309
|
+
const x = (i) => L + (data.length < 2 ? (W - L - R) / 2 : (i * (W - L - R)) / (data.length - 1));
|
|
310
|
+
const y = (v) => Tp + (H - Tp - B) * (1 - (v - lo) / (hi - lo || 1));
|
|
311
|
+
const s = svg("svg", { viewBox: "0 0 " + W + " " + H, width: W, height: H, role: "img" });
|
|
312
|
+
s.setAttribute("aria-label", (fig.querySelector("figcaption") || {}).textContent || "chart");
|
|
313
|
+
for (let t = 0; t <= ticks; t++) {
|
|
314
|
+
const v = lo + t * step;
|
|
315
|
+
s.append(svg("line", { class: v === lo ? "pots-axis pots-base" : "pots-axis", x1: L, x2: W - R, y1: y(v), y2: y(v) }));
|
|
316
|
+
s.append(svg("text", { x: L - 8, y: y(v) + 4, "text-anchor": "end" }, fmt(v)));
|
|
317
|
+
}
|
|
318
|
+
const every = Math.ceil(data.length / Math.max(2, Math.floor((W - L - R) / 64)));
|
|
319
|
+
data.forEach((d, i) => {
|
|
320
|
+
const end = i === data.length - 1 && data.length > 1;
|
|
321
|
+
// Every few labels, and the last one; a label too close to the last one gives way to it.
|
|
322
|
+
if (end || (i % every === 0 && data.length - 1 - i >= every * 0.6))
|
|
323
|
+
s.append(svg("text", { x: x(i), y: H - 6, "text-anchor": end ? "end" : i === 0 ? "start" : "middle" }, d.label));
|
|
324
|
+
});
|
|
325
|
+
series.forEach((se, k) => {
|
|
326
|
+
let p = "";
|
|
327
|
+
shown[k].forEach((i) => {
|
|
328
|
+
const v = se.pts[i];
|
|
329
|
+
if (isNaN(v)) return;
|
|
330
|
+
p += (p ? "L" : "M") + x(i).toFixed(1) + " " + y(v).toFixed(1);
|
|
331
|
+
});
|
|
332
|
+
s.append(svg("path", { d: p, class: "pots-s" + ((k % 7) + 1) }));
|
|
333
|
+
if (data.length <= 24)
|
|
334
|
+
se.pts.forEach((v, i) => {
|
|
335
|
+
if (!isNaN(v)) s.append(svg("circle", { cx: x(i), cy: y(v), r: 3.5, class: "pots-s" + ((k % 7) + 1) }));
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
box.replaceChildren(s);
|
|
339
|
+
if (series.length > 1) {
|
|
340
|
+
const legend = el("ul", "pots-legend");
|
|
341
|
+
series.forEach((se, k) => {
|
|
342
|
+
const li = el("li");
|
|
343
|
+
li.append(el("i", "pots-s" + ((k % 7) + 1)), el("span", null, se.name));
|
|
344
|
+
legend.append(li);
|
|
345
|
+
});
|
|
346
|
+
box.append(legend);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
render();
|
|
350
|
+
if (window.ResizeObserver) new ResizeObserver(() => requestAnimationFrame(render)).observe(box);
|
|
351
|
+
}
|
|
352
|
+
function niceStep(raw) {
|
|
353
|
+
const p = Math.pow(10, Math.floor(Math.log10(raw)));
|
|
354
|
+
const f = raw / p;
|
|
355
|
+
return (f <= 1 ? 1 : f <= 2 ? 2 : f <= 5 ? 5 : 10) * p;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/* ---- Sorting and filtering ---- */
|
|
359
|
+
function sortable(table) {
|
|
360
|
+
if (!table.tHead || !table.tHead.rows[0] || !table.tBodies[0] || !once(table, "sort")) return;
|
|
361
|
+
const ths = [...table.tHead.rows[0].cells];
|
|
362
|
+
// What a cell sorts by: its data-value, else its text.
|
|
363
|
+
const key = (r, i) => {
|
|
364
|
+
const c = r.cells[i];
|
|
365
|
+
return c ? (c.getAttribute("data-value") ?? c.textContent).trim() : "";
|
|
366
|
+
};
|
|
367
|
+
ths.forEach((th, i) => {
|
|
368
|
+
const b = el("button", "pots-sort");
|
|
369
|
+
b.type = "button";
|
|
370
|
+
while (th.firstChild) b.append(th.firstChild);
|
|
371
|
+
th.append(b);
|
|
372
|
+
b.addEventListener("click", () => {
|
|
373
|
+
const dir = th.getAttribute("aria-sort") === "ascending" ? "descending" : "ascending";
|
|
374
|
+
ths.forEach((t) => t.removeAttribute("aria-sort"));
|
|
375
|
+
th.setAttribute("aria-sort", dir);
|
|
376
|
+
const body = table.tBodies[0];
|
|
377
|
+
const rows = [...body.rows];
|
|
378
|
+
// Numbers when every cell that is not empty is one; else text, in the page's language.
|
|
379
|
+
const values = new Map(rows.map((r) => [r, r.cells[i] ? cellValue(r.cells[i]) : NaN]));
|
|
380
|
+
const numeric = rows.every((r) => !key(r, i) || !isNaN(values.get(r)));
|
|
381
|
+
rows.sort((a, b2) => {
|
|
382
|
+
const x = key(a, i), y = key(b2, i);
|
|
383
|
+
if (!x || !y) return !x && !y ? 0 : !x ? 1 : -1;
|
|
384
|
+
const c = numeric ? values.get(a) - values.get(b2) : x.localeCompare(y, lang, { numeric: true });
|
|
385
|
+
return dir === "ascending" ? c : -c;
|
|
386
|
+
});
|
|
387
|
+
body.append(...rows);
|
|
388
|
+
});
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function filter(input) {
|
|
393
|
+
let target = null;
|
|
394
|
+
try {
|
|
395
|
+
target = document.querySelector(input.getAttribute("data-pots-filter"));
|
|
396
|
+
} catch {}
|
|
397
|
+
if (!target || !once(input, "filter")) return;
|
|
398
|
+
const items = () =>
|
|
399
|
+
target.matches("table")
|
|
400
|
+
? [...(target.tBodies[0] || target).rows].filter((r) => r.parentElement !== target.tHead)
|
|
401
|
+
: target.querySelector("[data-pots-item]")
|
|
402
|
+
? [...target.querySelectorAll("[data-pots-item]")]
|
|
403
|
+
: [...target.children];
|
|
404
|
+
input.addEventListener("input", () => {
|
|
405
|
+
const q = input.value.trim().toLowerCase();
|
|
406
|
+
items().forEach((it) => it.classList.toggle("pots-hide", !!q && !it.textContent.toLowerCase().includes(q)));
|
|
407
|
+
target.querySelectorAll("[data-pots-group]").forEach((g) => {
|
|
408
|
+
const its = [...g.querySelectorAll("[data-pots-item]")];
|
|
409
|
+
g.classList.toggle("pots-hide", its.length > 0 && its.every((x) => x.classList.contains("pots-hide")));
|
|
410
|
+
});
|
|
411
|
+
target.dispatchEvent(new CustomEvent("pots-filter", { detail: { query: q } }));
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/* ---- Contents: mark the section on screen ---- */
|
|
416
|
+
const still = window.matchMedia ? matchMedia("(prefers-reduced-motion: reduce)") : null;
|
|
417
|
+
function toc(nav) {
|
|
418
|
+
if (!once(nav, "toc")) return;
|
|
419
|
+
const links = [...nav.querySelectorAll('a[href^="#"]')];
|
|
420
|
+
const targets = links.map((a) => {
|
|
421
|
+
try {
|
|
422
|
+
return document.getElementById(decodeURIComponent(a.hash.slice(1)));
|
|
423
|
+
} catch {
|
|
424
|
+
return null;
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
let queued = false, current = -2;
|
|
428
|
+
function update() {
|
|
429
|
+
queued = false;
|
|
430
|
+
let cur = -1;
|
|
431
|
+
targets.forEach((t, i) => {
|
|
432
|
+
if (t && t.getBoundingClientRect().top < 140) cur = i;
|
|
433
|
+
});
|
|
434
|
+
if (cur === current) return;
|
|
435
|
+
current = cur;
|
|
436
|
+
links.forEach((a, i) => (i === cur ? a.setAttribute("aria-current", "true") : a.removeAttribute("aria-current")));
|
|
437
|
+
if (cur >= 0 && nav.scrollWidth > nav.clientWidth + 2) {
|
|
438
|
+
const a = links[cur];
|
|
439
|
+
nav.scrollTo({ left: a.offsetLeft - nav.clientWidth / 2 + a.offsetWidth / 2, behavior: still && still.matches ? "auto" : "smooth" });
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
addEventListener("scroll", () => {
|
|
443
|
+
if (!queued) {
|
|
444
|
+
queued = true;
|
|
445
|
+
requestAnimationFrame(update);
|
|
446
|
+
}
|
|
447
|
+
}, { passive: true });
|
|
448
|
+
update();
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/* ---- Tables: a box that scrolls a wide table, so the table itself can take the full width ---- */
|
|
452
|
+
function scroller(table) {
|
|
453
|
+
if (!table.parentElement || table.parentElement.classList.contains("pots-scroll")) return;
|
|
454
|
+
const box = el("div", "pots-scroll" + (table.classList.contains("pots-wide") ? " pots-wide" : ""));
|
|
455
|
+
table.before(box);
|
|
456
|
+
box.append(table);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/* ---- Light or dark: the reader's choice, from a switch at the page's top corner ---- */
|
|
460
|
+
// It is kept in the browser for every page of this host, as it is not private. A choice that is the system's own
|
|
461
|
+
// is forgotten, so the page follows the system again. pots.js has the same switch; the first of the two makes it.
|
|
462
|
+
function themeSwitch() {
|
|
463
|
+
if (window.PotsTheme) return;
|
|
464
|
+
const root = document.documentElement, KEY = "pots-theme";
|
|
465
|
+
const system = window.matchMedia ? matchMedia("(prefers-color-scheme: dark)") : { matches: false };
|
|
466
|
+
const read = () => {
|
|
467
|
+
try {
|
|
468
|
+
const v = localStorage.getItem(KEY);
|
|
469
|
+
return v === "light" || v === "dark" ? v : null;
|
|
470
|
+
} catch {
|
|
471
|
+
return null;
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
const shown = () => read() || (system.matches ? "dark" : "light");
|
|
475
|
+
let button = null;
|
|
476
|
+
function apply() {
|
|
477
|
+
const v = read();
|
|
478
|
+
if (v) root.setAttribute("data-pots-theme", v);
|
|
479
|
+
else root.removeAttribute("data-pots-theme");
|
|
480
|
+
if (!button) return;
|
|
481
|
+
const dark = shown() === "dark";
|
|
482
|
+
button.setAttribute("data-icon", dark ? "sun" : "moon");
|
|
483
|
+
button.setAttribute("aria-pressed", String(dark));
|
|
484
|
+
}
|
|
485
|
+
function set(v) {
|
|
486
|
+
try {
|
|
487
|
+
if (v === (system.matches ? "dark" : "light")) localStorage.removeItem(KEY);
|
|
488
|
+
else localStorage.setItem(KEY, v);
|
|
489
|
+
} catch {}
|
|
490
|
+
apply();
|
|
491
|
+
}
|
|
492
|
+
apply();
|
|
493
|
+
window.PotsTheme = {
|
|
494
|
+
get: shown,
|
|
495
|
+
set,
|
|
496
|
+
// The button, once the body is there, and only on a page that uses pots.css.
|
|
497
|
+
button() {
|
|
498
|
+
if (button || !document.body || !getComputedStyle(root).getPropertyValue("--pots-bg")) return;
|
|
499
|
+
const zh = /^zh/i.test(lang || "");
|
|
500
|
+
button = el("button", "pots-theme");
|
|
501
|
+
button.type = "button";
|
|
502
|
+
button.setAttribute("aria-label", zh ? "深色模式" : "Dark mode");
|
|
503
|
+
button.title = zh ? "切换浅色或深色" : "Switch light or dark";
|
|
504
|
+
button.addEventListener("click", () => set(shown() === "dark" ? "light" : "dark"));
|
|
505
|
+
document.body.prepend(button);
|
|
506
|
+
apply();
|
|
507
|
+
},
|
|
508
|
+
};
|
|
509
|
+
if (system.addEventListener) system.addEventListener("change", apply);
|
|
510
|
+
addEventListener("storage", (e) => e.key === KEY && apply());
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// Each part on its own: one that fails keeps its plain HTML, and the others still start.
|
|
514
|
+
function each(selector, part) {
|
|
515
|
+
document.querySelectorAll(selector).forEach((e) => {
|
|
516
|
+
try {
|
|
517
|
+
part(e);
|
|
518
|
+
} catch (err) {
|
|
519
|
+
console.error("pots-ui:", err);
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
function start() {
|
|
524
|
+
each("table[data-pots-chart]", chart);
|
|
525
|
+
each("table", scroller);
|
|
526
|
+
each("table[data-pots-sort]", sortable);
|
|
527
|
+
each("[data-pots-tabs]", tabs);
|
|
528
|
+
each("input[data-pots-filter]", filter);
|
|
529
|
+
each("nav.pots-toc", toc);
|
|
530
|
+
try {
|
|
531
|
+
window.PotsTheme.button();
|
|
532
|
+
} catch (err) {
|
|
533
|
+
console.error("pots-ui:", err);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
document.addEventListener("invalid", opener, true);
|
|
537
|
+
// At once, before the page is shown when the script is not deferred, so the reader's choice does not flash.
|
|
538
|
+
try {
|
|
539
|
+
themeSwitch();
|
|
540
|
+
} catch (err) {
|
|
541
|
+
console.error("pots-ui:", err);
|
|
542
|
+
}
|
|
543
|
+
window.PotsUI = { num, tabs, chart, sortable, filter };
|
|
544
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", start);
|
|
545
|
+
else start();
|
|
546
|
+
})();
|