src/hg/js/autocomplete.js 1.1

1.1 2010/02/06 01:19:35 larrym
support stuff for auto-complete using jquery.autocomplete.js
Index: src/hg/js/autocomplete.js
===================================================================
RCS file: src/hg/js/autocomplete.js
diff -N src/hg/js/autocomplete.js
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ src/hg/js/autocomplete.js	6 Feb 2010 01:19:35 -0000	1.1
@@ -0,0 +1,50 @@
+// support stuff for auto-complete using jquery.autocomplete.js
+//
+// requires ajax.js
+// requires utils.js
+
+var suggestCache;
+
+function ajaxGet(getDb, cache)
+{
+// Returns jquery.autocomplete.js ajax_get function object
+// getDb should be a function which returns the relevant assembly (e.g. "hg18)
+// cache is an optional object used as a hash to cache responses from the server.
+    suggestCache = cache;
+    return function (key, cont) {
+        if(suggestCache == null || suggestCache[key] == null)
+        {
+            $.ajax({
+                       url: "../cgi-bin/suggest",
+                       data: "db=" + getDb() + "&prefix=" + key,
+                       // dataType: "json",  // XXXX this doesn't work under IE.
+                       trueSuccess: handleSuggest,
+                       success: catchErrorOrDispatch,
+                       error: function (request, status, errorThrown) {
+                           if (typeof console != "undefined") {
+                               console.dir(request);
+                               console.log(status);
+                           }
+                           var msg = "ajax call failed";
+                           if(status != "error")
+                               msg = msg + "; error: " + status;
+                           showWarning(msg + "; statusText: " + request.statusText + "; responseText: " + request.responseText);
+                       },
+                       key: key,
+                       cont: cont
+                   });
+        } else {
+            cont(eval(suggestCache[key]));
+        }
+        // showWarning(request.term);
+    }
+}
+
+function handleSuggest(response, status)
+{
+    // We seem to get a lot of duplicate requests (especially the first letters of words),
+    // so we keep a cache of the suggestions lists we've retreived.
+    if(suggestCache != null)
+        suggestCache[this.key] = response;
+    this.cont(eval(response));
+}