f2d0fb6dff296bfe17f3edf6e5360683a1baf5c1
larrym
  Fri Apr 6 13:10:00 2012 -0700
code cleanup and refactoring and remove some dead code
diff --git src/hg/js/hgTracks.js src/hg/js/hgTracks.js
index bfb42b4..ddb6f37 100644
--- src/hg/js/hgTracks.js
+++ src/hg/js/hgTracks.js
@@ -8,30 +8,31 @@
 /* Data passed in from CGI via the hgTracks object:
  *
  * string cgiVersion      // CGI_VERSION
  * string chromName       // current chromosome
  * int winStart           // genomic start coordinate (0-based, half-open)
  * int winEnd             // genomic end coordinate
  * int newWinWidth        // new width (in bps) if user clicks on the top ruler
  * boolean revCmplDisp    // true if we are in reverse display
  * int insideX            // width of side-bar (in pixels)
  * int rulerClickHeight   // height of ruler (in pixels) - zero if ruler is hidden
  * boolean inPlaceUpdate  // true if in-place-update is turned on
  * int imgBox*            // various drag-scroll values
  * boolean measureTiming  // true if measureTiming is on
  * Object trackDb         // hash of trackDb entries for tracks which are visible on current page
  */
+
 function initVars()
 {  // There are various entry points, so we call initVars in several places to make sure all is well
     if (!genomePos.original) {
         // remember initial position and size so we can restore it if user cancels
         genomePos.original = genomePos.getOriginalPos();
         genomePos.originalSize = $('#size').text();
         dragSelect.originalCursor = jQuery('body').css('cursor');
 
         imageV2.imgTbl = $('#imgTbl');
         // imageV2.enabled == true unless: advancedJavascript==false, or trackSearch, or config pg
         imageV2.enabled = (imageV2.imgTbl != undefined && imageV2.imgTbl.length > 0);
 
         jQuery.each(jQuery.browser, function(i, val) {
             if(val) {
                 browser = i;
@@ -111,30 +112,37 @@
     get: function ()
     {
     // Return current value of position box
         var ele = genomePos.getElement();
         if(ele != null) {
             return ele.value;
         }
         return null;
     },
 
     getOriginalPos: function ()
     {
         return genomePos.original || genomePos.get();
     },
 
+    revertToOriginalPos: function ()
+    {
+    // undo changes to position (i.e. after user aborts a drag-and-select).
+        this.set(this.original, this.originalSize);
+        this.original = this.originalSize = null;         // not sure if this is necessary.
+    },
+
     set: function (position, size)
     {   // Set value of position and size (in hiddens and input elements).
         // We assume size has already been commified.
         // Either position or size may be null.
         if(position) {
             // There are multiple tags with name == "position"
             // (one in TrackHeaderForm and another in TrackForm).
             var tags = document.getElementsByName("position");
             for (var i = 0; i < tags.length; i++) {
                 var ele = tags[i];
                 ele.value = position;
             }
         }
         if($('#positionDisplay').length) {
             $('#positionDisplay').text(position);
@@ -557,33 +565,32 @@
         }
         if(doIt) {
             // dragSelect.startTime is null if mouse has never been moved
             var singleClick = (  (selection.x2 == selection.x1)
                               || dragSelect.startTime == null
                               || (now.getTime() - dragSelect.startTime) < 100);
             var newPosition = genomePos.update(img, selection, singleClick);
             if(newPosition != undefined) {
                 if(imageV2.inPlaceUpdate) {
                     imageV2.navigateInPlace("position=" + newPosition, null, true);
                 } else {
                     jQuery('body').css('cursor', 'wait');
                     document.TrackHeaderForm.submit();
                 }
             }
-        } else {  // what is this doing?
-            genomePos.set(genomePos.original, genomePos.originalSize);
-            genomePos.original = genomePos.originalSize = null;         // <- XXXX I think this is unnecessary.
+        } else {
+            genomePos.revertToOriginalPos();
         }
         dragSelect.startTime = null;
         setTimeout('posting.allowMapClicks();',50); // Necessary incase the dragSelect.selectEnd was over a map item. select takes precedence.
         return true;
     },
 
     load: function (firstTime)
     {
         var imgHeight = 0;
         if (imageV2.enabled)
             imgHeight = imageV2.imgTbl.height();
 
         // No longer disable without ruler, because shift-drag still works
         if(typeof(hgTracks) != "undefined") {
 
@@ -1668,35 +1675,34 @@
             setTimeout(function() { rightClick.hitFinish(menuItemClicked, menuObject, cmd); }, 10);
             return;
         }
         if(cmd == 'selectWholeGene' || cmd == 'getDna') {
                 // bring whole gene into view or redirect to DNA screen.
                 var href = rightClick.selectedMenuItem.href;
                 var chromStart, chromEnd;
                 var a = /hgg_chrom=(\w+)&/.exec(href);
                 // Many links leave out the chrom (b/c it's in the server side cart as "c")
                 var chrom = hgTracks.chromName;
                 if(a) {
                     if(a && a[1])
                         chrom = a[1];
                     a = /hgg_start=(\d+)/.exec(href);
                     if(a && a[1])
-                        // XXXX does chromStart have to be incremented by 1?
-                        chromStart = a[1];
+                        chromStart = parseInt(a[1]) + 1;
                     a = /hgg_end=(\d+)/.exec(href);
                     if(a && a[1])
-                        chromEnd = a[1];
+                        chromEnd = parseInt(a[1]);
                 } else {
                     // a = /hgc.*\W+c=(\w+)/.exec(href);
                     a = /hgc.*\W+c=(\w+)/.exec(href);
                     if(a && a[1])
                         chrom = a[1];
                     a = /o=(\d+)/.exec(href);
                     if(a && a[1])
                         chromStart = parseInt(a[1]) + 1;
                     a = /t=(\d+)/.exec(href);
                     if(a && a[1])
                         chromEnd = parseInt(a[1]);
                 }
                 if(chrom == null || chromStart == null || chromEnd == null) {
                     warn("couldn't parse out genomic coordinates");
                 } else {
@@ -2806,31 +2812,30 @@
                         }
                     });
                     var style = $('#p_btn_' + track).attr('style');
                     style = style.replace(/height:\s*\d+/i, "height:" + rec.height);
                     $('#p_btn_' + track).attr('style', style);
                     if(hgTracks.trackDb[track].loadingId) {
                         hideLoadingImage(hgTracks.trackDb[track].loadingId);
                     }
                 }
             }
         }
     },
 
     navigateButtonClick: function (ele) // called from hgTracks.c
     {   // code to update just the imgTbl in response to navigation buttons (zoom-out etc.).
-        // This is currently experimental code (controlled by IN_PLACE_UPDATE in imageV2.h).
         if(imageV2.inPlaceUpdate) {
             var params = ele.name + "=" + ele.value;
             $(ele).attr('disabled', 'disabled');
             // dinking navigation needs additional data
             if(ele.name == "hgt.dinkLL" || ele.name == "hgt.dinkLR") {
                 params += "&dinkL=" + $("input[name='dinkL']").val();
             } else if(ele.name == "hgt.dinkRL" || ele.name == "hgt.dinkRR") {
                 params += "&dinkR=" + $("input[name='dinkR']").val();
             }
             imageV2.navigateInPlace(params, $(ele), false);
             return false;
         } else {
             return true;
         }
     },
@@ -2884,31 +2889,32 @@
                 // Following doesn't work b/c we get the hugo symbol from the suggest list, not the known gene id.
                 // $(document.TrackForm || document.TrackHeaderForm).append("<input type='hidden' name='hgFind.matches' " + "value='" + name + "'>");
             } else {
                 // turn this into a full text search.
                 genomePos.set(gene, null);
             }
         }
         return true;
     },
 
     navigateInPlace: function (params, disabledEle, keepCurrentTrackVisible)
     {
     // request an hgTracks image, using params
     // disabledEle is optional; this element will be enabled when update is complete
     // If keepCurrentTrackVisible is true, we try to maintain relative position of the item under the mouse after the in-place update.
-        jQuery('body').css('cursor', '');
+        // Tim thinks we should consider disabling all UI input while we are doing in-place update.
+        jQuery('body').css('cursor', 'wait');
         var currentId, currentIdYOffset;
         if(keepCurrentTrackVisible) {
             var item = rightClick.currentMapItem || imageV2.lastTrack;
             if(item) {
                 var top = $(document.getElementById("tr_" + item.id)).position().top;
                 if(top >= $(window).scrollTop()
                 || top < $(window).scrollTop() + $(window).height()) {
                     // don't bother if the item is not currently visible.
                     currentId = item.id;
                     currentIdYOffset = top - $(window).scrollTop();
                 }
             }
         }
         $.ajax({
                 type: "GET",
@@ -3015,88 +3021,51 @@
             $('#tabs').show();
             $("#tabs").tabs('option', 'selected', '#' + val);
             if(val =='simpleTab' && $('div#found').length < 1) {
                 $('input#simpleSearch').focus();
             }
             $("#tabs").css('font-family', jQuery('body').css('font-family'));
             $("#tabs").css('font-size', jQuery('body').css('font-size'));
             $('.submitOnEnter').keydown(trackSearch.searchKeydown);
             findTracks.normalize();
             findTracks.updateMdbHelp(0);
         }
     }
 }
 
 
