625569a67a2fceb5073d032ab1d26509b82dc694
angie
  Tue Apr 26 14:08:28 2016 -0700
Added logic to keep track of whether the position/search box has a gene selected from autocomplete or something the user typed/pasted in.
Also added hgsid to the form that sends us to hgTracks -- silly that I was forgetting that!
refs #15277

diff --git src/hg/js/hgGateway.js src/hg/js/hgGateway.js
index 572cd78..2ba5e86 100644
--- src/hg/js/hgGateway.js
+++ src/hg/js/hgGateway.js
@@ -760,40 +760,42 @@
 
     var getBetterBrowserMessage = '<P style="padding-left: 10px;">' +
                                   'Our website has detected that you are using ' +
                                   'an outdated browser that will prevent you from ' +
                                   'accessing certain features. An update is not ' +
                                   'required, but it is strongly recommended to ' +
                                   'improve your browsing experience. ' +
                                   'Please use the following links to upgrade your ' +
                                   'existing browser to ' +
                                   '<A HREF="https://www.mozilla.org/en-US/firefox/new/">' +
                                   ' FireFox</A> or ' +
                                   '<A HREF="https://www.google.com/chrome/browser/">' +
                                   'Chrome</A>.' +
                                   '</P>';
 
-    // Globals
+    // Globals (within this function scope)
     // Set this to true to see server requests and responses in the console.
     var debugCartJson = false;
     // This is a global (within wrapper function scope) so event handlers can use it
     // without needing to bind functions.
     var scrollbarWidth = 0;
     // This holds everything we need to know to draw the page: taxId, db, hubs, description etc.
     var uiState = {};
     // This is dbDbTree after pruning -- null if dbDbTree has no children left
     var prunedDbDbTree;
+    // This keeps track of which gene the user has selected most recently from autocomplete.
+    var selectedGene = null;
 
     function setupFavIcons() {
         // Set up onclick handlers for shortcut buttons and labels
         var i, name, taxId, onClick;
         for (i = 0;  i < favIconTaxId.length;  i++) {
             name = favIconTaxId[i][0];
             taxId = favIconTaxId[i][1];
             // When user clicks on icon, set the taxId (default database);
             // scroll the image to that species and clear the species autocomplete input.
             onClick = setTaxId.bind(null, taxId, null, true, true);
             // Onclick for both the icon and its sibling label:
             $('.jwIconSprite' + name).parent().children().click(onClick);
         }
     }
 
@@ -1165,35 +1167,40 @@
     }
 
     function onSelectGene(item) {
         // Set the position from an autocomplete result;
         // set hgFindMatches and make sure suggestTrack is in pack mode for highlighting the match.
         var newPos = item.id;
         var newPosComma = addCommasToPosition(newPos);
         var settings;
         $('#positionDisplay').text(newPosComma);
         if (uiState.suggestTrack) {
             settings = { 'hgFind.matches': item.internalId };
             settings[uiState.suggestTrack] = 'pack';
             cart.send({ cgiVar: settings });
             cart.flush();
         }
-        // Overwrite the selected item w/actual position after the autocomplete plugin is done:
-        function overwriteWithPos() {
-            $('#positionInput').val(newPosComma);
+        function overwriteWithGene() {
+            $('#positionInput').val(item.geneSymbol);
+        }
+        if (item.geneSymbol) {
+            selectedGene = item.geneSymbol;
+            // Overwrite item's long value with symbol after the autocomplete plugin is done:
+            window.setTimeout(overwriteWithGene, 0);
+        } else {
+            selectedGene = item.value;
         }
-        window.setTimeout(overwriteWithPos, 0);
     }
 
     function setAssemblyOptions(uiState) {
         var assemblySelectLabel = 'Assembly';
         if (uiState.dbOptions) {
             var html = '', option, i, selected;
             for (i = 0;  i < uiState.dbOptions.length;  i++) {
                 option = uiState.dbOptions[i];
                 selected = (option.value === uiState.db) ? 'selected ' : '';
                 html += '<option ' + selected + 'value="' + option.value + '">' +
                         option.label + '</option>';
             }
             $('#selectAssembly').html(html);
         }
         if (uiState.genomeLabel) {
@@ -1282,30 +1289,31 @@
         // Update the assembly menu, positionInput and description.
         var suggestUrl = null;
         if (uiState.suggestTrack) {
             suggestUrl = 'hgSuggest?db=' + uiState.db + '&prefix=';
         }
         setAssemblyOptions(uiState);
         if (uiState.position) {
             $('#positionDisplay').text(addCommasToPosition(uiState.position));
         }
         autocompleteCat.init($('#positionInput'),
                              { baseUrl: suggestUrl,
                                watermark: positionWatermark,
                                onSelect: onSelectGene,
                                enterSelectsIdentical: true,
                                onEnterTerm: goToHgTracks });
+        selectedGene = null;
         setAssemblyDescriptionTitle(uiState.db, uiState.genome);
         updateDescription(uiState.description);
         if (uiState.db && $('#findPositionContents').css('display') === 'none') {
             initFindPositionContents();
         }
     }
 
     function removeDups(inList, isDup) {
         // Return a list with only unique items from inList, using isDup(a, b) -> true if a =~ b
         var inLength = inList.length;
         // inListDups is an array of boolean flags for marking duplicates, parallel to inList.
         var inListDups = [];
         var outList = [];
         var i, j;
         for (i = 0;  i < inLength;  i++) {
@@ -1540,38 +1548,40 @@
         }
     }
 
     function onClickCopyPosition() {
         // Copy the displayed position into the position input:
         var posDisplay = $('#positionDisplay').text();
         $('#positionInput').val(posDisplay).focus();
     }
 
     function goToHgTracks() {
         // Create and submit a form for hgTracks with hidden inputs for org, db and position.
         var position = $('#positionInput').val();
         var posDisplay = $('#positionDisplay').text();
         var pix = uiState.pix || calculateHgTracksWidth();
         var $form;
-        if (! position || position === '' || position === positionWatermark) {
+        if (! position || position === '' || position === positionWatermark ||
+            position === selectedGene) {
             position = posDisplay;
         }
         // Show a spinner -- sometimes it takes a while for hgTracks to start displaying.
         $('.jwGoIcon').removeClass('fa-play').addClass('fa-spinner fa-spin');
         // Make a form and submit it.  In order for this to work in IE, the form
         // must be appended to the body.
         $form = $('<form action="hgTracks" method=GET id="mainForm">' +
+                  '<input type=hidden name="hgsid" value="' + window.hgsid + '">' +
                   '<input type=hidden name="org" value="' + uiState.genome + '">' +
                   '<input type=hidden name="db" value="' + uiState.db + '">' +
                   '<input type=hidden name="position" value="' + position + '">' +
                   '<input type=hidden name="pix" value="' + pix + '">' +
                   '</form>');
         $('body').append($form);
         $form.submit();
     }
 
     function replaceHgsidInLinks() {
         // Substitute '$hgsid' with real hgsid in <a> href's.
         $('a').each(function(ix, aEl) {
             var href = aEl.getAttribute('href');
             if (href && href.indexOf('$hgsid') >= 0) {
                 aEl.setAttribute('href', href.replace('$hgsid', window.hgsid));