[Home] Rework the search form to use the extra buttons (#881)

This commit is contained in:
Cinder 2025-02-03 09:00:41 -08:00 committed by GitHub
parent 8136799546
commit f6e9f44aef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 69 additions and 14 deletions

View File

@ -0,0 +1,46 @@
import Page from "./utility/page";
const Home = {};
Home.init = function () {
const $form = $("#home-search-form");
const $tags = $("#tags");
console.log("init");
let isEmpty = !$tags.val();
let wasEmpty = isEmpty;
if (isEmpty) $form.addClass("empty");
console.log("input", isEmpty, wasEmpty);
$tags.on("input", () => {
wasEmpty = isEmpty;
isEmpty = !$tags.val();
console.log("input", isEmpty, wasEmpty);
if (isEmpty && !wasEmpty) $form.addClass("empty");
else if (!isEmpty && wasEmpty) $form.removeClass("empty");
});
$(".home-buttons a").on("click", (event) => {
if (isEmpty) return; // Act like regular links
event.preventDefault();
const extraTags = $(event.currentTarget).attr("tags");
if (extraTags) {
$tags.val((index, value) => {
return value + " " + extraTags;
});
}
$form.trigger("submit");
return false;
});
};
$(() => {
if (!Page.matches("static", "home")) return;
Home.init();
});
export default Home;

View File

@ -75,6 +75,10 @@ body.c-static.a-home {
}
}
#home-search-form:not(.empty) .home-buttons a span::before {
content: "Search ";
}
// Footer
.home-footer-top {

View File

@ -4,21 +4,26 @@
<% end -%>
<section class="home-section">
<%= form_tag(posts_path, method: "get", class: "home-search") do %>
<%= text_field_tag("tags", "", autofocus: "autofocus", placeholder: "Search posts by tag", data: { shortcut: "q", autocomplete: "tag-query" }) %>
<%= tag.button(tag.i(class: "fa-solid fa-magnifying-glass"), type: "submit") %>
<% end %>
<%= form_tag(posts_path, method: "get", id: "home-search-form") do %>
<div class="home-buttons">
<a href="/posts">
<i class="fas fa-splotch"></i>
Latest
</a>
<a href="/popular">
<i class="fas fa-fire-alt"></i>
Popular
</a>
</div>
<!-- Primary Searchbar -->
<div class="home-search">
<%= text_field_tag("tags", "", autofocus: "autofocus", placeholder: "Search posts by tag", data: { shortcut: "q", autocomplete: "tag-query" }) %>
<%= tag.button(tag.i(class: "fa-solid fa-magnifying-glass"), type: "submit") %>
</div>
<!-- Secondary search buttons -->
<div class="home-buttons">
<a href="<%= posts_path %>" tags="">
<i class="fas fa-splotch"></i>
<span>Latest</span>
</a>
<a href="<%= posts_path(tags: "order:rank") %>" tags="order:rank">
<i class="fas fa-fire-alt"></i>
<span>Popular</span>
</a>
</div>
<% end %>
</section>
<% if news_update = NewsUpdate.recent %>