33b03663f2d373fbacb280826458f323d2be5439
larrym
  Tue Sep 13 15:53:51 2011 -0700
make sure db is in external links (was missing from luNega links); more changes to experimental remote tracks code
diff --git src/hg/js/hgTracks.js src/hg/js/hgTracks.js
index eab4e67..f6ce3b8 100644
--- src/hg/js/hgTracks.js
+++ src/hg/js/hgTracks.js
@@ -1431,30 +1431,52 @@
         //alert("Matched # ["+obj.href+"] or has target:"+obj.target);
         return true;
     }
     var thisForm=$(obj).parents('form');
     if(thisForm == undefined || $(thisForm).length == 0)
         thisForm=$("FORM");
     if($(thisForm).length > 1 )
         thisForm=$(thisForm)[0];
     if(thisForm != undefined && $(thisForm).length == 1) {
         //alert("posting form:"+$(thisForm).attr('name'));
         return postTheForm($(thisForm).attr('name'),obj.href);
     }
     return true;
 }
 
+function loadRemoteTracks()
+{
+    if(typeof(hgTracks.trackDb) != "undefined" && hgTracks.trackDb != null) {
+        for (var id in hgTracks.trackDb) {
+            var rec = hgTracks.trackDb[id];
+            if(rec.type == "remote") {
+                if($("#img_data_" + id).length > 0) {
+                    // load the remote track renderer via jsonp
+                    rec.loadingId = showLoadingImage("tr_" + id);
+                    var script = document.createElement('script');
+                    var pos = parsePosition(getPosition());
+                    var name = rec.remoteTrack || id;
+                    script.setAttribute('src', rec.url + "?track=" + name + "&jsonp=remoteTrackCallback&position=" +
+                                        encodeURIComponent(pos.chrom + ":" + pos.start + "-" + pos.end) +
+                                        "&pix=" + $('#imgTbl').width());
+                    document.getElementsByTagName('head')[0].appendChild(script);
+                }
+            }
+        }
+    }
+}
+
 $(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 (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();
@@ -1581,75 +1603,59 @@
         //$("#zoomSlider").slider({ min: -4, max: 3, step: 1 });//, handle: '.ui-slider-handle' });
 
         // Temporary warning while new imageV2 code is being worked through
         if($('#map').children("AREA").length > 0) {
             warn('Using imageV2, but old map is not empty!');
         }
 
         // Retrieve tracks via AJAX that may take too long to draw initialliy (i.e. a remote bigWig)
         var retrievables = $('#imgTbl').find("tr.mustRetrieve")
         if($(retrievables).length > 0) {
             $(retrievables).each( function (i) {
                 var trackName = $(this).attr('id').substring(3);
                 updateTrackImg(trackName,"","");
             });
         }
+        loadRemoteTracks();
     }
     if($('img#chrom').length == 1) {
         if($('area.cytoBand').length > 1) {
             $('img#chrom').chromDrag();
         }
     }
 
     if($("#tabs").length > 0) {
         // Search page specific code
 
         var val = $('#currentTab').val();
         $("#tabs").tabs({
                             show: function(event, ui) {
                                 $('#currentTab').val(ui.panel.id);
                             },
                             select: function(event, ui) { findTracksSwitchTabs(ui); }
                         });
         $('#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(searchKeydown);
         findTracksNormalize();
         updateMetaDataHelpLinks(0);
     }
-
-    if(typeof(hgTracks.trackDb) != "undefined" && hgTracks.trackDb != null) {
-        for (var id in hgTracks.trackDb) {
-            var rec = hgTracks.trackDb[id];
-            if(rec.type == "remote") {
-                if($("#img_data_" + id).length > 0) {
-                    // load the remote track renderer via jsonp
-                    var script = document.createElement('script');
-                    // XXXX add current image width
-                    var pos = parsePosition(getPosition());
-                    script.setAttribute('src', rec.url + "?track=" + id + "&jsonp=remoteTrackCallback&c=" + pos.chrom +
-                                        "&s=" + pos.start + "&e=" + pos.end);
-                    document.getElementsByTagName('head')[0].appendChild(script);
-                }
-            }
-        }
-    }
 });
 
 function rulerModeToggle (ele)
 {
     autoHideSetting = !ele.checked;
     var obj = imgAreaSelect.data('imgAreaSelect');
     obj.setOptions({autoHide : autoHideSetting});
 }
 
 function makeMapItem(id)
 {
     // Create a dummy mapItem on the fly (for objects that don't have corresponding entry in the map).
     if(typeof(hgTracks.trackDb) != "undefined" && hgTracks.trackDb != null) {
         var title;
         var rec = hgTracks.trackDb[id];
@@ -1941,37 +1947,42 @@
         var data = "hgt.imageV1=1&hgt.trackImgOnly=1&hgsid=" + getHgsid();
         jQuery('body').css('cursor', 'wait');
         $.ajax({
                    type: "GET",
                    url: "../cgi-bin/hgTracks",
                    data: data,
                    dataType: "html",
                    trueSuccess: handleViewImg,
                    success: catchErrorOrDispatch,
                    error: errorHandler,
                    cmd: cmd,
                    cache: false
                });
     } else if (cmd == 'openLink' || cmd == 'followLink') {
         var href = selectedMenuItem.href;
-        var vars = new Array("c", "l", "r");
+        var vars = new Array("c", "l", "r", "db");
         var valNames = new Array("chromName", "winStart", "winEnd");
         for (i in vars) {
             // make sure the link contains chrom and window width info (necessary b/c we are stripping hgsid and/or the cart may be empty);
             // but don't add chrom to wikiTrack links (see redmine #2476).
-            var val = hgTracks[valNames[i]];
             var v = vars[i];
+            var val;
+            if(v == "db") {
+                val = getDb();
+            } else {
+                val = hgTracks[valNames[i]];
+            }
             if(val && id != "wikiTrack" && (href.indexOf("?" + v + "=") == -1) && (href.indexOf("&" + v + "=") == -1)) {
                 href = href + "&" + v + "=" + val;
             }
         }
         if(cmd == 'followLink') {
             // XXXX This is blocked by Safari's popup blocker (without any warning message).
             location.assign(href);
         } else {
             // Remove hgsid to force a new session (see redmine ticket 1333).
             href = removeHgsid(href);
             if(window.open(href) == null) {
                 windowOpenFailedMsg();
             }
         }
     } else if (cmd == 'float') {
@@ -2639,30 +2650,31 @@
 {
 // Reload various UI widgets after updating imgTbl map.
     parseMap(null, true);
     $("map[name!=ideoMap]").each( function(t) { parseMap($(this, false));});
     initImgTblButtons();
     loadImgAreaSelect(false);
     // Do NOT reload context menu (otherwise we get the "context menu sticks" problem).
     // loadContextMenu($('#tr_' + id));
     if(trackImgTbl.tableDnDUpdate)
         trackImgTbl.tableDnDUpdate();
     reloadFloatingItem();
     // Turn on drag scrolling.
     if(hgTracks.imgBoxPortal) {
         $("div.scroller").panImages();
     }
+    loadRemoteTracks();
     markAsDirtyPage();
 }
 
 function updateTrackImgForId(html, id)
 {
 // update row in imgTbl for given id.
 // return true if we successfully pull slice for id and update it in imgTrack.
     var str = "<TR id='tr_" + id + "'[^>]*>([\\s\\S]+?)</TR>";
     var reg = new RegExp(str);
     var a = reg.exec(html);
     if(a && a[1]) {
         var tr = $('#tr_' + id);
         $(tr).html(a[1]);
         // NOTE: Want to examine the png? Uncomment:
         //var img = $('#tr_' + id).find("img[id^='img_data_']").attr('src');
@@ -2747,31 +2759,31 @@
     } else {
         if(imageV2) {
             // Implement in-place updating of hgTracks image
             setPositionByCoordinates(json.chromName, json.winStart + 1, json.winEnd);
             $("input[name='c']").val(json.chromName);
             $("input[name='l']").val(json.winStart);
             $("input[name='r']").val(json.winEnd);
             if(json.cgiVersion != hgTracks.cgiVersion) {
                 // Must reload whole page because of a new version on the server; this should happen very rarely.
                 // Note that we have already updated position based on the user's action.
                 jQuery('body').css('cursor', 'wait');
 	        document.TrackHeaderForm.submit();
             } else {
                 // We update rows one at a time (updating the whole imgTable at one time doesn't work in IE).
                 for (id in hgTracks.trackDb) {
-                    if(!updateTrackImgForId(response, id)) {
+                    if(hgTracks.trackDb[id].type != "remote" && !updateTrackImgForId(response, id)) {
                         showWarning("Couldn't parse out new image for id: " + id);
                         //alert("Couldn't parse out new image for id: " + id+"BR"+response);  // Very helpful
                     }
                 }
 /* This (disabled) code handles dynamic addition of tracks:
                 for (id in hgTracks.trackDb) {
                     if(oldTrackDb[id] == undefined) {
                         // XXXX Tim, what s/d abbr attribute be?
                         $('#imgTbl').append("<tr id='tr_" + id + "' class='imgOrd trDraggable'></tr>");
                         updateTrackImgForId(response, id);
                         updateVisibility(id, visibilityStrsOrder[hgTracks.trackDb[id].visibility]);
                     }
                 }
 */
                 hgTracks = json;
@@ -2874,45 +2886,54 @@
             setPosition(pos, null);
         } else {
             // turn this into a full text search.
             setPosition(gene, null);
         }
     }
     return true;
 }
 
 function remoteTrackCallback(rec)
 // jsonp callback to load a remote track.
 {
     if(rec.error) {
         alert("retrieval from remote site failed with error: " + rec.error)
     } else {
-        var track = rec.track;
-        $('#img_data_' + track).attr('style', '');
+        var remoteTrack = rec.track;
+        for (var track in hgTracks.trackDb) {
+            if(hgTracks.trackDb[track].remoteTrack == remoteTrack) {
+                $('#img_data_' + track).attr('style', "left:-116px; top: -23px;");
         $('#img_data_' + track).attr('height', rec.height);
-        $('#img_data_' + track).attr('width', rec.width);
+                // XXXX use width in some way?
+//        $('#img_data_' + track).attr('width', rec.width);
+                $('#img_data_' + track).attr('width', $('#img_data_ruler').width());
         $('#img_data_' + track).attr('src', rec.img);
         $('#td_data_' + track + ' > div').each(function(index) {
                                                    if(index == 1) {
                                                        var style = $(this).attr('style');
                                                        style = style.replace(/height:\s*\d+/i, "height:" + rec.height);
                                                        $(this).attr('style', style);
                                                    }
                                                });
         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);
+                }
+            }
+        }
     }
 }
 
 function searchKeydown(event)
 {
     if (event.which == 13) {
         // Required to fix problem on IE and Safari where value of hgt_tSearch is "-" (i.e. not "Search").
         $("input[name=hgt_tsPage]").val(0);  // NOTE: must match TRACK_SEARCH_PAGER in hg/inc/searchTracks.h
         $('#trackSearch').submit();
         // This doesn't work with IE or Safari.
         // $('#searchSubmit').click();
     }
 }
 
 function windowOpenFailedMsg()