Added a search bar to homepage

This commit is contained in:
Jan Böhmer 2024-02-29 22:46:19 +01:00
parent e00988047c
commit 1815162907
4 changed files with 52 additions and 18 deletions

View file

@ -59,6 +59,9 @@ export default class extends Controller {
//The URL of the placeholder picture //The URL of the placeholder picture
const placeholder_image = this.element.dataset.placeholderImage; const placeholder_image = this.element.dataset.placeholderImage;
//If the element is in navbar mode, or not
const navbar_mode = this.element.dataset.navbarMode === "true";
const that = this; const that = this;
const recentSearchesPlugin = createLocalStorageRecentSearchesPlugin({ const recentSearchesPlugin = createLocalStorageRecentSearchesPlugin({
@ -68,8 +71,9 @@ export default class extends Controller {
this._autocomplete = autocomplete({ this._autocomplete = autocomplete({
container: this.element, container: this.element,
panelContainer: document.getElementById("navbar-frame"), //Place the panel in the navbar, if the element is in navbar mode
panelPlacement: 'end', panelContainer: navbar_mode ? document.getElementById("navbar-frame") : document.body,
panelPlacement: this.element.dataset.panelPlacement,
plugins: [recentSearchesPlugin], plugins: [recentSearchesPlugin],
openOnFocus: true, openOnFocus: true,
placeholder: trans(SEARCH_PLACEHOLDER), placeholder: trans(SEARCH_PLACEHOLDER),

View file

@ -1,4 +1,5 @@
{% import "helper.twig" as helper %} {% import "helper.twig" as helper %}
{% import "components/search.macro.html.twig" as search %}
<nav class="navbar navbar-expand-md bg-body-tertiary border-bottom shadow-sm fixed-top py-0" id="navbar"> <nav class="navbar navbar-expand-md bg-body-tertiary border-bottom shadow-sm fixed-top py-0" id="navbar">
<div class="container-fluid"> <div class="container-fluid">
@ -58,7 +59,9 @@
</ul> </ul>
{% if is_granted('@parts.read') %} {% if is_granted('@parts.read') %}
{% include "_navbar_search.html.twig" %} {{ search.search_form("navbar") }}
{# {% include "_navbar_search.html.twig" %} #}
{% endif %} {% endif %}

View file

@ -1,7 +1,8 @@
<form id="navbar-search-form" action="{{ path('parts_search') }}" class="d-flex my-lg-0 ms-auto" method="get" data-turbo-permanent> {% macro settings_drodown(show_label_instead_icon = true) %}
<div class="dropdown"> <div class="dropdown">
<button class="btn dropdown-toggle my-2" type="button" id="navbar-search-options" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" data-bs-auto-close="false"> <button class="btn dropdown-toggle my-2" type="button" id="navbar-search-options" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" data-bs-auto-close="false">
{% trans %}search.options.label{% endtrans %} {% if show_label_instead_icon %}{% trans %}search.options.label{% endtrans %}{% else %}<i class="fa-solid fa-gear"></i>{% endif %}
<span class="caret"></span> <span class="caret"></span>
</button> </button>
<div class="dropdown-menu" aria-labelledby="navbar-search-options"> <div class="dropdown-menu" aria-labelledby="navbar-search-options">
@ -68,11 +69,37 @@
</div> </div>
</div> </div>
</div> </div>
{% endmacro %}
{# It is important that this element has an id field. Otherwise the dropdown panel wont appear after some link clicks #} {# Render a complete usable search form including the form tags. mode can be "standalone" or "navbar" #}
<div {{ stimulus_controller('elements/part_livesearch') }} class="mt-auto mb-auto" id="navbar-quicksearch-container" {% macro search_form(mode = "standalone") %}
data-placeholder-image="{{ asset('img/part_placeholder.svg') }}" {% set is_navbar = (mode == "navbar") %}
data-autocomplete="{{ path('typeahead_parts', {'query': '__QUERY__'}) }}" data-detail-url="{{ path('part_info', {'id': '__ID__'}) }}">
<input type="hidden" name="keyword" required {{ stimulus_target('elements/part_livesearch', 'input') }}> <form action="{{ path('parts_search') }}" class="d-flex {% if is_navbar %}my-lg-0{% endif%} ms-auto" method="get"
</div> {# Navbar element needs to be persistent #}
</form> {% if is_navbar %}id="navbar-search-form" data-turbo-permanent{% endif %}>
{# Show the options left in navbar #}
{% if is_navbar %}
{{ _self.settings_drodown(is_navbar) }}
{% endif %}
<div {{ stimulus_controller('elements/part_livesearch') }}
class="mt-auto mb-auto {% if not is_navbar %}w-100{% endif %}"
{% if is_navbar %}id="navbar-quicksearch-container"{% endif %}
data-panel-placement="{% if is_navbar %}end{% else %}input-wrapper-width{% endif %}"
data-mode-navbar="{% if is_navbar %}true{% else %}false{% endif %}"
data-placeholder-image="{{ asset('img/part_placeholder.svg') }}"
data-autocomplete="{{ path('typeahead_parts', {'query': '__QUERY__'}) }}"
data-detail-url="{{ path('part_info', {'id': '__ID__'}) }}">
<input type="hidden" name="keyword" required {{ stimulus_target('elements/part_livesearch', 'input') }}>
</div>
{# And right in the standalone mode #}
{% if not is_navbar %}
{{ _self.settings_drodown(is_navbar) }}
{% endif %}
</form>
{% endmacro %}

View file

@ -1,19 +1,19 @@
{% extends "base.html.twig" %} {% extends "base.html.twig" %}
{% import "components/new_version.macro.html.twig" as nv %} {% import "components/new_version.macro.html.twig" as nv %}
{% import "components/search.macro.html.twig" as search %}
{% block content %} {% block content %}
<div id="tessdgjds" {{ stimulus_controller('elements/part_livesearch') }} class="mb-2"
data-autocomplete="{{ path('typeahead_parts', {'query': '__QUERY__'}) }}" data-detail-url="{{ path('part_info', {'id': '__ID__'}) }}">
</div>
{% if is_granted('@system.show_updates') %} {% if is_granted('@system.show_updates') %}
{{ nv.new_version_alert(new_version_available, new_version, new_version_url) }} {{ nv.new_version_alert(new_version_available, new_version, new_version_url) }}
{% endif %} {% endif %}
{% if is_granted('@parts.read') %}
{{ search.search_form("standalone") }}
{% endif %}
<div class="rounded p-4 bg-body-secondary"> <div class="rounded p-4 bg-body-secondary">
<h1 class="display-3">{{ partdb_title }}</h1> <h1 class="display-3">{{ partdb_title }}</h1>
<h4> <h4>