Repository navigation
Expand file tree
/
Copy pathfilter-picker.ts
More file actions
137 lines (137 loc) · 5.64 KB
/
Copy pathfilter-picker.ts
File metadata and controls
137 lines (137 loc) · 5.64 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
/** 目录筛选弹层:复用 select 数据,提供搜索、键盘导航、关闭与焦点恢复。 */
import { t, msg } from "./i18n";
import { escapeHtml } from "./escape";
const descriptions: Record<string, string> = {
stars: t("按关联仓库的收藏数排列"),
updated: t("作者或维护者最近推送优先"),
downloads: t("npm 近 30 天下载次数,不代表调用量"),
name: t("按工具名称的字母顺序浏览"),
};
/** 将原生数据控件增强为可检索的按钮面板;筛选仍通过 change 事件进入页面状态。 */
export function setupFilterPicker(
select: HTMLSelectElement,
searchable: boolean,
): void {
const host = select.closest<HTMLElement>(".select-label")!;
const label = searchable ? t("筛选厂商") : t("排序方式");
const panelId = `${select.id}-panel`;
const button = document.createElement("button");
button.type = "button";
button.className = "picker-trigger";
button.setAttribute("aria-expanded", "false");
button.setAttribute("aria-controls", panelId);
const panel = document.createElement("div");
panel.id = panelId;
panel.className = "picker-panel";
panel.hidden = true;
panel.setAttribute("role", "group");
panel.setAttribute("aria-label", label);
panel.innerHTML = msg`${searchable ? t('<input class="picker-search" type="search" placeholder="搜索厂商…" aria-label="搜索厂商">') : t('<p class="picker-heading">排序方式</p>')}<div class="picker-options"></div><p class="picker-empty" hidden>没有匹配的厂商,试试其他名称。</p>`;
host.append(button, panel);
select.hidden = true;
const input = panel.querySelector<HTMLInputElement>("input");
const list = panel.querySelector<HTMLElement>(".picker-options")!;
function sync(): void {
const selected = select.selectedOptions[0];
button.innerHTML = `<span><small>${searchable ? t("厂商") : t("排序")}</small><strong>${escapeHtml(selected.textContent ?? "")}</strong></span><svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="1.5" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg>`;
button.setAttribute("aria-label", `${label}:${selected.textContent}`);
const query = input?.value.trim().toLocaleLowerCase() ?? "";
const options = [...select.options].filter((option) =>
option.text.toLocaleLowerCase().includes(query),
);
list.innerHTML = options
.map(
(option) =>
`<button type="button" class="picker-option" data-value="${escapeHtml(option.value)}" aria-pressed="${option.selected}"><span><strong>${escapeHtml(option.text)}</strong>${!searchable ? `<small>${descriptions[option.value]}</small>` : ""}</span><svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="m5 12 4 4L19 6"/></svg></button>`,
)
.join("");
panel.querySelector<HTMLElement>(".picker-empty")!.hidden =
options.length > 0;
}
function close(restore = false): void {
panel.hidden = true;
button.setAttribute("aria-expanded", "false");
if (restore) button.focus();
}
function positionPanel(): void {
if (panel.hidden) return;
panel.classList.remove("opens-up");
const bounds = panel.getBoundingClientRect();
if (
bounds.bottom > window.innerHeight - 12 &&
button.getBoundingClientRect().top > bounds.height + 12
)
panel.classList.add("opens-up");
}
function open(): void {
if (input) input.value = "";
sync();
panel.hidden = false;
positionPanel();
button.setAttribute("aria-expanded", "true");
(
input ??
list.querySelector<HTMLButtonElement>('[aria-pressed="true"]') ??
list.querySelector<HTMLButtonElement>("button")
)?.focus();
}
button.addEventListener("click", () => (panel.hidden ? open() : close(true)));
button.addEventListener("keydown", (event) => {
if (event.key === "ArrowDown") {
event.preventDefault();
event.stopPropagation();
open();
}
});
panel.addEventListener("click", (event) => {
const option = (event.target as HTMLElement).closest<HTMLButtonElement>(
"[data-value]",
);
if (!option) return;
select.value = option.dataset.value!;
select.dispatchEvent(new Event("change", { bubbles: true }));
sync();
close(true);
});
host.addEventListener("keydown", (event) => {
if (panel.hidden) return;
if (event.key === "Escape") {
event.preventDefault();
close(true);
return;
}
const options = [...list.querySelectorAll<HTMLButtonElement>("button")];
const current = options.indexOf(
document.activeElement as HTMLButtonElement,
);
if (!["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) return;
if (event.target === input && event.key !== "ArrowDown") return;
event.preventDefault();
const next =
event.key === "Home"
? 0
: event.key === "End"
? options.length - 1
: (current + (event.key === "ArrowDown" ? 1 : -1) + options.length) %
options.length;
options[next]?.focus();
});
host.addEventListener("focusout", (event) => {
if (!host.contains(event.relatedTarget as Node | null)) close();
});
document.addEventListener("pointerdown", (event) => {
if (!host.contains(event.target as Node)) close();
});
window.addEventListener("resize", positionPanel);
window.visualViewport?.addEventListener("resize", positionPanel);
document.addEventListener(
"scroll",
(event) => {
if (!panel.contains(event.target as Node)) positionPanel();
},
true,
);
input?.addEventListener("input", sync);
select.addEventListener("change", sync);
sync();
}