From 08cd3a47979ba52f452aca25947016d40d927127 Mon Sep 17 00:00:00 2001
From: Chris Hallberg <crhallberg@gmail.com>
Date: Tue, 15 Jan 2019 11:31:59 -0500
Subject: [PATCH] Make autocomplete auto-submitting configurable.

---
 config/vufind/searches.ini                      |  2 ++
 .../VuFind/src/VuFind/Search/Base/Options.php   | 17 +++++++++++++++++
 module/VuFind/src/VuFind/Search/EDS/Options.php |  5 ++++-
 .../VuFind/src/VuFind/Search/Solr/Options.php   |  5 ++++-
 .../src/VuFind/View/Helper/Root/SearchBox.php   | 13 +++++++++++++
 themes/bootstrap3/js/common.js                  | 15 ++++++++++-----
 .../bootstrap3/templates/search/searchbox.phtml |  2 +-
 7 files changed, 51 insertions(+), 8 deletions(-)

diff --git a/config/vufind/searches.ini b/config/vufind/searches.ini
index 6341a326194..43d556a90c0 100644
--- a/config/vufind/searches.ini
+++ b/config/vufind/searches.ini
@@ -502,6 +502,8 @@ sort = "last_indexed desc"
 enabled = true
 ; This handler will be used for all search types not covered by [Autocomplete_Types]
 default_handler = Solr
+; Auto-submit autocomplete on click or enter
+auto_submit = true
 
 ; In this section, set the key equal to a search handler from searchspecs.yaml and
 ; the value equal to an autocomplete handler in order to customize autocompletion
diff --git a/module/VuFind/src/VuFind/Search/Base/Options.php b/module/VuFind/src/VuFind/Search/Base/Options.php
index 9e30bf8e117..4efcedbd2f8 100644
--- a/module/VuFind/src/VuFind/Search/Base/Options.php
+++ b/module/VuFind/src/VuFind/Search/Base/Options.php
@@ -226,6 +226,13 @@ abstract class Options implements TranslatorAwareInterface
      */
     protected $autocompleteEnabled = false;
 
