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();
|
||||
})();
|
||||
+2
-2
@@ -11,7 +11,7 @@
|
||||
intro: (councilAccess ? 'Найдите сотрудника, подберите кандидатов в диссовет и проверьте публикации. '
|
||||
: 'Найдите сотрудника и изучите его профиль. ') + 'Подсказки можно повторить из меню «Как пользоваться».'},
|
||||
{id: 'employee-search', path: directory, title: 'Найдите сотрудника', region: '.directory__toolbar', tips: [
|
||||
['[name="q"]', 'Введите фамилию, часть ФИО или ссылку на профиль ВШЭ.'],
|
||||
['[name="q"]', 'Введите фамилию или ссылку на профиль ВШЭ. Поиск срабатывает с четвёртого символа.'],
|
||||
['[name="pps"]', 'Оставьте сотрудников профессорско-преподавательского состава.'],
|
||||
]},
|
||||
{id: 'employee-filters', path: directory, title: 'Уточните список', region: '.directory__filter-panel', tips: [
|
||||
@@ -25,7 +25,7 @@
|
||||
]},
|
||||
{id: 'employee-sort', path: directory, title: 'Настройте порядок списка', region: '.directory__toolbar', tips: [
|
||||
['[name="sort"]', 'Выберите поле сортировки. Стрелка вниз покажет наибольшие значения или новые даты первыми.'],
|
||||
['[name="limit"]', 'Выберите число строк и нажмите «Применить фильтры».'],
|
||||
['[name="limit"]', 'Выберите число строк. Список обновится автоматически.'],
|
||||
]},
|
||||
{id: 'employee-profile', path: directory, title: 'Откройте карточку сотрудника', region: '.directory__header', tips: [
|
||||
['[data-columns-open]', 'Выберите столбцы: например, должности, контакты и число публикаций.'],
|
||||
|
||||
Reference in new issue
Block a user