Use OOB to set results count and fix jumping when loading pages

This correctly centers the pagination spinner, and ensures there's only 1.
This commit is contained in:
Jake Howard 2022-10-23 18:52:39 +01:00
parent a70f789a8d
commit 3b02f21f81
Signed by: jake
GPG key ID: 57AFB45680EDD477
5 changed files with 19 additions and 32 deletions

View file

@ -1,15 +0,0 @@
window.addEventListener("DOMContentLoaded", () => {
const searchResults = document.getElementById("search-results");
const resultsCountDisplay = document.getElementById("result-count");
function handleSearchResults(event) {
const resultsCount = event.target.querySelectorAll(".listing-item").length;
if (resultsCount) {
resultsCountDisplay.textContent =
`Found ${resultsCount} result` + (resultsCount > 1 ? "s" : "");
} else {
resultsCountDisplay.textContent = "";
}
}
searchResults.addEventListener("htmx:afterSwap", handleSearchResults);
});

View file

@ -14,17 +14,15 @@ body.page-searchpage {
max-width: 80%; max-width: 80%;
} }
.htmx-request { .htmx-request i {
animation: search-loading 1.5s linear infinite; animation: search-loading 1.5s linear infinite;
} }
#search-results > p { #search-results > p {
font-size: 1.5rem; font-size: 1.5rem;
text-align: center; text-align: center;
} display: flex;
flex-direction: column;
#result-count {
margin-bottom: 1rem;
} }
input[type="search"] { input[type="search"] {
@ -46,8 +44,9 @@ body.page-searchpage {
margin-top: 2rem; margin-top: 2rem;
} }
.search-page-indicator { #search-page-indicator {
width: 100%; text-align: center;
margin-top: 1.5rem;
&:not(.htmx-request) { &:not(.htmx-request) {
display: none; display: none;

View file

@ -1 +1,3 @@
<p>Enter a search term (of at least {{ MIN_SEARCH_LENGTH }} characters) to search</p> <p>Enter a search term (of at least {{ MIN_SEARCH_LENGTH }} characters) to search</p>
{% if request.htmx %}<p id="result-count" hx-swap-oob="true"></p>{% endif %}

View file

@ -25,10 +25,9 @@
{% include "search/enter-search-term.html" %} {% include "search/enter-search-term.html" %}
{% endif %} {% endif %}
</div> </div>
<div class="htmx-indicator" id="search-page-indicator">
<i class="fas fa-circle-notch"></i>
</div>
</section> </section>
{% endblock %} {% endblock %}
{% block extra_js %}
{{ block.super }}
<script async defer type="text/javascript" src="{% static 'js/search.js' %}" integrity="{% sri_integrity_static 'js/search.js' %}"></script>
{% endblock %}

View file

@ -4,11 +4,13 @@
{% include "common/listing-item.html" %} {% include "common/listing-item.html" %}
{% endfor %} {% endfor %}
{% if not results and page_num == 1 %}<p>No results found</p>{% endif %} {% if not results and page_num == 1 %}
<p>No results found</p>
<p id="result-count" hx-swap-oob="true"></p>
{% else %}
<p id="result-count" hx-swap-oob="true">Found {{ paginator.count }} results</p>
{% endif %}
{% if results.has_next %} {% if results.has_next %}
<span hx-get="{{ search_url }}{% querystring page=results.next_page_number %}" hx-trigger="revealed" hx-indicator="#search-page-indicator-{{ results.next_page_number }}" hx-swap="outerHTML"></span> <span hx-get="{{ search_url }}{% querystring page=results.next_page_number %}" hx-trigger="revealed" hx-indicator="#search-page-indicator" hx-swap="outerHTML"></span>
<div class="icon mt-5 mx-auto htmx-indicator search-page-indicator" id="search-page-indicator-{{ results.next_page_number }}">
<i class="fas fa-circle-notch"></i>
</div>
{% endif %} {% endif %}