-/*
-
-// wait for jStore to prepare the storage engine (this token reload code is experimental and currently dead).
-jQuery.jStore && jQuery.jStore.ready(function(engine) {
-    // alert(engine.jri);
-    // wait for the storage engine to be ready.
-    engine.ready(function(){
-        var engine = this;
-        var newToken = hgTracks.time;
-        if(newToken) {
-            var oldToken = engine.get("token");
-            if(oldToken && oldToken == newToken) {
-                // user has hit the back button.
-                jQuery('body').css('cursor', 'wait');
-                window.location = "../cgi-bin/hgTracks?hgsid=" + getHgsid();
-            }
-        }
-        engine.set("token", newToken);
-    });
-});
-
-*/
-
   ///////////////
  //// READY ////
 ///////////////
 $(document).ready(function()
 {
     // The page may be reached via browser history (back button)
     // If so, then this code should detect if the image has been changed via js/ajax
     // and will reload the image if necessary.
     // NOTE: this is needed for IE but other browsers can detect the dirty page much earlier
     if (imageV2.isDirtyPage()) {
         // mark as non dirty to avoid infinite loop in chrome.
         $('#dirty').val('false');
         jQuery('body').css('cursor', 'wait');
             window.location = "../cgi-bin/hgTracks?hgsid=" + getHgsid();
             return false;
     }
     initVars();
 
     var db = getDb();
     suggestBox.init(db);
 
-    if(jQuery.jStore) {
-        // Experimental code to handle "user hits back button" problem by reloading the page based on the user's cart
-        if(jQuery.browser.msie && jQuery.browser.version < 8) {
-            // IE 7 requires flash to support jStore.
-            jQuery.extend(jQuery.jStore.defaults, {
-                              project: 'hgTracks',
-                              engine: 'flash',
-                              flash: '/jStore.Flash.html'
-                          });
-        }
-        jQuery.jStore.load();
-    }
-
-
     // Convert map AREA gets to post the form, ensuring that cart variables are kept up to date (but turn this off for search form).
     if($("FORM").length > 0 && $('#trackSearch').length == 0) {
         var allLinks = $('a');
         $( allLinks ).unbind('click');
         $( allLinks ).click( posting.saveSettings );
     }
     if($('#pdfLink').length == 1) {
         $('#pdfLink').click(function(i) {
             var thisForm=$('#TrackForm');
             if(thisForm != undefined && $(thisForm).length == 1) {
                 //alert("posting form:"+$(thisForm).attr('name'));
                 updateOrMakeNamedVariable($(thisForm),'hgt.psOutput','on');
                 return postTheForm($(thisForm).attr('name'),this.href);
             }
             return true;