From 63ac5c16547fba964ec3cd4d615421442df8173a Mon Sep 17 00:00:00 2001 From: Lebbeous Fogle-Weekley Date: Wed, 25 Jan 2012 14:47:03 -0500 Subject: [PATCH] Code re-org and more commentary. Easier to instantiate an autosuggest widget. Signed-off-by: Lebbeous Fogle-Weekley --- Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js | 67 +++++++++++++++++++++- Open-ILS/web/opac/skin/default/js/search_bar.js | 55 +++--------------- 2 files changed, 74 insertions(+), 48 deletions(-) diff --git a/Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js b/Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js index 8709704610..3788053faa 100644 --- a/Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js +++ b/Open-ILS/web/js/dojo/openils/widget/AutoSuggest.js @@ -3,6 +3,7 @@ if (!dojo._hasResource["openils.widget.AutoSuggest"]) { dojo._hasResource["openils.widget.AutoSuggest"] = true; dojo.require("dijit.form.ComboBox"); + dojo.require("openils.widget.Searcher"); dojo.require("openils.AutoSuggestStore"); dojo.declare( @@ -15,11 +16,75 @@ if (!dojo._hasResource["openils.widget.AutoSuggest"]) { "autoComplete": false, "searchDelay": 200, + /* Don't forget to these two parameters when instantiating. */ + "submitter": function() { console.log("No submitter connected"); }, + "type_selector": null, /* see openils.AutoSuggestStore for docs */ + + "_update_search_type_selector": function(id) { /* cmf id */ + /* Fail somewhat gracefully if a race condition, which I'm not + * /certain/ actually exists, lets us get here before + * openils.widget.Search has fully initialized. */ + var f; + try { + f = openils.widget.Searcher._cache.obj.cmf[id]; + } catch (E) { + console.log( + "o.w.Searcher couldn't help us with field #" + id + ); + return; + } + + var selector = this.type_selector; + var search_class = f.field_class + "|" + f.name; + var exact = dojo.indexOf( + dojo.map(selector.options, function(o) { return o.value; }), + search_class + ); + + if (exact > 0) { + selector.selectedIndex = exact; + } else { /* settle for class match if we can get it */ + for (var i = 0; i < selector.options.length; i++) { + if (selector.options[i].value.split("|")[0] == + f.field_class) { + selector.selectedIndex = i; + break; + } + } + } + }, + + /* 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(); + }, + + "onChange": function(value) { + if (typeof value.field == "number") + this._update_search_type_selector(value.field); + }, + "postMixInProperties": function() { this.inherited(arguments); - if (this.storeArgs) + if (this.storeArgs) { + /* Save the instantiator from needing to specify same thing + * twice, even though we need it and the store needs it too. + */ + if (this.type_selector && !this.storeArgs.type_selector) + this.storeArgs.type_selector = this.type_selector; + this.store = new openils.AutoSuggestStore(this.storeArgs); + } + }, + + "postCreate": function() { + this.inherited(arguments); + + dojo.connect(this, "onKeyPress", this, this._local_onKeyPress); } } ); diff --git a/Open-ILS/web/opac/skin/default/js/search_bar.js b/Open-ILS/web/opac/skin/default/js/search_bar.js index 44ca3cc0fb..be5e41f5d4 100644 --- a/Open-ILS/web/opac/skin/default/js/search_bar.js +++ b/Open-ILS/web/opac/skin/default/js/search_bar.js @@ -13,41 +13,8 @@ G.evt.common.init.push(searchBarInit); var newSearchLocation; var newSearchDepth = null; -dojo.require("openils.Util"); dojo.require("openils.widget.AutoSuggest"); -function updateSearchTypeSelector(id) { - /* Fail somewhat gracefully if a race condition, which I'm not /certain/ - * actually exists, lets us get here before openils.widget.Search has - * fully initialized. */ - var f; - try { - f = openils.widget.Searcher._cache.obj.cmf[id]; - } catch (E) { - console.log("o.w.Searcher couldn't help us with field #" + id); - return; - } - - var selector = G.ui.searchbar.type_selector; - var search_class = f.field_class + "|" + f.name; - var exact = dojo.indexOf( - dojo.map(selector.options, function(o) { return o.value; }), - search_class - ); - - if (exact > 0) { - selector.selectedIndex = exact; - } else { /* settle for class match if we can get it */ - for (var i = 0; i < selector.options.length; i++) { - /* XXX make sure IE can handle this syntax */ - if (selector.options[i].value.split("|")[0] == f.field_class) { - selector.selectedIndex = i; - break; - } - } - } -} - function autoSuggestInit() { var org_unit_getter = null; var global_flag = fieldmapper.standardRequest( @@ -57,33 +24,27 @@ function autoSuggestInit() { }] ).shift(); /* XXX do we want to use caching here? a cookie? */ - if (!global_flag || !openils.Util.isTrue(global_flag.enabled)) { + if (!global_flag || !isTrue(global_flag.enabled)) return; - } else if (global_flag.value && global_flag.value.match(/opac_visible/)) { + else if (global_flag.value && global_flag.value.match(/opac_visible/)) org_unit_getter = depthSelGetNewLoc; - } + /* See comments in openils.AutoSuggestStore, esp. near the constructor, + * to find out what you can control with the storeArgs object. */ var widg = new openils.widget.AutoSuggest( { "storeArgs": { - "type_selector": G.ui.searchbar.type_selector, "org_unit_getter": org_unit_getter }, - "onChange": function(value) { - if (typeof value.field == "number") - updateSearchTypeSelector(value.field); - }, - "onKeyPress": function(event) { - if (event.charOrCode == dojo.keys.ENTER) - searchBarSubmit(); - }, + "type_selector": G.ui.searchbar.type_selector, + "submitter": searchBarSubmit, "style": {"width": dojo.style("search_box", "width")}, "value": ((getTerm() != null) ? getTerm() : "") }, "search_box" ); G.ui.searchbar.text = widg.textbox; - widg.focus(); + setTimeout(function() { widg.focus(); }, 1000);/* raise chance of success */ } function searchBarInit() { @@ -120,7 +81,7 @@ function searchBarInit() { setSelector($('opac.result.sort'), getSort()+'.'+getSortDir()); } - autoSuggestInit(); /* XXX TODO make this conditional by global flag */ + autoSuggestInit(); } function searchBarSubmit(isFilterSort) { -- 2.11.0