@taekwondo-db/web-app 0.0.0-stage → 0.1.2
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/LICENSE +21 -0
- package/README.md +297 -2
- package/assets/css/abstracts/base.css +54 -0
- package/assets/css/abstracts/variables.css +125 -0
- package/assets/css/components/breadcrumb.css +14 -0
- package/assets/css/components/card.css +35 -0
- package/assets/css/components/categories.css +200 -0
- package/assets/css/components/detail.css +591 -0
- package/assets/css/components/grade.css +79 -0
- package/assets/css/components/header.css +131 -0
- package/assets/css/components/language.css +38 -0
- package/assets/css/components/layout.css +151 -0
- package/assets/css/components/link.css +26 -0
- package/assets/css/components/result.css +123 -0
- package/assets/css/components/search.css +193 -0
- package/assets/css/components/tag.css +137 -0
- package/assets/css/style.css +17 -0
- package/assets/fonts/noto-serif-kr.css +28 -0
- package/assets/js/email.js +11 -0
- package/assets/js/gallery.js +30 -0
- package/assets/js/language.js +11 -0
- package/assets/js/main.js +17 -0
- package/assets/js/pwa.js +48 -0
- package/assets/js/search.js +276 -0
- package/assets/js/sorting.js +26 -0
- package/bin/taekwondo-db.js +145 -0
- package/content/_content.gotmpl +27 -0
- package/hugo.toml +32 -0
- package/i18n/de.json +107 -0
- package/i18n/en.json +107 -0
- package/i18n/ko.json +107 -0
- package/layouts/404.html +23 -0
- package/layouts/_partials/breadcrumb.html +11 -0
- package/layouts/_partials/categories.html +17 -0
- package/layouts/_partials/category-cards.html +36 -0
- package/layouts/_partials/category-group.html +7 -0
- package/layouts/_partials/category-section.html +15 -0
- package/layouts/_partials/category-tile.html +4 -0
- package/layouts/_partials/description.html +10 -0
- package/layouts/_partials/email.html +6 -0
- package/layouts/_partials/favicons.html +42 -0
- package/layouts/_partials/fonts.html +33 -0
- package/layouts/_partials/grade-badge.html +11 -0
- package/layouts/_partials/grade-name.html +14 -0
- package/layouts/_partials/head.html +17 -0
- package/layouts/_partials/header.html +38 -0
- package/layouts/_partials/historic.html +23 -0
- package/layouts/_partials/icon.html +13 -0
- package/layouts/_partials/language-label.html +3 -0
- package/layouts/_partials/language.html +13 -0
- package/layouts/_partials/page-title.html +13 -0
- package/layouts/_partials/scripts.html +25 -0
- package/layouts/_partials/search-form.html +11 -0
- package/layouts/_partials/search-index.html +40 -0
- package/layouts/_partials/source-entry.html +13 -0
- package/layouts/_partials/t.html +2 -0
- package/layouts/_partials/technique/photo.html +45 -0
- package/layouts/_partials/technique/word-meaning.html +33 -0
- package/layouts/_partials/technique-name.html +5 -0
- package/layouts/_partials/technique-row.html +17 -0
- package/layouts/_partials/term/list.html +24 -0
- package/layouts/_partials/term-name.html +4 -0
- package/layouts/_partials/url.html +11 -0
- package/layouts/_shortcodes/email.html +2 -0
- package/layouts/_shortcodes/operator.html +18 -0
- package/layouts/baseof.html +39 -0
- package/layouts/group/page.html +24 -0
- package/layouts/groups.html +48 -0
- package/layouts/home.html +19 -0
- package/layouts/page.html +17 -0
- package/layouts/source/page.html +67 -0
- package/layouts/technique/page.html +160 -0
- package/layouts/term/page.html +97 -0
- package/lib/database.js +33 -0
- package/lib/export.js +423 -0
- package/lib/service-worker.js +167 -0
- package/lib/validate.js +179 -0
- package/package.json +42 -5
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Category markers. The colour hangs on the group, never on the single
|
|
3
|
+
* category — and it is never the only information: the name is always written
|
|
4
|
+
* next to it. Categories without a group stay neutral.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
.tag {
|
|
8
|
+
display: inline-block;
|
|
9
|
+
line-height: 1.4;
|
|
10
|
+
padding: 3px 10px;
|
|
11
|
+
border-radius: var(--radius-pill);
|
|
12
|
+
background: var(--tag-neutral-bg);
|
|
13
|
+
color: var(--tag-neutral-fg);
|
|
14
|
+
font-size: var(--text-xs);
|
|
15
|
+
font-weight: 600;
|
|
16
|
+
white-space: nowrap;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
a.tag:hover {
|
|
20
|
+
text-decoration: none;
|
|
21
|
+
filter: brightness(0.96);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.tag[data-group="attack"] {
|
|
25
|
+
background: var(--group-attack-bg);
|
|
26
|
+
color: var(--group-attack-fg);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.tag[data-group="defence"] {
|
|
30
|
+
background: var(--group-defence-bg);
|
|
31
|
+
color: var(--group-defence-fg);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.tag[data-group="grappling"] {
|
|
35
|
+
background: var(--group-grappling-bg);
|
|
36
|
+
color: var(--group-grappling-fg);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.tag[data-group="movement"] {
|
|
40
|
+
background: var(--group-movement-bg);
|
|
41
|
+
color: var(--group-movement-fg);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/* Category label above the title of a detail page. */
|
|
45
|
+
.pill-category {
|
|
46
|
+
align-self: flex-start;
|
|
47
|
+
line-height: 1.4;
|
|
48
|
+
padding: 3px 10px;
|
|
49
|
+
border-radius: var(--radius-pill);
|
|
50
|
+
background: var(--tag-neutral-bg);
|
|
51
|
+
color: var(--tag-neutral-fg);
|
|
52
|
+
font-size: var(--text-xs);
|
|
53
|
+
font-weight: 600;
|
|
54
|
+
letter-spacing: 0.06em;
|
|
55
|
+
text-transform: uppercase;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
a.pill-category:hover {
|
|
59
|
+
text-decoration: none;
|
|
60
|
+
filter: brightness(0.96);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.pill-category[data-group="attack"] {
|
|
64
|
+
background: var(--group-attack-bg);
|
|
65
|
+
color: var(--group-attack-fg);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.pill-category[data-group="defence"] {
|
|
69
|
+
background: var(--group-defence-bg);
|
|
70
|
+
color: var(--group-defence-fg);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
.pill-category[data-group="grappling"] {
|
|
74
|
+
background: var(--group-grappling-bg);
|
|
75
|
+
color: var(--group-grappling-fg);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
.pill-category[data-group="movement"] {
|
|
79
|
+
background: var(--group-movement-bg);
|
|
80
|
+
color: var(--group-movement-fg);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/* Terms have no group, so they get an outline pill instead. */
|
|
84
|
+
.pill-outline {
|
|
85
|
+
align-self: flex-start;
|
|
86
|
+
padding: 3px 10px;
|
|
87
|
+
border: 1px solid var(--color-accent-border);
|
|
88
|
+
border-radius: var(--radius-pill);
|
|
89
|
+
color: var(--color-accent);
|
|
90
|
+
font-size: var(--text-xs);
|
|
91
|
+
font-weight: 600;
|
|
92
|
+
letter-spacing: 0.06em;
|
|
93
|
+
text-transform: uppercase;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/* Colour mark in front of a group heading. */
|
|
97
|
+
.group-mark {
|
|
98
|
+
flex: none;
|
|
99
|
+
width: 12px;
|
|
100
|
+
height: 12px;
|
|
101
|
+
border-radius: 3px;
|
|
102
|
+
background: var(--color-border-strong);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
.group-mark[data-group="attack"] {
|
|
106
|
+
background: var(--group-attack-mark);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
.group-mark[data-group="defence"] {
|
|
110
|
+
background: var(--group-defence-mark);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
.group-mark[data-group="grappling"] {
|
|
114
|
+
background: var(--group-grappling-mark);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
.group-mark[data-group="movement"] {
|
|
118
|
+
background: var(--group-movement-mark);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/*
|
|
122
|
+
* Hangeul inside a pill or tag: the typeface applies, the rest does not — the
|
|
123
|
+
* pill sets its own colour and size, and the Hangeul must not fight it.
|
|
124
|
+
*/
|
|
125
|
+
.tag .hangeul,
|
|
126
|
+
.pill-category .hangeul,
|
|
127
|
+
.pill-outline .hangeul {
|
|
128
|
+
font-family: var(--font-hangeul);
|
|
129
|
+
/*
|
|
130
|
+
* Hangeul fills far more of its em box than uppercase Latin does, so at a
|
|
131
|
+
* shared size it towers over the label beside it and inflates the pill.
|
|
132
|
+
*/
|
|
133
|
+
font-size: 0.85em;
|
|
134
|
+
font-weight: inherit;
|
|
135
|
+
letter-spacing: 0;
|
|
136
|
+
color: inherit;
|
|
137
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
@import "vendor/bootstrap/bootstrap.min.css";
|
|
2
|
+
|
|
3
|
+
@import './abstracts/variables.css';
|
|
4
|
+
@import './abstracts/base.css';
|
|
5
|
+
|
|
6
|
+
@import './components/layout.css';
|
|
7
|
+
@import './components/header.css';
|
|
8
|
+
@import './components/language.css';
|
|
9
|
+
@import './components/search.css';
|
|
10
|
+
@import './components/result.css';
|
|
11
|
+
@import './components/tag.css';
|
|
12
|
+
@import './components/grade.css';
|
|
13
|
+
@import './components/card.css';
|
|
14
|
+
@import './components/breadcrumb.css';
|
|
15
|
+
@import './components/categories.css';
|
|
16
|
+
@import './components/detail.css';
|
|
17
|
+
@import './components/link.css';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Hangeul, Korean subset only.
|
|
3
|
+
*
|
|
4
|
+
* Declared here instead of taking `@fontsource/noto-serif-kr/korean-*.css`,
|
|
5
|
+
* because those also reference a `.woff` fallback — 2.4 MB per weight that
|
|
6
|
+
* every browser of the last decade ignores.
|
|
7
|
+
*
|
|
8
|
+
* Weight 500 carries Hangeul next to romanised text. Weight 700 carries the
|
|
9
|
+
* headings of the Korean pages and, elsewhere, only the syllable tiles inside
|
|
10
|
+
* the folded-away word-meaning card — an unused face is not downloaded, so it
|
|
11
|
+
* is preloaded on Korean pages only (see layouts/_partials/fonts.html).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
@font-face {
|
|
15
|
+
font-family: "Noto Serif KR";
|
|
16
|
+
font-style: normal;
|
|
17
|
+
font-weight: 500;
|
|
18
|
+
font-display: optional;
|
|
19
|
+
src: url("noto-serif-kr-korean-500-normal.woff2") format("woff2");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@font-face {
|
|
23
|
+
font-family: "Noto Serif KR";
|
|
24
|
+
font-style: normal;
|
|
25
|
+
font-weight: 700;
|
|
26
|
+
font-display: optional;
|
|
27
|
+
src: url("noto-serif-kr-korean-700-normal.woff2") format("woff2");
|
|
28
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Turns the addresses of the `email` shortcode into mailto links. */
|
|
2
|
+
export function initEmailLinks() {
|
|
3
|
+
const links = /** @type {NodeListOf<HTMLAnchorElement>} */ (
|
|
4
|
+
document.querySelectorAll("a[data-email]")
|
|
5
|
+
);
|
|
6
|
+
for (const link of links) {
|
|
7
|
+
const address = atob(link.dataset.email ?? "");
|
|
8
|
+
link.href = `mailto:${address}`;
|
|
9
|
+
link.textContent = address;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** Thumbnails switch the large image of a technique, its caption and the AI marker. */
|
|
2
|
+
export function initGallery() {
|
|
3
|
+
for (const gallery of document.querySelectorAll("[data-gallery]")) {
|
|
4
|
+
const image = /** @type {HTMLImageElement} */ (
|
|
5
|
+
gallery.querySelector("[data-gallery-image]")
|
|
6
|
+
);
|
|
7
|
+
const caption = /** @type {HTMLElement} */ (
|
|
8
|
+
gallery.querySelector("[data-gallery-caption]")
|
|
9
|
+
);
|
|
10
|
+
const ai = /** @type {HTMLElement} */ (
|
|
11
|
+
gallery.querySelector("[data-gallery-ai]")
|
|
12
|
+
);
|
|
13
|
+
const thumbs = /** @type {NodeListOf<HTMLElement>} */ (
|
|
14
|
+
gallery.querySelectorAll(".detail__thumb")
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
for (const thumb of thumbs) {
|
|
18
|
+
thumb.addEventListener("click", () => {
|
|
19
|
+
image.src = thumb.dataset.src;
|
|
20
|
+
image.alt = thumb.dataset.alt;
|
|
21
|
+
caption.textContent = thumb.dataset.caption;
|
|
22
|
+
caption.hidden = !thumb.dataset.caption;
|
|
23
|
+
ai.hidden = thumb.dataset.ai !== "true";
|
|
24
|
+
for (const other of thumbs) {
|
|
25
|
+
other.setAttribute("aria-pressed", String(other === thumb));
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Switching the language keeps the current page — and the current search. */
|
|
2
|
+
export function initLanguageSelect() {
|
|
3
|
+
const select = /** @type {HTMLSelectElement | null} */ (
|
|
4
|
+
document.querySelector("[data-language-select]")
|
|
5
|
+
);
|
|
6
|
+
select?.addEventListener("change", () => {
|
|
7
|
+
const target = new URL(select.value, location.href);
|
|
8
|
+
target.search = location.search;
|
|
9
|
+
location.assign(target);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { initEmailLinks } from "./email.js";
|
|
2
|
+
import { initGallery } from "./gallery.js";
|
|
3
|
+
import { initLanguageSelect } from "./language.js";
|
|
4
|
+
import { registerServiceWorker } from "./pwa.js";
|
|
5
|
+
import { initSearch } from "./search.js";
|
|
6
|
+
import { initSorting } from "./sorting.js";
|
|
7
|
+
|
|
8
|
+
const config = JSON.parse(
|
|
9
|
+
document.getElementById("app-config")?.textContent ?? "{}",
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
initSearch(config);
|
|
13
|
+
initGallery();
|
|
14
|
+
initSorting();
|
|
15
|
+
initLanguageSelect();
|
|
16
|
+
initEmailLinks();
|
|
17
|
+
registerServiceWorker(config);
|
package/assets/js/pwa.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** How often an open site asks the server whether a new version was deployed. */
|
|
2
|
+
const UPDATE_INTERVAL = 60 * 60 * 1000;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Registers the service worker that makes the site work offline (written by
|
|
6
|
+
* `taekwondo-db build`, so only in production builds).
|
|
7
|
+
*
|
|
8
|
+
* A new deploy is installed in the background and then offered — accepting
|
|
9
|
+
* activates it and reloads. Declining keeps the current version; the offer
|
|
10
|
+
* comes again on the next start. An installed app may stay open for days, so
|
|
11
|
+
* it also checks for updates periodically while online.
|
|
12
|
+
*/
|
|
13
|
+
export async function registerServiceWorker({ serviceWorker, home, homes, t }) {
|
|
14
|
+
if (!serviceWorker || !("serviceWorker" in navigator)) return;
|
|
15
|
+
|
|
16
|
+
const registration = await navigator.serviceWorker.register(serviceWorker);
|
|
17
|
+
navigator.serviceWorker.ready.then(({ active }) =>
|
|
18
|
+
active?.postMessage({ home, languages: homes, type: "PRECACHE" }),
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
// The first install takes control as well; only an accepted update reloads.
|
|
22
|
+
let accepted = false;
|
|
23
|
+
const offer = (worker) => {
|
|
24
|
+
if (!worker || !navigator.serviceWorker.controller) return;
|
|
25
|
+
if (confirm(t.updateAvailable)) {
|
|
26
|
+
accepted = true;
|
|
27
|
+
worker.postMessage({ type: "SKIP_WAITING" });
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
offer(registration.waiting);
|
|
32
|
+
registration.addEventListener("updatefound", () => {
|
|
33
|
+
const worker = registration.installing;
|
|
34
|
+
worker?.addEventListener("statechange", () => {
|
|
35
|
+
if (worker.state === "installed") offer(worker);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
navigator.serviceWorker.addEventListener("controllerchange", () => {
|
|
40
|
+
if (!accepted) return;
|
|
41
|
+
accepted = false;
|
|
42
|
+
location.reload();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
setInterval(() => {
|
|
46
|
+
if (navigator.onLine) registration.update();
|
|
47
|
+
}, UPDATE_INTERVAL);
|
|
48
|
+
}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The search: on the home page it replaces the category overview and keeps
|
|
3
|
+
* the query in `?search=`; on every other page it opens as a dialog over the
|
|
4
|
+
* page, whose query stays local — the history should not fill up with every
|
|
5
|
+
* keystroke.
|
|
6
|
+
*/
|
|
7
|
+
import {
|
|
8
|
+
createTechniqueSearch,
|
|
9
|
+
createTermSearch,
|
|
10
|
+
} from "js/vendor/taekwondo-search.mjs";
|
|
11
|
+
|
|
12
|
+
const CHEVRON =
|
|
13
|
+
'<svg class="icon" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m9 6 6 6-6 6"></path></svg>';
|
|
14
|
+
|
|
15
|
+
let index;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Loaded on first use; the pages themselves need none of it. The index holds
|
|
19
|
+
* one language, so translations and descriptions are plain strings.
|
|
20
|
+
*/
|
|
21
|
+
function loadIndex(url) {
|
|
22
|
+
index ??= fetch(url)
|
|
23
|
+
.then((response) => response.json())
|
|
24
|
+
.then(({ techniques, terms }) => ({
|
|
25
|
+
techniques: createTechniqueSearch(techniques),
|
|
26
|
+
terms: createTermSearch(terms),
|
|
27
|
+
}));
|
|
28
|
+
return index;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const escapeHtml = (text) =>
|
|
32
|
+
text.replace(
|
|
33
|
+
/[&<>"']/g,
|
|
34
|
+
(c) =>
|
|
35
|
+
({ '"': """, "&": "&", "'": "'", "<": "<", ">": ">" })[
|
|
36
|
+
c
|
|
37
|
+
],
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Marks the first occurrence of the query. The search is fuzzy, so there need
|
|
42
|
+
* not be one — then the text stays unmarked rather than marked up wrongly.
|
|
43
|
+
*/
|
|
44
|
+
function highlight(text, query) {
|
|
45
|
+
const needle = query.trim();
|
|
46
|
+
const at = needle
|
|
47
|
+
? text.toLocaleLowerCase().indexOf(needle.toLocaleLowerCase())
|
|
48
|
+
: -1;
|
|
49
|
+
if (at < 0) return escapeHtml(text);
|
|
50
|
+
return `${escapeHtml(text.slice(0, at))}<mark>${escapeHtml(text.slice(at, at + needle.length))}</mark>${escapeHtml(text.slice(at + needle.length))}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function row(item, query, tag) {
|
|
54
|
+
return `<li><a class="result-row" href="${escapeHtml(item.url)}">
|
|
55
|
+
<span class="result-row__text">
|
|
56
|
+
<span class="result-row__name">${highlight(item.display, query)}</span>
|
|
57
|
+
${item.subtitle ? `<span class="result-row__translation">${escapeHtml(item.subtitle)}</span>` : ""}
|
|
58
|
+
</span>
|
|
59
|
+
${tag}
|
|
60
|
+
${CHEVRON}
|
|
61
|
+
</a></li>`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** "Generic term", as in layouts/_partials/technique-row.html. */
|
|
65
|
+
function genericTag(item) {
|
|
66
|
+
return item.generic
|
|
67
|
+
? `<span class="tag">${escapeHtml(item.generic)}</span>`
|
|
68
|
+
: "";
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Same markup as layouts/_partials/grade-badge.html; "" without a grade. */
|
|
72
|
+
function gradeBadge(item) {
|
|
73
|
+
if (!item.grade) return "";
|
|
74
|
+
return `<span class="grade-badge" data-grade="${escapeHtml(item.grade)}"><span class="grade-badge__belt" aria-hidden="true"></span>${escapeHtml(item.gradeLabel)}</span>`;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function section(title, rows) {
|
|
78
|
+
return `<section class="stack" style="gap: 10px">
|
|
79
|
+
<h2 class="section-title">${escapeHtml(title)}</h2>
|
|
80
|
+
<ul class="result-list">${rows.join("")}</ul>
|
|
81
|
+
</section>`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function renderResults(container, query, found, filter, t) {
|
|
85
|
+
const { techniques, terms } = found;
|
|
86
|
+
if (techniques.length === 0 && terms.length === 0) {
|
|
87
|
+
container.innerHTML = `<p class="no-results"><em>${escapeHtml(t.noResults)}</em></p>`;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const chips = [
|
|
92
|
+
["all", t.all, techniques.length + terms.length],
|
|
93
|
+
["techniques", t.techniques, techniques.length],
|
|
94
|
+
["terms", t.terms, terms.length],
|
|
95
|
+
]
|
|
96
|
+
.map(
|
|
97
|
+
([value, label, count]) =>
|
|
98
|
+
`<button type="button" data-filter="${value}" aria-pressed="${filter === value}">${escapeHtml(label)} · ${count}</button>`,
|
|
99
|
+
)
|
|
100
|
+
.join("");
|
|
101
|
+
|
|
102
|
+
container.innerHTML = [
|
|
103
|
+
`<div class="filter-chips" role="group" aria-label="${escapeHtml(t.filterResults)}">${chips}</div>`,
|
|
104
|
+
filter !== "terms" && techniques.length > 0
|
|
105
|
+
? section(
|
|
106
|
+
t.techniques,
|
|
107
|
+
techniques.map((item) =>
|
|
108
|
+
row(
|
|
109
|
+
item,
|
|
110
|
+
query,
|
|
111
|
+
genericTag(item) +
|
|
112
|
+
gradeBadge(item) +
|
|
113
|
+
`<span class="tag" data-group="${escapeHtml(item.group)}">${escapeHtml(item.tag)}</span>`,
|
|
114
|
+
),
|
|
115
|
+
),
|
|
116
|
+
)
|
|
117
|
+
: "",
|
|
118
|
+
filter !== "techniques" && terms.length > 0
|
|
119
|
+
? section(
|
|
120
|
+
t.terms,
|
|
121
|
+
terms.map((item) =>
|
|
122
|
+
row(item, query, `<span class="tag">${escapeHtml(t.term)}</span>`),
|
|
123
|
+
),
|
|
124
|
+
)
|
|
125
|
+
: "",
|
|
126
|
+
].join("");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Arrow keys step from the search field through the result rows and back.
|
|
131
|
+
*/
|
|
132
|
+
function navigateResults(event) {
|
|
133
|
+
if (
|
|
134
|
+
(event.key !== "ArrowDown" && event.key !== "ArrowUp") ||
|
|
135
|
+
event.isComposing
|
|
136
|
+
) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const container = event.currentTarget;
|
|
140
|
+
const stops = [
|
|
141
|
+
container.querySelector(".search-form input"),
|
|
142
|
+
...container.querySelectorAll(".result-row"),
|
|
143
|
+
];
|
|
144
|
+
const at = stops.indexOf(document.activeElement);
|
|
145
|
+
if (at < 0) return;
|
|
146
|
+
const next = stops[at + (event.key === "ArrowDown" ? 1 : -1)];
|
|
147
|
+
if (next) {
|
|
148
|
+
event.preventDefault();
|
|
149
|
+
next.focus();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @param {Element} container
|
|
155
|
+
* @param {any} config
|
|
156
|
+
* @param {{ onQuery?: (query: string) => void }} [options]
|
|
157
|
+
*/
|
|
158
|
+
function bind(container, config, { onQuery } = {}) {
|
|
159
|
+
const form = container.querySelector(".search-form");
|
|
160
|
+
const input = form.querySelector("input");
|
|
161
|
+
const clear = /** @type {HTMLElement} */ (
|
|
162
|
+
form.querySelector("[data-search-clear]")
|
|
163
|
+
);
|
|
164
|
+
const results = /** @type {HTMLElement} */ (
|
|
165
|
+
container.querySelector("[data-search-results]")
|
|
166
|
+
);
|
|
167
|
+
let filter = "all";
|
|
168
|
+
let current = 0;
|
|
169
|
+
|
|
170
|
+
const update = async () => {
|
|
171
|
+
const query = input.value;
|
|
172
|
+
clear.hidden = !query;
|
|
173
|
+
results.hidden = !query;
|
|
174
|
+
onQuery?.(query);
|
|
175
|
+
if (!query) {
|
|
176
|
+
results.innerHTML = "";
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const run = ++current;
|
|
181
|
+
const { techniques, terms } = await loadIndex(config.searchIndex);
|
|
182
|
+
if (run !== current) return;
|
|
183
|
+
const found = {
|
|
184
|
+
techniques: techniques(query),
|
|
185
|
+
terms: terms(query),
|
|
186
|
+
};
|
|
187
|
+
renderResults(results, query, found, filter, config.t);
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
form.addEventListener("submit", (event) => event.preventDefault());
|
|
191
|
+
input.addEventListener("input", update);
|
|
192
|
+
clear.addEventListener("click", () => {
|
|
193
|
+
input.value = "";
|
|
194
|
+
update();
|
|
195
|
+
input.focus();
|
|
196
|
+
});
|
|
197
|
+
results.addEventListener("click", (event) => {
|
|
198
|
+
const chip = /** @type {HTMLElement | null} */ (
|
|
199
|
+
/** @type {Element} */ (event.target).closest("[data-filter]")
|
|
200
|
+
);
|
|
201
|
+
if (!chip) return;
|
|
202
|
+
filter = chip.dataset.filter;
|
|
203
|
+
update();
|
|
204
|
+
});
|
|
205
|
+
container.addEventListener("keydown", navigateResults);
|
|
206
|
+
|
|
207
|
+
return { input, update };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function hideWhileSearching(container) {
|
|
211
|
+
const idle = container.querySelectorAll("[data-search-idle]");
|
|
212
|
+
return (query) => {
|
|
213
|
+
for (const element of idle) element.hidden = Boolean(query);
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function initSearch(config) {
|
|
218
|
+
// The 404 page: the query stays out of the URL, which is the wrong one anyway.
|
|
219
|
+
const inline = document.querySelector("[data-search-inline]");
|
|
220
|
+
if (inline) {
|
|
221
|
+
bind(inline, config, { onQuery: hideWhileSearching(inline) });
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const home = document.querySelector("[data-search-home]");
|
|
226
|
+
if (home) {
|
|
227
|
+
const idle = hideWhileSearching(home);
|
|
228
|
+
const { input, update } = bind(home, config, {
|
|
229
|
+
onQuery(query) {
|
|
230
|
+
idle(query);
|
|
231
|
+
const url = new URL(location.href);
|
|
232
|
+
if (query) url.searchParams.set("search", query);
|
|
233
|
+
else url.searchParams.delete("search");
|
|
234
|
+
history.replaceState(null, "", url);
|
|
235
|
+
},
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
const query = new URLSearchParams(location.search).get("search");
|
|
239
|
+
if (query) {
|
|
240
|
+
input.value = query;
|
|
241
|
+
update();
|
|
242
|
+
// Arriving with a query: put the caret where typing can continue.
|
|
243
|
+
input.focus();
|
|
244
|
+
input.setSelectionRange(query.length, query.length);
|
|
245
|
+
}
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const dialog = /** @type {HTMLDialogElement | null} */ (
|
|
250
|
+
document.querySelector(".search-dialog")
|
|
251
|
+
);
|
|
252
|
+
const trigger = /** @type {HTMLElement | null} */ (
|
|
253
|
+
document.querySelector("[data-search-open]")
|
|
254
|
+
);
|
|
255
|
+
if (!dialog || !trigger) return;
|
|
256
|
+
|
|
257
|
+
const { input, update } = bind(dialog, config);
|
|
258
|
+
trigger.hidden = false;
|
|
259
|
+
|
|
260
|
+
// Must run inside the user's click, or mobile browsers keep the keyboard closed.
|
|
261
|
+
trigger.addEventListener("click", () => {
|
|
262
|
+
dialog.showModal();
|
|
263
|
+
input.focus();
|
|
264
|
+
});
|
|
265
|
+
dialog
|
|
266
|
+
.querySelector("[data-search-close]")
|
|
267
|
+
.addEventListener("click", () => dialog.close());
|
|
268
|
+
// The panel fills the dialog, so a click on the dialog itself hit the backdrop.
|
|
269
|
+
dialog.addEventListener("click", (event) => {
|
|
270
|
+
if (event.target === dialog) dialog.close();
|
|
271
|
+
});
|
|
272
|
+
dialog.addEventListener("close", () => {
|
|
273
|
+
input.value = "";
|
|
274
|
+
update();
|
|
275
|
+
});
|
|
276
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** The "by groups | A–Z" switch of the category page. */
|
|
2
|
+
export function initSorting() {
|
|
3
|
+
const root = document.querySelector("[data-sorting]");
|
|
4
|
+
const toggle = /** @type {HTMLElement | null} */ (
|
|
5
|
+
root?.querySelector("[data-sorting-switch]")
|
|
6
|
+
);
|
|
7
|
+
if (!toggle) return;
|
|
8
|
+
|
|
9
|
+
toggle.hidden = false;
|
|
10
|
+
const choices = /** @type {NodeListOf<HTMLElement>} */ (
|
|
11
|
+
toggle.querySelectorAll("[data-sorting-choice]")
|
|
12
|
+
);
|
|
13
|
+
for (const choice of choices) {
|
|
14
|
+
choice.addEventListener("click", () => {
|
|
15
|
+
const sorting = choice.dataset.sortingChoice;
|
|
16
|
+
for (const other of choices) {
|
|
17
|
+
other.setAttribute("aria-pressed", String(other === choice));
|
|
18
|
+
}
|
|
19
|
+
for (const view of /** @type {NodeListOf<HTMLElement>} */ (
|
|
20
|
+
root.querySelectorAll("[data-sorting-view]")
|
|
21
|
+
)) {
|
|
22
|
+
view.hidden = view.dataset.sortingView !== sorting;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|