+    /**
+     * Autocomplete auto submit setting
+     *
+     * @var bool
+     */
+    protected $autocompleteAutoSubmit = false;
+
     /**
      * Configuration file to read global settings from
      *
@@ -709,6 +716,16 @@ abstract class Options implements TranslatorAwareInterface
         return $this->autocompleteEnabled;
     }
 
+    /**
+     * Should autocomplete auto submit?
+     *
+     * @return bool
+     */
+    public function autocompleteAutoSubmit()
+    {
+        return $this->autocompleteAutoSubmit;
+    }
+
     /**
      * Get a string of the listviewOption (full or tab).
      *
diff --git a/module/VuFind/src/VuFind/Search/EDS/Options.php b/module/VuFind/src/VuFind/Search/EDS/Options.php
index b14e00526eb..2edae2858ff 100644
--- a/module/VuFind/src/VuFind/Search/EDS/Options.php
+++ b/module/VuFind/src/VuFind/Search/EDS/Options.php
@@ -137,10 +137,13 @@ class Options extends \VuFind\Search\Base\Options
                 $facetConf->Advanced_Facet_Settings->translated_facets->toArray()
             );
         }
-        // Load autocomplete preference:
+        // Load autocomplete preferences:
         if (isset($searchSettings->Autocomplete->enabled)) {
             $this->autocompleteEnabled = $searchSettings->Autocomplete->enabled;
         }
+        if (isset($searchSettings->Autocomplete->auto_submit)) {
+            $this->autocompleteAutoSubmit = $searchSettings->Autocomplete->auto_submit;
+        }
     }
 
     /**
diff --git a/module/VuFind/src/VuFind/Search/Solr/Options.php b/module/VuFind/src/VuFind/Search/Solr/Options.php
index 760b81e44b2..0b25383c2e1 100644
--- a/module/VuFind/src/VuFind/Search/Solr/Options.php
+++ b/module/VuFind/src/VuFind/Search/Solr/Options.php
@@ -208,10 +208,13 @@ class Options extends \VuFind\Search\Base\Options
             $this->highlight = true;
         }
 
-        // Load autocomplete preference:
+        // Load autocomplete preferences:
         if (isset($searchSettings->Autocomplete->enabled)) {
             $this->autocompleteEnabled = $searchSettings->Autocomplete->enabled;
         }
+        if (isset($searchSettings->Autocomplete->auto_submit)) {
+            $this->autocompleteAutoSubmit = $searchSettings->Autocomplete->auto_submit;
+        }
 
         // Load shard settings
         if (isset($searchSettings->IndexShards)
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/SearchBox.php b/module/VuFind/src/VuFind/View/Helper/Root/SearchBox.php
index 87f8bcb5d8b..6e0eb666f4e 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/SearchBox.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/SearchBox.php
@@ -126,6 +126,19 @@ class SearchBox extends \Zend\View\Helper\AbstractHelper
         return false;
     }
 
+    /**
+     * Is autocomplete enabled for the current context?
+     *
+     * @param string $activeSearchClass Active search class ID
+     *
+     * @return bool
+     */
+    public function autocompleteAutoSubmit($activeSearchClass)
+    {
+        $options = $this->optionsManager->get($activeSearchClass);
+        return $options->autocompleteAutoSubmit();
+    }
+
     /**
      * Are alphabrowse options configured to display in the search options
      * drop-down?
diff --git a/themes/bootstrap3/js/common.js b/themes/bootstrap3/js/common.js
index 24cb458c175..8496fc33bfa 100644
--- a/themes/bootstrap3/js/common.js
+++ b/themes/bootstrap3/js/common.js
@@ -246,17 +246,22 @@ function setupAutocomplete() {
   if (searchbox.length < 1) {
     return;
   }
+  // Auto-submit based on config
+  var acCallback = function ac_cb_noop() {};
+  if (searchbox.hasClass("ac-auto-submit")) {
+    acCallback = function autoSubmitAC(item, input) {
+      input.val(item.value);
+      $("#searchForm").submit();
+      return false;
+    };
+  }
   // Search autocomplete
   searchbox.autocomplete({
     rtl: $(document.body).hasClass("rtl"),
     maxResults: 10,
     loadingString: VuFind.translate('loading') + '...',
     // Auto-submit selected item
-    callback: function autoSubmitAC(item, input) {
-      input.val(item.value);
-      $("#searchForm").submit();
-      return false;
-    },
+    callback: acCallback,
     // AJAX call for autocomplete results
     handler: function vufindACHandler(input, cb) {
       var query = input.val();
diff --git a/themes/bootstrap3/templates/search/searchbox.phtml b/themes/bootstrap3/templates/search/searchbox.phtml
index 78d57f6b44d..f0f9470c95c 100644
--- a/themes/bootstrap3/templates/search/searchbox.phtml
+++ b/themes/bootstrap3/templates/search/searchbox.phtml
@@ -44,7 +44,7 @@
   <form id="searchForm" class="searchForm navbar-form navbar-left flip" method="get" action="<?=$this->url($basicSearch)?>" name="searchForm" autocomplete="off">
     <?= $this->context($this)->renderInContext('search/searchTabs', ['searchTabs' => $tabConfig['tabs']]); ?>
     <?php $placeholder = $this->searchbox()->getPlaceholderText($tabConfig['selected']['id'] ?? null); ?>
-    <input id="searchForm_lookfor" class="searchForm_lookfor form-control search-query<?php if($this->searchbox()->autocompleteEnabled($this->searchClassId)):?> autocomplete searcher:<?=$this->escapeHtmlAttr($this->searchClassId) ?><?php endif ?>" type="text" name="lookfor" value="<?=$this->escapeHtmlAttr($this->lookfor)?>"<?php if ($placeholder): ?> placeholder="<?=$this->transEsc($placeholder) ?>"<?php endif ?> aria-label="<?=$this->transEsc("search_terms")?>" />
+    <input id="searchForm_lookfor" class="searchForm_lookfor form-control search-query<?php if($this->searchbox()->autocompleteEnabled($this->searchClassId)):?> autocomplete searcher:<?=$this->escapeHtmlAttr($this->searchClassId) ?><?=$this->searchbox()->autocompleteAutoSubmit($this->searchClassId) ? ' ac-auto-submit' : '' ?><?php endif ?>" type="text" name="lookfor" value="<?=$this->escapeHtmlAttr($this->lookfor)?>"<?php if ($placeholder): ?> placeholder="<?=$this->transEsc($placeholder) ?>"<?php endif ?> aria-label="<?=$this->transEsc("search_terms")?>" />
     <?php if ($handlerCount > 1): ?>
       <select id="searchForm_type" class="searchForm_type form-control" name="type" data-native-menu="false" aria-label="<?=$this->transEsc("Search type")?>">
         <?php foreach ($handlers as $handler): ?>
-- 
GitLab