diff --git a/mkdocs_blogging_plugin/templates/pagination.js b/mkdocs_blogging_plugin/templates/pagination.js index 4f60833..91eb8ec 100644 --- a/mkdocs_blogging_plugin/templates/pagination.js +++ b/mkdocs_blogging_plugin/templates/pagination.js @@ -14,6 +14,35 @@ function scrollToTop() { }, 100); } +const onButtonClick = (ele) => { + var current = pagination.getElementsByClassName("active"); + if (current.length) { + current[0].className = current[0].className.replace( + " active", "" + ); + } + ele.className += " active"; + + // Togglg visibility of pages + const destPage = parseInt(ele.textContent) + var pages = document.getElementsByClassName("page") + if (destPage && pages.length) { + for (var j = 0; j < pages.length; j++) { + const pageId = parseInt(pages[j].id.replace("page", "")) + if (pageId != destPage) { + // This is not the destination page + if (!pages[j].className.includes("blog-hidden")) { + pages[j].className += " blog-hidden" + } + } else { + // This is the destination page + pages[j].className = pages[j].className.replace(" blog-hidden", "") + } + } + scrollToTop(); + } +}; + var pagination = document.getElementById("blog-pagination"); if (pagination) { var links = pagination.getElementsByClassName("page-number"); @@ -21,37 +50,10 @@ if (pagination) { for (var i = 0; i < links.length; i++) { // Toggle pagination highlight links[i].addEventListener("click", function () { - var current = pagination.getElementsByClassName("active"); - if (current.length) { - current[0].className = current[0].className.replace( - " active", "" - ); - } - this.className += " active"; - - // Togglg visibility of pages - const destPage = parseInt(this.textContent) - var pages = document.getElementsByClassName("page") - if (destPage && pages.length) { - for (var j = 0; j < pages.length; j++) { - const pageId = parseInt(pages[j].id.replace("page", "")) - if (pageId != destPage) { - // This is not the destination page - if (!pages[j].className.includes("blog-hidden")) { - pages[j].className += " blog-hidden" - } - } else { - // This is the destination page - pages[j].className = pages[j].className.replace(" blog-hidden", "") - } - } - scrollToTop(); - } + onButtonClick(this); }); } links[currentPage].className += " active" - if (currentPage > 0) { - links[currentPage].click(); - } + onButtonClick(links[currentPage]); } }