From 6396bbb3c9bbad618c5b4832545ac4f3ccfe3468 Mon Sep 17 00:00:00 2001
From: David Siegfried <david.siegfried@uni-vechta.de>
Date: Wed, 19 Apr 2023 10:57:56 +0000
Subject: [PATCH] add min-length to quicksearch, closes #2561

Closes #2561

Merge request studip/studip!1728
---
 app/views/course/timesrooms/editDate.php         |  1 +
 app/views/course/timesrooms/editStack.php        |  1 +
 lib/classes/QuickSearch.class.php                | 16 ++++++++++++++++
 resources/assets/javascripts/lib/quick_search.js |  4 ++--
 templates/quicksearch/inputfield.php             |  3 ++-
 5 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/app/views/course/timesrooms/editDate.php b/app/views/course/timesrooms/editDate.php
index fa9635f585e..3be5e7c709f 100644
--- a/app/views/course/timesrooms/editDate.php
+++ b/app/views/course/timesrooms/editDate.php
@@ -43,6 +43,7 @@
                     <? if ($room_search && !$only_bookable_rooms): ?>
                         <?= $room_search
                             ->setAttributes(['onFocus' => "jQuery('input[type=radio][name=room][value=room]').prop('checked', 'checked')"])
+                            ->setMinLength(2)
                             ->render() ?>
                     <? else: ?>
                         <? $selected_room_id = $date->room_booking->resource_id ?? ''; ?>
diff --git a/app/views/course/timesrooms/editStack.php b/app/views/course/timesrooms/editStack.php
index d14db1970de..c664f5e1f20 100644
--- a/app/views/course/timesrooms/editStack.php
+++ b/app/views/course/timesrooms/editStack.php
@@ -34,6 +34,7 @@
                         <span class="flex-row"></span>
                         <?= $room_search
                             ->setAttributes(['onFocus' => "jQuery('input[type=radio][name=action][value=room]').prop('checked', true)"])
+                            ->setMinLength(2)
                             ->render() ?>
                         <? if (!$only_bookable_rooms) : ?>
                             <?= $this->render_partial('course/timesrooms/_bookable_rooms_icon.php') ?>
diff --git a/lib/classes/QuickSearch.class.php b/lib/classes/QuickSearch.class.php
index 69a80f7c247..d3983a33bc7 100644
--- a/lib/classes/QuickSearch.class.php
+++ b/lib/classes/QuickSearch.class.php
@@ -94,6 +94,7 @@ class QuickSearch
     private $inputClass = null;
     private $inputStyle = null;
     private $specialQuery = null;
+    private $minLength = 3;
 
 
     /**
@@ -263,6 +264,20 @@ class QuickSearch
         return $this;
     }
 
+    /**
+     * Set the minimum length to start searching
+     *
+     * @param int $minLength
+     *
+     * @return QuickSearch
+     */
+    public function setMinLength(int $minLength)
+    {
+        $this->minLength = $minLength;
+
+        return $this;
+    }
+
     /**
      * disables the select-box, which is displayed for non-JS users who will
      * choose with this box, which item they want to have.
@@ -384,6 +399,7 @@ class QuickSearch
             $template->set_attribute('count_QS', self::$count_QS);
             $template->set_attribute('id', $this->getId());
             $template->set_attribute('query_id', $query_id);
+            $template->set_attribute('minLength', $this->minLength);
             $template->set_attribute('search_button_name', $this->search_button_name);
             $template->set_attribute('reset_button_name', $this->reset_button_name);
             $template->set_attribute('extendedLayout', $this->hasExtendedLayout());
diff --git a/resources/assets/javascripts/lib/quick_search.js b/resources/assets/javascripts/lib/quick_search.js
index a755184a571..115941b2918 100644
--- a/resources/assets/javascripts/lib/quick_search.js
+++ b/resources/assets/javascripts/lib/quick_search.js
@@ -13,7 +13,7 @@ const QuickSearch = {
      *        when user has selected something
      * @return: void
      */
-    autocomplete: function(name, url, func, disabled) {
+    autocomplete: function(name, url, func, disabled, minLength = 3) {
         if (disabled === undefined || disabled !== true) {
             var appendTo = 'body';
             if (jQuery(`#${name}_frame`).length > 0) {
@@ -23,7 +23,7 @@ const QuickSearch = {
             }
             jQuery('#' + name).quicksearch({
                 delay: 500,
-                minLength: 3,
+                minLength: minLength,
                 appendTo: appendTo,
                 create: function() {
                     if ($(this).is('[autofocus]')) {
diff --git a/templates/quicksearch/inputfield.php b/templates/quicksearch/inputfield.php
index 4dd569f9e35..6f2cb3fb01c 100644
--- a/templates/quicksearch/inputfield.php
+++ b/templates/quicksearch/inputfield.php
@@ -29,7 +29,8 @@
         STUDIP.QuickSearch.autocomplete("<?= $id ?>",
             "<?= URLHelper::getURL("dispatch.php/quicksearch/response/".$query_id) ?>",
             <?= $jsfunction ?: 'null' ?>,
-            <?= $autocomplete_disabled ? "true" : "false" ?>
+            <?= $autocomplete_disabled ? "true" : "false" ?>,
+            <?= $minLength ?>
             );
     });
 </script>
-- 
GitLab