src/hg/js/hgTracks.js 1.54

1.54 2010/02/16 01:28:14 larrym
add autocomplete code; add getDna support
Index: src/hg/js/hgTracks.js
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/js/hgTracks.js,v
retrieving revision 1.53
retrieving revision 1.54
diff -b -B -U 4 -r1.53 -r1.54
--- src/hg/js/hgTracks.js	13 Feb 2010 00:40:00 -0000	1.53
+++ src/hg/js/hgTracks.js	16 Feb 2010 01:28:14 -0000	1.54
@@ -21,28 +21,14 @@
 var autoHideSetting = true; // Current state of imgAreaSelect autoHide setting
 var selectedMenuItem;       // currently choosen context menu item (via context menu).
 var browser;                // browser ("msie", "safari" etc.)
 
-function commify (str) {
-    if(typeof(str) == "number")
-	str = str + "";
-    var n = str.length;
-    if (n <= 3) {
-	return str;
-    } else {
-	var pre = str.substring(0, n-3);
-	var post = str.substring(n-3);
-	var pre = commify(pre);
-	return pre + "," + post;
-    }
-}
-
 function initVars(img)
 {
 // There are various entry points, so we call initVars in several places to make sure this variables get updated.
     if(!originalPosition) {
         // remember initial position and size so we can restore it if user cancels
-        originalPosition = $('#positionHidden').val() || getPosition();
+        originalPosition = getOriginalPosition();
         originalSize = $('#size').text();
         originalCursor = jQuery('body').css('cursor');
     }
 }
@@ -75,23 +61,38 @@
     setPosition(newPosition, commify(end - start + 1));
     return newPosition;
 }
 
-function getPosition()
+function getPositionElement()
 {
-// Return current value of position box
+// Return position box object
     var tags = document.getElementsByName("position");
     // There are multiple tags with name == "position" (the visible position text input
     // and a hidden with id='positionHidden'); we return value of visible element.
     for (var i = 0; i < tags.length; i++) {
 	    var ele = tags[i];
             if(ele.id != "positionHidden") {
-	        return ele.value;
+	        return ele;
             }
     }
     return null;
 }
 
+function getPosition()
+{
+// Return current value of position box
+    var ele = getPositionElement();
+    if(ele != null) {
+	return ele.value;
+    }
+    return null;
+}
+
+function getOriginalPosition()
+{
+    return originalPosition || getPosition();
+}
+
 function setPosition(position, size)
 {
 // Set value of position and size (in hiddens and input elements).
 // We assume size has already been commified.
@@ -108,8 +109,13 @@
         $('#size').text(size);
     }
 }
 
