1 if (!dojo._hasResource["openils.widget.AutoSuggest"]) {
2 dojo.provide("openils.widget.AutoSuggest");
3 dojo._hasResource["openils.widget.AutoSuggest"] = true;
5 dojo.require("dijit.form.ComboBox");
6 dojo.require("openils.AutoSuggestStore");
8 /* Here's a monkey patch to assist in making clicking on a suggestion lead
9 * directly to search. Relies on overridden _startSearch() in the widget
11 var _orig_onMouseUp = dijit.form._ComboBoxMenu.prototype._onMouseUp;
12 dijit.form._ComboBoxMenu.prototype._onMouseUp = function(evt) {
13 if (this.parent_widget)
14 this.parent_widget.mouse_used_most_recently = true;
15 dojo.hitch(this, _orig_onMouseUp)(evt);
19 "openils.widget.AutoSuggest", [dijit.form.ComboBox], {
24 "hasDownArrow": false,
25 "autoComplete": false,
28 /* Don't forget to these two parameters when instantiating. */
29 "submitter": function() { console.log("No submitter connected"); },
30 "type_selector": null, /* see openils.AutoSuggestStore for docs */
34 "mouse_used_most_recently": false,
36 "_update_search_type_selector": function(id) { /* cmf id */
37 if (!this.store.cm_cache.is_done) {
39 "can't update search type selector; " +
40 "store doesn't have config.metabib_* caches available"
45 var f = this.store.cm_cache.cmf[id];
46 var selector = this.type_selector;
47 var search_class = f.field_class + "|" + f.name;
48 var exact = dojo.indexOf(
49 dojo.map(selector.options, function(o) { return o.value; }),
54 selector.selectedIndex = exact;
55 } else { /* settle for class match if we can get it */
56 for (var i = 0; i < selector.options.length; i++) {
57 if (selector.options[i].value.split("|")[0] ==
59 selector.selectedIndex = i;
66 "_startSearch": function() {
67 this.inherited(arguments);
68 this._popupWidget.parent_widget = this;
71 "onChange": function(value) {
72 if (typeof value.field == "number") {
73 this._update_search_type_selector(value.field);
75 /* If onChange fires and the following condition is true,
76 * it must mean that the user clicked on an actual
77 * suggestion with the mouse. To match the behavior
78 * of well known autosuggesting things out there (like
79 * with Google search), we should actually perform our
80 * search now. We also perform our search when the user
81 * presses enter (handled elsewhere), but not when the user
82 * selects something and tabs out. */
83 if (this.mouse_used_most_recently)
88 "postMixInProperties": function() {
89 this.inherited(arguments);
91 if (typeof this.submitter == "string")
92 this.submitter = dojo.hitch(this, this.submitter);
94 if (typeof this.type_selector == "string")
95 this.type_selector = dojo.byId(this.type_selector);
97 /* Save the instantiator from needing to specify same thing
98 * twice, even though we need it and the store needs it too.
100 if (this.type_selector && !this.store_args.type_selector)
101 this.store_args.type_selector = this.type_selector;
103 this.store = new openils.AutoSuggestStore(this.store_args);
106 "postCreate": function() {
107 this.inherited(arguments);
110 this, "onKeyPress", this, function(evt) {
111 this.mouse_used_most_recently = false;
112 if (evt.keyCode == dojo.keys.ENTER)