$(document).ready(() => { let allAuthors = []; let filteredAuthors = []; let currentPage = 1; let pageSize = 12; let currentSort = "name_asc"; loadAuthors(); function loadAuthors() { showLoadingState(); fetch("/api/authors") .then((response) => { if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return response.json(); }) .then((data) => { allAuthors = data.authors; applyFiltersAndSort(); }) .catch((error) => { console.error("Error loading authors:", error); showErrorState(); }); } function applyFiltersAndSort() { const searchQuery = $("#author-search-input").val().trim().toLowerCase(); filteredAuthors = allAuthors.filter((author) => author.name.toLowerCase().includes(searchQuery) ); filteredAuthors.sort((a, b) => { const nameA = a.name.toLowerCase(); const nameB = b.name.toLowerCase(); if (currentSort === "name_asc") { return nameA.localeCompare(nameB, "ru"); } else { return nameB.localeCompare(nameA, "ru"); } }); updateResultsCounter(); renderAuthors(); renderPagination(); } function updateResultsCounter() { const $counter = $("#results-counter"); const total = filteredAuthors.length; if (total === 0) { $counter.text("Авторы не найдены"); } else { const wordForm = getWordForm(total, ["автор", "автора", "авторов"]); $counter.text(`Найдено: ${total} ${wordForm}`); } } function getWordForm(number, forms) { const cases = [2, 0, 1, 1, 1, 2]; const index = number % 100 > 4 && number % 100 < 20 ? 2 : cases[Math.min(number % 10, 5)]; return forms[index]; } function renderAuthors() { const $container = $("#authors-container"); $container.empty(); if (filteredAuthors.length === 0) { $container.html(`

Авторы не найдены

Попробуйте изменить параметры поиска

`); return; } const startIndex = (currentPage - 1) * pageSize; const endIndex = startIndex + pageSize; const pageAuthors = filteredAuthors.slice(startIndex, endIndex); const $grid = $('
'); pageAuthors.forEach((author) => { const firstLetter = author.name.charAt(0).toUpperCase(); const $authorCard = $(`
${firstLetter}

${escapeHtml(author.name)}

ID: ${author.id}

`); $grid.append($authorCard); }); $container.append($grid); $container.off("click", ".author-card").on("click", ".author-card", function () { const authorId = $(this).data("id"); window.location.href = `/author/${authorId}`; }); } function renderPagination() { const $paginationContainer = $("#pagination-container"); $paginationContainer.empty(); const totalPages = Math.ceil(filteredAuthors.length / pageSize); if (totalPages <= 1) return; const $pagination = $(`
`); const $pageNumbers = $pagination.find("#page-numbers"); const pages = generatePageNumbers(currentPage, totalPages); pages.forEach((page) => { if (page === "...") { $pageNumbers.append(`...`); } else { const isActive = page === currentPage; $pageNumbers.append(` `); } }); $paginationContainer.append($pagination); $paginationContainer.find("#prev-page").on("click", function () { if (currentPage > 1) { currentPage--; renderAuthors(); renderPagination(); scrollToTop(); } }); $paginationContainer.find("#next-page").on("click", function () { if (currentPage < totalPages) { currentPage++; renderAuthors(); renderPagination(); scrollToTop(); } }); $paginationContainer.find(".page-btn").on("click", function () { const page = parseInt($(this).data("page")); if (page !== currentPage) { currentPage = page; renderAuthors(); renderPagination(); scrollToTop(); } }); } function generatePageNumbers(current, total) { const pages = []; const delta = 2; for (let i = 1; i <= total; i++) { if ( i === 1 || i === total || (i >= current - delta && i <= current + delta) ) { pages.push(i); } else if (pages[pages.length - 1] !== "...") { pages.push("..."); } } return pages; } function scrollToTop() { $("html, body").animate({ scrollTop: 0 }, 300); } function showLoadingState() { const $container = $("#authors-container"); $container.html(`
${Array(6) .fill() .map( () => `
` ) .join("")}
`); } function showErrorState() { const $container = $("#authors-container"); $container.html(`

Ошибка загрузки

Не удалось загрузить список авторов

`); $("#retry-btn").on("click", loadAuthors); } function escapeHtml(text) { if (!text) return ""; const div = document.createElement("div"); div.textContent = text; return div.innerHTML; } function initializeFilters() { const $authorSearch = $("#author-search-input"); const $resetBtn = $("#reset-filters-btn"); const $sortRadios = $('input[name="sort"]'); let searchTimeout; $authorSearch.on("input", function () { clearTimeout(searchTimeout); searchTimeout = setTimeout(() => { currentPage = 1; applyFiltersAndSort(); }, 300); }); $authorSearch.on("keypress", function (e) { if (e.which === 13) { clearTimeout(searchTimeout); currentPage = 1; applyFiltersAndSort(); } }); $sortRadios.on("change", function () { currentSort = $(this).val(); currentPage = 1; applyFiltersAndSort(); }); $resetBtn.on("click", function () { $authorSearch.val(""); $('input[name="sort"][value="name_asc"]').prop("checked", true); currentSort = "name_asc"; currentPage = 1; applyFiltersAndSort(); }); } initializeFilters(); const $guestLink = $("#guest-link"); const $userBtn = $("#user-btn"); const $userDropdown = $("#user-dropdown"); const $userArrow = $("#user-arrow"); const $userAvatar = $("#user-avatar"); const $dropdownName = $("#dropdown-name"); const $dropdownUsername = $("#dropdown-username"); const $dropdownEmail = $("#dropdown-email"); const $logoutBtn = $("#logout-btn"); let isDropdownOpen = false; function openDropdown() { isDropdownOpen = true; $userDropdown.removeClass("hidden"); $userArrow.addClass("rotate-180"); } function closeDropdown() { isDropdownOpen = false; $userDropdown.addClass("hidden"); $userArrow.removeClass("rotate-180"); } $userBtn.on("click", function (e) { e.stopPropagation(); isDropdownOpen ? closeDropdown() : openDropdown(); }); $(document).on("click", function (e) { if (isDropdownOpen && !$(e.target).closest("#user-menu-container").length) { closeDropdown(); } }); $(document).on("keydown", function (e) { if (e.key === "Escape" && isDropdownOpen) { closeDropdown(); } }); $logoutBtn.on("click", function () { localStorage.removeItem("access_token"); localStorage.removeItem("refresh_token"); window.location.reload(); }); function showGuest() { $guestLink.removeClass("hidden"); $userBtn.addClass("hidden").removeClass("flex"); closeDropdown(); } function showUser(user) { $guestLink.addClass("hidden"); $userBtn.removeClass("hidden").addClass("flex"); const displayName = user.full_name || user.username; const firstLetter = displayName.charAt(0).toUpperCase(); $userAvatar.text(firstLetter); $dropdownName.text(displayName); $dropdownUsername.text("@" + user.username); $dropdownEmail.text(user.email); } function updateUserAvatar(email) { if (!email) return; const cleanEmail = email.trim().toLowerCase(); const emailHash = sha256(cleanEmail); const avatarUrl = `https://www.gravatar.com/avatar/${emailHash}?d=identicon&s=200`; const avatarImg = document.getElementById("user-avatar"); if (avatarImg) { avatarImg.src = avatarUrl; } } const token = localStorage.getItem("access_token"); if (!token) { showGuest(); } else { fetch("/api/auth/me", { headers: { Authorization: "Bearer " + token }, }) .then((response) => { if (response.ok) return response.json(); throw new Error("Unauthorized"); }) .then((user) => { showUser(user); updateUserAvatar(user.email); document.getElementById("user-btn").classList.remove("hidden"); document.getElementById("guest-link").classList.add("hidden"); }) .catch(() => { localStorage.removeItem("access_token"); localStorage.removeItem("refresh_token"); showGuest(); }); } });