diff --git a/app/helpers/blacklight/blacklight_helper_behavior.rb b/app/helpers/blacklight/blacklight_helper_behavior.rb index 37322335ec..de1b4a8cc3 100644 --- a/app/helpers/blacklight/blacklight_helper_behavior.rb +++ b/app/helpers/blacklight/blacklight_helper_behavior.rb @@ -65,7 +65,11 @@ def extra_body_classes # Render the search navbar # @return [String] def render_search_bar - render :partial => 'catalog/search_form' + search_bar_presenter.render + end + + def search_bar_presenter + @search_bar ||= search_bar_presenter_class.new(controller, blacklight_config) end ## @@ -263,6 +267,10 @@ def index_presenter_class(_document) blacklight_config.index.document_presenter_class end + def search_bar_presenter_class + blacklight_config.index.search_bar_presenter_class + end + ## # Open Search discovery tag for HTML links def opensearch_description_tag title, href diff --git a/app/helpers/blacklight/catalog_helper_behavior.rb b/app/helpers/blacklight/catalog_helper_behavior.rb index 63ae9af881..e41d8c0649 100644 --- a/app/helpers/blacklight/catalog_helper_behavior.rb +++ b/app/helpers/blacklight/catalog_helper_behavior.rb @@ -196,6 +196,7 @@ def should_autofocus_on_search_box? action_name == "index" && !has_search_parameters? end + deprecation_deprecate should_autofocus_on_search_box?: "use SearchBarPresenter#autofocus?" ## # Does the document have a thumbnail to render? diff --git a/app/helpers/blacklight/suggest_helper_behavior.rb b/app/helpers/blacklight/suggest_helper_behavior.rb index 497c4d3ef9..24b9965153 100644 --- a/app/helpers/blacklight/suggest_helper_behavior.rb +++ b/app/helpers/blacklight/suggest_helper_behavior.rb @@ -7,5 +7,6 @@ def autocomplete_enabled? blacklight_config.autocomplete_enabled.present? && blacklight_config.autocomplete_path.present? end + deprecation_deprecate autocomplete_enabled?: "use SearchBarPresenter#autocomplete_enabled?" end end diff --git a/app/presenters/blacklight/search_bar_presenter.rb b/app/presenters/blacklight/search_bar_presenter.rb new file mode 100644 index 0000000000..25afc83383 --- /dev/null +++ b/app/presenters/blacklight/search_bar_presenter.rb @@ -0,0 +1,37 @@ +module Blacklight + class SearchBarPresenter + attr_reader :configuration, :view_context, :controller + + # Set the partial this presenter draws + class_attribute :partial + self.partial = 'catalog/search_form' + + def initialize(controller, configuration = view_context.blacklight_config) + @controller = controller + @view_context = controller.view_context + @configuration = configuration + end + + def render + view_context.render partial, presenter: self + end + + ## + # @return [Boolean] should autocomplete be enabled in the UI + def autocomplete_enabled? + configuration.autocomplete_enabled.present? && + configuration.autocomplete_path.present? + end + + ## + # If no search parameters have been given, we should + # auto-focus the user's cursor into the searchbox + # + # @return [Boolean] + def autofocus? + controller.is_a?(Blacklight::Catalog) && + controller.action_name == "index" && + !controller.has_search_parameters? + end + end +end diff --git a/app/views/catalog/_search_form.html.erb b/app/views/catalog/_search_form.html.erb index 1e2ac63081..99afc0ae20 100644 --- a/app/views/catalog/_search_form.html.erb +++ b/app/views/catalog/_search_form.html.erb @@ -15,7 +15,7 @@ <% end %> - <%= text_field_tag :q, params[:q], placeholder: t('blacklight.search.form.search.placeholder'), class: "search_q q form-control", id: "q", autofocus: should_autofocus_on_search_box?, data: { autocomplete_enabled: autocomplete_enabled?, autocomplete_path: search_action_path(action: :suggest) } %> + <%= text_field_tag :q, params[:q], placeholder: t('blacklight.search.form.search.placeholder'), class: "search_q q form-control", id: "q", autofocus: presenter.autofocus?, data: { autocomplete_enabled: presenter.autocomplete_enabled?, autocomplete_path: search_action_path(action: :suggest) } %>