This commit is contained in:
Mateusz Gruszczyński committed 2026-10-20 22:47:13 +02:00
1 parent ee174bc283
commit 52e7ce7b46
24 files changed
+672 -418

No files matched your search

+84
View File
@@ -0,0 +1,84 @@
"use strict";
window.rustProxyAclPicker = (() => {
const LIMIT = 8;
const profiles = () => envelope?.config?.acl_profiles || [];
const chosenIds = root => [...(root?.querySelectorAll('.acl-picker-option input[type="checkbox"]:checked') || [])].map(input => input.value);
const preview = profile => {
const rules = profile.acl?.rules || [], country = profile.country_access || {};
const lines = rules.slice(0, 3).map(rule => `${rule.action === "deny" ? t("deny") : t("allow")} ${rule.network}`);
if (rules.length > 3) lines.push(`+${rules.length - 3} ${t("aclPickerMoreRules")}`);
if (country.mode === "allow" || country.mode === "block") {
const codes = (country.countries || []).slice(0, 5).join(", ");
const extra = (country.countries || []).length - 5;
lines.push(`${t(country.mode === "block" ? "aclPickerBlockedCountries" : "aclPickerAllowedCountries")}: ${codes}${extra > 0 ? ` +${extra}` : ""}`);
}
if (!lines.length) lines.push(t(profile.acl ? "aclPickerNoRules" : "aclPickerNoIpRules"));
return lines.join(" · ");
};
function create(selected = [], className = "") {
const root = el("div", undefined, `acl-picker ${className}`.trim());
const details = el("details", undefined, "acl-picker-details"), summary = el("summary", undefined, "acl-picker-summary");
const heading = el("span", t("aclPickerChoose"), "acl-picker-heading");
const 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 = t("aclPickerSearch"); search.setAttribute("aria-label", t("aclPickerSearch"));
const options = el("div", undefined, "acl-picker-options");
const empty = el("p", t("aclPickerNoMatches"), "fine acl-picker-empty");
const maxHint = el("small", t("aclPickerLimit"), "fine acl-picker-limit");
dropdown.append(search, options, empty, maxHint); details.append(dropdown);
const selectedTags = el("div", undefined, "acl-picker-selected");
root.append(details, selectedTags);
const items = profiles();
const checks = new Map(), selectedSet = new Set(selected);
if (!items.length) {
const noProfiles = el("p", t("aclPickerNoProfiles"), "fine");
options.append(noProfiles); search.disabled = true;
}
for (const profile of items) {
const label = el("label", undefined, "acl-picker-option");
const check = el("input"); check.type = "checkbox"; check.value = profile.id; check.checked = selectedSet.has(profile.id);
const content = el("span", undefined, "acl-picker-option-content");
const headingRow = el("span", undefined, "acl-picker-option-heading");
const name = el("strong", profile.name || profile.id), identity = el("code", profile.id, "acl-picker-id");
const badge = el("span", t(!profile.acl ? "aclPickerNoIpRules" : profile.acl.default === "allow" ? "aclPickerDefaultAllow" : "aclPickerDefaultDeny"), `acl-picker-policy ${profile.acl?.default === "deny" ? "is-deny" : profile.acl?.default === "allow" ? "is-allow" : "is-none"}`);
headingRow.append(name, identity, badge);
content.append(headingRow, el("small", preview(profile), "acl-picker-rule-preview"));
label.append(check, content); options.append(label); checks.set(profile.id, check);
const allRules = (profile.acl?.rules || []).map(rule => `${rule.action} ${rule.network}`).join(" ");
const countries = (profile.country_access?.countries || []).join(" ");
const searchText = `${profile.name || ""} ${profile.id} ${allRules} ${countries}`.toLocaleLowerCase();
label.dataset.search = searchText;
}
const syncSearch = () => {
const query = search.value.trim().toLocaleLowerCase(); let visible = 0;
for (const label of options.querySelectorAll(".acl-picker-option")) {
label.hidden = !!query && !label.dataset.search.includes(query);
if (!label.hidden) visible++;
}
empty.hidden = visible > 0 || items.length === 0;
};
const sync = () => {
const ids = chosenIds(root), full = ids.length >= LIMIT;
counter.textContent = `${ids.length}/${LIMIT}`;
heading.textContent = ids.length ? t("aclPickerSelected").replace("{count}", ids.length) : t("aclPickerChoose");
selectedTags.replaceChildren();
for (const id of ids) {
const profile = items.find(item => item.id === id);
const tag = el("span", undefined, "acl-picker-tag");
const label = el("span", profile?.name || id); label.title = preview(profile || {});
const remove = el("button", "×", "acl-picker-remove"); remove.type = "button"; remove.setAttribute("aria-label", `${t("remove")} ${profile?.name || id}`);
remove.addEventListener("click", () => { const box = checks.get(id); box.checked = false; box.dispatchEvent(new Event("change", { bubbles: true })); });
tag.append(label, remove); selectedTags.append(tag);
}
if (!ids.length) selectedTags.append(el("span", t("aclPickerNone"), "fine acl-picker-no-selection"));
for (const check of checks.values()) check.disabled = full && !check.checked;
maxHint.hidden = !full;
};
options.addEventListener("change", event => { if (event.target.matches('input[type="checkbox"]')) sync(); });
search.addEventListener("input", syncSearch);
search.addEventListener("keydown", event => { if (event.key === "Escape") { search.value = ""; syncSearch(); details.open = false; summary.focus(); } });
syncSearch(); sync(); return root;
}
return { create, read: chosenIds, count: root => chosenIds(root).length };
})();