7546bfdb2eee5c214099574a70f5dabbd932b77e larrym Fri Jun 29 11:39:40 2012 -0700 code cleanup (get rid of suggestCache global) diff --git src/hg/js/autocomplete.js src/hg/js/autocomplete.js index b8a7bc5..dd492ea 100644 --- src/hg/js/autocomplete.js +++ src/hg/js/autocomplete.js @@ -1,72 +1,63 @@ // support stuff for auto-complete using jQuery UI's autocomplete widget // // requires ajax.js // requires utils.js -var suggestCache; +/* suggest (aka gene search) + Requires three elements on page: positionDisplay (static display), positionInput (input textbox) and position (hidden). +*/ -function ajaxGet(db, cache) -{ -// Returns jquery.autocomplete.js ajax_get function object +var suggestBox = { + ajaxGet: function ajaxGet(db) { + // Returns autocomplete source function // db is the relevant assembly (e.g. "hg18") -// cache is an optional object used as a hash to cache responses from the server. - suggestCache = cache; + var cache = new Object; // cache is is used as a hash to cache responses from the server. return function (request, callback) { var key = request.term; - if(suggestCache == null || suggestCache[key] == null) - { + if(cache[key] == null) { $.ajax({ url: "../cgi-bin/hgSuggest", data: "db=" + db + "&prefix=" + key, // dataType: "json", // XXXX this doesn't work under IE, so we retrieve as text and do an eval to force to an object. - trueSuccess: handleSuggest, + trueSuccess: function (response, status) { + // We get a lot of duplicate requests (especially the first letters of words), + // so we keep a cache of the suggestions lists we've retreived. + cache[this.key] = response; + this.cont(eval(response)); + }, 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; warn(msg + "; statusText: " + request.statusText + "; responseText: " + request.responseText); }, key: key, cont: callback }); } else { - callback(eval(suggestCache[key])); + callback(eval(cache[key])); } // warn(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)); -} - -/* suggest (aka gene search) - Requires three elements on page: positionDisplay (static display), positionInput (input textbox) and position (hidden). -*/ + }, -var suggestBox = { clearFindMatches: function() { // clear any hgFind.matches set by a previous user selection (e.g. when user directly edits the search box) if($('#hgFindMatches').length) $('#hgFindMatches').remove(); }, updateFindMatches: function(val) { // highlight genes choosen from suggest list (#6330) if($('#hgFindMatches').length) $('#hgFindMatches').val(val); else $('#positionInput').parents('form').append("<input type='hidden' id='hgFindMatches' name='hgFind.matches' " + "value='" + val + "'>"); }, @@ -75,31 +66,31 @@ { // selectCallback(item): called when the user selects a new genomic position from the list // clickCallback(position): called when the user clicks on positionDisplay var lastSelected = null; // this is the last value entered by the user via a suggestion (used to distinguish manual entry in the same field) var str; if(assemblySupportsGeneSuggest) { str = "enter new position, gene symbol or annotation search terms"; } else { str = "enter new position or annotation search terms"; } $('#positionInput').Watermark(str, '#686868'); if(assemblySupportsGeneSuggest) { $('#positionInput').autocomplete({ delay: 500, minLength: 2, - source: ajaxGet(db, new Object), + source: this.ajaxGet(db), open: function(event, ui) { var pos = $(this).offset().top + $(this).height(); if (!isNaN(pos)) { var maxHeight = $(window).height() - pos - 30; // take off a little more because IE needs it var auto = $('.ui-autocomplete'); var curHeight = $(auto).children().length * 21; if (curHeight > maxHeight) $(auto).css({maxHeight: maxHeight+'px', overflow:'scroll'}); else $(auto).css({maxHeight: 'none', overflow:'hidden'}); } }, select: function (event, ui) { selectCallback(ui.item); lastSelected = ui.item.value;