Allow setting a minimum length for autosuggest, but default to the current behavior... user/miker/autosuggest-min-length
authorMike Rylander <mrylander@gmail.com>
Thu, 9 Jun 2016 14:42:13 +0000 (10:42 -0400)
committerMike Rylander <mrylander@gmail.com>
Tue, 9 May 2017 20:03:09 +0000 (16:03 -0400)
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Open-ILS/src/templates/opac/parts/searchbar.tt2
Open-ILS/web/js/dojo/openils/AutoSuggestStore.js
Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js

index 5e25605..b11e14c 100644 (file)
@@ -39,7 +39,7 @@ END;
                 %]" value="[% is_advanced ? ctx.naive_query_scrub(ctx.user_query) : CGI.param('query') | html %]"
                 [%- IF use_autosuggest.enabled == "t" %]
                 dojoType="openils.widget.AutoSuggest" type_selector="'qtype'"
-                submitter="this.textbox.form.submit();"
+                submitter="this.textbox.form.submit();" min_search_length="1"
                 [%-     IF use_autosuggest.value.search('opac_visible') %]
                 store_args='{"org_unit_getter": function() { return [% ctx.search_ou %]; }}'
                 [%-     END # opac_visible -%]
index d598159..0251e40 100644 (file)
@@ -34,6 +34,7 @@ if (!dojo._hasResource["openils.AutoSuggestStore"]) {
                                        are search_classes (required) */
         "org_unit_getter": null,    /* function that returns int (OU ID) */
 
+        "min_search_length": 1,       /* Minimum number of characters in the search term before we suggest */
         "limit": 10,                /* number of suggestions at once */
         "highlight_max": null,      /* TS_HEADLINE()'s MaxWords option */
         "highlight_min": null,      /* TS_HEADLINE()'s MinWords option */
@@ -130,6 +131,10 @@ if (!dojo._hasResource["openils.AutoSuggestStore"]) {
             if (!term || term.length < 1 || term == "*") return null;
             if (term.match(/[^\s*]$/)) term += " ";
             term = term.replace(/\*$/, "");
+            term = term.replace(/\s+/g, " "); /* remove extra spaces */
+
+            /* test minimum length after normalization */
+            if (term.length < this.min_search_length) return null;
 
             var params = [
                 "query=" + encodeURIComponent(term),
index 8192751..69adbc7 100644 (file)
@@ -59,6 +59,7 @@ if (!dojo._hasResource["openils.widget.AutoSuggest"]) {
             "hasDownArrow": false,
             "autoComplete": false,
             "searchDelay": 200,
+            "min_search_length": 1,
 
             /* Don't forget to these two parameters when instantiating. */
             "submitter": function() { console.log("No submitter connected"); },
@@ -140,11 +141,16 @@ if (!dojo._hasResource["openils.widget.AutoSuggest"]) {
                 if (typeof this.type_selector == "string")
                     this.type_selector = dojo.byId(this.type_selector);
 
-                /* Save the instantiator from needing to specify same thing
-                 * twice, even though we need it and the store needs it too. */
+                /* Save the instantiator from needing to specify same things
+                 * twice, even though we need them and the store needs them too. */
                 if (this.type_selector && !this.store_args.type_selector)
                     this.store_args.type_selector = this.type_selector;
 
+                if (this.min_search_length && !this.store_args.min_search_length) {
+                    var val = parseInt(this.min_search_length);
+                    if (!isNaN(val)) this.store_args.min_search_length = val;
+                }
+
                 this.store = new openils.AutoSuggestStore(this.store_args);
             },