From 206ce50734186dc306b394cc82f2e73675fde5ed Mon Sep 17 00:00:00 2001 From: Lebbeous Fogle-Weekley Date: Mon, 13 Feb 2012 12:53:20 -0500 Subject: [PATCH] AutoSuggest: Make directly clicking a suggestion initiate search Formerly, I thought it best not to do this, so that users could choose a suggestion and refine their search term further, but that put the widget's behavior at odds with that of certain ubiquitous search engines. Users can still highlight a suggestion and press tab to modify their search further before submitting, if they wish (and if they know this or figure it out somehow). Signed-off-by: Lebbeous Fogle-Weekley Signed-off-by: Mike Rylander --- Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js | 43 ++++++++++++++++++---- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js b/Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js index d93ab3e58d..f7e628394f 100644 --- a/Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js +++ b/Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js @@ -5,6 +5,16 @@ if (!dojo._hasResource["openils.widget.AutoSuggest"]) { dojo.require("dijit.form.ComboBox"); dojo.require("openils.AutoSuggestStore"); + /* Here's a monkey patch to assist in making clicking on a suggestion lead + * directly to search. Relies on overridden _startSearch() in the widget + * below. */ + var _orig_onMouseUp = dijit.form._ComboBoxMenu.prototype._onMouseUp; + dijit.form._ComboBoxMenu.prototype._onMouseUp = function(evt) { + if (this.parent_widget) + this.parent_widget.mouse_used_most_recently = true; + dojo.hitch(this, _orig_onMouseUp)(evt); + }; + dojo.declare( "openils.widget.AutoSuggest", [dijit.form.ComboBox], { @@ -21,6 +31,8 @@ if (!dojo._hasResource["openils.widget.AutoSuggest"]) { "store_args": {}, + "mouse_used_most_recently": false, + "_update_search_type_selector": function(id) { /* cmf id */ if (!this.store.cm_cache.is_done) { console.warn( @@ -51,17 +63,26 @@ if (!dojo._hasResource["openils.widget.AutoSuggest"]) { } }, - /* Something subtle is going on such that it's ungood to just - * declare the onKeyPress directly here, so we connect() it later. - */ - "_local_onKeyPress": function(ev) { - if (ev.keyCode == dojo.keys.ENTER) - this.submitter(); + "_startSearch": function() { + this.inherited(arguments); + this._popupWidget.parent_widget = this; }, "onChange": function(value) { - if (typeof value.field == "number") + if (typeof value.field == "number") { this._update_search_type_selector(value.field); + + /* If onChange fires and the following condition is true, + * it must mean that the user clicked on an actual + * suggestion with the mouse. To match the behavior + * of well known autosuggesting things out there (like + * with Google search), we should actually perform our + * search now. We also perform our search when the user + * presses enter (handled elsewhere), but not when the user + * selects something and tabs out. */ + if (this.mouse_used_most_recently) + this.submitter(); + } }, "postMixInProperties": function() { @@ -85,7 +106,13 @@ if (!dojo._hasResource["openils.widget.AutoSuggest"]) { "postCreate": function() { this.inherited(arguments); - dojo.connect(this, "onKeyPress", this, this._local_onKeyPress); + dojo.connect( + this, "onKeyPress", this, function(evt) { + this.mouse_used_most_recently = false; + if (evt.keyCode == dojo.keys.ENTER) + this.submitter(); + } + ); } } ); -- 2.11.0