From bf9dc51a403f93a4622c2a74d0bbcbf81a0adaae Mon Sep 17 00:00:00 2001 From: Mike Rylander Date: Thu, 9 Jun 2016 10:42:13 -0400 Subject: [PATCH] Allow setting a minimum length for autosuggest, but default to the current behavior of 1 character Signed-off-by: Mike Rylander --- Open-ILS/src/templates/opac/parts/searchbar.tt2 | 2 +- Open-ILS/web/js/dojo/openils/AutoSuggestStore.js | 5 +++++ Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js | 10 ++++++++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Open-ILS/src/templates/opac/parts/searchbar.tt2 b/Open-ILS/src/templates/opac/parts/searchbar.tt2 index 5e2560588d..b11e14c4aa 100644 --- a/Open-ILS/src/templates/opac/parts/searchbar.tt2 +++ b/Open-ILS/src/templates/opac/parts/searchbar.tt2 @@ -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 -%] diff --git a/Open-ILS/web/js/dojo/openils/AutoSuggestStore.js b/Open-ILS/web/js/dojo/openils/AutoSuggestStore.js index d598159f0c..0251e40eee 100644 --- a/Open-ILS/web/js/dojo/openils/AutoSuggestStore.js +++ b/Open-ILS/web/js/dojo/openils/AutoSuggestStore.js @@ -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), diff --git a/Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js b/Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js index 81927517b9..69adbc7fbd 100644 --- a/Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js +++ b/Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js @@ -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); }, -- 2.11.0