This commit is contained in:
Mateusz Gruszczyński committed 2026-11-14 09:57:21 +01:00
1 parent ad0ff60632
commit 6b59434ffa
60 files changed
+7418 -732

No files matched your search

+88
View File
@@ -82,3 +82,91 @@ window.rustProxyAclPicker = (() => {
}
return { create, read: chosenIds, count: root => chosenIds(root).length };
})();
window.rustProxyChoicePicker = (() => {
function create(options) {
const limit = options.limit || 16, labels = options.labels || {};
const label = (key, fallback) => labels[key] ? t(labels[key]) : t(fallback);
const root = el("div", undefined, `acl-picker choice-picker ${options.className || ""}`.trim());
const details = el("details", undefined, "acl-picker-details"), summary = el("summary", undefined, "acl-picker-summary");
const heading = el("span", "", "acl-picker-heading"), counter = el("span", "", "acl-picker-counter");
summary.append(heading, counter); details.append(summary);
const dropdown = el("div", undefined, "acl-picker-dropdown");
const search = el("input", "", "acl-picker-search"); search.type = "search"; search.placeholder = label("search", "aclPickerSearch"); search.setAttribute("aria-label", search.placeholder);
const optionsBox = el("div", undefined, "acl-picker-options"), empty = el("p", label("noMatches", "aclPickerNoMatches"), "fine acl-picker-empty");
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;
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) 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;
};
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 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++; }
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); }
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");
selectedTags.replaceChildren();
ids.forEach((id, index) => {
const item = items.find(entry => entry.id === id), tag = el("span", undefined, `acl-picker-tag${index === 0 && options.markFirst ? " is-primary" : ""}`);
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 })); });
tag.append(text);
if (index > 0 && options.markFirst && options.makeFirst) {
const promote = el("button", "★", "acl-picker-promote"); promote.type = "button"; promote.title = t(options.makeFirst); promote.setAttribute("aria-label", `${t(options.makeFirst)}: ${item?.title || id}`);
promote.addEventListener("click", () => { order.splice(order.indexOf(id), 1); order.unshift(id); sync(); root.dispatchEvent(new Event("change", { bubbles: true })); });
tag.append(promote);
}
tag.append(remove); selectedTags.append(tag);
});
if (!ids.length) selectedTags.append(el("span", label("none", "aclPickerNone"), "fine acl-picker-no-selection"));
for (const box of checks.values()) box.disabled = full && !box.checked;
maxHint.hidden = !full; root.dispatchEvent(new CustomEvent("choicechange", { detail: ids }));
};
const addCustom = () => {
const raw = search.value.trim(); if (!raw || !options.custom) return;
const values = raw.split(/[\s,;]+/).filter(Boolean);
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;
}
search.value = ""; syncSearch(); sync(); root.dispatchEvent(new Event("change", { bubbles: true }));
};
optionsBox.addEventListener("change", event => { if (event.target.matches('input[type="checkbox"]')) sync(); });
search.addEventListener("input", syncSearch);
search.addEventListener("keydown", event => {
if (event.key === "Enter") { event.preventDefault(); if (options.custom) addCustom(); else { const first = [...optionsBox.querySelectorAll(".acl-picker-option:not([hidden]) input:not(:disabled)")][0]; if (first) { first.checked = !first.checked; first.dispatchEvent(new Event("change", { bubbles: true })); } } }
if (event.key === "Escape") { search.value = ""; syncSearch(); details.open = false; summary.focus(); }
});
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(); };
syncSearch(); sync(); return root;
}
return { create, read: root => root?.read?.() || [] };
})();