This commit is contained in:
Mateusz Gruszczyński committed 2026-11-16 13:46:24 +01:00
1 parent 2d0d0f12c7
commit 963ec64398
11 files changed
+114 -28

No files matched your search

+35 -13
View File
@@ -99,32 +99,43 @@ window.rustProxyChoicePicker = (() => {
const maxHint = el("small", t("aclPickerLimit"), "fine acl-picker-limit");
dropdown.append(search, optionsBox, empty, maxHint); details.append(dropdown);
const selectedTags = el("div", undefined, "acl-picker-selected"); root.append(details, selectedTags);
const items = [...(options.items || [])], checks = new Map(), order = [];
const addOption = (item, checked) => {
if (checks.has(item.id)) { const box = checks.get(item.id); if (checked && !box.checked) { box.checked = true; order.push(item.id); } return box; }
const row = el("label", undefined, "acl-picker-option"), check = el("input"); check.type = "checkbox"; check.value = item.id; check.checked = !!checked;
// Option rows are built on the first open: long lists (countries) would
// otherwise add thousands of DOM nodes per picker. `state` is the source of
// truth for every option; `checks` holds only rendered checkboxes.
const items = [...(options.items || [])], state = new Map(), checks = new Map(), order = [];
let rendered = false;
const isChosen = id => state.get(id) === true;
const renderOption = item => {
const row = el("label", undefined, "acl-picker-option"), check = el("input"); check.type = "checkbox"; check.value = item.id; check.checked = isChosen(item.id);
const content = el("span", undefined, "acl-picker-option-content"), headingRow = el("span", undefined, "acl-picker-option-heading");
headingRow.append(el("strong", item.title || item.id));
if (item.title && item.title !== item.id && !item.title.startsWith(`${item.id} `)) headingRow.append(el("code", item.id, "acl-picker-id"));
if (item.badge) headingRow.append(el("span", item.badge, `acl-picker-policy ${item.badgeClass || "is-none"}`));
content.append(headingRow); if (item.detail) content.append(el("small", item.detail, "acl-picker-rule-preview"));
row.append(check, content); row.dataset.search = `${item.id} ${item.title || ""} ${item.search || ""} ${item.detail || ""}`.toLocaleLowerCase();
optionsBox.append(row); checks.set(item.id, check); if (!items.includes(item)) items.push(item); if (checked) order.push(item.id);
return check;
optionsBox.append(row); checks.set(item.id, check);
};
const addOption = (item, checked) => {
if (!state.has(item.id)) { state.set(item.id, false); if (!items.includes(item)) items.push(item); if (rendered) renderOption(item); }
if (checked && !isChosen(item.id)) { state.set(item.id, true); order.push(item.id); const box = checks.get(item.id); if (box) box.checked = true; }
};
const renderOptions = () => { if (rendered) return; rendered = true; for (const item of items) renderOption(item); syncSearch(); sync(); };
const setChosen = (id, value) => { state.set(id, value); const box = checks.get(id); if (box) box.checked = value; };
const selected = [...new Set(options.selected || [])];
for (const item of items.slice()) addOption(item, false);
for (const id of selected) addOption(items.find(item => item.id === id) || { id, title: id, detail: options.unknownDetail ? t(options.unknownDetail) : "" }, true);
const chosen = () => order.filter(id => checks.get(id)?.checked);
const chosen = () => order.filter(isChosen);
const syncSearch = () => {
const query = search.value.trim().toLocaleLowerCase(); let visible = 0;
for (const row of optionsBox.querySelectorAll(".acl-picker-option")) { row.hidden = !!query && !row.dataset.search.includes(query); if (!row.hidden) visible++; }
if (!rendered) visible = items.length;
empty.hidden = visible > 0 || (!query && !items.length);
empty.textContent = query && options.custom ? label("addHint", "choicePickerAddHint").replace("{value}", search.value.trim()) : label("noMatches", "aclPickerNoMatches");
if (!items.length && !query) { empty.hidden = false; empty.textContent = label("empty", "choicePickerEmpty"); }
};
const sync = () => {
for (const [id, box] of checks) { const index = order.indexOf(id); if (box.checked && index < 0) order.push(id); if (!box.checked && index >= 0) order.splice(index, 1); }
for (const [id, box] of checks) state.set(id, box.checked);
for (const [id, value] of state) { const index = order.indexOf(id); if (value && index < 0) order.push(id); if (!value && index >= 0) order.splice(index, 1); }
const ids = chosen(), full = ids.length >= limit;
counter.textContent = `${ids.length}/${limit}`;
heading.textContent = ids.length ? label("selected", "aclPickerSelected").replace("{count}", ids.length) : label("choose", "aclPickerChoose");
@@ -134,7 +145,7 @@ window.rustProxyChoicePicker = (() => {
const text = el("span", item?.title || id); text.title = item?.detail || id;
if (index === 0 && options.markFirst) tag.title = t(options.markFirst);
const remove = el("button", "×", "acl-picker-remove"); remove.type = "button"; remove.setAttribute("aria-label", `${t("remove")} ${item?.title || id}`);
remove.addEventListener("click", () => { const box = checks.get(id); box.checked = false; box.dispatchEvent(new Event("change", { bubbles: true })); });
remove.addEventListener("click", () => { const box = checks.get(id); if (box) { box.checked = false; box.dispatchEvent(new Event("change", { bubbles: true })); return; } setChosen(id, false); sync(); root.dispatchEvent(new Event("change", { bubbles: true })); });
tag.append(text);
if (index > 0 && options.markFirst && options.makeFirst) {
const promote = el("button", "★", "acl-picker-promote"); promote.type = "button"; promote.dataset.uiIconName = "none"; promote.title = t(options.makeFirst); promote.setAttribute("aria-label", `${t(options.makeFirst)}: ${item?.title || id}`);
@@ -153,11 +164,13 @@ window.rustProxyChoicePicker = (() => {
for (const value of values) {
let item; try { item = options.custom(value); } catch (error) { toast(error.message, true); return; }
if (!item) continue;
if (chosen().length >= limit && !checks.get(item.id)?.checked) { toast(t("aclPickerLimit"), true); break; }
const box = addOption(items.find(entry => entry.id === item.id) || item, false); box.checked = true;
if (chosen().length >= limit && !isChosen(item.id)) { toast(t("aclPickerLimit"), true); break; }
addOption(items.find(entry => entry.id === item.id) || item, false); setChosen(item.id, true);
}
search.value = ""; syncSearch(); sync(); root.dispatchEvent(new Event("change", { bubbles: true }));
};
details.addEventListener("toggle", () => { if (details.open) renderOptions(); });
search.addEventListener("focus", renderOptions);
optionsBox.addEventListener("change", event => { if (event.target.matches('input[type="checkbox"]')) sync(); });
search.addEventListener("input", syncSearch);
search.addEventListener("keydown", event => {
@@ -167,7 +180,7 @@ window.rustProxyChoicePicker = (() => {
search.addEventListener("blur", () => { if (options.custom && search.value.trim()) addCustom(); });
root.read = chosen;
root.setDisabled = disabled => { root.classList.toggle("is-disabled", disabled); details.open = details.open && !disabled; summary.tabIndex = disabled ? -1 : 0; summary.setAttribute("aria-disabled", String(disabled)); };
root.select = ids => { for (const [id, box] of checks) box.checked = ids.includes(id); order.length = 0; order.push(...ids.filter(id => checks.has(id))); sync(); };
root.select = ids => { for (const id of state.keys()) setChosen(id, ids.includes(id)); order.length = 0; order.push(...ids.filter(id => state.has(id))); sync(); };
syncSearch(); sync(); return root;
}
return { create, read: root => root?.read?.() || [] };
@@ -210,7 +223,16 @@ window.rustProxyChipInput = (() => {
window.rustProxyCountryPicker = (() => {
const CODES = "AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ BA BB BD BE BF BG BH BI BJ BL BM BN BO BQ BR BS BT BV BW BY BZ CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CW CX CY CZ DE DJ DK DM DO DZ EC EE EG EH ER ES ET FI FJ FK FM FO FR GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY HK HM HN HR HT HU ID IE IL IM IN IO IQ IR IS IT JE JM JO JP KE KG KH KI KM KN KP KR KW KY KZ LA LB LC LI LK LR LS LT LU LV LY MA MC MD ME MF MG MH MK ML MM MN MO MP MQ MR MS MT MU MV MW MX MY MZ NA NC NE NF NG NI NL NO NP NR NU NZ OM PA PE PF PG PH PK PL PM PN PR PS PT PW PY QA RE RO RS RU RW SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR SS ST SV SX SY SZ TC TD TF TG TH TJ TK TL TM TN TO TR TT TV TW TZ UA UG UM US UY UZ VA VC VE VG VI VN VU WF WS XK YE YT ZA ZM ZW".split(" ");
const nameOf = code => { try { return new Intl.DisplayNames([document.documentElement.lang || "en"], { type: "region" }).of(code) || code; } catch { return code; } };
// Country names are resolved once per language: constructing Intl.DisplayNames
// for every lookup was a large part of rendering many country pickers.
const countryNames = new Map();
const nameOf = code => {
const lang = document.documentElement.lang || "en";
let names = countryNames.get(lang);
if (!names) { let display = null; try { display = new Intl.DisplayNames([lang], { type: "region" }); } catch { /* unsupported locale */ } names = { display, cache: new Map() }; countryNames.set(lang, names); }
if (!names.cache.has(code)) { let name = code; try { name = names.display?.of(code) || code; } catch { /* unknown region */ } names.cache.set(code, name); }
return names.cache.get(code);
};
const parse = text => [...new Set(String(text || "").split(/[\s,;]+/).map(value => value.trim().toUpperCase()).filter(Boolean))];
function attach(textarea, options = {}) {
if (!textarea || textarea.dataset.countryPicker) return null;