+function getDb()
+{
+    return document.getElementsByName("db")[0].value;
+}
+
 function checkPosition(img, selection)
 {
 // return true if user's selection is still w/n the img (including some slop).
     var imgWidth = jQuery(img).width();
@@ -1004,9 +1010,9 @@
 {
     blockUseMap=true;
 }
 
-// wait for jStore to prepare the storage engine (this token reload code is currently dead code).
+// 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(){
@@ -1025,10 +1031,30 @@
 });
 
 $(document).ready(function()
 {
+    if(jQuery.fn.autocomplete && $('input#suggest')) {
+        var db = getDb();
+        $('input#suggest').autocomplete({
+                                            delay: 500,
+                                            minchars: 1,
+                                            ajax_get: ajaxGet(function () {return db;}, new Object),
+                                            callback: function (obj) {
+                                                setPosition(obj.id, commify(getSizeFromCoordinates(obj.id)));
+                                                // jQuery('body').css('cursor', 'wait');
+                                                // document.TrackHeaderForm.submit();
+                                            }
+                                        });
+        
+        // I want to set focus to the suggest element, but unforunately that prevents PgUp/PgDn from
+        // working, which is a major annoyance.
+        // $('input#suggest').focus();
+    }
+    initVars();
+    
     if(jQuery.jStore) {
-        if(0) {
+        // Experimental (currently dead) code to handle "user hits back button" problem.
+        if(false) {
             jQuery.extend(jQuery.jStore.defaults, {
                               project: 'hgTracks',
                               engine: 'flash',
                               flash: '/jStore.Flash.html'
@@ -1234,10 +1260,10 @@
         // showWarning("Spinning: menu is still shown");
         setTimeout(function() { contextMenuHitFinish(menuItemClicked, menuObject, cmd); }, 10);
         return;
     }
-    if(cmd == 'selectWholeGene') {
-            // bring whole gene into view
+    if(cmd == 'selectWholeGene' || cmd == 'getDna') {
+            // bring whole gene into view or redirect to DNA screen.
             var href = 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")
@@ -1266,8 +1292,13 @@
             }
             if(chrom == null || chromStart == null || chromEnd == null) {
                 showWarning("couldn't parse out genomic coordinates");
             } else {
+                if(cmd == 'getDna')
+                {
+                    // start coordinate seems to be off by one (+1).
+                    window.location = "../cgi-bin/hgc?hgsid=" + getHgsid() + "&g=getDna&i=mixed&c=" + chrom + "&l=" + chromStart + "&r=" + chromEnd;
+                } else {
                 var newPosition = setPositionByCoordinates(chrom, chromStart, chromEnd);
                 if(browser == "safari" || imageV2) {
                     // We need to parse out more stuff to support resetting the position under imageV2 via ajax, but it's probably possible.
                     // See comments below on safari problems.
@@ -1286,8 +1317,9 @@
                                cache: false
                            });
                 }
             }
+            }
     } else if (cmd == 'hgTrackUi') {
         // data: ?
         jQuery('body').css('cursor', 'wait');
         $.ajax({
@@ -1436,18 +1468,22 @@
                     var o = new Object();
                     if(isGene || isHgc) {
                         var title = selectedMenuItem.title || "feature";
                         o["Zoom to " +  title] = {onclick: function(menuItemClicked, menuObject) { contextMenuHit(menuItemClicked, menuObject, "selectWholeGene"); return true; }};
+                        o["Get DNA for " +  title] = {onclick: function(menuItemClicked, menuObject) { contextMenuHit(menuItemClicked, menuObject, "getDna"); return true; }};
                         o["Open Link in New Window"] = {onclick: function(menuItemClicked, menuObject) { contextMenuHit(menuItemClicked, menuObject, "openLink"); return true; }};
                     } else {
                         o[selectedMenuItem.title] = {onclick: function(menuItemClicked, menuObject) { contextMenuHit(menuItemClicked, menuObject, "hgTrackUi"); return true; }};
                     }
                     menu.push(o);
                 }
             }
             if(!done) {
-                var str = "drag-and-zoom mode";
+                if(false) {
+                    // Currently toggling b/n drag-and-zoom mode and hilite mode is disabled b/c we don't know how to keep hilite mode from disabling the
+                    // context menus.
                 var o = new Object();
+                    var str = "drag-and-zoom mode";
                 if(autoHideSetting) {
                     str += selectedImg;
                     // menu[str].className = 'context-menu-checked-item';
                 }
@@ -1460,8 +1496,9 @@
                     str += selectedImg;
                 }
                 o[str] = { onclick: function(menuItemClicked, menuObject) { contextMenuHit(menuItemClicked, menuObject, "hilightMode"); return true; }};
                 menu.push(o);
+                }
                 menu.push({"view image": {onclick: function(menuItemClicked, menuObject) { contextMenuHit(menuItemClicked, menuObject, "viewImg"); return true; }}});
             }
             return menu;
         },
@@ -1502,29 +1539,12 @@
     }
     return mapItems;
 }
 
-function showWarning(str)
-{
-    $("#warningText").text(str);
-    $("#warning").show();
-}
-
-function catchErrorOrDispatch(obj,status)
-{
-    if(obj.err)
-    {
-        showWarning(obj.err);
-        jQuery('body').css('cursor', '');
-    }
-    else
-        this.trueSuccess(obj,status);
-}
-
 function handleTrackUi(response, status)
 {
 // Take html from hgTrackUi and put it up as a modal dialog.
-    $('#hgTrackUiDialog').html(response);
+    $('#hgTrackUiDialog').html("<div style='font-size:80%'>" + response + "</div>");
     $('#hgTrackUiDialog').dialog({
                                ajaxOptions: {
                                    // This doesn't work
                                    cache: true
@@ -1664,4 +1684,23 @@
         }
     }
     showWarning("Couldn't parse out img src");
 }
+
+function jumpButtonOnClick()
+{
+// onClick handler for the "jump" button.
+// Handles situation where user types a gene name into the gene box and immediately hits the jump button,
+// expecting the browser to jump to that gene.
+    var gene = $('#suggest').val();
+    if(gene && gene.length > 0 && getOriginalPosition() == getPosition()) {
+        var db = getDb();
+        pos = lookupGene(db, gene);
+        if(pos) {
+            setPosition(pos, null);
+        } else {
+            // turn this into a full text search.
+            setPosition(gene, null);
+        }
+    }
+    return true;
+}