fjkit 0.1.0.dev0__py3-none-any.whl
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.
- fjkit/__init__.py +49 -0
- fjkit/auth/__init__.py +54 -0
- fjkit/auth/csrf.py +88 -0
- fjkit/auth/errors.py +31 -0
- fjkit/auth/plugin.py +496 -0
- fjkit/auth/sources.py +24 -0
- fjkit/auth/stores.py +141 -0
- fjkit/auth/types.py +141 -0
- fjkit/cli/__init__.py +67 -0
- fjkit/cli/build_css.py +135 -0
- fjkit/cli/check.py +193 -0
- fjkit/cli/eject.py +562 -0
- fjkit/cli/ejected.py +202 -0
- fjkit/cli/vocabulary.py +85 -0
- fjkit/config.py +130 -0
- fjkit/errors.py +156 -0
- fjkit/flash.py +189 -0
- fjkit/forms.py +145 -0
- fjkit/htmx.py +76 -0
- fjkit/icons.py +60 -0
- fjkit/messages.py +227 -0
- fjkit/mounting.py +72 -0
- fjkit/plugins.py +223 -0
- fjkit/py.typed +0 -0
- fjkit/rendering.py +554 -0
- fjkit/signing.py +63 -0
- fjkit/static/brand/torii-bolt.png +0 -0
- fjkit/static/dist/fjkit-luma.css +2 -0
- fjkit/static/dist/fjkit-lyra.css +2 -0
- fjkit/static/dist/fjkit-maia.css +2 -0
- fjkit/static/dist/fjkit-mira.css +2 -0
- fjkit/static/dist/fjkit-nova.css +2 -0
- fjkit/static/dist/fjkit-rhea.css +2 -0
- fjkit/static/dist/fjkit-sera.css +2 -0
- fjkit/static/dist/fjkit-vega.css +2 -0
- fjkit/static/icons/lucide.json +1 -0
- fjkit/static/js/errors.js +117 -0
- fjkit/static/js/multiselect.js +126 -0
- fjkit/static/js/reveal.js +51 -0
- fjkit/static/js/select.js +113 -0
- fjkit/static/src/fjkit.css +385 -0
- fjkit/static/vendor/basecoat/base/base.css +136 -0
- fjkit/static/vendor/basecoat/basecoat-base.css +2 -0
- fjkit/static/vendor/basecoat/basecoat-components.css +39 -0
- fjkit/static/vendor/basecoat/basecoat-luma.css +2 -0
- fjkit/static/vendor/basecoat/basecoat-lyra.css +2 -0
- fjkit/static/vendor/basecoat/basecoat-maia.css +2 -0
- fjkit/static/vendor/basecoat/basecoat-mira.css +2 -0
- fjkit/static/vendor/basecoat/basecoat-nova.css +2 -0
- fjkit/static/vendor/basecoat/basecoat-rhea.css +2 -0
- fjkit/static/vendor/basecoat/basecoat-sera.css +2 -0
- fjkit/static/vendor/basecoat/basecoat-vega.css +2 -0
- fjkit/static/vendor/basecoat/basecoat.all.css +1 -0
- fjkit/static/vendor/basecoat/basecoat.css +1 -0
- fjkit/static/vendor/basecoat/components/accordion.css +27 -0
- fjkit/static/vendor/basecoat/components/alert.css +38 -0
- fjkit/static/vendor/basecoat/components/avatar.css +47 -0
- fjkit/static/vendor/basecoat/components/badge.css +6 -0
- fjkit/static/vendor/basecoat/components/breadcrumb.css +30 -0
- fjkit/static/vendor/basecoat/components/button-group.css +54 -0
- fjkit/static/vendor/basecoat/components/button.css +6 -0
- fjkit/static/vendor/basecoat/components/card.css +26 -0
- fjkit/static/vendor/basecoat/components/chart.css +56 -0
- fjkit/static/vendor/basecoat/components/checkbox.css +12 -0
- fjkit/static/vendor/basecoat/components/collapsible.css +21 -0
- fjkit/static/vendor/basecoat/components/combobox.css +99 -0
- fjkit/static/vendor/basecoat/components/command.css +140 -0
- fjkit/static/vendor/basecoat/components/dialog.css +101 -0
- fjkit/static/vendor/basecoat/components/drawer.css +89 -0
- fjkit/static/vendor/basecoat/components/dropdown-menu.css +56 -0
- fjkit/static/vendor/basecoat/components/empty.css +26 -0
- fjkit/static/vendor/basecoat/components/field.css +82 -0
- fjkit/static/vendor/basecoat/components/form.css +9 -0
- fjkit/static/vendor/basecoat/components/input-group.css +74 -0
- fjkit/static/vendor/basecoat/components/input.css +32 -0
- fjkit/static/vendor/basecoat/components/item.css +50 -0
- fjkit/static/vendor/basecoat/components/kbd.css +6 -0
- fjkit/static/vendor/basecoat/components/label.css +13 -0
- fjkit/static/vendor/basecoat/components/native-select.css +12 -0
- fjkit/static/vendor/basecoat/components/popover.css +107 -0
- fjkit/static/vendor/basecoat/components/progress.css +14 -0
- fjkit/static/vendor/basecoat/components/radio.css +11 -0
- fjkit/static/vendor/basecoat/components/range.css +50 -0
- fjkit/static/vendor/basecoat/components/scrollbar.css +46 -0
- fjkit/static/vendor/basecoat/components/select.css +64 -0
- fjkit/static/vendor/basecoat/components/sidebar.css +101 -0
- fjkit/static/vendor/basecoat/components/skeleton.css +6 -0
- fjkit/static/vendor/basecoat/components/switch.css +8 -0
- fjkit/static/vendor/basecoat/components/table.css +34 -0
- fjkit/static/vendor/basecoat/components/tabs.css +31 -0
- fjkit/static/vendor/basecoat/components/textarea.css +7 -0
- fjkit/static/vendor/basecoat/components/toast.css +57 -0
- fjkit/static/vendor/basecoat/components/tooltip.css +82 -0
- fjkit/static/vendor/basecoat/js/all.min.js +1 -0
- fjkit/static/vendor/basecoat/styles/luma.css +1376 -0
- fjkit/static/vendor/basecoat/styles/lyra.css +1379 -0
- fjkit/static/vendor/basecoat/styles/maia.css +1364 -0
- fjkit/static/vendor/basecoat/styles/mira.css +1373 -0
- fjkit/static/vendor/basecoat/styles/nova.css +1387 -0
- fjkit/static/vendor/basecoat/styles/rhea.css +1381 -0
- fjkit/static/vendor/basecoat/styles/sera.css +1397 -0
- fjkit/static/vendor/basecoat/styles/vega.css +1386 -0
- fjkit/static/vendor/htmx/htmx.min.js +1 -0
- fjkit/static/vendor/htmx/json-enc.js +38 -0
- fjkit/styles.py +30 -0
- fjkit/templates/errors/page.html +92 -0
- fjkit/templates/ui/attrs.html +18 -0
- fjkit/templates/ui/button.html +64 -0
- fjkit/templates/ui/data.html +362 -0
- fjkit/templates/ui/disclosure.html +89 -0
- fjkit/templates/ui/feedback.html +313 -0
- fjkit/templates/ui/form.html +595 -0
- fjkit/templates/ui/icon.html +13 -0
- fjkit/templates/ui/layout.html +189 -0
- fjkit/templates/ui/nav.html +110 -0
- fjkit/templates/ui/overlay.html +624 -0
- fjkit/templates/ui/shell.html +170 -0
- fjkit/templates/ui/sidebar.html +170 -0
- fjkit/templates/ui/table.html +427 -0
- fjkit/templates/ui/tabs.html +133 -0
- fjkit/templating.py +365 -0
- fjkit/vendored.py +46 -0
- fjkit-0.1.0.dev0.dist-info/METADATA +119 -0
- fjkit-0.1.0.dev0.dist-info/RECORD +127 -0
- fjkit-0.1.0.dev0.dist-info/WHEEL +4 -0
- fjkit-0.1.0.dev0.dist-info/entry_points.txt +2 -0
- fjkit-0.1.0.dev0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Renders a 422 under the fields of the form that sent it.
|
|
3
|
+
|
|
4
|
+
Reads FastAPI's `{"detail": [{"loc": ["body", "title"], "msg": …}]}`, finds
|
|
5
|
+
each control by `name`, and writes the message into the `<p>` named by its
|
|
6
|
+
`aria-describedby`, creating that `<p>` when the field has no hint. A message
|
|
7
|
+
with no control to land on is raised as a `fjkit:toast`. The next request from
|
|
8
|
+
the same form clears every message and restores the hints.
|
|
9
|
+
|
|
10
|
+
Loaded by the shell on every page.
|
|
11
|
+
*/
|
|
12
|
+
(() => {
|
|
13
|
+
const PAINTED = "data-fjkit-error"
|
|
14
|
+
const TOAST = "fjkit:toast"
|
|
15
|
+
|
|
16
|
+
const form = (elt) => (elt instanceof Element ? elt.closest("form") : null)
|
|
17
|
+
|
|
18
|
+
/* `("body", "items", 0, "title")` -> `items.0.title`, the same rule as
|
|
19
|
+
`field_name` in `fjkit.forms`. Anything not under `body` is not a field. */
|
|
20
|
+
const fieldName = (loc) =>
|
|
21
|
+
Array.isArray(loc) && loc.length > 1 && loc[0] === "body" ? loc.slice(1).map(String).join(".") : null
|
|
22
|
+
|
|
23
|
+
/* The element carrying `aria-describedby`: the radiogroup wrapper for a
|
|
24
|
+
radio, the control itself otherwise. */
|
|
25
|
+
const holder = (control) => control.closest('[role="radiogroup"]') || control
|
|
26
|
+
|
|
27
|
+
/* The `<p>` under the control; when there is none, a new one shaped like
|
|
28
|
+
`_message()` writes. */
|
|
29
|
+
const message = (control) => {
|
|
30
|
+
const owner = holder(control)
|
|
31
|
+
let p = document.getElementById(owner.getAttribute("aria-describedby") || "")
|
|
32
|
+
if (p) return p
|
|
33
|
+
const id = `${owner.id || control.id || control.name}-hint`
|
|
34
|
+
p = document.createElement("p")
|
|
35
|
+
p.id = id
|
|
36
|
+
p.className = "text-muted-foreground text-xs"
|
|
37
|
+
p.hidden = true
|
|
38
|
+
owner.after(p)
|
|
39
|
+
owner.setAttribute("aria-describedby", id)
|
|
40
|
+
return p
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const paint = (control, text) => {
|
|
44
|
+
const p = message(control)
|
|
45
|
+
if (!p.hasAttribute(PAINTED)) {
|
|
46
|
+
p.setAttribute(PAINTED, p.hidden ? "" : p.textContent)
|
|
47
|
+
p.classList.replace("text-muted-foreground", "text-destructive")
|
|
48
|
+
}
|
|
49
|
+
p.textContent = text
|
|
50
|
+
p.hidden = false
|
|
51
|
+
holder(control).setAttribute("aria-invalid", "true")
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const clear = (scope) => {
|
|
55
|
+
for (const p of scope.querySelectorAll(`[${PAINTED}]`)) {
|
|
56
|
+
const hint = p.getAttribute(PAINTED)
|
|
57
|
+
p.removeAttribute(PAINTED)
|
|
58
|
+
p.classList.replace("text-destructive", "text-muted-foreground")
|
|
59
|
+
p.textContent = hint
|
|
60
|
+
p.hidden = !hint
|
|
61
|
+
}
|
|
62
|
+
for (const el of scope.querySelectorAll('[aria-invalid="true"]')) el.removeAttribute("aria-invalid")
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const toast = (titles) => {
|
|
66
|
+
if (!titles.length) return
|
|
67
|
+
const messages = titles.map((title) => ({ category: "error", title }))
|
|
68
|
+
document.body.dispatchEvent(new CustomEvent(TOAST, { detail: { messages } }))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/* Handles only a 422 carrying JSON; anything else swaps as usual. `isError`
|
|
72
|
+
stays untouched, because `reset_on_success` reads `detail.successful`,
|
|
73
|
+
which derives from it. */
|
|
74
|
+
document.addEventListener("htmx:beforeSwap", (event) => {
|
|
75
|
+
const { xhr, requestConfig } = event.detail
|
|
76
|
+
if (!xhr || xhr.status !== 422) return
|
|
77
|
+
if (!(xhr.getResponseHeader("content-type") || "").includes("application/json")) return
|
|
78
|
+
|
|
79
|
+
let detail
|
|
80
|
+
try {
|
|
81
|
+
detail = JSON.parse(xhr.responseText).detail
|
|
82
|
+
} catch {
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
if (!Array.isArray(detail)) return
|
|
86
|
+
|
|
87
|
+
event.detail.shouldSwap = false
|
|
88
|
+
|
|
89
|
+
const scope = form(requestConfig && requestConfig.elt) || document
|
|
90
|
+
clear(scope)
|
|
91
|
+
|
|
92
|
+
const loose = []
|
|
93
|
+
const seen = new Set()
|
|
94
|
+
for (const entry of detail) {
|
|
95
|
+
const name = fieldName(entry.loc)
|
|
96
|
+
const control = name && scope.querySelector(`[name="${CSS.escape(name)}"]`)
|
|
97
|
+
if (!control) {
|
|
98
|
+
loose.push(name ? `${name}: ${entry.msg}` : String(entry.msg))
|
|
99
|
+
continue
|
|
100
|
+
}
|
|
101
|
+
/* First message per field only. */
|
|
102
|
+
if (seen.has(name)) continue
|
|
103
|
+
seen.add(name)
|
|
104
|
+
paint(control, String(entry.msg))
|
|
105
|
+
}
|
|
106
|
+
toast(loose)
|
|
107
|
+
|
|
108
|
+
const first = scope.querySelector('[aria-invalid="true"]')
|
|
109
|
+
if (first && typeof first.focus === "function") first.focus({ preventScroll: false })
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
/* A new request from the form clears the previous rejection. */
|
|
113
|
+
document.addEventListener("htmx:beforeRequest", (event) => {
|
|
114
|
+
const scope = form(event.detail && event.detail.elt)
|
|
115
|
+
if (scope) clear(scope)
|
|
116
|
+
})
|
|
117
|
+
})()
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Re-emits a multi-select as repeated form fields.
|
|
3
|
+
|
|
4
|
+
Basecoat's `select` and `combobox` both support multiple selection — their JS
|
|
5
|
+
keys off `aria-multiselectable="true"` on the listbox — and both serialise the
|
|
6
|
+
answer the same way: one hidden input carrying `JSON.stringify(values)`. The
|
|
7
|
+
server therefore receives a string. A route declaring
|
|
8
|
+
`labels: list[str] = Form([])` is handed `'["bug","ui"]'` and rejects it, and
|
|
9
|
+
the 422 names a field the page looks like it filled in correctly.
|
|
10
|
+
|
|
11
|
+
So this file re-emits the choice in the shape HTML has used for a repeated
|
|
12
|
+
field since forms existed:
|
|
13
|
+
|
|
14
|
+
labels=bug&labels=ui
|
|
15
|
+
|
|
16
|
+
Nothing downstream has to know a multi-select was involved. One declaration
|
|
17
|
+
reads a checkbox group, a native `<select multiple>` and this. `encoding="json"`
|
|
18
|
+
keeps working too, because the vendored json-enc collects repeated keys into an
|
|
19
|
+
array on its own.
|
|
20
|
+
|
|
21
|
+
Two paths, because `form()` has two: htmx submits a form with `target=`, the
|
|
22
|
+
browser submits a form without one. They are mutually exclusive, and the guard
|
|
23
|
+
below keeps them that way — a form handled by both posts every value twice.
|
|
24
|
+
|
|
25
|
+
The marker is `data-fjkit-multi` on the hidden input, written by the macros.
|
|
26
|
+
Not a class and not the JSON's own shape: a value that merely looks like an
|
|
27
|
+
array is not a reason to rewrite somebody's field.
|
|
28
|
+
|
|
29
|
+
Loaded per page by `multiselect_scripts()`, never from the shell (CHARTER §7).
|
|
30
|
+
*/
|
|
31
|
+
(() => {
|
|
32
|
+
const HIDDEN = 'input[type="hidden"][data-fjkit-multi]'
|
|
33
|
+
|
|
34
|
+
/* Forms htmx submits itself. `hx-boost` is listed even though `form()` never
|
|
35
|
+
emits it, because an app can boost a region containing one. */
|
|
36
|
+
const HTMX_FORM = "[hx-post],[hx-get],[hx-put],[hx-patch],[hx-delete],[hx-boost]," +
|
|
37
|
+
"[data-hx-post],[data-hx-get],[data-hx-put],[data-hx-patch]," +
|
|
38
|
+
"[data-hx-delete],[data-hx-boost]"
|
|
39
|
+
|
|
40
|
+
/* An unparseable value posts nothing rather than the raw string. Basecoat
|
|
41
|
+
rewrites this input on every change, so only a hand edit produces a
|
|
42
|
+
malformed one — and a literal `["bug"` arriving at a route as a label is
|
|
43
|
+
worse than an absent field. */
|
|
44
|
+
const values = (input) => {
|
|
45
|
+
let parsed
|
|
46
|
+
try {
|
|
47
|
+
parsed = JSON.parse(input.value || "[]")
|
|
48
|
+
} catch {
|
|
49
|
+
return []
|
|
50
|
+
}
|
|
51
|
+
if (!Array.isArray(parsed)) return []
|
|
52
|
+
return parsed.filter((value) => value !== null && typeof value !== "object").map(String)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/* The htmx path. `event.detail.parameters` is a `FormData` in htmx 2, so the
|
|
56
|
+
rewrite is delete-then-append and htmx encodes the repeats.
|
|
57
|
+
|
|
58
|
+
Keyed off the parameters rather than a DOM subtree: htmx has already decided
|
|
59
|
+
what this request carries — `hx-include`, `hx-vals` and a button outside its
|
|
60
|
+
form all land here — and re-deriving that from `elt` gives a different
|
|
61
|
+
answer than htmx did. */
|
|
62
|
+
document.addEventListener("htmx:configRequest", (event) => {
|
|
63
|
+
const params = event.detail.parameters
|
|
64
|
+
if (!params || typeof params.getAll !== "function") return
|
|
65
|
+
|
|
66
|
+
for (const name of [...new Set(params.keys())]) {
|
|
67
|
+
const raw = params.get(name)
|
|
68
|
+
if (typeof raw !== "string" || raw.charAt(0) !== "[") continue
|
|
69
|
+
const input = document.querySelector(`${HIDDEN}[name="${CSS.escape(name)}"]`)
|
|
70
|
+
/* Matches on name and value: two selects can share a name across a swap,
|
|
71
|
+
and a parameter htmx built from something else is not ours to touch. */
|
|
72
|
+
if (!input || input.value !== raw) continue
|
|
73
|
+
params.delete(name)
|
|
74
|
+
for (const value of values(input)) params.append(name, value)
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
/* The browser-submitted path: the form that needs this script and nothing
|
|
79
|
+
else.
|
|
80
|
+
|
|
81
|
+
Capture phase, so the expansion is in place before any submit handler an app
|
|
82
|
+
added can call `preventDefault`. The original input is disabled rather than
|
|
83
|
+
emptied, because a disabled control is omitted from the submission and still
|
|
84
|
+
holds the value the page has to keep showing.
|
|
85
|
+
|
|
86
|
+
Everything is undone on the next tick. A submit the browser did not complete
|
|
87
|
+
— blocked by constraint validation, cancelled by a handler — otherwise
|
|
88
|
+
leaves the form carrying both representations, and the next submit posts
|
|
89
|
+
each value twice. */
|
|
90
|
+
document.addEventListener(
|
|
91
|
+
"submit",
|
|
92
|
+
(event) => {
|
|
93
|
+
const form = event.target
|
|
94
|
+
if (!(form instanceof HTMLFormElement)) return
|
|
95
|
+
if (form.matches(HTMX_FORM) || form.closest(HTMX_FORM)) return
|
|
96
|
+
|
|
97
|
+
const injected = []
|
|
98
|
+
const disabled = []
|
|
99
|
+
for (const input of form.querySelectorAll(HIDDEN)) {
|
|
100
|
+
/* `anchor` walks forward as fields are inserted. Inserting each one
|
|
101
|
+
after the original input reverses the selection, because every insert
|
|
102
|
+
pushes the previous one further down — and a form field's document
|
|
103
|
+
order is the order it is posted in. */
|
|
104
|
+
let anchor = input
|
|
105
|
+
for (const value of values(input)) {
|
|
106
|
+
const field = document.createElement("input")
|
|
107
|
+
field.type = "hidden"
|
|
108
|
+
field.name = input.name
|
|
109
|
+
field.value = value
|
|
110
|
+
anchor.after(field)
|
|
111
|
+
anchor = field
|
|
112
|
+
injected.push(field)
|
|
113
|
+
}
|
|
114
|
+
input.disabled = true
|
|
115
|
+
disabled.push(input)
|
|
116
|
+
}
|
|
117
|
+
if (!injected.length && !disabled.length) return
|
|
118
|
+
|
|
119
|
+
setTimeout(() => {
|
|
120
|
+
for (const field of injected) field.remove()
|
|
121
|
+
for (const input of disabled) input.disabled = false
|
|
122
|
+
})
|
|
123
|
+
},
|
|
124
|
+
true,
|
|
125
|
+
)
|
|
126
|
+
})()
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Switches a password field between hidden and readable.
|
|
3
|
+
|
|
4
|
+
The control itself is markup: `input_group_field(revealable=true)` writes the
|
|
5
|
+
button, its `aria-pressed` and the `aria-controls` that names the input. This
|
|
6
|
+
file is only the four lines that flip `input.type`. It lives in the kit rather
|
|
7
|
+
than in every app because the two things that make it correct are not things a
|
|
8
|
+
caller can be expected to derive.
|
|
9
|
+
|
|
10
|
+
It is delegated from `document`, not bound to the button. The form holding a
|
|
11
|
+
password field is the form most likely to be replaced: a rejected sign-in swaps
|
|
12
|
+
the panel out, and a fresh button arrives with no listener on it. A page that
|
|
13
|
+
bound one at DOMContentLoaded gets a reveal that works exactly once and then
|
|
14
|
+
fails silently — no console error, and the button still looks like a button.
|
|
15
|
+
|
|
16
|
+
The input is found through `aria-controls`, never by a convention. The macro
|
|
17
|
+
decides the field's id (`f-<name>`, or whatever `id=` overrode it), so the
|
|
18
|
+
attribute a screen reader already needs is also the only honest way to find the
|
|
19
|
+
input. Nothing here knows the `f-` prefix.
|
|
20
|
+
|
|
21
|
+
State is read from the DOM — `input.type` — and never cached. After a swap the
|
|
22
|
+
markup is authoritative, and a remembered boolean would describe the element
|
|
23
|
+
that used to be there.
|
|
24
|
+
|
|
25
|
+
Loaded per page by `reveal_scripts()`, never from the shell (CHARTER §7).
|
|
26
|
+
*/
|
|
27
|
+
(() => {
|
|
28
|
+
const BUTTON = "[data-fjkit-reveal]"
|
|
29
|
+
|
|
30
|
+
document.addEventListener("click", (event) => {
|
|
31
|
+
const button = event.target instanceof Element ? event.target.closest(BUTTON) : null
|
|
32
|
+
if (!button) return
|
|
33
|
+
|
|
34
|
+
/* `aria-controls` may name an element a swap has taken away, or one an app
|
|
35
|
+
pointed somewhere else. Doing nothing is right in both cases: this is a
|
|
36
|
+
progressive enhancement on a field that works without it. */
|
|
37
|
+
const input = document.getElementById(button.getAttribute("aria-controls") || "")
|
|
38
|
+
if (!(input instanceof HTMLInputElement)) return
|
|
39
|
+
|
|
40
|
+
const revealed = input.type === "text"
|
|
41
|
+
input.type = revealed ? "password" : "text"
|
|
42
|
+
button.setAttribute("aria-pressed", String(!revealed))
|
|
43
|
+
|
|
44
|
+
/* The label changes along with `aria-pressed`, because the two say the same
|
|
45
|
+
thing to different readers and a button reading "Show" over a visible
|
|
46
|
+
password is wrong for everyone. Both strings come from the macro, so a
|
|
47
|
+
translated page translates them. */
|
|
48
|
+
const next = revealed ? button.dataset.show : button.dataset.hide
|
|
49
|
+
if (next) button.textContent = next
|
|
50
|
+
})
|
|
51
|
+
})()
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Keeps a table's batch selection consistent: the header box, the row boxes,
|
|
3
|
+
the tint on a picked row, and the count a toolbar shows.
|
|
4
|
+
|
|
5
|
+
The checkboxes need nothing from here — they are checkboxes, and a form posts
|
|
6
|
+
them. What needs watching is everything that has to agree with them, none of
|
|
7
|
+
it expressible in HTML:
|
|
8
|
+
|
|
9
|
+
- a header box that ticks and clears its column
|
|
10
|
+
- the third state that box has to show, `indeterminate`, which is a DOM
|
|
11
|
+
property with no attribute and therefore cannot be rendered at all
|
|
12
|
+
- `data-state="selected"` on each picked row, which is how Basecoat tints it
|
|
13
|
+
- a live "3 selected" readout, so the person can see what a bulk action is
|
|
14
|
+
about to reach
|
|
15
|
+
|
|
16
|
+
Selections are keyed by NAME, not by table. The name is the form field the
|
|
17
|
+
boxes post under — `data-fjkit-select="selected"` on the rows,
|
|
18
|
+
`data-fjkit-select-all="selected"` on the header — and it is the right key
|
|
19
|
+
because it already decides what lands in one request. Two tables sharing a
|
|
20
|
+
name are one selection to the server, so they are one selection here too. A
|
|
21
|
+
page wanting two independent ones gives them two names.
|
|
22
|
+
|
|
23
|
+
Everything is delegated from `document`, and every state is read out of the
|
|
24
|
+
DOM on each pass. Sorting and paging replace the table, so a listener bound to
|
|
25
|
+
a header box — or a count cached in a variable — would describe the table that
|
|
26
|
+
used to be there. Nothing below reconciles against a swap for the same reason:
|
|
27
|
+
the fresh markup is already correct.
|
|
28
|
+
|
|
29
|
+
Loaded per page by `select_scripts()`, never from the shell (CHARTER §7).
|
|
30
|
+
*/
|
|
31
|
+
;(() => {
|
|
32
|
+
const ALL = "data-fjkit-select-all"
|
|
33
|
+
const ROW = "data-fjkit-select"
|
|
34
|
+
const COUNT = "data-fjkit-select-count"
|
|
35
|
+
|
|
36
|
+
/* `CSS.escape` produces backslash escapes, which are valid inside a CSS
|
|
37
|
+
string as well as in an identifier, so this is safe for a name with a
|
|
38
|
+
bracket or a dot in it — `items[0]` is a legal form field name. */
|
|
39
|
+
const all = (attribute, name) =>
|
|
40
|
+
[...document.querySelectorAll(`[${attribute}="${CSS.escape(name)}"]`)]
|
|
41
|
+
|
|
42
|
+
const boxes = (name) => all(ROW, name).filter((el) => el instanceof HTMLInputElement)
|
|
43
|
+
|
|
44
|
+
/* Bring everything that describes the selection called `name` back into line
|
|
45
|
+
with the boxes, which are the only state there is. */
|
|
46
|
+
const sync = (name) => {
|
|
47
|
+
const rows = boxes(name)
|
|
48
|
+
const picked = rows.filter((box) => box.checked)
|
|
49
|
+
|
|
50
|
+
for (const box of rows) {
|
|
51
|
+
/* `closest`, not `parentElement.parentElement`: the cell is the macro's,
|
|
52
|
+
but the row is the caller's and may wrap the cell in anything. A box
|
|
53
|
+
outside a table simply has no row to tint. */
|
|
54
|
+
const row = box.closest("tr")
|
|
55
|
+
if (!row) continue
|
|
56
|
+
if (box.checked) row.setAttribute("data-state", "selected")
|
|
57
|
+
else row.removeAttribute("data-state")
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
for (const header of all(ALL, name)) {
|
|
61
|
+
if (!(header instanceof HTMLInputElement)) continue
|
|
62
|
+
/* An empty column leaves the header unticked rather than ticked: `every`
|
|
63
|
+
is vacuously true over no rows, and a header that ticks itself on an
|
|
64
|
+
empty table claims a selection nobody made. */
|
|
65
|
+
header.checked = rows.length > 0 && picked.length === rows.length
|
|
66
|
+
header.indeterminate = picked.length > 0 && picked.length < rows.length
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
for (const readout of all(COUNT, name)) {
|
|
70
|
+
/* Both strings come from the macro, so a translated page translates them
|
|
71
|
+
and nothing here has an English word in it. A readout with no zero
|
|
72
|
+
text is one that hides while the selection is empty. */
|
|
73
|
+
const zero = readout.getAttribute("data-fjkit-select-zero")
|
|
74
|
+
const template = readout.getAttribute("data-fjkit-select-label") || "{n}"
|
|
75
|
+
readout.textContent = picked.length ? template.replace("{n}", String(picked.length)) : (zero ?? "")
|
|
76
|
+
readout.hidden = picked.length === 0 && zero === null
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
document.addEventListener("change", (event) => {
|
|
81
|
+
const target = event.target
|
|
82
|
+
if (!(target instanceof HTMLInputElement)) return
|
|
83
|
+
|
|
84
|
+
const header = target.getAttribute(ALL)
|
|
85
|
+
if (header !== null) {
|
|
86
|
+
for (const box of boxes(header)) box.checked = target.checked
|
|
87
|
+
sync(header)
|
|
88
|
+
return
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const row = target.getAttribute(ROW)
|
|
92
|
+
if (row !== null) sync(row)
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
/* One pass over whatever is on the page, so a selection the server rendered
|
|
96
|
+
as already ticked arrives with its rows tinted and its header showing the
|
|
97
|
+
dash. htmx fires `htmx:load` for the initial document as well as for every
|
|
98
|
+
swap, so this covers both without a `DOMContentLoaded` of its own — and
|
|
99
|
+
`htmx:load` on a swap gives us the fresh markup, which is the only time
|
|
100
|
+
any of this needs recomputing.
|
|
101
|
+
|
|
102
|
+
`names` is derived from the header boxes, not from the row boxes: a table
|
|
103
|
+
with no header box has nothing here to correct. */
|
|
104
|
+
const syncAll = () => {
|
|
105
|
+
for (const name of new Set([...document.querySelectorAll(`[${ALL}]`)].map((el) => el.getAttribute(ALL)))) {
|
|
106
|
+
sync(name)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
document.addEventListener("htmx:load", syncAll)
|
|
111
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", syncAll)
|
|
112
|
+
else syncAll()
|
|
113
|
+
})()
|