forked from admin/miem_workers
Refresh employee results from primary filters automatically
This commit is contained in:
1 parent
8d332c7d60
commit
e0599b946a
9 files changed
+115
-23
No files matched your search
+98
-5
@@ -78,6 +78,103 @@
|
||||
});
|
||||
}
|
||||
|
||||
function setupDirectoryFilters() {
|
||||
const form = document.querySelector("[data-directory-filters]");
|
||||
if (!form) return;
|
||||
const search = form.elements.q;
|
||||
const summary = document.querySelector("[data-directory-summary]");
|
||||
const exportLink = document.querySelector("[data-directory-export]");
|
||||
const results = document.querySelector("[data-directory-results]");
|
||||
const pagination = document.querySelector("[data-directory-pagination]");
|
||||
const fields = ["q", "pps", "sort", "direction", "limit"];
|
||||
const readControls = () => {
|
||||
const data = new FormData(form);
|
||||
return new URLSearchParams(fields.map((name) => [name, data.get(name) || ""]));
|
||||
};
|
||||
let applied = readControls();
|
||||
let appliedSummary = summary.textContent;
|
||||
let timer;
|
||||
let pending;
|
||||
|
||||
form.querySelector(".directory__filter-panel").addEventListener("invalid", (event) => {
|
||||
event.currentTarget.open = true;
|
||||
}, true);
|
||||
form.addEventListener("formdata", (event) => {
|
||||
const text = search.value.trim();
|
||||
if (Array.from(text).length >= 4) event.formData.set("q", text);
|
||||
else event.formData.delete("q");
|
||||
});
|
||||
|
||||
async function refresh() {
|
||||
const next = readControls();
|
||||
if (next.toString() === applied.toString()) {
|
||||
if (summary.textContent !== appliedSummary) summary.textContent = appliedSummary;
|
||||
return;
|
||||
}
|
||||
// Keep applied additional filters even if their fields have unsubmitted edits.
|
||||
const url = new URL(window.location.href);
|
||||
url.searchParams.delete("offset");
|
||||
for (const [name, value] of next) {
|
||||
if (value) url.searchParams.set(name, value);
|
||||
else url.searchParams.delete(name);
|
||||
}
|
||||
const request = new AbortController();
|
||||
pending = request;
|
||||
results.setAttribute("aria-busy", "true");
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
signal: AbortSignal.any([request.signal, AbortSignal.timeout(15000)]),
|
||||
});
|
||||
if (!response.ok) throw new Error("directory request failed");
|
||||
const page = new DOMParser().parseFromString(await response.text(), "text/html");
|
||||
if (request.signal.aborted) return;
|
||||
const nextSummary = page.querySelector("[data-directory-summary]");
|
||||
const nextExport = page.querySelector("[data-directory-export]");
|
||||
const nextResults = page.querySelector("[data-directory-results]");
|
||||
const nextPagination = page.querySelector("[data-directory-pagination]");
|
||||
if (!nextSummary || !nextExport || !nextResults || !nextPagination) throw new Error("directory results missing");
|
||||
history.replaceState(history.state, "", url);
|
||||
results.replaceChildren(...nextResults.childNodes);
|
||||
pagination.replaceChildren(...nextPagination.childNodes);
|
||||
applyColumns(readColumns());
|
||||
exportLink.setAttribute("href", nextExport.getAttribute("href"));
|
||||
appliedSummary = nextSummary.textContent;
|
||||
summary.textContent = appliedSummary;
|
||||
applied = next;
|
||||
} catch (_error) {
|
||||
if (!request.signal.aborted) {
|
||||
summary.textContent = "Не удалось обновить список. Показаны предыдущие результаты.";
|
||||
}
|
||||
} finally {
|
||||
if (pending === request) {
|
||||
results.removeAttribute("aria-busy");
|
||||
pending = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function schedule(delay = 0) {
|
||||
clearTimeout(timer);
|
||||
pending?.abort();
|
||||
if (delay) timer = setTimeout(refresh, delay);
|
||||
else refresh();
|
||||
}
|
||||
|
||||
search.addEventListener("input", (event) => {
|
||||
if (!event.isComposing) schedule(250);
|
||||
});
|
||||
search.addEventListener("compositionend", () => schedule(250));
|
||||
search.addEventListener("keydown", (event) => {
|
||||
if (event.key === "Enter" && !event.isComposing) {
|
||||
event.preventDefault();
|
||||
schedule();
|
||||
}
|
||||
});
|
||||
form.querySelector(".directory__toolbar").addEventListener("change", (event) => {
|
||||
if (event.target !== search) schedule();
|
||||
});
|
||||
}
|
||||
|
||||
function setupProgress() {
|
||||
const panel = document.querySelector("[data-progress-panel]");
|
||||
if (!panel) return;
|
||||
@@ -148,11 +245,7 @@
|
||||
}, 4000);
|
||||
}
|
||||
|
||||
const directoryFilters = document.querySelector(".directory__filter-panel");
|
||||
directoryFilters?.addEventListener("invalid", () => {
|
||||
directoryFilters.open = true;
|
||||
}, true);
|
||||
|
||||
setupColumns();
|
||||
setupDirectoryFilters();
|
||||
setupProgress();
|
||||
})();
|
||||
Reference in new issue
Block a user